#!/usr/bin/env python import numpy as np import os, sys path2Pymods = os.path.join(os.path.dirname(__file__), '../../') if not sys.path.count(path2Pymods): sys.path.append(path2Pymods) from pymods.support.wrapLogbook import wrapLogger verb = 'INFO' logger = wrapLogger(loggerName='MkovM.log', streamVerb=verb, logFile=None) class MkovM: def __init__(self, S, T): self.S = np.asarray(S) self.T = np.asarray(T) self.states = np.size(S, 0) self.vars = np.size(S, 1) self.lmbda = self.getlmbda() self.E = self.getE() self.Et = self.getEt() self.V = self.getV() self.E2 = self.getE2() self.E2_lag = self.getE2_lag() self.getCov = self.getV def __getitem__(self, key): if key == 'E': return self.E if key == 'Et':
def mcInterface(method, uncMean, persistence, varCov, nval, verb = 'DEBUG'): ''' Inputs: 1) method 2) uncMean as column vector 3) persistence as matrix 4) varCov as matrix 5) nval as column vector ''' # Get a logger instance logger = wrapLogger(loggerName = 'mcInterface', streamVerb = verb, logFile = '') # Make sure that input matrices are not arrays to allow for * notation # Also enforce column vectors nVars = len(nval) uncMean = np.mat(uncMean).reshape(len(uncMean), 1) persistence = np.mat(persistence) varCov = np.mat(varCov) nval = np.mat(nval).reshape(len(nval), 1) if nval[0, 0] == 1: ShockMatrix = uncMean.T TransMatrix = 1 return # Map unconditional mean into an intercept of a VAR process I = np.mat(np.eye(len(nval))) intercept = ( I - persistence ) * uncMean # Derivation: varEps # see wave Variance Covariance Vector Operations for rules # Y_t = alpha + theta Y_{t-1} + eps_{t+1} # V(Y_t) = V(theta Y_{t-1} + V(eps_{t+1}) # V(Y) = theta V(Y) theta' + V(eps) # V(eps) = (I-theta I theta') V(Y) varEps = ( I - persistence * I * persistence.T ) * varCov logger.debug('uncMean %s' % uncMean) logger.debug('persistence %s' % persistence) logger.debug('varCov %s' % varCov) if method.lower() == 'Tauchen1987Joh'.lower(): Ns = nval[0, 0] bandwidth = 1 retDict = Tauchen1987Joh(persistence, intercept, varCov, Ns, bandwidth, verb) ShockMatrix = retDict['y'] TransMatrix = retDict['P'] elif method.lower() == 'KnotekTerry'.lower(): ''' needs 3 shock states to match standard deviation and/or persistence. ''' A0 = np.eye(nVars) A1 = intercept A2 = persistence sigma = varEps N = nval random_draws = 10000 method = 1 [P,states,zbar] = fn_var_to_markov(A0,A1,A2,sigma,N,random_draws,method) ShockMatrix = states.T TransMatrix = P elif method.lower() == 'Tauchen1987fortran'.lower(): ''' doesn't match persistence ''' pathToFile = os.path.dirname( __file__ ) if not os.path.isfile(os.path.join(os.getcwd(), 'GHQUAD.DAT')): shutil.copy(os.path.join(pathToFile, 'GHQUAD.DAT'), os.getcwd()) markovInputFile = open('PARMS', 'w') print >> markovInputFile, len(nval) print >> markovInputFile, 1 for ele in nval.flat: print >> markovInputFile, ele for ele in uncMean.flat: print >> markovInputFile, ele for ele in persistence.flat: print >> markovInputFile, ele for ele in varCov.flat: print >> markovInputFile, ele markovInputFile.close() states = nval.prod() vars = len(nval) TransMatrix, ShockMatrix = markov.markov(states, vars) os.remove('GHQUAD.DAT') os.remove('PARMS') os.remove('dog') elif method.lower() == 'momentMatching'.lower(): dims = {} # all variables have the same # of states. dims['states'] = np.prod(nval) dims['vars'] = len(nval) shockTarget = {} shockTarget['E'] = np.ravel(uncMean) shockTarget['Std'] = np.asarray(np.sqrt(np.diag(varCov))) shockTarget['Cor'] = np.asarray([varCov[0, 1] / np.sqrt(varCov[0,0]) * np.sqrt(varCov[1,1])]) S = optShock(dims, shockTarget) # sort S ind = np.lexsort((S.T[1], S.T[0])) ShockMatrix = S[ind].copy() transTarget = {} transTarget['Theta'] = persistence TransMatrix = optTrans(dims, transTarget, S) else: print('method not implemented. exiting... ') os._exit(1) logger.debug('ShockMatrix %s' % ShockMatrix) logger.debug('TransMatrix %s' % TransMatrix) return ShockMatrix, TransMatrix
#!/usr/bin/env python import numpy as np import os, sys path2Pymods = os.path.join(os.path.dirname(__file__), '../../') if not sys.path.count(path2Pymods): sys.path.append(path2Pymods) from pymods.support.wrapLogbook import wrapLogger verb = 'INFO' logger = wrapLogger(loggerName = 'MkovM.log', streamVerb = verb, logFile = None) class MkovM: def __init__(self, S, T): self.S = np.asarray(S) self.T = np.asarray(T) self.states = np.size(S, 0) self.vars = np.size(S, 1) self.lmbda = self.getlmbda() self.E = self.getE() self.Et = self.getEt() self.V = self.getV() self.E2 = self.getE2() self.E2_lag = self.getE2_lag() self.getCov = self.getV def __getitem__(self, key): if key == 'E': return self.E if key == 'Et': Et = self.getEt()
def mcInterface(method, uncMean, persistence, varCov, nval, verb='DEBUG'): ''' Inputs: 1) method 2) uncMean as column vector 3) persistence as matrix 4) varCov as matrix 5) nval as column vector ''' # Get a logger instance logger = wrapLogger(loggerName='mcInterface', streamVerb=verb, logFile='') # Make sure that input matrices are not arrays to allow for * notation # Also enforce column vectors nVars = len(nval) uncMean = np.mat(uncMean).reshape(len(uncMean), 1) persistence = np.mat(persistence) varCov = np.mat(varCov) nval = np.mat(nval).reshape(len(nval), 1) if nval[0, 0] == 1: ShockMatrix = uncMean.T TransMatrix = 1 return # Map unconditional mean into an intercept of a VAR process I = np.mat(np.eye(len(nval))) intercept = (I - persistence) * uncMean # Derivation: varEps # see wave Variance Covariance Vector Operations for rules # Y_t = alpha + theta Y_{t-1} + eps_{t+1} # V(Y_t) = V(theta Y_{t-1} + V(eps_{t+1}) # V(Y) = theta V(Y) theta' + V(eps) # V(eps) = (I-theta I theta') V(Y) varEps = (I - persistence * I * persistence.T) * varCov logger.debug('uncMean %s' % uncMean) logger.debug('persistence %s' % persistence) logger.debug('varCov %s' % varCov) if method.lower() == 'Tauchen1987Joh'.lower(): Ns = nval[0, 0] bandwidth = 1 retDict = Tauchen1987Joh(persistence, intercept, varCov, Ns, bandwidth, verb) ShockMatrix = retDict['y'] TransMatrix = retDict['P'] elif method.lower() == 'KnotekTerry'.lower(): ''' needs 3 shock states to match standard deviation and/or persistence. ''' A0 = np.eye(nVars) A1 = intercept A2 = persistence sigma = varEps N = nval random_draws = 10000 method = 1 [P, states, zbar] = fn_var_to_markov(A0, A1, A2, sigma, N, random_draws, method) ShockMatrix = states.T TransMatrix = P elif method.lower() == 'Tauchen1987fortran'.lower(): ''' doesn't match persistence ''' pathToFile = os.path.dirname(__file__) if not os.path.isfile(os.path.join(os.getcwd(), 'GHQUAD.DAT')): shutil.copy(os.path.join(pathToFile, 'GHQUAD.DAT'), os.getcwd()) markovInputFile = open('PARMS', 'w') print >> markovInputFile, len(nval) print >> markovInputFile, 1 for ele in nval.flat: print >> markovInputFile, ele for ele in uncMean.flat: print >> markovInputFile, ele for ele in persistence.flat: print >> markovInputFile, ele for ele in varCov.flat: print >> markovInputFile, ele markovInputFile.close() states = nval.prod() vars = len(nval) TransMatrix, ShockMatrix = markov.markov(states, vars) os.remove('GHQUAD.DAT') os.remove('PARMS') os.remove('dog') elif method.lower() == 'momentMatching'.lower(): dims = {} # all variables have the same # of states. dims['states'] = np.prod(nval) dims['vars'] = len(nval) shockTarget = {} shockTarget['E'] = np.ravel(uncMean) shockTarget['Std'] = np.asarray(np.sqrt(np.diag(varCov))) shockTarget['Cor'] = np.asarray( [varCov[0, 1] / np.sqrt(varCov[0, 0]) * np.sqrt(varCov[1, 1])]) S = optShock(dims, shockTarget) # sort S ind = np.lexsort((S.T[1], S.T[0])) ShockMatrix = S[ind].copy() transTarget = {} transTarget['Theta'] = persistence TransMatrix = optTrans(dims, transTarget, S) else: print('method not implemented. exiting... ') os._exit(1) logger.debug('ShockMatrix %s' % ShockMatrix) logger.debug('TransMatrix %s' % TransMatrix) return ShockMatrix, TransMatrix