Filter löschen
Filter löschen

Inf resulted when calculating mean

21 Ansichten (letzte 30 Tage)
King To Leung
King To Leung am 30 Jul. 2022
Kommentiert: Walter Roberson am 31 Jul. 2022
I am calculating the mean pe each year from 1993-2021. I have rounded my data set, drdata, to the 2 decimal place. However, the stockpe resulted in Inf for every year. I don't understand why this happens. I have already rounded it.
%drdata column 2 is date
%drdata column 4 is the pe ratio data
[year,~,~] = datevec(728110)
%storing the years in y (months and dates are not necessary)
%dates are stored in the 2nd column
[y,~,~] = datevec(drdata(:,2));
%calculating mean stock return for each year, return is in the 7th column
unique(y)
mask = y >= 1993 & y <= 2021;
nnz(mask)
nnz(isnan(drdata(mask,4)))
for k=1993:2021
stockpe(k-1992) = round(mean(drdata(y==k,4)),2);
%1st element will correspond to 1993, 2nd - 1994 and so on
end
  4 Kommentare
Walter Roberson
Walter Roberson am 31 Jul. 2022
temp = drdata(y==1993,4);
size(temp)
min(temp), max(temp)
Please show the output of these debugging commands
King To Leung
King To Leung am 31 Jul. 2022
temp = 33479 x 1 double
size(temp) = 33479
min (temp) = -2950
max (temp) = Inf

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 31 Jul. 2022
You have inf in your data. mean() of data is sum() of the data divided by the number of elements of the data. sum() that includes inf is going to be inf (unless the data includes nan or -inf) and inf divided by a finite number is inf.
If the inf represent missing data, delete those entries before processing the mean()
Also consider using grpstats() or splitapply()
  6 Kommentare
King To Leung
King To Leung am 31 Jul. 2022
Thank you very much! with this code I was able to find inf no. and replace them with nan. The codes work now. Thanks a lot!!!!!
Walter Roberson
Walter Roberson am 31 Jul. 2022
I would suggest
mask = ~isfinite(drdata(:,4));
drdata(mask,4) = nan;
You do not need to loop.
Or you could
drdata = standardizemissing(drdata, inf);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by