我正在用scipy.optimize.minimize求一个函数的最大值,有人能帮我找出错误吗?

tcbh2hod  于 2022-11-29  发布在  其他
关注(0)|答案(1)|浏览(96)
price = pd.read_csv('C:\\Users\\mypath\\price.csv', index_col= [0,1], usecols=[0,5,6])
yt = price.loc['AUS']
yt = yt.pct_change().dropna().values

def p(u, sigma, pi):
    
    d = pi / (2*np.pi*sigma)**0.5 * np.exp(-(yt-u)**2 / (2*sigma**2))
    
    return d

def Lf(u, sigma, pi):
    
    prob = p(u[0], sigma[0], pi[0]) + p(u[1], sigma[1], pi[1])
    L = np.sum(-np.log(prob))
    
    return L

pi_init = (0.5,0.5)
sigma_init = (0.1,0.1)
u_init = (0.1,0.1)

res = opt.minimize(Lf, (u_init, sigma_init, pi_init), method='L-BFGS-B')

如果我运行Lf(),我会得到一个真实的,但当我运行minimize时,我会得到以下错误消息:
TypeError:Lf()缺少2个必需的位置参数:“西格玛”和“圆周率”
这条信息对我来说没有意义...

yyyllmsg

yyyllmsg1#

x0 = np.array([...]) # suitably shaped numpy array of your init values
res = opt.minimize(Lf, x0, args=(u_init, sigma_init, pi_init), method='L-BFGS-B')

也许你可以这样打电话试试

相关问题