is it possible to constrain the scipy.optimize.curve_fit function?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

is it possible to constrain the scipy.optimize.curve_fit function?

servant mathieu
Dear scipy users,
 
I'm trying to fit to data a power law of the form :
 

 

def func (x, a,b, r):

      return r + a*np.power(x,-b)

 

 

I would like to constrain the curve_fit routine to only allow positive parameter values. How is it possible to do so?

 

Kind regards,

Mathieu 


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: is it possible to constrain the scipy.optimize.curve_fit function?

David Baddeley
The quick and dirty way is to do a variable substitution with the square of your parameter and fit, e.g. r + (a**2)*np.power(x, -b**2)
You can take the sqrt of the parameters later.


From: servant mathieu <[hidden email]>
To: [hidden email]
Sent: Thursday, 17 May 2012 4:20 AM
Subject: [SciPy-User] is it possible to constrain the scipy.optimize.curve_fit function?

Dear scipy users,
 
I'm trying to fit to data a power law of the form :
 
 
def func (x, a,b, r):
      return r + a*np.power(x,-b)
 
 
I would like to constrain the curve_fit routine to only allow positive parameter values. How is it possible to do so?
 
Kind regards,
Mathieu 

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user



_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: is it possible to constrain the scipy.optimize.curve_fit function?

servant mathieu
Hi David,
That's really dirty but it works thanks :-). More generally, is there a standard procedure to constrain a specific parameter to fall in a specific range (e.g., [200, 400]) during the fitting process?
 
Mathieu

2012/5/16 David Baddeley <[hidden email]>
The quick and dirty way is to do a variable substitution with the square of your parameter and fit, e.g. r + (a**2)*np.power(x, -b**2)
You can take the sqrt of the parameters later.


From: servant mathieu <[hidden email]>
To: [hidden email]
Sent: Thursday, 17 May 2012 4:20 AM
Subject: [SciPy-User] is it possible to constrain the scipy.optimize.curve_fit function?

Dear scipy users,
 
I'm trying to fit to data a power law of the form :
 
 
def func (x, a,b, r):
      return r + a*np.power(x,-b)
 
 
I would like to constrain the curve_fit routine to only allow positive parameter values. How is it possible to do so?
 
Kind regards,
Mathieu 

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user



_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user



_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: is it possible to constrain the scipy.optimize.curve_fit function?

federico vaggi-2
In reply to this post by servant mathieu
Afaik,

there was a big discussion about this a while ago, and the short answer is, currently there is no 'automatic' way to do it.  However, in your case, it's pretty easy.

Simply define:

def func (x, a,b, r):
     a = abs(a)
     b = abs(b)
     r = abs(r)
     return r + a*np.power(x,-b)

And that will do the trick.  If you need to more complex boundaries, you can simply use a combination of period functions with a given amplitude or what have you.  Alternatively, there are *a lot* of optimization libraries available for Python that are not a part of scipy that offer the possibility to specify boundaries.

For example:

http://newville.github.com/lmfit-py/
http://ab-initio.mit.edu/wiki/index.php/NLopt_Python_Reference

Federico



Date: Wed, 16 May 2012 18:20:27 +0200
From: servant mathieu <[hidden email]>
Subject: [SciPy-User] is it possible to constrain the
       scipy.optimize.curve_fit function?
To: [hidden email]
Message-ID:
       <[hidden email]>
Content-Type: text/plain; charset="iso-8859-1"

Dear scipy users,

I'm trying to fit to data a power law of the form :




def func (x, a,b, r):

     return r + a*np.power(x,-b)




I would like to constrain the curve_fit routine to only allow
positive parameter values. How is it possible to do so?



Kind regards,

Mathieu

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: is it possible to constrain the scipy.optimize.curve_fit function?

David Baddeley
In reply to this post by servant mathieu

I'd caution against using abs, as abs(x) is not differentiable around 0 and could cause a gradient descent solver to get stuck/confused. x**2 on the other hand is fully differentiable, but requires you to take the sqrt of the parameters after fitting.



------------------------------
On Thu, May 17, 2012 9:51 PM NZST federico vaggi wrote:

>Afaik,
>
>there was a big discussion about this a while ago, and the short answer is,
>currently there is no 'automatic' way to do it.  However, in your case,
>it's pretty easy.
>
>Simply define:
>
>def func (x, a,b, r):
>     a = abs(a)
>     b = abs(b)
>     r = abs(r)
>     return r + a*np.power(x,-b)
>
>And that will do the trick.  If you need to more complex boundaries, you
>can simply use a combination of period functions with a given amplitude or
>what have you.  Alternatively, there are *a lot* of optimization libraries
>available for Python that are not a part of scipy that offer the
>possibility to specify boundaries.
>
>For example:
>
>http://newville.github.com/lmfit-py/
>http://ab-initio.mit.edu/wiki/index.php/NLopt_Python_Reference
>
>Federico
>
>
>
>Date: Wed, 16 May 2012 18:20:27 +0200
>> From: servant mathieu <[hidden email]>
>> Subject: [SciPy-User] is it possible to constrain the
>>        scipy.optimize.curve_fit function?
>> To: [hidden email]
>> Message-ID:
>>        <[hidden email]
>> >
>> Content-Type: text/plain; charset="iso-8859-1"
>>
>> Dear scipy users,
>>
>> I'm trying to fit to data a power law of the form :
>>
>>
>>
>>
>> def func (x, a,b, r):
>>
>>      return r + a*np.power(x,-b)
>>
>>
>>
>>
>> I would like to constrain the curve_fit routine to only allow
>> positive parameter values. How is it possible to do so?
>>
>>
>>
>> Kind regards,
>>
>> Mathieu
>>

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: is it possible to constrain the scipy.optimize.curve_fit function?

Pauli Virtanen-3
In reply to this post by federico vaggi-2
17.05.2012 11:51, federico vaggi kirjoitti:
[clip]
> And that will do the trick.  If you need to more complex boundaries, you
> can simply use a combination of period functions with a given amplitude
> or what have you.  Alternatively, there are *a lot* of optimization
> libraries available for Python that are not a part of scipy that offer
> the possibility to specify boundaries.

Note that Scipy has several solvers that support bounds in optimization
problems --- to use those for least squares, you'll just need to do
"return (r**2).sum()" yourself. This is AFAIK also what lmfit does, in
addition to clipping parameter values within the bounds in the residual
function (I'm not sure how robust the results such clipping produces are).

        Pauli

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user