我刚刚遇到了这个问题,我知道它的悠久历史,但是我希望其他人可以从中受益。尽管前面的评论对ML优化是一个很好的描述,但是没有人提供伪代码来实现它。 Python在Scipy中有一个最小化器可以做到这一点。这是线性回归的伪代码。
# import the packages
import numpy as np
from scipy.optimize import minimize
import scipy.stats as stats
import time
# Set up your x values
x = np.linspace(0, 100, num=100)
# Set up your observed y values with a known slope (2.4), intercept (5), and sd (4)
yObs = 5 + 2.4*x + np.random.normal(0, 4, 100)
# Define the likelihood function where params is a list of initial parameter estimates
def regressLL(params):
# Resave the initial parameter guesses
b0 = params[0]
b1 = params[1]
sd = params[2]
# Calculate the predicted values from the initial parameter guesses
yPred = b0 + b1*x
# Calculate the negative log-likelihood as the negative sum of the log of a normal
# PDF where the observed values are normally distributed around the mean (yPred)
# with a standard deviation of sd
logLik = -np.sum( stats.norm.logpdf(yObs, loc=yPred, scale=sd) )
# Tell the function to return the NLL (this is what will be minimized)
return(logLik)
# Make a list of initial parameter guesses (b0, b1, sd)
initParams = [1, 1, 1]
# Run the minimizer
results = minimize(regressLL, initParams, method='nelder-mead')
# Print the results. They should be really close to your actual values
print results.x
这对我来说很棒。当然,这只是基础。它不会分析或给出参数估计的CI,但它是一个开始。您也可以使用ML技术来查找ODE和其他模型的估算值,如我在此处所述。
我知道这个问题很旧,希望您从那时起就已经解决了,但希望其他人也会受益。
0
我需要编写一个最大似然估计器,以估计某些玩具数据的均值和方差。我有一个带有
numpy.random.randn(100)
创建的100个样本的向量。数据应具有零均值和单位方差高斯分布。我检查了维基百科和一些其他资源,但是由于没有统计背景,我有点困惑。
最大似然估计器是否有任何伪代码?我得到了MLE的直觉,但是我不知道从哪里开始编码。
Wiki表示采用对数可能性的argmax。我的理解是:我需要使用不同的参数来计算对数似然率,然后我将采用给出最大概率的参数。我没有得到的是:首先我将在哪里找到参数?如果我随机尝试不同的均值和方差以获得高概率,那么我什么时候应该停止尝试?