[SciPy-User] is it possible to constrain the scipy.optimize.curve_fit function?

David Baddeley david_baddeley@yahoo.com...
Thu May 17 14:47:43 CDT 2012


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 <servant.mathieu@gmail.com>
>> Subject: [SciPy-User] is it possible to constrain the
>>        scipy.optimize.curve_fit function?
>> To: scipy-user@scipy.org
>> Message-ID:
>>        <CALnu5bM+c9L7taG_CBHdJhw7xpe5amHSVBRUEX7pa7gnRN+-7Q@mail.gmail.com
>> >
>> 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
>>



More information about the SciPy-User mailing list