本文介绍了如何使用 scipy.optimize.minimize 进行最大似然回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 scipy.optimize.minimize 进行最大似然回归?我特别想在这里使用 minimize 函数,因为我有一个复杂的模型,需要添加一些约束.我目前正在尝试使用以下内容的简单示例:

How can I do a maximum likelihood regression using scipy.optimize.minimize? I specifically want to use the minimize function here, because I have a complex model and need to add some constraints. I am currently trying a simple example using the following:

from scipy.optimize import minimize

def lik(parameters):
    m = parameters[0]
    b = parameters[1]
    sigma = parameters[2]
    for i in np.arange(0, len(x)):
        y_exp = m * x + b
    L = sum(np.log(sigma) + 0.5 * np.log(2 * np.pi) + (y - y_exp) ** 2 / (2 * sigma ** 2))
    return L

x = [1,2,3,4,5]
y = [2,3,4,5,6]
lik_model = minimize(lik, np.array([1,1,1]), method='L-BFGS-B', options={'disp': True})

当我运行它时,收敛失败.有谁知道我的代码有什么问题?

When I run this, convergence fails. Does anyone know what is wrong with my code?

我运行此程序时收到的消息是ABNORMAL_TERMINATION_IN_LNSRCH".我使用的算法与我在 R 中使用 optim 的算法相同.

The message I get running this is 'ABNORMAL_TERMINATION_IN_LNSRCH'. I am using the same algorithm that I have working using optim in R.

推荐答案

谢谢 Aleksander.你是正确的,我的似然函数是错误的,而不是代码.使用我在维基百科上找到的公式,我将代码调整为:

Thank you Aleksander. You were correct that my likelihood function was wrong, not the code. Using a formula I found on wikipedia I adjusted the code to:

import numpy as np
from scipy.optimize import minimize

def lik(parameters):
    m = parameters[0]
    b = parameters[1]
    sigma = parameters[2]
    for i in np.arange(0, len(x)):
        y_exp = m * x + b
    L = (len(x)/2 * np.log(2 * np.pi) + len(x)/2 * np.log(sigma ** 2) + 1 /
         (2 * sigma ** 2) * sum((y - y_exp) ** 2))
    return L

x = np.array([1,2,3,4,5])
y = np.array([2,5,8,11,14])
lik_model = minimize(lik, np.array([1,1,1]), method='L-BFGS-B')
plt.scatter(x,y)
plt.plot(x, lik_model['x'][0] * x + lik_model['x'][1])
plt.show()

现在它似乎可以工作了.

Now it seems to be working.

感谢您的帮助!

这篇关于如何使用 scipy.optimize.minimize 进行最大似然回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 17:18