Beispiel #1
0
def genMarkov(markovFilePath, verb='INFO', nSimulation=int(5.e+4)):

    logger.setStreamVerb(verb=verb)
    logger.info('')
    #os.system('cat ' + markovFilePath)

    logger.debug('markovFilePath is %s' % markovFilePath)

    # Read paramter file
    beta = float(getParameter(markovFilePath, 'beta', 'bar-separated'))
    muG = float(getParameter(markovFilePath, 'muG', 'bar-separated'))
    sigmaG = float(getParameter(markovFilePath, 'sigmaG', 'bar-separated'))
    p = float(getParameter(markovFilePath, 'p', 'bar-separated'))
    dy = float(getParameter(markovFilePath, 'dy', 'bar-separated'))
    nAgent = int(getParameter(markovFilePath, 'nAgent', 'bar-separated'))
    theta = float(getParameter(markovFilePath, 'theta', 'bar-separated'))
    periodsPerYear = int(
        getParameter(markovFilePath, 'periodsPerYear', 'bar-separated'))

    # Output read parameters
    logger.debug('')
    logger.debug('read the following yearly parameters: ')
    logger.debug('beta           = %s' % beta)
    logger.debug('muG           = %s' % muG)
    logger.debug('sigmaG        = %s' % sigmaG)
    logger.debug('p              = %s' % p)
    logger.debug('dy             = %s' % dy)
    logger.debug('nAgent         = %s' % nAgent)
    logger.debug('theta          = %s' % theta)
    logger.debug('periodsPerYear = %s' % periodsPerYear)

    # perform consitency check on paramters:
    assert periodsPerYear >= 1
    assert beta >= 0.8 and beta <= 1.0
    assert sigmaG >= 0.
    assert muG <= 1.0

    # computing scaled parameters
    muG_sc = (1. + muG)**(1. / periodsPerYear)
    sigmaG_sc = sigmaG / np.sqrt(periodsPerYear)
    beta_sc = beta**(1. / periodsPerYear)

    # scaled parameters:
    logger.info('')
    logger.info('computed the following scaled 1/%s parameters: ' %
                periodsPerYear)
    logger.info('beta_sc        = %s' % beta_sc)
    logger.info('muG_sc        = %s' % (muG_sc - 1.))
    logger.info('sigmaG_sc     = %s' % sigmaG_sc)
    logger.info('')

    # Build trans matrix
    TransMatrix = np.array([[p, 1. - p], [1. - p, p]])

    ShockMatrix = np.array([[muG_sc - sigmaG_sc], [muG_sc + sigmaG_sc]])
    mkov = MkovM(ShockMatrix, TransMatrix)
    logger.debug(ShockMatrix)
    logger.debug(TransMatrix)
    #mkov.simulation()
    logger.debug(mkov)

    if nAgent > 1:
        # Add unemployment shocks
        #TransMatrixUnem = np.triu(np.ones( (nAgent, nAgent)), 1 ) * ( ( 1. - p) / ( nAgent - 1.) ) + np.tril(np.ones( (nAgent, nAgent)), -1 ) * ( ( 1. - p) / ( nAgent - 1.) ) + np.eye(nAgent) * p
        ShockMatrixUnem = np.triu(np.ones(
            (nAgent, nAgent)
        ), 1) * dy / (nAgent - 1) + np.tril(np.ones(
            (nAgent, nAgent)), -1) * dy / (nAgent - 1) + np.eye(nAgent) * (-dy)

        # Print unemployment shock matrix
        logger.info('ShockMatrixUnem \n %s' % ShockMatrixUnem)

        # Combine aggregate uncertainty with idiosyncratic unemployment uncertainty
        fullShockMatrix = np.hstack(
            (np.repeat(ShockMatrix, nAgent,
                       axis=0), np.tile(ShockMatrixUnem, (2, 1))))
        #fullTransMatrix = np.kron(TransMatrix, TransMatrixUnem)

        # ugly ugly ugly
        fullShockMatrix = fullShockMatrix[:nAgent + 1]
        fullShockMatrix[nAgent][ShockMatrix.shape[1]:] = np.zeros((1, nAgent))

        # more ugly ugly ugly
        pers = TransMatrix[0, 0]
        fullTransMatrix = np.vstack(
            (np.hstack((np.eye(nAgent) * pers, np.ones(
                (nAgent, 1)) * (1. - pers))),
             np.append(((1. - pers) / nAgent) * np.ones(nAgent), pers)))
    else:
        fullShockMatrix = ShockMatrix
        fullTransMatrix = TransMatrix

    np.set_printoptions(precision=7, linewidth=300)

    logger.info('fullShock\n %s' % fullShockMatrix)
    logger.info('fullTrans\n %s' % fullTransMatrix)

    ###################
    # Compute PD

    # nD_t+1 = nYbar_t+1 - n(1-theta)E_tYbar_t+1
    # divide through with Ybar_t+1
    # nd_t+1 = n  - n(1-theta) E_t[g_t+1]/ g_t+1
    g = fullShockMatrix[:, 0]
    Etg = np.dot(fullTransMatrix, g)
    PD = 1. - (1. - theta) * np.reshape(Etg, (len(Etg), 1)) / g
    PB = np.reshape(Etg, (len(Etg), 1)) / g
    ##################

    try:
        np.savetxt(os.path.join(os.getcwd(), 'output', 'shockMatrix.in'),
                   fullShockMatrix,
                   fmt='%15.10f')
        np.savetxt(os.path.join(os.getcwd(), 'output', 'transMatrix.in'),
                   fullTransMatrix,
                   fmt='%15.10f')
        np.savetxt(os.path.join(os.getcwd(), 'output', 'p_a.in'),
                   PD,
                   fmt='%15.10f')
        np.savetxt(os.path.join(os.getcwd(), 'output', 'p_b.in'),
                   PB,
                   fmt='%15.10f')
    except IOError:
        logger.critical('cannot write to output/.')

    logger.info('')
    #Estimate moments for full markov chain.
    fullMChain = MkovM(fullShockMatrix, fullTransMatrix)
    varSim, Theta = fullMChain.simulation(nSimulation)
    # rename columns
    varSim = varSim.rename(columns={'0': 'agIncGrowth'})
    for agent in range(1, nAgent):
        varSim = varSim.rename(columns={str(agent): 'dy_agent' + str(agent)})
    qPers = Theta[1, 1]
    yearlyStockSeries = pandas.DataFrame(varSim[::4])
    model = scikits.statsmodels.tsa.api.VAR(yearlyStockSeries)
    results = model.fit(1)
    Theta = results.params[1:, :]
    aPers = Theta[1, 1]
    logger.info('quarterly pers %s, annual pers %s, diagnol pers %s' %
                (qPers, aPers, TransMatrix[0, 0]))

    np.set_string_function(None)

    varSim['simInc'] = float(nAgent)
    varSim['incGrIndAg0'] = float(1.)
    varSim['incShareAg0'] = (1. + varSim['dy_agent1']) / nAgent

    for row in varSim.rows():
        if row > 0:
            varSim['simInc'][row] = varSim['simInc'][
                row - 1] * varSim['agIncGrowth'][row]
            varSim['incGrIndAg0'][row] = (
                varSim['incShareAg0'][row] /
                varSim['incShareAg0'][row - 1]) * varSim['agIncGrowth'][row]

    varSim['simIndIncAg0'] = varSim['incShareAg0'] * varSim['simInc']

    x = varSim.ix[:, ['0', 'incShareAg0', 'incGrIndAg0']]
    logger.info(x[:100])
    ##  logger.info('VAR for growth, incshareAg0, incGrIndAg0')
    ##  model = scikits.statsmodels.tsa.api.VAR(x)
    ##  result = model.fit(1)
    ##  logger.info(result.summary())

    logger.info('incShareAg0')
    incShareAg0 = x['incShareAg0']
    logger.info(incShareAg0.describe())
    logger.info('skewness %s' % scipy.stats.kurtosis(incShareAg0))
    logger.info('kurtosis %s' % scipy.stats.skew(incShareAg0))

    logger.info('\nincGrIndAg0')
    incGrIndAg0 = x['incGrIndAg0']
    logger.info(incGrIndAg0.describe())
    logger.info('skewness %s' % scipy.stats.skew(incGrIndAg0))
    logger.info('kurtosis %s' % (scipy.stats.kurtosis(incGrIndAg0)))
    #scikits.statsmodels.tsa.ar_model.AR(np.asarray(varSim['simIndIncAg0']))

    #varSim = varSim[:100]
    #N = len(varSim)
    #rng = pandas.DateRange('1/1/1900', periods = N, timeRule = 'Q@JAN')
    #ts = pandas.Series(varSim['1'], index = rng)

    return fullShockMatrix, fullTransMatrix, beta_sc, g, Etg, PD, PB, incGrIndAg0
