How to do lognormal fit
22 views (last 30 days)
Show older comments
I got the following plot after the simulation. Now, I want to fit it into log normal curve. Please, can you tell me the code to do that. http://i.imgur.com/VT70iYb.jpg
2 Comments
Image Analyst
on 5 Mar 2013
You could get a more accurate fit by using more bins in your histogram. The first few bins of a log normal are lower than the peak but you don't have enough bins to resolve that. Increase the number of bins and you'll probably see that.
the cyclist
on 5 Mar 2013
What Image Analyst says is true, but if you have the underlying data, you should fit the data directly, not the bin counts of the data.
Answers (1)
the cyclist
on 5 Mar 2013
Edited: the cyclist
on 5 Mar 2013
If you have the Statistics Toolbox, you can use the lognfit() function.
Here is an example of using the function:
% Make up some data. (You should use your real data in place of x.)
x = lognrnd(1,0.3,10000,1);
% Fit the data
parmhat = lognfit(x);
% Plot comparison of the histogram of the data, and the fit
figure
hold on
% Empirical distribution
hist(x,0.1:0.1:10);
% Fitted distribution
xt = 0.1:0.1:10;
plot(xt,1000*lognpdf(xt,parmhat(1),parmhat(2)),'r')
6 Comments
the cyclist
on 9 Jan 2016
I can see why that would be confusing. Sorry. Here is a better version of the code, where I have specified parameter names, instead of hard-coded numbers:
% Number of data points
N = 10000;
% Specify bin locations for histogram and fit
BIN_WIDTH = 0.1;
BIN_MAX = 10;
BIN_RANGE = BIN_WIDTH:BIN_WIDTH:BIN_MAX;
% Make up some data. (You should use your real data in place of x.)
x = lognrnd(1,0.3,N,1);
% Fit the data
parmhat = lognfit(x);
% Plot comparison of the histogram of the data, and the fit
figure
hold on
% Empirical distribution
hist(x,BIN_RANGE);
% Fitted distribution
xt = BIN_RANGE;
y_probability = BIN_WIDTH*lognpdf(xt,parmhat(1),parmhat(2));
y_count = N * y_probability;
h = plot(xt,y_count,'r');
set(h,'LineWidth',2)
The probability of landing in a particular bin is the pdf times the bin width. The count in a particular bin is that probability times the number in the sample. That is where the scaling factor came from. It was the bin width (0.1) times the number in the sample (10000).
I hope that is clearer now.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!