-1
我要估計參數 'K,RU,西格瑪' 那maximumize功能 'FUNC' ( 'RU' 裝置R upperba)scipy.minimize錯誤:設置一個數組元素與序列
The'func '公式是強制性的,所以我想上傳圖片來展示這個公式,但是我沒有足夠的聲望。
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.
我該如何解決這個問題?
爲什麼'p'和'p0' 2維?你習慣於不支持任意維數組的語言嗎? – user2357112
p和p0應該是一維數組?那麼p = np.array(σ,k,rup)是正確的? – hyunsu
[理解scipy.optimize.minimize()函數的錯誤]的可能的重複(http://stackoverflow.com/questions/22844347/understanding-the-error-for-scipy-optimize-minimize-function) – kazemakase