# power law fitting of data

Hi,

I have (x,y) data that I want to fit to the formula
y = a * x^b
to determine a and b. How can I do it? The current
manual only lists linear fit and polynomial fit.

Or, putting it in a more general setting, is there a
module to do fitting to an arbitrary function?
It would be something like

pars = fit(x, y, func)

where func is a function like

y = func(x, pars)

with pars a 1-D array.

Thanks,
Ping

Ping,

You should investigate scipy.optimize.lsqFit for using least squares
to fit an arbitrary function and scipy.odr for regular or orthogonal
least squares fitting.

barry

···

On Dec 5, 2007 9:45 PM, Ping Yeh <ping.nsr.yeh@...287...> wrote:

Hi,

I have (x,y) data that I want to fit to the formula
y = a * x^b
to determine a and b. How can I do it? The current
manual only lists linear fit and polynomial fit.

Or, putting it in a more general setting, is there a
module to do fitting to an arbitrary function?
It would be something like

pars = fit(x, y, func)

where func is a function like

y = func(x, pars)

with pars a 1-D array.

Thanks,
Ping

-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell. From the desktop to the data center, Linux is going
mainstream. Let it simplify your IT future.
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi,

You could use another package, like openopt and the generic optimizers that give you what you want provided that you create at least the gradient of the function (I didn’t create a class that can numerically derive a fit function).

For instance http://projects.scipy.org/scipy/scikits/wiki/Optimization/tutorial#FittingData gives you an example.

Matthieu

2007/12/6, Ping Yeh <ping.nsr.yeh@…287…>:

···

Hi,

I have (x,y) data that I want to fit to the formula
y = a * x^b
to determine a and b. How can I do it? The current
manual only lists linear fit and polynomial fit.

Or, putting it in a more general setting, is there a

module to do fitting to an arbitrary function?
It would be something like

pars = fit(x, y, func)

where func is a function like

y = func(x, pars)

with pars a 1-D array.

Thanks,

Ping

SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell. From the desktop to the data center, Linux is going

mainstream. Let it simplify your IT future.

Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

French PhD student
Website : http://miles.developpez.com/
Blogs : http://matt.eifelle.com
and http://blog.developpez.com/?blog=92

Ahhh... Yes, I should turn to scipy for this. Great suggestion!

I'll look for least square fit and maximum likelihood fit.

My next question is about plotting any function f(x) on top of data.
I know I could just produce enough (x,y) points and plot(x,y).
But a convenience function like plot(f, minx, maxx) would be great.
If there is no existing one I can write one.

cheers,
Ping

···

On Dec 6, 2007 2:50 PM, Matthieu Brucher <matthieu.brucher@...287...> wrote:

Hi,

You could use another package, like openopt and the generic optimizers that
give you what you want provided that you create at least the gradient of
the function (I didn't create a class that can numerically derive a fit
function).
For instance
http://projects.scipy.org/scipy/scikits/wiki/Optimization/tutorial#FittingData
gives you an example.

Matthieu

2007/12/6, Ping Yeh <ping.nsr.yeh@...287...>:
>
>
>
> Hi,
>
> I have (x,y) data that I want to fit to the formula
> y = a * x^b
> to determine a and b. How can I do it? The current
> manual only lists linear fit and polynomial fit.
>
> Or, putting it in a more general setting, is there a
> module to do fitting to an arbitrary function?
> It would be something like
>
> pars = fit(x, y, func)
>
> where func is a function like
>
> y = func(x, pars)
>
> with pars a 1-D array.
>
> Thanks,
> Ping
>
>
> -------------------------------------------------------------------------
> SF.Net email is sponsored by: The Future of Linux Business White Paper
> from Novell. From the desktop to the data center, Linux is going
> mainstream. Let it simplify your IT future.
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>

--
French PhD student
Website : http://miles.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell. From the desktop to the data center, Linux is going
mainstream. Let it simplify your IT future.
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Ping Yeh wrote:

Hi,

I have (x,y) data that I want to fit to the formula
y = a * x^b
to determine a and b. How can I do it? The current
manual only lists linear fit and polynomial fit.

If you just want quick power law fit without turning to the other
solutions, you can just transform your variables to make it a linear fit
problem:

log(y) = log(a * x^b) = log(a) + b * log(x)

So just do the linear regression with the logarithms of x and y, and the
slope you get back will be b, and the intercept will be log(a).

Ryan

···

--
Ryan May
School of Meteorology
University of Oklahoma

Hi,

I just happened to do the same thing two days ago. If you want uncertainties as well, here is some code that uses scipy.optimize. I just put up a preliminary example on the scipy wiki:

http://www.scipy.org/Cookbook/FittingData?action=show

Not the cleanest or most sophisticated code in the world, but it works for simple things.

Cheers,
Jessica

···

On Dec 6, 2007, at 8:06 AM, Ryan May wrote:

Ping Yeh wrote:

Hi,

I have (x,y) data that I want to fit to the formula
y = a * x^b
to determine a and b. How can I do it? The current
manual only lists linear fit and polynomial fit.

If you just want quick power law fit without turning to the other
solutions, you can just transform your variables to make it a linear fit
problem:

log(y) = log(a * x^b) = log(a) + b * log(x)

So just do the linear regression with the logarithms of x and y, and the
slope you get back will be b, and the intercept will be log(a).

Ryan

--
Ryan May