def denhaanerrors( cmodel, dr, s0, horizon=100, n_sims=10, sigma=None, seed=0 ): from dolo.numeric.global_solution import step_residual from dolo.numeric.quadrature import gauss_hermite_nodes from dolo.numeric.newton import newton_solver from dolo.numeric.simulations import simulate # cmodel should be an fg model # the code is almost duplicated from simulate_without_error # dr is used to approximate future steps # monkey patch: cmodel = cmodel.as_type('fg') if sigma is None: sigma = cmodel.sigma from dolo.symbolic.model import Model if isinstance(cmodel,Model): from dolo.compiler.compiler_global import CModel_fg model = cmodel cmodel = CModel_fg(model) [y,x,parms] = model.read_calibration() parms = cmodel.model.read_calibration()[2] mean = sigma[0,:]*0 n_x = len(cmodel.controls) n_s = len(cmodel.states) orders = [5]*len(mean) [nodes, weights] = gauss_hermite_nodes(orders, sigma) s0 = numpy.atleast_2d(s0.flatten()).T x0 = dr(s0) # standard simulation simul = simulate(cmodel, dr, s0, sigma, horizon=horizon, n_exp=n_sims, parms=parms, seed=seed) simul_se = simulate(cmodel, dr, s0, sigma, horizon=horizon, n_exp=n_sims, parms=parms, seed=seed, solve_expectations=True, nodes=nodes, weights=weights) x_simul = simul[n_s:,:,:] x_simul_se = simul_se[n_s:,:,:] diff = abs( x_simul_se - x_simul ) error_1 = (diff).max(axis=2).mean(axis=1) error_2 = (diff).mean(axis=2).mean(axis=1) return [error_1, error_2]
#phi = MultilinearInterpolator( smin, smax, [10,10]) #psi = MultilinearInterpolator( smin, smax, [10,10]) import numpy from dolo.compiler.cmodel_fgh import CModel_fgah, CModel_fgh_from_fgah cmt = CModel_fgah(model) cm = CModel_fgh_from_fgah(cmt) sigma = cm.model.read_covariances() params = cm.model.read_calibration()[2] from dolo.numeric.quadrature import gauss_hermite_nodes [nodes, weights] = gauss_hermite_nodes( [10], sigma) [x_sol, z_sol] = pea_solve( cm, grid, dr_x, dr_h, params, nodes, weights ) # <codecell> dr_x.set_values(x_sol) # <codecell> # <codecell> #dr_glob_3 = global_solve(model_bis, smolyak_order=3) #dr_glob_4 = global_solve(model_bis, smolyak_order=4)
def global_solve(model, bounds=None, verbose=False, initial_dr=None, pert_order=2, interp_type='smolyak', smolyak_order=3, interp_orders=None, maxit=500, numdiff=True, polish=True, tol=1e-8, integration='gauss-hermite', integration_orders=[], compiler='numpy', memory_hungry=True, T=200, n_s=2, N_e=40 ): def vprint(t): if verbose: print(t) [y, x, parms] = model.read_calibration() sigma = model.read_covariances() if initial_dr == None: initial_dr = approximate_controls(model, order=pert_order) if interp_type == 'perturbations': return initial_dr if bounds is not None: pass elif 'approximation' in model['original_data']: vprint('Using bounds specified by model') # this should be moved to the compiler ssmin = model['original_data']['approximation']['bounds']['smin'] ssmax = model['original_data']['approximation']['bounds']['smax'] ssmin = [model.eval_string(str(e)) for e in ssmin] ssmax = [model.eval_string(str(e)) for e in ssmax] ssmin = [model.eval_string(str(e)) for e in ssmin] ssmax = [model.eval_string(str(e)) for e in ssmax] [y, x, p] = model.read_calibration() d = {v: y[i] for i, v in enumerate(model.variables)} d.update({v: p[i] for i, v in enumerate(model.parameters)}) smin = [expr.subs(d) for expr in ssmin] smax = [expr.subs(d) for expr in ssmax] smin = numpy.array(smin, dtype=numpy.float) smax = numpy.array(smax, dtype=numpy.float) bounds = numpy.row_stack([smin, smax]) bounds = numpy.array(bounds, dtype=float) else: vprint('Using bounds given by second order solution.') from dolo.numeric.timeseries import asymptotic_variance # this will work only if initial_dr is a Taylor expansion Q = asymptotic_variance(initial_dr.A.real, initial_dr.B.real, initial_dr.sigma, T=T) devs = numpy.sqrt(numpy.diag(Q)) bounds = numpy.row_stack([ initial_dr.S_bar - devs * n_s, initial_dr.S_bar + devs * n_s, ]) smin = bounds[0, :] smax = bounds[1, :] if interp_orders == None: interp_orders = [5] * bounds.shape[1] if interp_type == 'smolyak': from dolo.numeric.interpolation.smolyak import SmolyakGrid dr = SmolyakGrid(bounds[0, :], bounds[1, :], smolyak_order) elif interp_type == 'spline': from dolo.numeric.interpolation.splines import MultivariateSplines dr = MultivariateSplines(bounds[0, :], bounds[1, :], interp_orders) elif interp_type == 'multilinear': from dolo.numeric.interpolation.multilinear import MultilinearInterpolator dr = MultilinearInterpolator(bounds[0, :], bounds[1, :], interp_orders) elif interp_type == 'sparse_linear': from dolo.numeric.interpolation.interpolation import SparseLinear dr = SparseLinear(bounds[0, :], bounds[1, :], smolyak_order) elif interp_type == 'linear': from dolo.numeric.interpolation.interpolation import LinearTriangulation, TriangulatedDomain, RectangularDomain rec = RectangularDomain(smin, smax, interp_orders) domain = TriangulatedDomain(rec.grid) dr = LinearTriangulation(domain) from dolo.compiler.compiler_global import CModel cm = CModel(model, solve_systems=True, compiler=compiler) cm = cm.as_type('fg') if integration == 'optimal_quantization': from dolo.numeric.quantization import quantization_nodes # number of shocks [epsilons, weights] = quantization_nodes(N_e, sigma) elif integration == 'gauss-hermite': from dolo.numeric.quadrature import gauss_hermite_nodes if not integration_orders: integration_orders = [3] * sigma.shape[0] [epsilons, weights] = gauss_hermite_nodes(integration_orders, sigma) vprint('Starting time iteration') from dolo.numeric.global_solution import time_iteration from dolo.numeric.global_solution import stochastic_residuals_2, stochastic_residuals_3 xinit = initial_dr(dr.grid) xinit = xinit.real # just in case... dr = time_iteration(dr.grid, dr, xinit, cm.f, cm.g, parms, epsilons, weights, x_bounds=cm.x_bounds, maxit=maxit, tol=tol, nmaxit=50, numdiff=numdiff, verbose=verbose) if polish and interp_type == 'smolyak': # this works with smolyak only vprint('\nStarting global optimization') import time t1 = time.time() if cm.x_bounds is not None: lb = cm.x_bounds[0](dr.grid, parms) ub = cm.x_bounds[1](dr.grid, parms) else: lb = None ub = None xinit = dr(dr.grid) dr.set_values(xinit) shape = xinit.shape if not memory_hungry: fobj = lambda t: stochastic_residuals_3(dr.grid, t, dr, cm.f, cm.g, parms, epsilons, weights, shape, deriv=False) dfobj = lambda t: stochastic_residuals_3(dr.grid, t, dr, cm.f, cm.g, parms, epsilons, weights, shape, deriv=True)[1] else: fobj = lambda t: stochastic_residuals_2(dr.grid, t, dr, cm.f, cm.g, parms, epsilons, weights, shape, deriv=False) dfobj = lambda t: stochastic_residuals_2(dr.grid, t, dr, cm.f, cm.g, parms, epsilons, weights, shape, deriv=True)[1] from dolo.numeric.solver import solver x = solver(fobj, xinit, lb=lb, ub=ub, jac=dfobj, verbose=verbose, method='ncpsolve', serial_problem=False) dr.set_values(x) # just in case t2 = time.time() # test solution res = stochastic_residuals_2(dr.grid, x, dr, cm.f, cm.g, parms, epsilons, weights, shape, deriv=False) if numpy.isinf(res.flatten()).sum() > 0: raise ( Exception('Non finite values in residuals.')) vprint('Finished in {} s'.format(t2 - t1)) return dr
def denhaanerrors(cmodel, dr, s0, horizon=100, n_sims=10, sigma=None, seed=0): from dolo.numeric.global_solution import step_residual from dolo.numeric.quadrature import gauss_hermite_nodes from dolo.numeric.newton import newton_solver from dolo.numeric.simulations import simulate # cmodel should be an fg model # the code is almost duplicated from simulate_without_error # dr is used to approximate future steps # monkey patch: if sigma is None: sigma = cmodel.sigma # from dolo.symbolic.model import SModel # # if isinstance(cmodel,SModel): # from dolo.compiler.compiler_global import CModel_fg # model = cmodel # cmodel = CModel_fg(model) # [y,x,parms] = model.read_calibration() # # parms = cmodel.model.read_calibration()[2] parms = cmodel.calibration['parameters'] mean = sigma[0, :] * 0 n_x = len(cmodel.symbols['controls']) n_s = len(cmodel.symbols['states']) orders = [5] * len(mean) [nodes, weights] = gauss_hermite_nodes(orders, sigma) s0 = numpy.atleast_2d(s0.flatten()).T x0 = dr(s0) # standard simulation simul = simulate(cmodel, dr, s0, sigma, horizon=horizon, n_exp=n_sims, parms=parms, seed=seed) simul_se = simulate(cmodel, dr, s0, sigma, horizon=horizon, n_exp=n_sims, parms=parms, seed=seed, solve_expectations=True, nodes=nodes, weights=weights) x_simul = simul[n_s:, :, :] x_simul_se = simul_se[n_s:, :, :] diff = abs(x_simul_se - x_simul) error_1 = (diff).max(axis=2).mean(axis=1) error_2 = (diff).mean(axis=2).mean(axis=1) return [error_1, error_2]
dr_x.set_values(xh_init[:n_x, :]) dr_h.set_values(xh_init[n_x:, :]) #phi = MultilinearInterpolator( smin, smax, [10,10]) #psi = MultilinearInterpolator( smin, smax, [10,10]) import numpy from dolo.compiler.cmodel_fgh import CModel_fgah, CModel_fgh_from_fgah cmt = CModel_fgah(model) cm = CModel_fgh_from_fgah(cmt) sigma = cm.model.read_covariances() params = cm.model.read_calibration()[2] from dolo.numeric.quadrature import gauss_hermite_nodes [nodes, weights] = gauss_hermite_nodes([10], sigma) [x_sol, z_sol] = pea_solve(cm, grid, dr_x, dr_h, params, nodes, weights) # <codecell> dr_x.set_values(x_sol) # <codecell> # <codecell> #dr_glob_3 = global_solve(model_bis, smolyak_order=3) #dr_glob_4 = global_solve(model_bis, smolyak_order=4)