0

I have some data (x and y coordinates) coming from a study and I have to plot them and to find the best curve that fits data. My curves are:

  • polynomial up to 6th degree;
  • power law;
  • exponential.

I am able to find the best fit for polynomial with

while(i < 6):
    coefs, val = poly.polyfit(x, y, i, full=True)

and I take the degree that minimizes val.

When I have to fit a power law (the most probable in my study), I do not know how to do it correctly. This is what I have done. I have applied the log function to all x and y and I have tried to fit it with a linear polynomial. If the error (val) is lower than the others polynomial tried before, I choose the power law function(naturally if m of the line is negative). Am I correct?

Now how can I reconstruct my power law starting from the line y = mx + q in order to draw it with the original points? I need also to display the function found.

I have tried with:

def power_law(x, m, q):
    return q * (x**m)

using

x_new = np.linspace(x[0], x[-1], num=len(x)*10)
y1 = power_law(x_new, coefs[0], coefs[1])
popt, pcov = curve_fit(power_law, x_new, y1)

but the resulting curve is not fitting the data.

Nadir
  • 139
  • 1
  • 3
  • 11
  • 2
    What does *"seems not to work well"* mean? Errors (provide full traceback)? Unexpected output (provide inputs and expected and actual output)? – jonrsharpe Jun 08 '14 at 09:20
  • This is a methodological question, I do not ask for a complete code, code is not necessary here. I am not asking it. There is a question "Am I correct? How to reconstruct the power law?" – Nadir Jun 08 '14 at 10:47
  • The curve is not fitting the data, so, probably, my idea is not the right one, methodologically speaking. – Nadir Jun 08 '14 at 10:48
  • If code is not necessary, and this is a methodological question, it is not on-topic here. Try http://stats.stackexchange.com – jonrsharpe Jun 08 '14 at 10:52
  • are you using the correct distribution that describes your data? I.E the power law. if you think your data follows a power law distribution, then it should fit according to your return q*(x**m) model. THE MISTAKE I BELIEVE YOU ARE DOING IS using y1 in your curve_fit.. YOU SHOULD USE y of the data – Srivatsan Jun 08 '14 at 10:52
  • when you say you have x and y, just use curve_fit(power_law,x,y) when you are so sure that your power law model fits the curve. TRY VARYING THE INITIAL PARAMETERS UNTIL YOU GET YOUR CHI-SQUARE TO A MINIMUM – Srivatsan Jun 08 '14 at 10:57

1 Answers1

3

If you google the phrase "curve fitting", my web site is the top return - so I know a bit about this sort of thing.

I recommend not making any log or other transform of the data, as scipy has a nonlinear solver that is perfect for this type of fitting. Look at:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

I use the scipy nonlinear solver on my web site, which can directly fit your data online. Try:

http://zunzun.com/Equation/2/Power/Standard%20Power/

and to ensure there was no experimentally-introduced offset, such as a DC offset voltage for example, try:

http://zunzun.com/Equation/2/Power/Standard%20Power%20With%20Offset/

One problem you may run in to with non-linear fitting is choice of a suitable starting set of parameters for the non-linear solver to iteratively refine. The BSD-licensed source code for the web site uses a genetic algorithm to determine a starting point automatically, so you may want to try it yourself. It comes with many examples, including a "function finder" that fits hundreds of equations and ranks them - which you can also try online. The source code is at the Google Code Repository at:

https://code.google.com/p/pyeq2/

or links to zipped and tgz'd source distributions are at the bottom of every page on the web site.

Please contact me directly if you have any questions, I will be glad to help. I love this stuff.

James zunzun@zunzun.com

James Phillips
  • 4,526
  • 3
  • 13
  • 11
  • Zunzun migrated to github. [google search results](https://www.google.com/search?q=zunzun%20github&ie=utf-8&oe=utf-8) –  Mar 08 '17 at 10:38
  • I became too blind to keep the site running, my apologies. I can still see enlarged text with my right eye, so I can read and respond to email. The site source code for zunzun.com is at https://github.com/zunzun/zunzunsite/ (Python 2.X) and at https://github.com/zunzun/zunzunsite3/ (Python 3). – James Phillips Mar 09 '17 at 12:48