/*
**  MLEGAME.SRC    Procedure that estimates the structural parameters 
**                 of a model of entry of incomplete information
**                 using MAximum Likelihood
**
**  by Victor Aguirregabiria
**  Last revision: May, 2001
**
**  ---------------------------------------------------------------------
**                              MODEL
**  ---------------------------------------------------------------------
**      Profits:
**      --------
**      No entry (ai = 0):
**          Ui(0,a_i,xi,ei) = beta0 + beta'*xi + gama'*z - delta*ln(Sum(aj) + 1)
**      Entry (ai = 1):
**          Ui(1,a_i,xi,ei) = beta0 + beta'*xi + gama'*z - delta*ln(Sum(aj) + 1)
**
**      where:
**          xi = (kx x 1) vector of firm i characteristics
**          z  = (kz x 1) vector of market characteristics
**          ai = Indicator of entry for firm i: belongs to {0,1}
**          a_i = (nplayer x 1) vector with aj's for all firm except i
**          nplayer = Number of potential entrants
**          beta0, beta, gama, delta = Parameters
**          ei(ai) = Private information: iid extreme valued distributed
**
**  ----------------------------------------------------------------------------
**
**  FORMAT:
**  { bmle , varbmle } = mlegame(theta0) ;
**
**  Inputs:
**
**      theta0  - (kx+kz+2 x 1) vector of parameters: First, the constant
**                term, second the parameters associated to x0, third the 
**
**  Outputs:
**
**      bmle    - (kx+kz+2 x 1) vector with estimated parameters
**
**      varbmle - (kx+kz+2 x kx+kz+2) matrix with estimated variances
**                covariance matrices of the MLE
**
**  Important note!!!
**  -----------------
**      The following variables are GLOBALS and should have 
**      the same names in the program
**
**      aobs    - (nplayer*numt x 1) vector with observations of the players'
**                decisions of entry (1 = entry ; 0 = no entry)
**                Ordered first by market and then by player
**
**      xobs    - (nplayer*numt x kx) vector with observations x 
**                Not including constant term
**                Rows are ordered first by market and then by player
**
**      zobs    - (numt x kz) vector with observations z 
**                Rows are ordered by market
**
**      nplayer - Number of players in the game (potential entrants)
**
**      numt    - Number of markets
**
*/

proc (1) = likegame(theta) ;
  local pstari, t, x0, z0, prob0, index1, index2, 
        buff1, buff2, llike ;
  prob0 = 0.5*ones(nplayer,1) ;
  pstari = zeros(numt*nplayer,1) ;
  t=1 ;
  do while t<=numt ;  
    index1 = (t-1)*nplayer + 1 ;
    index2 = t*nplayer ;
    x0 = xobs[index1:index2,.] ;
    z0 = zobs[t,.] ;
    {pstari[index1:index2],buff1,buff2} =
             equiprob(x0,z0,theta,prob0) ;
    t=t+1 ;
  endo ;
  llike = aobs.*ln(pstari) + (1-aobs).*ln(1-pstari) ;
  retp(llike) ;
endp ;

proc (2) = mlegame(theta0) ;
  local nobs, kx, kz, nparam, namesb, eps, iter, llike, 
        criter, ilike, idlike, dlike, d2like, theta1,
        avarb, sdb, tstat, numy1, numy0, logL0, lri, k ;
  
  nobs = rows(aobs) ;
  kx = cols(xobs) ;
  kz = cols(zobs) ;
  nparam = rows(theta0) ;
  namesb = "beta0"
         | (0 $+ "beta" $+ ftocv(seqa(1,1,kx),1,0))
         | (0 $+ "gama" $+ ftocv(seqa(1,1,kz),1,0))
         | "delta" ;       

  eps = 1E-6 ;
  llike = 1000 ;
  criter = 1000 ;
  iter=1 ;
  do while (criter>eps) ;
  
    "" ;
    "Iteration                = " iter ;
    "Log-Likelihood function  = " llike ;
    "Norm of b(k)-b(k-1)      = " criter ;
    "" ;
    
    ilike = likegame(theta0) ;
    llike = sumc(ilike) ;
    idlike = gradp(&likegame,theta0) ;
    dlike = sumc(idlike) ;
    d2like = idlike'*idlike ;
    theta1 = theta0 + inv(d2like)*dlike ;
    criter = sqrt( (theta1-theta0)'*(theta1-theta0) ) ;
    theta0 = theta1 ;
    iter = iter + 1 ;
  endo ;

  avarb  = inv(d2like) ;
  sdb    = sqrt(diag(avarb)) ;
  tstat  = theta0./sdb ;
  numy1  = sumc(aobs) ;
  numy0  = nobs - numy1 ;
  logL0  = numy1*ln(numy1) + numy0*ln(numy0) - nobs*ln(nobs) ;
  lri    = 1 - llike/logL0 ;
/*
  "Number of Iterations     = " iter ;
  "Log-Likelihood function  = " llike ;
  "Likelihood Ratio Index   = " lri ;

  "" ;
  "------------------------------------------------------------------";
  "       Parameter     Estimate        Standard        t-ratios";
  "                                     Errors" ;
  "------------------------------------------------------------------";
  k=1;
  do while k<=nparam;
    print $namesb[k];;theta0[k];;sdb[k];;tstat[k];
    k=k+1 ;
  endo;
  "------------------------------------------------------------------";
*/         
  retp(theta1,avarb) ;
endp ;                


