is it possible to constrain the scipy.optimize.curve_fit function?
![](https://secure.gravatar.com/avatar/6ea880631d63d0c297d751d85af262a1.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/1017b035e6dfe852e9cff6fe6da7bb22.jpg?s=120&d=mm&r=g)
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 <servant.mathieu@gmail.com> To: scipy-user@scipy.org 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 SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/6ea880631d63d0c297d751d85af262a1.jpg?s=120&d=mm&r=g)
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 <david_baddeley@yahoo.com.au>
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 <servant.mathieu@gmail.com> *To:* scipy-user@scipy.org *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 SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
participants (2)
-
David Baddeley
-
servant mathieu