How to do lognormal fit

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

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.
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.

Sign in to comment.

Answers (1)

the cyclist
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

what inputs should i give to this function.
I assume that you had a vector that was an input to the histogram you have. (Suppose that vector was the variable X.) You should be able to put that vector as the input to lognfit:
>> parmhat = lognfit(X)
(I edited my answer to give an example.)
I also suggest you read the documentation:
>> doc lognfit
Rob
Rob on 12 Aug 2015
Can you explain how you determine the scaling factor of 1000 in the last line of your code?
Why do you put 1000?
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.
Do you know how can I regress a lognrnd time series?

Sign in to comment.

Categories

Find more on Curve Fitting Toolbox in Help Center and File Exchange

Asked:

on 5 Mar 2013

Community Treasure Hunt

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

Start Hunting!