Fitting a power law to data

I wanted to fit a power law function to data, not a polynomial. Here’s how I did it.

I used the least squares method.

How to fit a power law to the data

power fit to L4 data

Example of a power law fit to data

If you prefer other types of functions, just change the function form to whatever you want to fit when you define it.

The inputs for leastsq are the error function (difference between data and a function you want to fit) and initial conditions. When full_output = nonzero, it returns the covariance matrix in addition to the parameters that minimize the sum of squares of the error function.

What would you fit a power law function to?

One thought on “Fitting a power law to data

  1. I attended the Summer School on Statistics for Astronomers 2017 at Penn State University last week and came to know that least square fitting is not a good idea to fit the power law models. If you need more accurate results, use other methods like maximum likelihood method. You also may want to see fittings for Pareto distribution for which they have well defined analytic solutions for fitting power laws.

Leave a Reply

Your email address will not be published. Required fields are marked *