/*
** ---------------------------------------------------------------------
** INIPROB.SRC
**
** Obtains initial reduced form estimates of conditional choice
** probabilities to be used as starting value for the K-stage PIE
**
** The estimated model is an unrestricted multinomial logit
** where the vector of  explanatory variables is provided
** by the user (e.g., a polynomial of order q in state variables)
**
** by Victor Aguirregabiria
**
** Last revision: September 2001
** ---------------------------------------------------------------------
**
** Format:  { p0 } = iniprob(inda,indx,x0)
**
** INPUTS:
**          inda    - (nobs x 1) vector with observed indexes of
**                    decision variables
**
**          indx    - (nobs x 1) vector with observed indexes of
**                    state variables
**
**          x0      - (numx x kprob0) matrix of explanatory 
**                    variables in the reduced form estimates
**                    of choice probabilities
**
**  OUTPUT:
**          p0      - (numx x numj) matrix of estimated choice
**                    probabilities for each value in the space
**                    of state variables (rows) and for each 
**                    choice alternative (columns)
** ---------------------------------------------------------------------
*/

proc (1) = iniprob(inda,indx,x0) ;
  local numx, kpar, nalt, explan, j, best, varest, p0, xbstar ;
  
  numx = rows(x0) ;
  kpar = cols(x0) ;
  nalt = maxc(inda) ;
  explan = x0[indx,.] ;  

  {best,varest} = multilog(inda,explan) ;
  best = reshape(best,nalt-1,kpar)' ;
  p0 = zeros(numx,1) ~ (x0 * best) ;
  xbstar = maxc(p0') ;
  p0 = exp(p0-xbstar) ;
  p0 = p0./(sumc(p0')) ;
  retp(p0) ;
endp ;