# [✓] Perform a nonlinear model fit?

GROUPS:
 Dear All, I have some "x, y" data values, and I am trying to use non-linear model fit command to fit Inverse Gaussian Distribution to the data. Yet (perhaps due to the negativity in a square root), I am getting an error. Can you please look at the attached code, and kindly let me know how can I avoid such errors and solve the particular problem? Thanks a lot. Attachments:
1 year ago
4 Replies
 This is a small set of data and does not really require an attached notebook. It can all be placed in the question (less clicking and downloading that way).
 The error message gives a strong hint as to what the problem is: One can see that NonlinearModelFit tries $\lambda=-1.13888$ but you want to restrict $\lambda$ to be greater than zero. modelinvgauss = PDF[InverseGaussianDistribution[\[Mu], \[Lambda]]]; invgauss = NonlinearModelFit[ datanew, {modelinvgauss[x], \[Lambda] > 0}, {{\[Mu], 10}, {\[Lambda], 10}}, x]; invgauss["BestFitParameters"] (* {\[Mu] -> 11.098760475474151, \[Lambda] -> 35.72763989840714} *) Show[ListPlot[datanew, PlotRange -> {{0, 20}, {0, 0.12}}], Plot[invgauss[x], {x, 0, 20}]] The fit isn't so great. I have to wonder why you want to fit a probability density function using a regression. Usually a probability density function is fit with a random sample from that distribution. If what you have is really a regression and you're just interested in the form of the density function being an inverse Gaussian, then usually translation and/or scaling parameters are usually added.
 The error message gives a strong hint as to what the problem is: One can see that NonlinearModelFit tries $\lambda=-1.13888$ but you want to restrict $\lambda$ to be greater than zero. modelinvgauss = PDF[InverseGaussianDistribution[\[Mu], \[Lambda]]]; invgauss = NonlinearModelFit[ datanew, {modelinvgauss[x], \[Lambda] > 0}, {{\[Mu], 10}, {\[Lambda], 10}}, x]; invgauss["BestFitParameters"] (* {\[Mu] -> 11.098760475474151, \[Lambda] -> 35.72763989840714} *) Show[ListPlot[datanew, PlotRange -> {{0, 20}, {0, 0.12}}], Plot[invgauss[x], {x, 0, 20}]] The fit isn't so great. I have to wonder why you want to fit a probability density function using a regression. Usually a probability density function is fit with a random sample from that distribution. If what you have is really a regression and you're just interested in the form of the density function being an inverse Gaussian, then usually translation and/or scaling parameters are usually added.