def test_second_order_accuracy(self): # This solves the optimal growth example at second order # and computes the second order correction to the steady-state # We test that both the statefree method and the perturbation to states # yield the same result. from dolo.misc.yamlfile import yaml_import model = yaml_import('examples/global_models/rbc.yaml', compiler=None) from dolo.numeric.perturbations import solve_decision_rule from dolo.numeric.perturbations_to_states import approximate_controls coeffs = approximate_controls(model, order=2, return_dr=False) state_perturb = coeffs[0] dr = solve_decision_rule(model) statefree_perturb = dr['ys'] + dr['g_ss'] / 2.0 ctls = model.symbols_s['controls'] ctls_ind = [model.variables.index(v) for v in ctls] # the two methods should yield exactly the same result from numpy.testing import assert_almost_equal A = statefree_perturb[ctls_ind] B = state_perturb assert_almost_equal(A, B) # we compare the risk-adjusted constants
def test_second_order_accuracy(self): # This solves the optimal growth example at second order # and computes the second order correction to the steady-state # We test that both the statefree method and the perturbation to states # yield the same result. from dolo.misc.yamlfile import yaml_import model = yaml_import('examples/global_models/rbc.yaml', compiler=None) from dolo.numeric.perturbations import solve_decision_rule from dolo.numeric.perturbations_to_states import approximate_controls coeffs = approximate_controls(model,order=2, return_dr=False) state_perturb = coeffs[0] dr = solve_decision_rule(model) statefree_perturb = dr['ys'] + dr['g_ss']/2.0 ctls = model.symbols_s['controls'] ctls_ind = [model.variables.index(v) for v in ctls] # the two methods should yield exactly the same result from numpy.testing import assert_almost_equal A = statefree_perturb[ctls_ind] B = state_perturb assert_almost_equal(A, B) # we compare the risk-adjusted constants
def test_perturbation(self): # This solves the optimal growth example at second order # and computes the second order correction to the steady-state # We test that both the statefree method and the perturbation to states # yield the same result. from dolo.misc.yamlfile import yaml_import model = yaml_import('../examples/global_models/optimal_growth.yaml') from dolo.numeric.perturbations_to_states import approximate_controls [Xbar, X_s, X_ss] = approximate_controls(model, 2) state_perturb = Xbar from dolo.numeric.perturbations import solve_decision_rule dr = solve_decision_rule(model) statefree_perturb = dr['ys'] + dr['g_ss'] / 2.0 ctls = model['variables_groups']['controls'] + model[ 'variables_groups']['expectations'] ctls_ind = [model.variables.index(v) for v in ctls] # the two methods should yield exactly the same result from numpy.testing import assert_almost_equal A = statefree_perturb[ctls_ind] B = state_perturb assert_almost_equal(A, B)
def test_perturbation(self): # This solves the optimal growth example at second order # and computes the second order correction to the steady-state # We test that both the statefree method and the perturbation to states # yield the same result. from dolo.misc.yamlfile import yaml_import model = yaml_import('../examples/global_models/optimal_growth.yaml') from dolo.numeric.perturbations_to_states import approximate_controls [Xbar, X_s,X_ss] = approximate_controls(model,2) state_perturb = Xbar from dolo.numeric.perturbations import solve_decision_rule dr = solve_decision_rule(model) statefree_perturb = dr['ys'] + dr['g_ss']/2.0 ctls = model['variables_groups']['controls'] + model['variables_groups']['expectations'] ctls_ind = [model.variables.index(v) for v in ctls] # the two methods should yield exactly the same result from numpy.testing import assert_almost_equal A = statefree_perturb[ctls_ind] B = state_perturb assert_almost_equal(A, B)
def perturbation_solution(self,with_bounds=False): """ Returns perturbation solution around the steady-state Result is [X,Y,Z] representing : s_t = X s_{t-1} + Y e_t x_t = Z s_t """ model = self.model data = self.read_model() controls_i = [model.variables.index(v) for v in data['controls'] ] states_i = [model.variables.index(v) for v in data['states_vars'] ] from dolo.numeric.perturbations import solve_decision_rule dr = solve_decision_rule(model,order=1) A = dr['g_a'][states_i,:] B = dr['g_a'][controls_i,:] [Z,err,rank,junk] = np.linalg.lstsq(A.T,B.T) Z = Z.T X = A[:,states_i] + np.dot( A[:,controls_i], Z ) Y = dr.ghu[states_i,:] # steady_state values s_ss = dr['ys'][states_i,] x_ss = dr['ys'][controls_i,] # We also compute bounds for distribution at a given probability interval if with_bounds: M = np.linalg.solve( 1-X, Y) Sigma = np.array(model.covariances).astype(np.float64) [V,P] = np.linalg.eigh(Sigma) # we have Sigma == P * diag(V) * P.T # unconditional distribution is ( P*diag(V) ) * N # where N is the normal distribution associated H = np.dot(Y,np.dot(P,np.diag(V))) n_s = Sigma.shape[0] I = np.eye(n_s) points = np.concatenate([H,-H],axis=1) lam = 2 # this coefficient should be more cleverly defined p_infs = np.min(points,axis = 1) * lam p_max = np.max(points,axis = 1) * lam bounds = np.row_stack([ s_ss + p_infs, s_ss + p_max ]) else: bounds = None return [[s_ss,x_ss],[X,Y,Z],bounds]
def check(self): var_string = self.ui.lineEdit_var.text() shocks_string = self.ui.lineEdit_shocks.text() parameters_string = self.ui.lineEdit_parameters.text() params_definition = self.ui.plainTextEdit.toPlainText() initval_string = self.ui.plainTextEdit_2.toPlainText() l = [el.get_text() for el in self.widgets] l = [e for e in l if e] content = str.join(';\n', l) if len(l) > 0: content += ';' simple_mod = ''' var {vars}; varexo {varexo}; parameters {parms}; {params_definition} model; {equations} end; initval; {initval_string} end; '''.format(vars=var_string, varexo=shocks_string, parms=parameters_string, equations=content, params_definition=params_definition, initval_string=initval_string) from dolo.misc.modfile import parse_dynare_text try: model = parse_dynare_text(simple_mod) model.check(verbose=True) info = model.info info['name'] = model.fname txt = ''' Model check {name}: Number of variables : {n_variables} Number of equations : {n_equations} Number of shocks : {n_shocks}'''.format(**info) except Exception as e: txt = '\nModel is not valid' self.ui.textEdit.setText(txt) return try: from dolo.numeric.perturbations import solve_decision_rule dr = solve_decision_rule(model, order=1) txt += '\nBlanchard-Kahn conditions are met.' except ImportError as e: txt += '\nImpossible to solve the model (lapack could not be imported)' except Exception as e: txt += '\nImpossible to solve the model (yet).' print e self.ui.textEdit.setText(txt)
def check(self): var_string = self.ui.lineEdit_var.text() shocks_string = self.ui.lineEdit_shocks.text() parameters_string = self.ui.lineEdit_parameters.text() params_definition = self.ui.plainTextEdit.toPlainText() initval_string = self.ui.plainTextEdit_2.toPlainText() l = [ el.get_text() for el in self.widgets] l = [ e for e in l if e] content = str.join(';\n',l) if len(l)>0: content += ';' simple_mod = ''' var {vars}; varexo {varexo}; parameters {parms}; {params_definition} model; {equations} end; initval; {initval_string} end; '''.format(vars = var_string,varexo = shocks_string, parms=parameters_string,equations = content, params_definition=params_definition, initval_string=initval_string) from dolo.misc.modfile import parse_dynare_text try: model = parse_dynare_text(simple_mod) model.check(verbose=True) info = model.info info['name'] = model.fname txt =''' Model check {name}: Number of variables : {n_variables} Number of equations : {n_equations} Number of shocks : {n_shocks}'''.format(**info) except Exception as e: txt = '\nModel is not valid' self.ui.textEdit.setText(txt) return try: from dolo.numeric.perturbations import solve_decision_rule dr = solve_decision_rule( model, order = 1 ) txt += '\nBlanchard-Kahn conditions are met.' except ImportError as e: txt += '\nImpossible to solve the model (lapack could not be imported)' except Exception as e: txt += '\nImpossible to solve the model (yet).' print e self.ui.textEdit.setText(txt)