Fitting a log-normal distribution

34 Ansichten (letzte 30 Tage)
Lewis MacKenzie
Lewis MacKenzie am 14 Dez. 2016
Kommentiert: John D'Errico am 16 Dez. 2016
Hello,
I'm trying to plot a fit to a log-normal distribution. I have the statistics and machine learning toolbox, but I am confused as how to apply the log-normal fit function to this data.
Below is some minimum working code to create a log-normal distribution, but I do not know how to progress further with this fit. The 'lognfit' function requires only a 1 dimensional input vector, not the two input parameters I have (i.e. x and p).
As an alternative, I've also tried fitting using cftool and inputting the log-normal probability distribution function, but unfortunately I did not get a successful fit.
Any advice that you could give would be greatly appreciated.
Lewis
% create x data
x = (0.01:0.02:11);
% define lognormal distribution parameters
sigma=1;
mu=1;
% create a log normal distribution with these parameters
y = lognpdf(x,sigma,mu);
% plot the resulting distribution
figure, plot(x,y);

Akzeptierte Antwort

John D'Errico
John D'Errico am 14 Dez. 2016
Bearbeitet: John D'Errico am 14 Dez. 2016
Your problem is a not uncommon one for people who don't understand random distributions.
The fact is, you don't have data as samples from a lognormal distribution. You have points taken as values off the lognormal PDF. You cannot use lognfit to fit that data.
Lets see how to do it, in a way that will work. I'll start with a simple example, using a normal.
Here, I'll generate some random samples using randn. The mean should be 2, standard deviation 3. But the estimated values for those parameters will not be exactly those numbers of course since they are estimates, taken from a finite sample.
X = randn(1000,1)*3 + 2;
[MUHAT,SIGMAHAT] = normfit(X)
MUHAT =
1.9021041197117
SIGMAHAT =
2.99689596815649
As you can see, it did pretty well. But you need to recognize that normfit (and the cousin, lognfit that you tried to use) work on points that are assumed to follow that distribution, as I did above.
Random samples that are assumed to follow a given distribution are NOT the same thing as points taken off a PDF.
  3 Kommentare
Tom Lane
Tom Lane am 15 Dez. 2016
Before you stop barking, and in support of what John wrote, here is an example of fitting the lognormal distribution to data:
rng(0)
x = lognrnd(5,.3,100,1);
histfit(x,15,'lognormal')
And here is an example of fitting a model that is of the shape of a lognormal density to data that looks like it has that shape:
xx = linspace(0,500)';
yy = lognpdf(xx,5,.3) + randn(100,1)/10000;
F = @(b,x)lognpdf(x,b(1),b(2));
b = nlinfit(xx,yy,F,[5,.3])
plot(xx,yy,'.', xx,F(b,xx),'r-')
Both use functions from the Statistics and Machine Learning Toolbox.
John D'Errico
John D'Errico am 16 Dez. 2016
You CAN use the function lognfit on real data. The problem is that in your test, you did not have real data. You did not really have data at all. i.e., random samples from the lognormal distribution. You had values of the lognormal pdf, taken at a list of points. The two things are very different, though perhaps confusing.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by