解决此类问题的一般方法是使数据的(正规化)可能性最大化。
LL(y0,a,b,σ0,c,d)=∑i=1nlogϕ(yi,y0+axi+bti,σ0+cxi+dti)
ϕ(x,μ,σ)=12π−−√σe−(x−μ)22σ2
θ^θ=(y0,a,b,σ0,c,d)
Hθnθ^H−1
这是Python中的示例代码:
import scipy
import numpy as np
# generate toy data for the problem
np.random.seed(1) # fix random seed
n = 1000 # fix problem size
x = np.random.normal(size=n)
t = np.random.normal(size=n)
mean = 1 + x * 2 + t * 3
std = 4 + x * 0.5 + t * 0.6
y = np.random.normal(size=n, loc=mean, scale=std)
# create negative log likelihood
def neg_log_lik(theta):
est_mean = theta[0] + x * theta[1] + t * theta[2]
est_std = np.maximum(theta[3] + x * theta[4] + t * theta[5], 1e-10)
return -sum(scipy.stats.norm.logpdf(y, loc=est_mean, scale=est_std))
# maximize
initial = np.array([0,0,0,1,0,0])
result = scipy.optimize.minimize(neg_log_lik, initial)
# extract point estimation
param = result.x
print(param)
# extract standard error for confidence intervals
std_error = np.sqrt(np.diag(result.hess_inv))
print(std_error)
σσ10−10
该代码产生的结果(参数估计值及其标准误差)为:
[ 0.8724218 1.75510897 2.87661843 3.88917283 0.63696726 0.5788625 ]
[ 0.15073344 0.07351353 0.09515104 0.08086239 0.08422978 0.0853192 ]
您可以看到估计值接近其真实值,这证实了此模拟的正确性。