Beispiel #2
0
def lucasOneAgent(shockMatrix, transMatrix, beta, g, Etg, PD, PB, markovFilePath, deterministic = False):
  '''
    markovFilePath: path to parameters.in file
    determistic:    boolean indicating whether to compute special determistic or stochastic case. 
    
    One agent economy. Therefore we have: 
      C_t = Y_t
      C_{t+1} = Y_{t+1}
    In the normalied world: 
      c_t = 1
      c_{t+1} = 1
  '''
  
  logFile     = os.path.join(os.getcwd(), 'output/logs/lucasOneAgent.log')
  lucasLogger = wrapLogger(loggerName = 'lucasOneAgentLog', streamVerb = verb, logFile = logFile)

  gamma       = float(getParameter(markovFilePath, 'gamma', 'bar-separated'))
  psi         = float(getParameter(markovFilePath, 'psi', 'bar-separated'))
  cBar        = float(getParameter(markovFilePath, 'cBar', 'bar-separated'))
  mkov        = MkovM(shockMatrix, transMatrix)
  
  lucasLogger.debug('\n')
  lucasLogger.info('log file written to %s\n' % logFile)
  #np.set_printoptions(precision = 7, linewidth = 300)
  
  if deterministic:
    shockMatrix = np.array([[g]])
    transMatrix = np.array([[1]])
   
  states   = len(shockMatrix)
  c        = np.ones(states)
  rho      = 1. / psi
  gammaSeq = [ gamma ] * len(PD)

  if psi < 0: # CRRA case
    lucasLogger.debug('Using CRRA Euler Equations')
    # infinitely lived stock (lucas tree)
    qS_init = np.ones(states)
    def compute_qS(qS):
      euler_qS = eulerCRRA(g, beta, gamma, transMatrix, PD + qS) - qS
      return euler_qS  
    qS = scipy.optimize.newton_krylov(compute_qS, qS_init)
      
    # Price remaining assets
    for state in range(len(shockMatrix)):
      qD = eulerCRRA(g, beta, gamma, transMatrix, payoff = PD)
      qB = eulerCRRA(g, beta, gamma, transMatrix, payoff = PB)
  elif psi >= 0: # Epstein Zin case
    lucasLogger.debug('Using Epstein Zin Euler Equations')
    # guess initial value function
    V_init = g
    lucasLogger.debug('Initial V:      %s ' % V_init)
    V = V_init
    lucasLogger.info('starting backward recursion: ')
    dif = 1.
    counter = 0
    while dif > 1.e-10:
      newV = EpsteinZin(c - cBar, g, V, beta, gamma, psi, transMatrix)
      dif = np.sum(np.abs(newV - V))
      V = newV.copy()
      lucasLogger.debug('iteration: %d dif: %g V: %s' % ( counter, dif, V))
      counter += 1
    lucasLogger.info('converged after %d iterations: ' % counter)
    lucasLogger.info('converged value function: %s' % V)

    # infinitely lived stock 
    qS_init = np.ones(states)
    def compute_qS(qS):
      euler_qS = eulerEpsteinZin(g, V, beta, gamma, psi, transMatrix, payoff = PD + qS) - qS
      return euler_qS  
    qS = scipy.optimize.newton_krylov(compute_qS, qS_init)
    
    # Price remaining assets
    for state in range(len(shockMatrix)):
      qD = eulerEpsteinZin(g, V, beta, gamma, psi, transMatrix, payoff = PD)
      qB = eulerEpsteinZin(g, V, beta, gamma, psi, transMatrix, payoff = PB)
    
  # Generate output:   
  
  # stock
  lucasLogger.info('')
  lucasLogger.info('qS:                    %s' % qS)
  EtPS = inner1d(transMatrix, ( PD + qS ) * g)
  lucasLogger.info('EtPS:                  %s ' % EtPS)
  EtRS = EtPS / qS
  lucasLogger.info('EtRS:                  %s' % EtRS)
  if not deterministic:
    ERS = np.dot(mkov.getlmbda(), EtRS)
    lucasLogger.info('ERS:                   %s' % ERS)
  
  # bond
  lucasLogger.info('')
  lucasLogger.info('qB:                    %s' % qB)
