plot the distribution of a dataset

17 ビュー (過去 30 日間)
Locks
Locks 2013 年 5 月 6 日
Hi,
I have a time series and I would like to plot the pdf of an empirical and the lognormal distribution with the same mean and std deviation.
The code I am using is the following:
%SPX
SPX=dataSet(:,2);
[n,x] = hist(SPX,50);
plot(x,n/10000/diff(x(1:2)))
hold on
m=mean(SPX)
s=std(SPX)
plot(x,normpdf(x,m,s),'r')
hold on
mu = m;
sd = s;
ix = -5*sd:1e-3:5*sd;
iy = pdf('lognormal', ix, mu, sd);
plot(ix,iy);
plotting the pdf of the empirical distribution seems to work, but I do not understand what the smaller figures, which looks more than a mountain than as a pdf, is showing me In addition, plotting a lognormal distribution with the same mean and std deviaton does no work. Does anybody have an idea what I am missing?

採用された回答

Tom Lane
Tom Lane 2013 年 5 月 7 日
I suggest you plot the histogram this way:
n = n/length(SPX)/diff(x(1:2));
bar(x,n,'hist')
Then you plotted the normal distribution just fine. But for the lognormal distribution, the parameters are the mean and std of the log of the data. Try this:
mu = mean(log(SPX));
sd = std(log(SPX));
ix = linspace(min(SPX),max(SPX));
iy = pdf('lognormal', ix, mu, sd);
plot(ix,iy,'g-');
  5 件のコメント
Tom Lane
Tom Lane 2013 年 5 月 8 日
When I run your code I see a histogram with a green normal density superimposed. When I run histfit with the same number of bins, I see the same histogram with the same density, but red instead of green.
If you want a smooth density function that is like the histogram, try the ksdensity function. You could superimpose that over both the histogram and the normal density if you like.
Locks
Locks 2013 年 5 月 10 日
ksdesity was excatly what I was looking for, thanks!!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by