scipy.minimize错误:使用序列

时间:2015-05-27 20:26:51

标签: python scipy minimize

我想估计参数&k; k,ru,sigma'最大化功能' func' (' ru'表示r upperba)

' func'公式是compex,所以我想上传图片以显示这个公式,但我没有足够的声誉。

    import numpy as np

sigma,k,ru=0.01,0.001,5

p0=np.array([[0.01,0.01,6]])

p=np.array([[sigma,k,ru]])

def func(p,r):
   T=91/365
   y=1/(np.sqrt(2*(np.pi)*p[0]**2/(2*p[1])*(1-np.exp(-(2*p[1]*T)))))*np.exp((r-p[2]-np.exp(-(p[1]*T))*(r-p[2]))**2/(p[0]**2/((-4)*p[1])*(1-np.exp(-(2*p[1]*T)))))    
   return -y


from scipy.optimize import minimize

r=np.array([[1.45,2.5,2.6,1.67,1.2]])

# r has 1350 datas like this

res=minimize(func,p0,args=(r))
Traceback (most recent call last):

  File "<ipython-input-9-b94a05d2ede8>", line 1, in <module>
    res=minimize(func,p0,args=(r))

  File "C:\Users\hyun su\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py", line 419, in minimize
    return _minimize_bfgs(fun, x0, args, jac, callback, **options)

  File "C:\Users\hyun su\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 837, in _minimize_bfgs
    gfk = myfprime(x0)

  File "C:\Users\hyun su\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 282, in function_wrapper
    return function(*(wrapper_args + args))

  File "C:\Users\hyun su\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 616, in approx_fprime
    return _approx_fprime_helper(xk, f, epsilon, args=args)

  File "C:\Users\hyun su\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 556, in _approx_fprime_helper
    grad[k] = (f(*((xk + d,) + args)) - f0) / d[k]

ValueError: setting an array element with a sequence.

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

func这里接受一个向量,但它必须是一个或多个变量的标量函数,如scipy.optimize.minimize doc

中所示