Add constraints to scipy.optimize.curve_fit?

微笑、不失礼 提交于 2019-12-22 05:24:42

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/38340477/add-constraints-to-scipy-optimize-curve-fit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!