为了初始化贝叶斯MCMC的参数,我需要计算beta和gamma的MLE,定义如下。
观察到Y_1
,...,Y_N
,Y_i
采用J
有序类别之一。存在一个潜在的连续随机变量Z_i
,其分布为N(x_i^T,beta)
,我们观察到Y_i
,其中Y_i=j
为gamma_(j-1)<Z_i<gamma_j
。
如何使用Python找到那些MLE?
我尝试过:
def lik(parameters):
#initialize parameters
gamma = parameters[0]
beta1 = parameters[1]
beta2 = parameters[2]
beta3 = parameters[3]
N = X.shape[0]
xb= []
#compute beta*X:
for i in range(N):
xb.append(beta1*X.iloc[i,0]+beta2*X.iloc[i,1]+beta3*X.iloc[i,2])
L = 0
for i in range(N):
if Y_m[i] ==0:
L += norm.cdf(-xb[i])
if Y_m[i] ==1:
L += (norm.cdf(gamma - xb[i]) - norm.cdf(-xb[i]))
if Y_m[i] ==2:
L += (1 - norm.cdf(gamma -xb[i]))
return -L
print(lik(np.array([1,2,3, 4])))
guess = np.array([1,1,1,1])
results = minimize(lik, guess, method = 'Nelder-Mead', options={'disp': True})
但这似乎不起作用...