Optimization 优化(scipy.optimize)L-BFGS-B包装参数将数组元素视为一个变量

Optimization 优化(scipy.optimize)L-BFGS-B包装参数将数组元素视为一个变量,optimization,parameters,scipy,wrapper,Optimization,Parameters,Scipy,Wrapper,我无法理解此错误的来源: 第327行,函数_包装中 返回函数(*(包装器参数+参数)) TypeError:SSVOptionPriceObjFunc()缺少1个必需的位置参数:“marketVolSurface” 相关代码如下: x0 = [1.0, 0.0] # (lambda0, rho) x0 = np.asarray(x0) args = (spot, 0.01*r, daysInYear, mktPrices, volSurface) # constraints: lambd0 &g

我无法理解此错误的来源:

第327行,函数_包装中 返回函数(*(包装器参数+参数)) TypeError:SSVOptionPriceObjFunc()缺少1个必需的位置参数:“marketVolSurface”

相关代码如下:

x0 = [1.0, 0.0] # (lambda0, rho)
x0 = np.asarray(x0)
args = (spot, 0.01*r, daysInYear, mktPrices, volSurface)
# constraints: lambd0 >0, -1<= rho <=1
boundsHere = ((0, None), (-1, 1))
res = minimize(SSVOptionPriceObjFunc, x0, args, method='L-BFGS-B', jac=None, 
bounds=boundsHere,options={'xtol': 1e-8, 'disp': True})
我的目的是找到(lambd0,rho)给出的最小值。从调试器中,我的初始猜测x0似乎被解释为单个变量,而不是向量,给出了缺少位置参数的错误。我曾尝试将x0作为列表、元组和ndarray传递;都失败了。有人能发现错误,或建议解决方法吗?先谢谢你

更新:我找到了一个解决方案:使用functools包中的包装函数来设置参数。 将工具作为ft导入 SSVOptionPriceObjFuncWrapper=ft.partial(SSVOptionPriceObjFunc,spot=spot, spotInterestRate=0.01*r,daysInYear=daysInYear,marketPrices=mktPrices, marketVolSurface=volSurface)

然后将SSVOOptionPriceObjFuncWrapper传递给args=None的最小值


感谢您的回复。

认真对待记录在案的
最小化
输入。您的工作是编写适合
最小化
功能的函数,而不是相反

scipy.optimize.minimize(fun, x0, args=(), 
    fun: callable
    The objective function to be minimized.

    fun(x, *args) -> float

    where x is an 1-D array with shape (n,) and args is a tuple of the fixed 
    parameters needed to completely specify the function.

(不用尝试):我认为函数中的
lambda0
将传递一个大小为2的1d数组。这可能不是你所期望的。尝试将函数更改为:
def SSVOptionPriceObjFunc(x,spot,spotInterestRate,daysInYear,marketPrices,marketVolSurface)
plus inside
lambda0,rho=x
也可以看到,这实际上意味着行为:
fun(x,*args)->float
目标函数的第一个参数是
x0
,迭代变量。其余的从
args
参数获取值,并且是固定的。您必须了解如何将
lambda0
rho
组合到一个数组中。
scipy.optimize.minimize(fun, x0, args=(), 
    fun: callable
    The objective function to be minimized.

    fun(x, *args) -> float

    where x is an 1-D array with shape (n,) and args is a tuple of the fixed 
    parameters needed to completely specify the function.