<commit_before>from dolo import *
import numpy
from numpy import *

def global_solve(model, bounds=None, initial_dr=None, smolyak_order=3, T=200, n_s=2, N_e=40, maxit=500, polish=True, memory_hungry=True):
    [y,x,parms] = model.read_calibration()
    sigma = model.read_covariances()
    
    if bounds == None or initial_dr == None:
        pert_dr = approximate_controls(model, order=2, substitute_auxiliary=True, solve_systems=True)
        
    if bounds == None:
        from dolo.numeric.timeseries import asymptotic_variance
        
        Q = asymptotic_variance(pert_dr.A.real, pert_dr.B.real, pert_dr.sigma, T=T)
        
        devs = numpy.sqrt( numpy.diag(Q) )
        bounds  = numpy.row_stack([
                                   pert_dr.S_bar - devs * n_s,
                                   pert_dr.S_bar + devs * n_s,
                                   ])
        
    from dolo.numeric.smolyak import SmolyakGrid
    sg = SmolyakGrid( bounds, smolyak_order )
    
    if initial_dr == None:
        xinit = pert_dr(sg.grid) # initial value on the grid
        xinit = xinit.real
        xinit[:2,:] = numpy.maximum(xinit[:2,:],0.01)
    else:
        xinit = initial_dr(sg.grid)
            
    from dolo.compiler.compiler_global import GlobalCompiler, time_iteration, stochastic_residuals_2, stochastic_residuals_3
    gc = GlobalCompiler(model, substitute_auxiliary=True, solve_systems=True)
    
    from dolo.numeric.quantization import quantization_weights
    # number of shocks
    [weights,epsilons] = quantization_weights(N_e, sigma)
    <commit_msg>global_solve: optional choice of perturbation order for initial guess<commit_after>from dolo import *
import numpy
from numpy import *

def global_solve(model, bounds=None, initial_dr=None, pert_order=2, smolyak_order=3, T=200, n_s=2, N_e=40, maxit=500, polish=True, memory_hungry=True):
    [y,x,parms] = model.read_calibration()
    sigma = model.read_covariances()
    
    if bounds == None or initial_dr == None:
        pert_dr = approximate_controls(model, order=pert_order, substitute_auxiliary=True, solve_systems=True)
        
    if bounds == None:
        from dolo.numeric.timeseries import asymptotic_variance
        
        Q = asymptotic_variance(pert_dr.A.real, pert_dr.B.real, pert_dr.sigma, T=T)
        
        devs = numpy.sqrt( numpy.diag(Q) )
        bounds  = numpy.row_stack([
                                   pert_dr.S_bar - devs * n_s,
                                   pert_dr.S_bar + devs * n_s,
                                   ])
        
    from dolo.numeric.smolyak import SmolyakGrid
    sg = SmolyakGrid( bounds, smolyak_order )
    
    if initial_dr == None:
        xinit = pert_dr(sg.grid) # initial value on the grid
        xinit = xinit.real
        xinit[:2,:] = numpy.maximum(xinit[:2,:],0.01)
    else:
        xinit = initial_dr(sg.grid)
            
    from dolo.compiler.compiler_global import GlobalCompiler, time_iteration, stochastic_residuals_2, stochastic_residuals_3
    gc = GlobalCompiler(model, substitute_auxiliary=True, solve_systems=True)
    
    from dolo.numeric.quantization import quantization_weights
    # number of shocks
    [weights,epsilons] = quantization_weights(N_e, sigma)
    
{'fraction_non_alphanumeric': 0.09270095472744072, 'fraction_numerical': 0.01108715737603942, 'mean_word_length': 2.522776572668113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1189264', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1026, 'n_words': 215}