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.

6
  • 2
    What does "seems not to work well" mean? Errors (provide full traceback)? Unexpected output (provide inputs and expected and actual output)? Commented Jun 8, 2014 at 9: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?" Commented Jun 8, 2014 at 10:47
  • The curve is not fitting the data, so, probably, my idea is not the right one, methodologically speaking. Commented Jun 8, 2014 at 10:48
  • If code is not necessary, and this is a methodological question, it is not on-topic here. Try stats.stackexchange.com Commented Jun 8, 2014 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 Commented Jun 8, 2014 at 10:52

1 Answer 1

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 [email protected]

Sign up to request clarification or add additional context in comments.

2 Comments

Zunzun migrated to github. google search results
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 github.com/zunzun/zunzunsite (Python 2.X) and at github.com/zunzun/zunzunsite3 (Python 3).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.