Does anybody understand how to make this code work?

105 views Asked by At

I am trying to estimate a multiple linear probit model (killer_apps is the limited dependent variable) using the maximum likelihood approach. Therefore, in this code, I am trying to estimate the regressors and the respective standard errors. Can anybody tell me why do I get this error and what, in general, am I doing wrong? I was trying to expand the code from a sample used for a simple linear probit model

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'
1

There are 1 answers

0
smcs On

Tough, because the error is in line 691. Plus there are no comments at all and the passing of arguments seems very convoluted. Start debugging and when it fails at line 691, check the actual values of *((xk,) + args). Apparently, only two values are passed to probitlik() while five are required--that is explained in the last line you posted in the question.