def test_omega_errors(self): from dolo.misc.yamlfile import yaml_import from dolo.numeric.global_solve import global_solve model = yaml_import('examples/global_models/rbc.yaml') from dolo.compiler.converter import GModel_fg_from_fga model = GModel_fg_from_fga( model ) from dolo.numeric.perturbations_to_states import approximate_controls dr = approximate_controls(model) dr_global = global_solve(model, smolyak_order=4, verbose=False, pert_order=1, method='newton', polish=True) sigma = model.calibration['covariances'] # cmodel = CModel(model) model.sigma = sigma s_0 = dr.S_bar from dolo.numeric.error_measures import omega res = omega( dr, model, dr_global.bounds, [10,10], time_weight=[50, 0.96,s_0])
def solve_something(name, args): from dolo.misc.yamlfile import yaml_import from dolo.numeric.global_solve import global_solve model = yaml_import('examples/global_models/{}.yaml'.format(name)) dr = global_solve(model, **args) return dr
def test_omega_errors(self): from dolo.misc.yamlfile import yaml_import from dolo.numeric.global_solve import global_solve model = yaml_import('examples/global_models/rbc.yaml') from dolo.compiler.converter import GModel_fg_from_fga model = GModel_fg_from_fga(model) from dolo.numeric.perturbations_to_states import approximate_controls dr = approximate_controls(model) dr_global = global_solve(model, smolyak_order=4, verbose=False, pert_order=1, method='newton', polish=True) sigma = model.calibration['covariances'] # cmodel = CModel(model) model.sigma = sigma s_0 = dr.S_bar from dolo.numeric.error_measures import omega res = omega(dr, model, dr_global.bounds, [10, 10], time_weight=[50, 0.96, s_0])
def test_denhaan_errors(self): from dolo.misc.yamlfile import yaml_import from dolo.numeric.global_solve import global_solve model = yaml_import('examples/global_models/rbc.yaml') from dolo.compiler.compiler_global import CModel from dolo.numeric.perturbations_to_states import approximate_controls dr = approximate_controls(model) dr_global = global_solve(model, smolyak_order=4, verbose=False, pert_order=1, method='newton', polish=True) sigma = model.calibration['covariances'] model.sigma = sigma s_0 = dr.S_bar from dolo.numeric.error_measures import denhaanerrors [error_1, error_2] = denhaanerrors(model, dr, s_0) [error_1_glob, error_2_glob] = denhaanerrors(model, dr_global, s_0) print(error_1) print(error_1_glob) assert( max(error_1_glob) < 10-7) # errors with solyak colocations at order 4 are very small assert( max(error_2_glob) < 10-7)
def test_denhaan_errors(self): from dolo.misc.yamlfile import yaml_import from dolo.numeric.global_solve import global_solve model = yaml_import('examples/global_models/rbc.yaml') from dolo.compiler.compiler_global import CModel from dolo.numeric.perturbations_to_states import approximate_controls dr = approximate_controls(model) dr_global = global_solve(model, smolyak_order=4, verbose=False, pert_order=1, method='newton', polish=True) sigma = model.calibration['covariances'] model.sigma = sigma s_0 = dr.S_bar from dolo.numeric.error_measures import denhaanerrors [error_1, error_2] = denhaanerrors(model, dr, s_0) [error_1_glob, error_2_glob] = denhaanerrors(model, dr_global, s_0) print(error_1) print(error_1_glob) assert (max(error_1_glob) < 10 - 7 ) # errors with solyak colocations at order 4 are very small assert (max(error_2_glob) < 10 - 7)
from dolo.numeric.perturbations_to_states import approximate_controls model = yaml_import('../../../examples/global_models/rbc_fgah.yaml') dr_pert = approximate_controls(model, order=1, substitute_auxiliary=True) print(dr_pert.X_bar) from dolo.numeric.interpolation.smolyak import SmolyakGrid from dolo.numeric.global_solve import global_solve dr_smol = global_solve(model, smolyak_order=2, maxit=2, polish=True) smin = dr_smol.bounds[0,:] smax = dr_smol.bounds[1,:] dr_x = SmolyakGrid( smin, smax, 3) dr_h = SmolyakGrid( smin, smax, 3) grid = dr_x.grid xh_init = dr_pert(dr_x.grid) n_x = len(model['variables_groups']['controls'])
from dolo.numeric.global_solve import global_solve model = yaml_import( 'examples/global_models/rbc_fgah.yaml') model_bis = yaml_import( 'examples/global_models/rbc.yaml') print(model) print(model) dr_pert = approximate_controls(model_bis) cm = CModel_fgah(model) cm_fg = cm.as_type('fg') import time t = time.time() for n in range(10): global_solve(model_bis, initial_dr=dr_pert, polish=False, interp_type='multilinear') s = time.time() print('Elapsed : {}'.format(s-t) ) # t = time.time() # for n in range(10): # global_solve_new(cm, initial_dr=dr_pert, polish=False, interp_type='multilinear') # s = time.time() # print('Elapsed : {}'.format(s-t) )
import copy from numpy import concatenate calibration = copy.copy(self.parent.calibration) # calibration['controls'] = concatenate( [calibration['controls'], calibration['auxiliary']] ) self.calibration = calibration def get_calibration(self, name): return self.parent.get_calibration(name) @property def x_bounds(self): return self.parent.x_bounds if __name__ == "__main__": from dolo import * model = yaml_import("examples/global_models/rbc.yaml") model_fg = GModel_fg_from_fga(model) print( model_fg.get_calibration(['alpha','rho']) ) model_fg.set_calibration(alpha=0.1) print( model_fg.get_calibration(['alpha','rho']) ) print(model_fg.x_bounds) from dolo.numeric.global_solve import global_solve dr = global_solve(model_fg, verbose=True) print(dr)
from dolo import yaml_import from dolo.numeric.perturbations_to_states import approximate_controls model = yaml_import('../../../examples/global_models/rbc_fgah.yaml') dr_pert = approximate_controls(model, order=1, substitute_auxiliary=True) print(dr_pert.X_bar) from dolo.numeric.interpolation.smolyak import SmolyakGrid from dolo.numeric.global_solve import global_solve dr_smol = global_solve(model, smolyak_order=2, maxit=2, polish=True) smin = dr_smol.bounds[0, :] smax = dr_smol.bounds[1, :] dr_x = SmolyakGrid(smin, smax, 3) dr_h = SmolyakGrid(smin, smax, 3) grid = dr_x.grid xh_init = dr_pert(dr_x.grid) n_x = len(model['variables_groups']['controls']) dr_x.set_values(xh_init[:n_x, :]) dr_h.set_values(xh_init[n_x:, :])
#dr2 = approximate_controls(model, order=2, substitute_auxiliary=True) #drss = solve_model_around_risky_ss(fname) #dr_smol = global_solve(model) #dr_spline = global_solve(model, interp_type='spline', interp_orders=[50,5]) #drs = [dr1, dr2, drss, dr_smol, dr_spline] # dr3 = approximate_controls(model, order=3, substitute_auxiliary=True) # drg_smol = global_solve(model, bounds=bounds) # drg_smol_4 = global_solve(model, bounds=bounds, smolyak_order=2, N_e=80) # drs = [dr1,dr2,dr3,drg_smol, drg_smol_4] #errors_sup = [omega(dr, model, bounds, [20,20], exponent='inf') for dr in drs] #errors_L2 = [omega(dr, model, bounds, [20,20], exponent='L2') for dr in drs] #print errors_sup #print errors_L2 exit() # dr3 = approximate_controls(model, order=3) drg_smol = global_solve(model) drg_spli = global_solve(model, interp_type='spline') # drs = [dr1,dr2,dr3] drs = [dr1, dr2, drg_smol, drg_spli] # print dr2.S_bar print bounds errors_sup = [omega_sup(dr, model, bounds, [50, 50]) for dr in drs] print errors_sup
from numpy import concatenate calibration = copy.copy(self.parent.calibration) # calibration['controls'] = concatenate( [calibration['controls'], calibration['auxiliary']] ) self.calibration = calibration def get_calibration(self, name): return self.parent.get_calibration(name) @property def x_bounds(self): return self.parent.x_bounds if __name__ == "__main__": from dolo import * model = yaml_import("examples/global_models/rbc.yaml") model_fg = GModel_fg_from_fga(model) print(model_fg.get_calibration(['alpha', 'rho'])) model_fg.set_calibration(alpha=0.1) print(model_fg.get_calibration(['alpha', 'rho'])) print(model_fg.x_bounds) from dolo.numeric.global_solve import global_solve dr = global_solve(model_fg, verbose=True) print(dr)
#drss = solve_model_around_risky_ss(fname) #dr_smol = global_solve(model) #dr_spline = global_solve(model, interp_type='spline', interp_orders=[50,5]) #drs = [dr1, dr2, drss, dr_smol, dr_spline] # dr3 = approximate_controls(model, order=3, substitute_auxiliary=True) # drg_smol = global_solve(model, bounds=bounds) # drg_smol_4 = global_solve(model, bounds=bounds, smolyak_order=2, N_e=80) # drs = [dr1,dr2,dr3,drg_smol, drg_smol_4] #errors_sup = [omega(dr, model, bounds, [20,20], exponent='inf') for dr in drs] #errors_L2 = [omega(dr, model, bounds, [20,20], exponent='L2') for dr in drs] #print errors_sup #print errors_L2 exit() # dr3 = approximate_controls(model, order=3) drg_smol = global_solve(model) drg_spli = global_solve(model, interp_type='spline') # drs = [dr1,dr2,dr3] drs = [dr1,dr2, drg_smol, drg_spli] # print dr2.S_bar print bounds errors_sup = [omega_sup(dr, model, bounds, [50,50]) for dr in drs] print errors_sup
from dolo import * from dolo.numeric.global_solve import global_solve filename = '../examples/global_models/rbc.yaml' model = yaml_import(filename) dr = global_solve(model, pert_order=1, smolyak_order=5, memory_hungry=True)