MATLAB Answers


plot the distribution of a dataset

Asked by Locks
on 6 May 2013


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:

[n,x] = hist(SPX,50); 
hold on
hold on 
mu = m; 
sd = s; 
ix = -5*sd:1e-3:5*sd; 
iy = pdf('lognormal', ix, mu, sd); 

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?


1 Answer

Answer by Tom Lane
on 7 May 2013
 Accepted answer

I suggest you plot the histogram this way:

n = n/length(SPX)/diff(x(1:2));

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); 


on 8 May 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

Tom Lane
on 8 May 2013

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.

on 10 May 2013

ksdesity was excatly what I was looking for, thanks!!

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!