# is it possible to constrain the scipy.optimize.curve_fit function? Classic List Threaded 6 messages Open this post in threaded view
|

## 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
Open this post in threaded view
|

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

 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
Open this post in threaded view
|

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

 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?  Mathieu2012/5/16 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 _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user
Open this post in threaded view
|

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

 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