#  EtPB = np.dot(transMatrix, PB * g)
  EtPB = inner1d(transMatrix, PB * g)
  lucasLogger.info('EtPB:                  %s' % EtPB)
  EtRB = EtPB / qB
  lucasLogger.info('EtRB:                  %s' % EtRB)
  if not deterministic:
    ERB = np.dot(mkov.getlmbda(), EtRB)
    lucasLogger.info('ERB:                   %s' % ERB)
    
  
  # dividend asset
  lucasLogger.info('')
  lucasLogger.info('qD:                    %s' % qD)
  EtPD = np.dot(transMatrix, PD * g)
  lucasLogger.info('EtPD:                  %s ' % EtPD)
  EtRD = EtPS / qD
  lucasLogger.info('RtD:                   %s' % EtRD)
  if not deterministic:
    ERD = np.dot(mkov.getlmbda(), EtRD)
    lucasLogger.info('ERD:                   %s' % ERD)
  
  # risk premium
  c_rp = EtRS - EtRB
  lucasLogger.info('\ncond risk prem:        %s' % c_rp)
  if not deterministic:
    rp = np.dot(mkov.getlmbda(), c_rp)
    lucasLogger.info('unc risk premium:      %s' % rp)
    lucasLogger.debug('\ndone with riskpremium computation')
  
  # Write prices to file
  savePath = os.path.join(os.getcwd(), 'output')
  lucasLogger.debug('writing qS/qB to path %slucasOneagent_qS|lucasOneagent_qS' % savePath)
  np.savetxt(os.path.join(savePath, 'lucasOneAgent_qS.in'), qS, fmt = '%15.10f')
  np.savetxt(os.path.join(savePath, 'lucasOneAgent_qB.in'), qB, fmt = '%15.10f')
  if psi > 0:
    np.savetxt(os.path.join(savePath, 'lucasOneAgent_V.in'), V, fmt = '%15.10f')
