Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add constraints to scipy.optimize.curve_fit?

I have the option to add bounds to sio.curve_fit. Is there a way to expand upon this bounds feature that involves a function of the parameters? In other words, say I have an arbitrary function with two or more unknown constants. And then let's also say that I know the sum of all of these constants is less than 10. Is there a way I can implement this last constraint?

import numpy as np
import scipy.optimize as sio
def f(x, a, b, c):
    return a*x**2 + b*x + c

x = np.linspace(0, 100, 101)
y = 2*x**2 + 3*x + 4

popt, pcov = sio.curve_fit(f, x, y, \
     bounds = [(0, 0, 0), (10 - b - c, 10 - a - c, 10 - a - b)]) # a + b + c < 10

Now, this would obviously error, but I think it helps to get the point across. Is there a way I can incorporate a constraint function involving the parameters to a curve fit?

Thanks!

like image 533
Peter Avatar asked Jul 12 '16 23:07

Peter


2 Answers

With lmfit, you would define 4 parameters (a, b, c, and delta). a and b can vary freely. delta is allowed to vary, but has a maximum value of 10 to represent the inequality. c would be constrained to be delta-a-b (so, there are still 3 variables: c will vary, but not independently from the others). If desired, you could also put bounds on the values for a, b, and c. Without testing, your code would be approximately::

import numpy as np
from lmfit import Model, Parameters

def f(x, a, b, c):
    return a*x**2 + b*x + c

x = np.linspace(0, 100.0, 101)
y = 2*x**2 + 3*x + 4.0

fmodel = Model(f)
params = Parameters()
params.add('a', value=1, vary=True)
params.add('b', value=4, vary=True)
params.add('delta', value=5, vary=True, max=10)
params.add('c', expr = 'delta - a - b')

result = fmodel.fit(y, params, x=x)
print(result.fit_report())

Note that if you actually get to a situation where the constraint expression or bounds dictate the values for the parameters, uncertainties may not be estimated.

like image 118
M Newville Avatar answered Sep 19 '22 12:09

M Newville


curve_fit and least_squares only accept box constraints. In scipy.optimize, SLSQP can deal with more complicated constraints. For curve fitting specifically, you can have a look at lmfit package.

like image 45
ev-br Avatar answered Sep 16 '22 12:09

ev-br