Least Squares Fitting—Power Law Fit

Author

Eric W. Weisstein
August 6, 2002

This notebook downloaded from http://mathworld.wolfram.com/notebooks/Statistics/LeastSquaresFittingPowerLaw.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html.

©2005 Wolfram Research, Inc. except for portions noted otherwise

Notes

This is some of Eric's very old code.  It should be rewritten and cleaned up.

Code

PowerFit[x_List,y_List]:=Module[
		{n=Length[x],i,slnxlny,slnx,slny,slnx2,b},
		slnxlny=Sum[Log[x[[i]]]Log[y[[i]]],{i,n}];
		slnx=Sum[Log[x[[i]]],{i,n}];
		slny=Sum[Log[y[[i]]],{i,n}];
		slnx2=Sum[Log[x[[i]]]^2,{i,n}];
		b=(n slnxlny-slnx slny)/(n slnx2-slnx^2);
		{Exp[(slny-b slnx)/n],b}
]

x=N[Table[i,{i,50}]];
y=Table[3.4x[[i]]^2.45+ Random[Real,{-5,5}],{i,50}];
{a,b}=PowerFit[x, y]

yfit=Table[a i^b,{i,50}];
Show[{ListPlot[y,DisplayFunction->Identity],ListPlot[yfit,PlotJoined->True,DisplayFunction->Identity]},
  DisplayFunction->$DisplayFunction]