Beispiel #3
0
def lucasOneAgent(shockMatrix, transMatrix, beta, g, Etg, PD, PB, markovFilePath, deterministic = False):
  '''
    markovFilePath: path to parameters.in file
    determistic:    boolean indicating whether to compute special determistic or stochastic case. 
    
    One agent economy. Therefore we have: 
      C_t = Y_t
      C_{t+1} = Y_{t+1}
    In the normalied world: 
      c_t = 1
      c_{t+1} = 1
  '''
  
  logFile     = os.path.join(os.getcwd(), 'output/logs/lucasOneAgent.log')
  lucasLogger = wrapLogger(loggerName = 'lucasOneAgentLog', streamVerb = verb, logFile = logFile)

  gamma       = float(getParameter(markovFilePath, 'gamma', 'bar-separated'))
  psi         = float(getParameter(markovFilePath, 'psi', 'bar-separated'))
  cBar        = float(getParameter(markovFilePath, 'cBar', 'bar-separated'))
  mkov        = MkovM(shockMatrix, transMatrix)
  
  lucasLogger.debug('\n')
  lucasLogger.info('log file written to %s\n' % logFile)
  #np.set_printoptions(precision = 7, linewidth = 300)
  
  if deterministic:
    shockMatrix = np.array([[g]])
    transMatrix = np.array([[1]])
   
  states   = len(shockMatrix)
  c        = np.ones(states)
  rho      = 1. / psi
  gammaSeq = [ gamma ] * len(PD)

  if psi < 0: # CRRA case
    lucasLogger.debug('Using CRRA Euler Equations')
    # infinitely lived stock (lucas tree)
    qS_init = np.ones(states)
    def compute_qS(qS):
      euler_qS = eulerCRRA(g, beta, gamma, transMatrix, PD + qS) - qS
      return euler_qS  
    qS = scipy.optimize.newton_krylov(compute_qS, qS_init)
      
    # Price remaining assets
    for state in range(len(shockMatrix)):
      qD = eulerCRRA(g, beta, gamma, transMatrix, payoff = PD)
      qB = eulerCRRA(g, beta, gamma, transMatrix, payoff = PB)
  elif psi >= 0: # Epstein Zin case
    lucasLogger.debug('Using Epstein Zin Euler Equations')
    # guess initial value function
    V_init = g
    lucasLogger.debug('Initial V:      %s ' % V_init)
    V = V_init
    lucasLogger.info('starting backward recursion: ')
    dif = 1.
    counter = 0
    while dif > 1.e-10:
      newV = EpsteinZin(c - cBar, g, V, beta, gamma, psi, transMatrix)
      dif = np.sum(np.abs(newV - V))
      V = newV.copy()
      lucasLogger.debug('iteration: %d dif: %g V: %s' % ( counter, dif, V))
      counter += 1
    lucasLogger.info('converged after %d iterations: ' % counter)
    lucasLogger.info('converged value function: %s' % V)

    # infinitely lived stock 
    qS_init = np.ones(states)
    def compute_qS(qS):
      euler_qS = eulerEpsteinZin(g, V, beta, gamma, psi, transMatrix, payoff = PD + qS) - qS
      return euler_qS  
    qS = scipy.optimize.newton_krylov(compute_qS, qS_init)
    
    # Price remaining assets
    for state in range(len(shockMatrix)):
      qD = eulerEpsteinZin(g, V, beta, gamma, psi, transMatrix, payoff = PD)
      qB = eulerEpsteinZin(g, V, beta, gamma, psi, transMatrix, payoff = PB)
    
  # Generate output:   
  
  # stock
  lucasLogger.info('')
  lucasLogger.info('qS:                    %s' % qS)
  EtPS = inner1d(transMatrix, ( PD + qS ) * g)
  lucasLogger.info('EtPS:                  %s ' % EtPS)
  EtRS = EtPS / qS
  lucasLogger.info('EtRS:                  %s' % EtRS)
  if not deterministic:
    ERS = np.dot(mkov.getlmbda(), EtRS)
    lucasLogger.info('ERS:                   %s' % ERS)
  
  # bond
  lucasLogger.info('')
  lucasLogger.info('qB:                    %s' % qB)
