我正在尝试使用最大似然方法来估计多重线性概率模型(killer_apps 是有限因变量)。因此,在这段代码中,我试图估计回归量和各自的标准误差。谁能告诉我为什么我会收到这个错误,一般来说,我做错了什么?我试图从用于简单线性概率模型的示例中扩展代码
def probitlik(beta,y,x1,x2,x3):
beta1 = beta[0]
beta2 = beta[1]
beta3 = beta[2]
beta4 = beta[3]
prob = stats.norm.cdf(beta1 + beta2*x1 + beta3*x2 + beta4*x3)
vlog=y*np.log(prob)+(1-y)*np.log(1-prob)
loglik=-1*sum(vlog)
return(loglik)
probitmodel = minimize(probitlik, x0 = np.asarray([0.5,0.55,0.6,1]),args = (killer_app,size,price,score),method='BFGS')
def hessian(x0,*args):
epsilon=1.e-3
f1 = approx_fprime(x0,probitlik,*args)
n = x0.shape[0]
hessian = np.zeros ([n,n])
xx = x0
for j in range(0,n):
xx0 = xx[j]
xx[j] = xx0 + epsilon
f2 = approx_fprime(x0,probitlik,*args)
hessian[:,j] = (f2 - f1)/epsilon
xx[j] = xx0
return hessian
eps = 1e-07
hess = hessian(probitmodel.x,[eps, np.sqrt(200) * eps],[killer_app,size, price, score])
inv_hess = np.linalg.inv(hess)
std_beta = np.sqrt(np.diag(inv_hess))
print(std_beta)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-b78abca11ff5> in <module>()
26
27 eps = 1e-07
---> 28 hess = hessian(probitmodel.x,[eps, np.sqrt(200) * eps],[killer_app,size, price, score])
29 inv_hess = np.linalg.inv(hess)
30 std_beta = np.sqrt(np.diag(inv_hess))
2 frames
/usr/local/lib/python3.6/dist-packages/scipy/optimize/optimize.py in _approx_fprime_helper(xk, f, epsilon, args, f0)
689 """
690 if f0 is None:
--> 691 f0 = f(*((xk,) + args))
692 grad = numpy.zeros((len(xk),), float)
693 ei = numpy.zeros((len(xk),), float)
TypeError: probitlik() missing 3 required positional arguments: 'x1', 'x2', and 'x3'