简体   繁体   English

python 中的 MLE 用于 2 个参数

[英]MLE in python for 2 parameters

I have a data set X which i need to use to maximise the parameters by MLE.我有一个数据集 X,我需要使用它来通过 MLE 最大化参数。 I have the log likelihood function我有对数似然 function

def llh(alpha, beta):
  a = [0]*999
  for i in range(1, 1000):
    a[i-1] = (-0.5)*(((1/beta)*(X[i]-np.sin((alpha)*X[i-1])))**2)
  return sum(a)

I need to maximise this but i have no idea how.我需要最大化这个,但我不知道如何。 I can only think of plotting 3d graphs to find the maximum point but that gives me weird answers that are not what I want.我只能想到绘制 3d 个图表来找到最大值,但这给了我奇怪的答案,这不是我想要的。

This is the plot I got这是我拿到的plot

在此处输入图像描述

Is there any other possible way to get my maximum parameters or am I going about this the wrong way?有没有其他可能的方法来获取我的最大参数,或者我是否以错误的方式解决这个问题? My dataset model function is Xk = sin(alphaXk-1) + betaWk where Wk is normally distributed with mean 0 and sigma 1. Any help would be appreciated.Thank you!我的数据集 model function 是 Xk = sin(alphaXk-1) + betaWk,其中 Wk 服从均值 0 和 sigma 1 的正态分布。我们将不胜感激。谢谢!

You have to find the maximum of your likelihood numerically.你必须在数字上找到你的可能性的最大值。 In practice this is done by computing the negative (log) likelihood and using numerical minimization to find the most likely parameters of your model to describe your data.实际上,这是通过计算负(对数)似然并使用数值最小化来找到 model 最可能的参数来描述您的数据来完成的。 Make use of scipy.optimize.minimize to minimize your likelihood.使用scipy.optimize.minimize来最小化你的可能性。

I implemented a short example for normal distributed data.我为正常的分布式数据实现了一个简短的例子。

import numpy as np

from scipy.stats import norm
from scipy.optimize import minimize

def neg_llh(popt, X):
    return -np.log(norm.pdf(X, loc = popt[0], scale = popt[1])).sum()

# example data
X = np.random.normal(loc = 5, scale = 2, size = 1000)

# minimize log likelihood
res = minimize(neg_llh, x0 = [2, 2], args = (X))

print(res.x)
array([5.10023503, 2.01174199])

Since you are using sum I suppose the likelihood you defined above is already a (negative?) log-likelihood.由于您使用的是sum我想您在上面定义的可能性已经是(负数?)对数似然。

def neg_llh(popt, X):
    alpha = popt[0]
    beta = popt[1]

    return np.sum((-0.5)*(((1 / beta)*(X - np.sin((alpha) * X)))**2))

Try minimizing your negative likelihood.尽量减少你的负面可能性。 Using your plot you can make a good initial guess ( x0 ) about the values of alpha and beta .使用您的 plot 您可以对alphabeta的值做出很好的初始猜测 ( x0 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM