How to find the initial parameters for nlinfit?
Show older comments
I have such set data:
t =
[ 0
0.0058
0.0116
0.0174
0.0233
0.0291
0.0349
0.0407
0.0465
0.0523
0.0581
0.0640
0.0698
0.0756
0.0814
0.0872]
y=1.0e-04. *
[0.0177
0.0977
0.1102
0.0797
0.0174
0.0010
0.0003
0.0003
0.0001
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000]
I want to use scaled gamma distribution function to fit them. The problem is I do not know how to set the suitable initial guess for parameters. I tried many sets but all failed when calling the nlinfit function. The code is just like this:
lambda0=[0.5 1 1] %this set is not right
lambda2 = nlinfit(t,y,@low_pass_fun,lambda0);
% % show the filter
figure(22);
SetGrayscale;
plot(t,y,'k*');hold on;
plot(t,low_pass_fun(lambda2,t),'g');hold off;
function yfun = low_pass_fun(lambda,t)
esp=1e-30;
yfun = lambda(1)*(t+esp).^(lambda(2)-1) .* exp(-t./lambda(3))./(gamma(lambda(2))*lambda(3)^lambda(2)); % scaled gamma distribution
end
How to find the initial parameters? Thanks a lot.
Accepted Answer
More Answers (2)
Tom Lane
on 10 Apr 2012
Here's how I approach problems like this. Other variants may work for you. I'll approximate the mean as the sample mode (location of peak), and the standard deviation is something like the width of the peak or 0.01. I can look up the gamma mean and standard deviation as a function of the two parameters a and b. Then I can solve for a and b.
>> t(find(y==max(y))) % mode
ans =
0.0116
>> % a*b = .0116, sqrt(a)*b = .01 % known formula for mean and std dev
>> a = (.0116/.01)^2 % solve for a
a =
1.3456
>> b = .0116/a % solve for b
b =
0.0086
Now I need the scale factor, which is the integral, or roughly:
>> sum(y(2:end).*diff(t))
ans =
1.7806e-07
So my starting values could be something like
lambda0 = [2e-7,1.3,.009]
4 Comments
Yunhui
on 11 Apr 2012
Tom Lane
on 12 Apr 2012
I would have to see exactly what you did. I tried your problem, including your data, using the lambda0 that I calculated. No warnings. It is possible to get a good fit with warnings; for example if you had redundant parameters that could happen.
Yunhui
on 13 Apr 2012
JAY R
on 27 Apr 2015
nice description sir.
Tom Lane
on 14 Apr 2012
0 votes
I just tried your problem in R2010a using your code and my lambda0, and all worked well. In R2012a the directory structure of the Statistics Toolbox is indeed different. You might want to try "which pathdef" and delete the file you find if it sets the path incorrectly. You could also "cd(matlabroot)" and "cd toolbox/stats/stats" if you just want to try it once, but I do not recommend this as a long-term solution.
4 Comments
Yunhui
on 17 Apr 2012
Yunhui
on 17 Apr 2012
Good Morning sir, This is regarding your answer I have read http://in.mathworks.com/matlabcentral/answers/35074-how-to-find-the-initial-parameters-for-nlinfit. you have explain
>> t(find(y==max(y))) % mode ans = 0.0116
>> % a*b = .0116, sqrt(a)*b = .01 % known formula for mean and std dev
>> a = (.0116/.01)^2 % solve for a a = 1.3456 >> b = .0116/a % solve for b b = 0.0086 Now I need the scale factor, which is the integral, or roughly:
>> sum(y(2:end).*diff(t)) ans = 1.7806e-07 So my starting values could be something like
lambda0 = [2e-7,1.3,.009]
***********************************
I have a small doubt? If I have one variable let us say vec1 is 32x5 and vec2 is 32x1 then how to find the initial guesses if the function is of five paramter ?
You may take random value for vec1 and vec2 for the explanation. Thanks in advance
Tom Lane
on 27 Apr 2015
The material you quote uses specific knowledge of the function to be fit, namely the form of the gamma distribution, to figure out starting values. The initial guesses in your case would depend on the form of the function you intend to fit.
Categories
Find more on F Distribution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!