plot the distribution of a dataset

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?

 Risposta accettata

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 Commenti

I have change the code in order to compute log-retruns which should be normally distributed, this leads to the following code (part of it is from your suggestion in the last post)
SPX=dataSet(:,2);
%log difference SPX
lastSPX = [NaN;SPX(1:end-1)];
logReturn=log(SPX./lastSPX);
logReturn = logReturn(~isnan(logReturn));
[n,x] = hist(logReturn,50)
n = n/length(logReturn)/diff(x(1:2));
bar(x,n,'hist')
mu = mean(logReturn);
sd = std(logReturn);
ix = linspace(min(logReturn),max(logReturn));
iy = pdf('normal', ix, mu, sd);
plot(ix,iy,'g-');
What I get now is just the green density function, but I would like to have two density functions. One that shows the empirical distribution (distribution of logReturn) and one of the normal distribution with the same mean and std deviation as the empirical distribution.
the reson for this is that I would like to show that the empirical distribution is left-skewed and has fat-tails.
You forgot the "hold on" from your original. Just do this before the plot command and the green density will plot on top of the histogram.
Locks
Locks il 8 Mag 2013
that gives me the histogramm and the green normal distribution, but I am looking for the probability density function of the data which is at the moment plotted as the histogram
something similar to this:
but for some reason I am not able to combine those two elements
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 il 10 Mag 2013
ksdesity was excatly what I was looking for, thanks!!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by