#  EtPB = np.dot(transMatrix, PB * g)
  EtPB = inner1d(transMatrix, PB * g)
  lucasLogger.info('EtPB:                  %s' % EtPB)
  EtRB = EtPB / qB
  lucasLogger.info('EtRB:                  %s' % EtRB)
  if not deterministic:
    ERB = np.dot(mkov.getlmbda(), EtRB)
    lucasLogger.info('ERB:                   %s' % ERB)
    
  
  # dividend asset
  lucasLogger.info('')
  lucasLogger.info('qD:                    %s' % qD)
  EtPD = np.dot(transMatrix, PD * g)
  lucasLogger.info('EtPD:                  %s ' % EtPD)
  EtRD = EtPS / qD
  lucasLogger.info('RtD:                   %s' % EtRD)
  if not deterministic:
    ERD = np.dot(mkov.getlmbda(), EtRD)
    lucasLogger.info('ERD:                   %s' % ERD)
  
  # risk premium
  c_rp = EtRS - EtRB
  lucasLogger.info('\ncond risk prem:        %s' % c_rp)
  if not deterministic:
    rp = np.dot(mkov.getlmbda(), c_rp)
    lucasLogger.info('unc risk premium:      %s' % rp)
    lucasLogger.debug('\ndone with riskpremium computation')
  
  # Write prices to file
  savePath = os.path.join(os.getcwd(), 'output')
  lucasLogger.debug('writing qS/qB to path %slucasOneagent_qS|lucasOneagent_qS' % savePath)
  np.savetxt(os.path.join(savePath, 'lucasOneAgent_qS.in'), qS, fmt = '%15.10f')
  np.savetxt(os.path.join(savePath, 'lucasOneAgent_qB.in'), qB, fmt = '%15.10f')
  if psi > 0:
    np.savetxt(os.path.join(savePath, 'lucasOneAgent_V.in'), V, fmt = '%15.10f')
