def test_direct(): from dolo import yaml_import from dolo.algos.dtcscc.perturbations import approximate_controls from dolo.algos.dtcscc.time_iteration import time_iteration_direct, time_iteration model = yaml_import("examples/models/compat/rbc_full.yaml") # Check without complementarity conditions dr = time_iteration_direct(model, with_complementarities=False) ddr = time_iteration(model, with_complementarities=False) x0 = dr(dr.grid) x1 = ddr(dr.grid) print(abs(x1 - x0).max()<1e-5) # Check with complementarity conditions dr = time_iteration_direct(model, with_complementarities=True) ddr = time_iteration(model, with_complementarities=True) x0 = dr(dr.grid) x1 = ddr(dr.grid) print(abs(x1 - x0).max()<1e-5)
def test_omega_errors(): from dolo import yaml_import from dolo.algos.dtcscc.time_iteration import time_iteration as time_iteration model = yaml_import('examples/models/rbc.yaml') from dolo.algos.dtcscc.perturbations import approximate_controls dr = approximate_controls(model) dr_global = time_iteration(model, smolyak_order=3, verbose=False, pert_order=1) sigma = model.covariances model.sigma = sigma s_0 = dr.S_bar from dolo.algos.dtcscc.accuracy import omega res_1 = omega( model, dr, orders=[10,10], time_discount=0.96) res_2 = omega( model, dr_global) print(res_1) print(res_2)
def test_model_print(): from dolo import yaml_import fname = 'examples/models/rbc_mfga.yaml' model = yaml_import(fname) print(model)
def test_denhaan_errors(): from dolo import yaml_import from dolo.algos.fg.time_iteration import time_iteration as global_solve model = yaml_import('examples/models/rbc.yaml') from dolo.algos.fg.perturbations import approximate_controls dr = approximate_controls(model) dr_global = global_solve(model, interp_type='smolyak', smolyak_order=4, verbose=False) sigma = model.covariances model.sigma = sigma from dolo.algos.fg.accuracy import denhaanerrors denerr_1 = denhaanerrors(model, dr) denerr_2 = denhaanerrors(model, dr_global) print(denerr_1) print(denerr_2) print(denerr_2['max_errors'][0]) assert( max(denerr_2['max_errors']) < 10-7) # errors with solyak colocations at order 4 are very small
def test_denhaan_errors(): from dolo import yaml_import from dolo.algos.time_iteration import time_iteration as global_solve model = yaml_import('examples/models/rbc.yaml') from dolo.algos.perturbations import approximate_controls dr = approximate_controls(model) dr_global = global_solve(model, interp_type='smolyak', smolyak_order=4, verbose=False) sigma = model.covariances model.sigma = sigma from dolo.algos.accuracy import denhaanerrors denerr_1 = denhaanerrors(model, dr) denerr_2 = denhaanerrors(model, dr_global) print(denerr_1) print(denerr_2) print(denerr_2['max_errors'][0]) assert (max(denerr_2['max_errors']) < 10 - 7 ) # errors with solyak colocations at order 4 are very small
def test_denhaan_errors(): from dolo import yaml_import from dolo.algos.dtcscc.time_iteration import time_iteration as time_iteration model = yaml_import('examples/models/rbc.yaml') from dolo.algos.dtcscc.perturbations import approximate_controls dr = approximate_controls(model) dr_global = time_iteration(model, verbose=False) s0 = model.calibration['states'] sigma = model.get_distribution().sigma from dolo.algos.dtcscc.accuracy import denhaanerrors denerr_1 = denhaanerrors(model, dr) denerr_2 = denhaanerrors(model, dr_global) print(denerr_1) print(denerr_2) print(denerr_2['max_errors'][0]) assert( max(denerr_2['max_errors']) < 10-7) # errors with solyak colocations at order 4 are very small
def test_denhaan_errors(): from dolo import yaml_import from dolo.algos.dtcscc.time_iteration import time_iteration as time_iteration model = yaml_import('examples/models/rbc.yaml') from dolo.algos.dtcscc.perturbations import approximate_controls dr = approximate_controls(model) dr_global = time_iteration(model, verbose=False) s0 = model.calibration['states'] sigma = model.get_distribution().sigma from dolo.algos.dtcscc.accuracy import denhaanerrors denerr_1 = denhaanerrors(model, dr) denerr_2 = denhaanerrors(model, dr_global) print(denerr_1) print(denerr_2) print(denerr_2['max_errors'][0]) assert (max(denerr_2['max_errors']) < 10 - 7 ) # errors with solyak colocations at order 4 are very small
def test_options(): from dolo import yaml_import fname = 'examples/models/rbc_dtmscc.yaml' model = yaml_import(fname) print(model.options)
def test_direct(): from dolo import yaml_import from dolo.algos.dtcscc.perturbations import approximate_controls from dolo.algos.dtcscc.time_iteration import time_iteration_direct, time_iteration model = yaml_import("examples/models/rbc_full.yaml") # Check without complementarity conditions dr = time_iteration_direct(model, with_complementarities=False) ddr = time_iteration(model, with_complementarities=False) x0 = dr(dr.grid) x1 = ddr(dr.grid) print(abs(x1 - x0).max() < 1e-5) # Check with complementarity conditions dr = time_iteration_direct(model, with_complementarities=True) ddr = time_iteration(model, with_complementarities=True) x0 = dr(dr.grid) x1 = ddr(dr.grid) print(abs(x1 - x0).max() < 1e-5)
def test_global_solution(self): from dolo import yaml_import, global_solve filename = 'examples/global_models/rbc.yaml' model = yaml_import(filename) print model.calibration['covariances'] import time dr = global_solve(model, pert_order=1, maxit=500, smolyak_order=3, verbose=True, polish=False, method='newton') t1 = time.time() dr = global_solve(model, pert_order=1, maxit=5, smolyak_order=5, verbose=True, polish=False, method='newton') t2 = time.time() dr = global_solve(model, pert_order=1, maxit=5, interp_type='multilinear', verbose=True, polish=False, method='newton') t3 = time.time() print(t2-t1) print(t3-t2)
def test_options(self): from dolo import yaml_import fname = 'examples/models/rbc_mfga.yaml' model = yaml_import(fname) print(model.options)
def test_mfg_model(): from dolo import yaml_import # from dolo.algos.commands import time_iteration, simulate, evaluate_policy from dolo.algos.dtmscc.time_iteration import time_iteration from dolo.algos.dtmscc.value_iteration import evaluate_policy from dolo.algos.dtmscc.simulations import simulate, plot_decision_rule model = yaml_import("examples/models/sudden_stop.yaml") mdr = time_iteration(model) # sim = simulate(model, mdr, 0, horizon=50, n_exp=0) # irf assert(sim.shape==(50,6)) sim = simulate(model, mdr, 0, horizon=50, n_exp=1) # one stochastic simulation assert(sim.shape==(50,6)) sim = simulate(model, mdr, 0, horizon=50, n_exp=10) # many stochastic simulations assert(sim.shape==(10,50,6)) mv = evaluate_policy(model, mdr, verbose=True, maxit=500) mv = evaluate_policy(model, mdr, verbose=True, maxit=500, initial_guess=mv) sim_v = simulate(model, mdr, 0, drv=mv, horizon=50, n_exp=10) sim_irf = simulate(model, mdr, 0, drv=mv, horizon=50, n_exp=0) sim_dr = plot_decision_rule(model, mdr, 'l') print(sim_dr)
def test_ergo_mc(): from dolo import yaml_import, improved_time_iteration, ergodic_distribution model = yaml_import("examples/models/rbc_mc.yaml") sol = improved_time_iteration(model) Π, μ = ergodic_distribution(model, sol.dr) assert (μ.ndim == 2)
def test_division(self): import yaml with open('examples/models/rbc.yaml') as f: txt = f.read() yaml_content = yaml.load(txt) yaml_content['equations']['transition'][0] += ' + 1/2 - 0.5' # modify first transition equation yaml_content['calibration']['alpha'] = '1/3' txt = yaml.dump(yaml_content) from dolo import yaml_import print(txt) model = yaml_import('nofile', txt=txt) alpha = model.get_calibration('alpha') print(alpha) assert( abs(alpha - 1.0/3.0) < 0.00000001) res = model.residuals() print( abs(res['transition'][0] ) ) assert( abs(res['transition'][0]) < 0.00000001)
def test_dtcscc_model(): # test a model defined with auxiliary variables from dolo import yaml_import model = yaml_import('examples/models/rbc_full.yaml') s = model.calibration['states'] x = model.calibration['controls'] X = x y = model.calibration['auxiliaries'] E = model.calibration['shocks'] V = model.calibration['values'] p = model.calibration['parameters'] S = model.functions['transition'](s,x,E,p) r = model.functions['arbitrage'](s,x,E,S,X,p) y = model.functions['auxiliary'](s,x,p) v = model.functions['value'](s,x,S,X,V,p) z = model.functions['expectation'](S, X, p) x1 = model.functions['direct_response'](s,z, p) print('x1') print(x) print(x1) assert(abs(x-x1).max()<1e-12)
def test_global_solution(self): from dolo import yaml_import, global_solve filename = 'examples/global_models/rbc.yaml' model = yaml_import(filename) import time dr = global_solve(model, pert_order=2, maxit=5, smolyak_order=3, verbose=True, polish=True) t1 = time.time() dr = global_solve(model, pert_order=1, maxit=5, smolyak_order=5, verbose=True, polish=False) t2 = time.time() dr = global_solve(model, pert_order=1, maxit=5, interp_type='multilinear', verbose=True, polish=False) t3 = time.time() print(t2-t1) print(t3-t2)
def test_options(self): from dolo import yaml_import fname = 'examples/models/rbc_mfga.yaml' model = yaml_import(fname) print( model.options )
def test_options(): from dolo import yaml_import fname = 'examples/models/rbc_dtmscc.yaml' model = yaml_import(fname) print( model.options )
def test_mfg_model(): from dolo import yaml_import # from dolo.algos.commands import time_iteration, simulate, evaluate_policy from dolo.algos.dtmscc.time_iteration import time_iteration from dolo.algos.dtmscc.value_iteration import evaluate_policy from dolo.algos.dtmscc.simulations import simulate, plot_decision_rule model = yaml_import("examples/models/compat/sudden_stop.yaml") print(model.exogenous) print(model.is_dtmscc()) mdr = time_iteration(model) # sim = simulate(model, mdr, 0, horizon=50, n_exp=0) # irf assert (sim.shape == (50, 6)) sim = simulate(model, mdr, 0, horizon=50, n_exp=1) # one stochastic simulation assert (sim.shape == (50, 6)) sim = simulate(model, mdr, 0, horizon=50, n_exp=10) # many stochastic simulations assert (sim.shape == (10, 50, 6)) mv = evaluate_policy(model, mdr, verbose=True, maxit=500) mv = evaluate_policy(model, mdr, verbose=True, maxit=500, initial_guess=mv) sim_v = simulate(model, mdr, 0, drv=mv, horizon=50, n_exp=10) sim_irf = simulate(model, mdr, 0, drv=mv, horizon=50, n_exp=0) sim_dr = plot_decision_rule(model, mdr, 'l') print(sim_dr)
def test_omega_errors(): from dolo import yaml_import from dolo.algos.time_iteration import time_iteration as global_solve model = yaml_import('examples/models/rbc.yaml') from dolo.algos.perturbations import approximate_controls dr = approximate_controls(model) dr_global = global_solve(model, smolyak_order=3, verbose=False, pert_order=1) sigma = model.covariances model.sigma = sigma s_0 = dr.S_bar from dolo.algos.accuracy import omega res_1 = omega(model, dr, orders=[10, 10], time_discount=0.96) res_2 = omega(model, dr_global) print(res_1) print(res_2)
def solve_something(name, args): from dolo import yaml_import from dolo.algos.time_iteration import time_iteration as global_solve model = yaml_import('examples/models/{}.yaml'.format(name)) dr = global_solve(model, **args) return dr
def test_web_import(): from dolo import yaml_import model = yaml_import( "https://raw.githubusercontent.com/EconForge/dolo/master/examples/models/rbc.yaml" ) assert (len(model.symbols['states']) == 2)
def test_egm_iid(): from dolo import yaml_import from dolo.algos.egm import egm import numpy as np model = yaml_import("examples/models/consumption_savings_iid.yaml") sol = egm(model, a_grid=np.linspace(0.1, 10, 10) ** 2, verbose=True)
def process(txt): # model = yaml_import(None, txt=txt) # s = model._repr_html_() try: s = yaml_import(None, txt=txt, check_only=True) s = "<div>"+s+"</div>" except: s = "Failed" return HTML(s)
def test_time_iteration_smolyak(): from dolo import yaml_import, time_iteration filename = 'examples/models/rbc.yaml' model = yaml_import(filename) import time dr = time_iteration(model, pert_order=1, maxit=500, verbose=True)
def test_irf(): from dolo import yaml_import, perturb, response import numpy as np model = yaml_import("examples/models/rbc.yaml") dr = perturb(model).dr irf = response(model, dr, "e_z") print(irf)
def test_time_iteration_smolyak(): from dolo import yaml_import, time_iteration filename = 'examples/models/rbc.yaml' model = yaml_import(filename) import time dr = time_iteration(model, pert_order=1, maxit=500, smolyak_order=3, verbose=True)
def test_time_iteration_spline(): import time from dolo import yaml_import, time_iteration filename = 'examples/models/rbc.yaml' model = yaml_import(filename) print(model.__class__) dr = time_iteration(model, pert_order=1, maxit=5, verbose=True)
def test_vfi(): from dolo.algos.value_iteration import value_iteration as value_iteration, evaluate_policy from dolo import yaml_import from dolo.algos.time_iteration import time_iteration model_iid = yaml_import('examples/models/rbc_iid.yaml') # mdr = time_iteration(model_iid, with_complementarities=False, verbose=False) # drv = evaluate_policy(model_iid, mdr) dr_iid = value_iteration(model_iid, verbose=True, maxit=5)
def test_dtcscc__functions(): # test a model defined without auxiliary variables from dolo import yaml_import model = yaml_import('examples/models/rbc_fg.yaml') s = model.calibration['states'] x = model.calibration['controls'] e = model.calibration['shocks'] p = model.calibration['parameters'] r = model.functions['arbitrage'](s,x,e,s,x,p)
def test_markov_chain(): from dolo import yaml_import fname = 'examples/models/rbc_mfga.yaml' model = yaml_import(fname) from dolo.numeric.discretization import multidimensional_discretization import numpy sigma = numpy.array([[0.01]]) rho = 0.01 [P,Q] = multidimensional_discretization(rho, sigma, 3) print(model.markov_chain)
def test_global_solution(self): from dolo import yaml_import, global_solve filename = 'examples/global_models/rbc.yaml' model = yaml_import(filename) import time t1 = time.time() dr = global_solve(model, pert_order=2, maxit=5, interp_type='spline', verbose=True)
def test_global_solution(self): from dolo import yaml_import, time_iteration filename = 'examples/models/rbc.yaml' model = yaml_import(filename) import time t1 = time.time() dr = time_iteration(model, pert_order=1, maxit=5, verbose=True)
def test_fga_higher_order_perturbations(): from dolo import yaml_import from dolo.algos.dtcscc.perturbations_higher_order import approximate_controls model = yaml_import('examples/models/rbc.yaml') # for i in [1,2,3]: dr1 = approximate_controls(model, order=1) dr2 = approximate_controls(model, order=2) dr3 = approximate_controls(model, order=3) assert(dr1.order==1) assert(dr1.X_s.ndim==2) assert(dr3.X_ss.ndim==3)
def finite_time_iteration(): from dolo import yaml_import, time_iteration from dolo.numeric.decision_rule import CustomDR import matplotlib.pyplot as plt model = yaml_import("examples/models/consumption_savings_iid.yaml") # in the near future this will become: # """c[t] = w[t]""" values = {"c": "w"} edr = CustomDR(values, model) sol = time_iteration(model, dr0=edr, maxit=5, trace=True)
def test_fga_higher_order_perturbations(): from dolo import yaml_import from dolo.algos.dtcscc.perturbations_higher_order import approximate_controls model = yaml_import('examples/models/rbc.yaml') # for i in [1,2,3]: dr1 = approximate_controls(model, order=1) dr2 = approximate_controls(model, order=2) dr3 = approximate_controls(model, order=3) assert (dr1.order == 1) assert (dr1.X_s.ndim == 2) assert (dr3.X_ss.ndim == 3)
def test_parameterized_expectations(): from dolo import yaml_import from dolo.algos.dtcscc.parameterized_expectations import parameterized_expectations from dolo.algos.dtcscc.time_iteration import time_iteration model = yaml_import("examples/models/compat/rbc_full.yaml") dr_ti = time_iteration(model) dr_pea = parameterized_expectations(model, direct=False) x_ti = dr_ti(dr_ti.grid) x_pea = dr_pea(dr_ti.grid) print(abs(x_ti - x_pea).max() < 1e-5)
def test_direct(): from dolo import yaml_import from dolo.algos.dtcscc.perturbations import approximate_controls from dolo.algos.dtcscc.time_iteration import time_iteration_direct, time_iteration model = yaml_import("examples/models/rbc_full.yaml") dr = time_iteration_direct(model) ddr = time_iteration(model) x0 = dr(dr.grid) x1 = ddr(dr.grid) print(abs(x1 - x0).max() < 1e-5)
def test_direct(): from dolo import yaml_import from dolo.algos.dtcscc.perturbations import approximate_controls from dolo.algos.dtcscc.time_iteration import time_iteration_direct, time_iteration model = yaml_import("examples/models/rbc_full.yaml") dr = time_iteration_direct(model) ddr = time_iteration(model) x0 = dr(dr.grid) x1 = ddr(dr.grid) print(abs(x1 - x0).max()<1e-5)
def test_eval_formula(): from dolo.compiler.eval_formula import eval_formula from dolo import yaml_import, approximate_controls, simulate model = yaml_import('examples/models/rbc.yaml') dr = approximate_controls(model) sim = simulate(model, dr) rr = eval_formula("delta*k-i", sim, context=model.calibration) rr = eval_formula("y(1) - y", sim, context=model.calibration) sim['diff'] = model.eval_formula("delta*k-i", sim) model.eval_formula("y(1) - y", sim) sim['ddiff'] = model.eval_formula("diff(1)-diff(-1)", sim)
def test_vfi(): from dolo.algos.value_iteration import value_iteration as value_iteration, evaluate_policy from dolo import yaml_import from dolo.algos.time_iteration import time_iteration model_iid = yaml_import('examples/models/rbc_dtcc_iid_ar1.yaml') endo_grid = model_iid.get_grid() exo_grid = model_iid.exogenous.discretize() mdr = time_iteration(model_iid) drv = evaluate_policy(model_iid, mdr) dr_iid = value_iteration(model_iid, verbose=True, maxit=5)
def test_eval_formula(): from dolo.compiler.eval_formula import eval_formula from dolo import yaml_import, time_iteration, simulate model = yaml_import('examples/models/rbc.yaml') dr = time_iteration(model) sim = simulate(model, dr) sim = sim.sel(N=0) sim = sim.to_pandas() rr = eval_formula("delta*k-i", sim, context=model.calibration) rr = eval_formula("y(1) - y", sim, context=model.calibration) sim['diff'] = model.eval_formula("delta*k-i", sim) model.eval_formula("y(1) - y", sim) sim['ddiff'] = model.eval_formula("diff(1)-diff(-1)", sim)
def test_big_system(): from dolo import yaml_import from dolo.algos.dtcscc.nonlinearsystem import nonlinear_system from dolo.algos.dtcscc.time_iteration import time_iteration model = yaml_import('examples/models/compat/rbc.yaml') dr = time_iteration(model, grid={'type': 'smolyak', 'mu': 3}, verbose=True) sol = nonlinear_system(model, grid={'type': 'smolyak', 'mu': 3}) diff = (sol.__values__) - dr.__values__ assert(abs(diff).max() < 1e-6) sol_high_precision = nonlinear_system(model, grid={'type': 'smolyak', 'mu': 5}, initial_dr=sol) assert(sol_high_precision.grid.shape == (145, 2))
def test_big_system(): from dolo import yaml_import from dolo.algos.dtcscc.nonlinearsystem import nonlinear_system from dolo.algos.dtcscc.time_iteration import time_iteration model = yaml_import("examples/models/rbc.yaml") dr = time_iteration(model, grid={"type": "smolyak", "mu": 3}, verbose=True) sol = nonlinear_system(model, grid={"type": "smolyak", "mu": 3}) diff = (sol.__values__) - dr.__values__ assert abs(diff).max() < 1e-6 sol_high_precision = nonlinear_system(model, grid={"type": "smolyak", "mu": 5}, initial_dr=sol) assert sol_high_precision.grid.shape == (145, 2)
def test_global_solution(self): from dolo import yaml_import, global_solve filename = 'examples/models/rbc.yaml' model = yaml_import(filename) import time t1 = time.time() print('ok') dr = global_solve(model, pert_order=1, maxit=5, interp_type='spline', verbose=True)
def test_global_solution(self): import time from dolo import yaml_import, global_solve filename = 'examples/models/rbc.yaml' model = yaml_import(filename) print(model.__class__) t3 = time.time() dr = global_solve(model, pert_order=1, maxit=5, interp_type='spline', verbose=True, interp_orders=[100,100]) t4 = time.time() print(t4-t3)
def model_evaluation(compiler='numpy', data_layout='columns'): from dolo import yaml_import #model = yaml_import('examples/models/rbc_fg.yaml', compiler=compiler, order=data_layout) model = yaml_import('examples/models/rbc_fg.yaml') s0 = model.calibration['states'] x0 = model.calibration['controls'] e0 = model.calibration['shocks'] p = model.calibration['parameters'] assert(s0.ndim == 1) assert(x0.ndim == 1) assert(p.ndim == 1) N = 10 f = model.functions['arbitrage'] if data_layout == 'rows': ss = numpy.ascontiguousarray( numpy.tile(s0, (N,1)).T ) xx = numpy.ascontiguousarray( numpy.tile(x0, (N,1)).T ) ee = e0[:,None].repeat(N,axis=1) else: ss = ( numpy.tile(s0, (N,1)) ) xx = ( numpy.tile(x0, (N,1)) ) ee = e0[None,:].repeat(N,axis=0) ss = s0[None,:].repeat(N,axis=0) xx = x0[None,:].repeat(N,axis=0) ee = e0[None,:].repeat(N,axis=0) vec_res = f(ss,xx,ee,ss,xx,p) res = f(s0, x0, e0, s0, x0, p) assert(res.ndim==1) d = 0 for i in range(N): d += abs(vec_res[i,:] - res).max() assert(d == 0)
def test_lint_all(): from dolo import yaml_import import os import re examples_dir = 'examples/models/' examples = os.listdir(examples_dir) regex = re.compile("(.*).yaml") for f in examples: if not regex.match(f): continue print("Checking: {}".format(f)) check = yaml_import(examples_dir + f, check_only=True) # TODO: need to fix Dynare lint before reenabling # if len(check)>1: # assert("Linter Error" not in check[0]) # assert there is no linter error print()
def test_omega_errors(): from dolo import yaml_import from dolo.algos.dtcscc.time_iteration import time_iteration as time_iteration model = yaml_import('examples/models/compat/rbc.yaml') from dolo.algos.dtcscc.perturbations import approximate_controls dr = approximate_controls(model) dr_global = time_iteration(model, verbose=False, pert_order=1) sigma = model.get_distribution().sigma s_0 = dr.S_bar from dolo.algos.dtcscc.accuracy import omega res_1 = omega(model, dr, grid=dict(orders=[10, 10]), time_discount=0.96) res_2 = omega(model, dr_global) print(res_1) print(res_2)
def test_eval_formula(): from dolo.compiler.eval_formula import eval_formula from dolo import yaml_import, time_iteration, simulate import dolo.config from dolo import perturb model = yaml_import('examples/models/rbc.yaml') dr = perturb(model) sim = simulate(model, dr) sim = sim.sel(N=0) sim = sim.to_pandas() print(sim.columns) rr = eval_formula("delta*k(0)-i(0)", sim, context=model.calibration) rr = eval_formula("y(1) - y(0)", sim, context=model.calibration) sim['diff'] = model.eval_formula("delta*k(0)-i(0)", sim) model.eval_formula("y(1) - y(0)", sim) sim['ddiff'] = model.eval_formula("diff(1)-diff(-1)", sim)