/*
**  SIMDYGAM.SRC    Simulates data of state and decision variables
**                  from the steady-state distribution of 
**                  a Markov Perfect Equilibrium in a dynamic
**                  game of firms' market entry/exit with 
**                  incomplete information
**
**  by Victor Aguirregabiria
**  First version: August, 2003
**  Last revision: November, 2003
**
**  ---------------------------------------------------------------------
**                              MODEL
**  ---------------------------------------------------------------------
**  i = Firm index; t = Time index
**      a[it] = Indicator of the event 'firm i operates 
**               in the market at period t'
**      x[t] = Characteristics of the market at period t.
**      e0[it] and e1[it] = Firm i's private information.
**
**  Profits:
**  --------
**  No active:  U(0) = e0[it]
**
**  Active:     U(1) = alphai - thetaE*(1-a[i,t-1]) 
**                   + beta * x[t]
**                   - delta * log(N[-it]) + 1) + e1[it]
**  where:
**      alphai = Firm i's fixed effect
**      N[-it] = Number of firms, other than i, 
**               operating in the market at period t. 
**      thetaE, beta and delta are parameters
**
**  ----------------------------------------------------------------------------
**
**  FORMAT:
**  { aobs, aobs_1, indxobs } = 
**     simdygam(nobs,pchoice,psteady,xval)
**
**  Inputs:
**      nobs    - Number of simulations (markets)
**
**      pchoice - (nstate x nplayer) matrix with MPE probs of entry
**
**      psteady - (nstate x 1) vector with steady-state 
**                distribution of {x[t],a[t-1]}
**
**      mstate  - (nstate x (nplayer+1)) matrix with values 
**                of state variables {x[t],a[t-1]}. 
**                The states in the rows of prob are ordered 
**                as follows. Example: xval=(1|2) and 3 players:
**                          x[t]    a[1,t-1]    a[2,t-1]    a[3,t-1]
**               Row 1:     1           0           0           0
**               Row 2:     1           0           0           1
**               Row 3:     1           0           1           0
**               Row 4:     1           0           1           1
**               Row 5:     1           1           0           0
**               Row 6:     1           1           0           1
**               Row 7:     1           1           1           0
**               Row 8:     1           1           1           1
**               Row 9:     2           0           0           0
**               Row 10:    2           0           0           1
**               Row 11:    2           0           1           0
**               Row 12:    2           0           1           1
**               Row 13:    2           1           0           0
**               Row 14:    2           1           0           1
**               Row 15:    2           1           1           0
**               Row 16:    2           1           1           1
**
**  Outputs:
**      aobs   - (nobs x nplayer) matrix with players' choices.
**      aobs_1 - (nobs x nplayer) matrix with players' initial states.
**      xobs   - (nobs x 1) vector with market characteristics.
**
*/

proc (3) = simdygam(nobs,pchoice,psteady,mstate) ;
  local nplay, nums, numa, numx, i,
        pbuff0, pbuff1, uobs, xobs, aobs_1 ;
        
  nplay = cols(pchoice) ;
  nums = rows(pchoice) ;
  numa = 2^nplay ;
  numx = nums/numa ;
 
  @ --------------------------------------------@
  @ 2. Generating random draws of (x[t],a[t-1]) @
  @ --------------------------------------------@
  pbuff1 = cumsumc(psteady) ;
  pbuff0 = cumsumc((0|psteady[1:nums-1])) ;
  uobs = rndu(nobs,1) ;
  uobs = ((uobs.>=(pbuff0')).*(uobs.<=(pbuff1'))) ;
  uobs = uobs * seqa(1,1,nums) ;
  xobs = mstate[uobs,1] ;
  aobs_1 = mstate[uobs,2:nplay+1] ;
  clear pbuff0, pbuff1 ;
  
  @ --------------------------------------------------------@
  @ 3. Generating random draws for a[t] (given x[t],a[t-1]) @
  @ --------------------------------------------------------@
  pchoice = pchoice[uobs,.] ;
  uobs = rndu(nobs,nplay) ;
  aobs = (uobs.<=pchoice) ;
      
  retp(aobs, aobs_1, xobs) ;
endp ;