Beispiel #4
0
def genMarkov(markovFilePath, verb = 'INFO', nSimulation = int(5.e+4)):

  logger.setStreamVerb(verb = verb)
  logger.info('')
  #os.system('cat ' + markovFilePath)
  
  logger.debug('markovFilePath is %s' % markovFilePath)
  
  # Read paramter file
  beta = float(getParameter(markovFilePath, 'beta', 'bar-separated'))
  muG = float(getParameter(markovFilePath, 'muG', 'bar-separated'))
  sigmaG = float(getParameter(markovFilePath, 'sigmaG', 'bar-separated'))
  p = float(getParameter(markovFilePath, 'p', 'bar-separated'))
  dy = float(getParameter(markovFilePath, 'dy', 'bar-separated'))
  nAgent = int(getParameter(markovFilePath, 'nAgent', 'bar-separated'))
  theta = float(getParameter(markovFilePath, 'theta', 'bar-separated'))
  periodsPerYear = int(getParameter(markovFilePath, 'periodsPerYear', 'bar-separated'))
  
  # Output read parameters
  logger.debug('')
  logger.debug('read the following yearly parameters: ')
  logger.debug('beta           = %s' % beta)
  logger.debug('muG           = %s' % muG)
  logger.debug('sigmaG        = %s' % sigmaG)
  logger.debug('p              = %s' % p)
  logger.debug('dy             = %s' % dy)
  logger.debug('nAgent         = %s' % nAgent)
  logger.debug('theta          = %s' % theta)
  logger.debug('periodsPerYear = %s' % periodsPerYear)

  # perform consitency check on paramters: 
  assert periodsPerYear >= 1
  assert beta >= 0.8 and beta <= 1.0
  assert sigmaG >= 0.
  assert muG <= 1.0
  
  # computing scaled parameters
  muG_sc = (1. + muG) ** (1./ periodsPerYear)
  sigmaG_sc = sigmaG / np.sqrt(periodsPerYear)
  beta_sc  = beta ** (1./ periodsPerYear)
  
  # scaled parameters: 
  logger.info('')
  logger.info('computed the following scaled 1/%s parameters: ' % periodsPerYear)
  logger.info('beta_sc        = %s' % beta_sc)
  logger.info('muG_sc        = %s' % (muG_sc - 1.))
  logger.info('sigmaG_sc     = %s' % sigmaG_sc)
  logger.info('')
  
  # Build trans matrix
  TransMatrix = np.array([[p, 1.-p], [1.-p, p]])
  
  ShockMatrix = np.array([[ muG_sc - sigmaG_sc], [muG_sc + sigmaG_sc]])
  mkov = MkovM(ShockMatrix, TransMatrix)
  logger.debug(ShockMatrix)
  logger.debug(TransMatrix)
  #mkov.simulation()
  logger.debug(mkov)
  
  if nAgent > 1:
    # Add unemployment shocks
    #TransMatrixUnem = np.triu(np.ones( (nAgent, nAgent)), 1 ) * ( ( 1. - p) / ( nAgent - 1.) ) + np.tril(np.ones( (nAgent, nAgent)), -1 ) * ( ( 1. - p) / ( nAgent - 1.) ) + np.eye(nAgent) * p
    ShockMatrixUnem = np.triu(np.ones( (nAgent, nAgent)), 1 ) *  dy / ( nAgent - 1 ) + np.tril(np.ones( (nAgent, nAgent)), -1 ) * dy / (nAgent - 1 ) + np.eye(nAgent) * ( -dy )
  
    # Print unemployment shock matrix
    logger.info('ShockMatrixUnem \n %s' % ShockMatrixUnem)
    
    # Combine aggregate uncertainty with idiosyncratic unemployment uncertainty
    fullShockMatrix = np.hstack( ( np.repeat(ShockMatrix, nAgent, axis = 0), np.tile(ShockMatrixUnem, (2, 1))) )
    #fullTransMatrix = np.kron(TransMatrix, TransMatrixUnem)
    
    # ugly ugly ugly
    fullShockMatrix = fullShockMatrix[:nAgent + 1]
    fullShockMatrix[nAgent][ShockMatrix.shape[1]:] = np.zeros( (1, nAgent) )
    
    # more ugly ugly ugly
    pers = TransMatrix[0,0]
    fullTransMatrix = np.vstack( (np.hstack( ( np.eye(nAgent) * pers, np.ones( (nAgent, 1) ) * (1.-pers) ) ), np.append(((1.-pers)/ nAgent) *np.ones(nAgent), pers) ) )
  else: 
    fullShockMatrix = ShockMatrix
    fullTransMatrix = TransMatrix
  
  np.set_printoptions(precision = 7, linewidth = 300)
    
  logger.info('fullShock\n %s' % fullShockMatrix )  
  logger.info('fullTrans\n %s' % fullTransMatrix ) 

###################  
  # Compute PD
  
  # nD_t+1 = nYbar_t+1 - n(1-theta)E_tYbar_t+1
  # divide through with Ybar_t+1
  # nd_t+1 = n  - n(1-theta) E_t[g_t+1]/ g_t+1
  g       = fullShockMatrix[:, 0]
  Etg     = np.dot(fullTransMatrix, g)
  PD      = 1. - ( 1. - theta ) * np.reshape(Etg, (len(Etg), 1)) / g
  PB      = np.reshape(Etg, (len(Etg), 1)) / g
##################


  try: 
    np.savetxt(os.path.join(os.getcwd(), 'output', 'shockMatrix.in'), fullShockMatrix, fmt = '%15.10f')
    np.savetxt(os.path.join(os.getcwd(), 'output', 'transMatrix.in'), fullTransMatrix, fmt = '%15.10f')
    np.savetxt(os.path.join(os.getcwd(), 'output', 'p_a.in'), PD, fmt = '%15.10f')
    np.savetxt(os.path.join(os.getcwd(), 'output', 'p_b.in'), PB, fmt = '%15.10f')
  except IOError:
    logger.critical('cannot write to output/.')
    
  logger.info('')
  #Estimate moments for full markov chain. 
  fullMChain = MkovM(fullShockMatrix, fullTransMatrix)
  varSim, Theta = fullMChain.simulation(nSimulation)
  # rename columns
  varSim = varSim.rename(columns = {'0': 'agIncGrowth'})
  for agent in range(1, nAgent):
    varSim = varSim.rename(columns = {str(agent): 'dy_agent' + str(agent)})
  qPers = Theta[1, 1]
  yearlyStockSeries = pandas.DataFrame(varSim[::4])
  model = scikits.statsmodels.tsa.api.VAR(yearlyStockSeries)
  results = model.fit(1)
  Theta = results.params[1:,:]
  aPers = Theta[1, 1]
  logger.info('quarterly pers %s, annual pers %s, diagnol pers %s' % (qPers, aPers, TransMatrix[0, 0]) )
  
   
  np.set_string_function(None)
  
  varSim['simInc'] = float(nAgent)
  varSim['incGrIndAg0'] = float(1.)
  varSim['incShareAg0'] = ( 1. + varSim['dy_agent1'] ) / nAgent

  for row in varSim.rows():
    if row > 0:
      varSim['simInc'][row] = varSim['simInc'][row-1] * varSim['agIncGrowth'][row]
      varSim['incGrIndAg0'][row] = ( varSim['incShareAg0'][row] / varSim['incShareAg0'][row - 1] ) * varSim['agIncGrowth'][row]

  varSim['simIndIncAg0'] = varSim['incShareAg0'] * varSim['simInc']
  
  x = varSim.ix[:,['0','incShareAg0', 'incGrIndAg0']]
  logger.info(x[:100])
##  logger.info('VAR for growth, incshareAg0, incGrIndAg0')
##  model = scikits.statsmodels.tsa.api.VAR(x)
##  result = model.fit(1)
##  logger.info(result.summary())
  
  logger.info('incShareAg0')
  incShareAg0 = x['incShareAg0']
  logger.info(incShareAg0.describe())
  logger.info('skewness %s' % scipy.stats.kurtosis(incShareAg0))
  logger.info('kurtosis %s' % scipy.stats.skew(incShareAg0))
  
  logger.info('\nincGrIndAg0')
  incGrIndAg0 = x['incGrIndAg0']
  logger.info(incGrIndAg0.describe())
  logger.info('skewness %s' % scipy.stats.skew(incGrIndAg0))
  logger.info('kurtosis %s' % (scipy.stats.kurtosis(incGrIndAg0)))
  #scikits.statsmodels.tsa.ar_model.AR(np.asarray(varSim['simIndIncAg0']))
  
  #varSim = varSim[:100]
  #N = len(varSim)
  #rng = pandas.DateRange('1/1/1900', periods = N, timeRule = 'Q@JAN')    
  #ts = pandas.Series(varSim['1'], index = rng)

  return fullShockMatrix, fullTransMatrix, beta_sc, g, Etg, PD, PB, incGrIndAg0