Пример #1
0
 def dynamic_residuals(X, return_dr=False):
     x = X[:, 0]
     dx = X[:, 1:]
     d = {}
     for i in range(n_pfs):
         p = pf_parms[i]
         v = pf_vars[i]
         d[p] = x[i]
         d[v] = x[i]
         for j in range(n_states):
             d[pf_dparms[i][j]] = dx[i, j]
     new_model.set_calibration(d)
     if return_dr:
         dr = approximate_controls(new_model, order=2, lambda_name='lam')
         return dr
     else:
         [X_bar, X_s, X_ss, X_sss] = approximate_controls(new_model,
                                                          order=3,
                                                          return_dr=False,
                                                          lambda_name='lam')
         crit = numpy.column_stack([
             X_bar[n_controls - n_pfs:n_controls],
             X_s[n_controls - n_pfs:n_controls, :],
         ])
         return crit
Пример #2
0
    def constant_residuals(x, return_dr=False):
        d = {}
        for i in range(n_pfs):
            p = pf_parms[i]
            v = pf_vars[i]
            d[p] = x[i]
            d[v] = x[i]
        new_model.set_calibration(d)
            # new_model.parameters_values[p] = x[i]
            # new_model.init_values[v] = x[i]
        if return_dr:
            dr = approximate_controls(new_model, order=1, return_dr=True, lambda_name='lam')
            return dr
        X_bar, X_s, X_ss = approximate_controls(new_model, order=2, return_dr=False, lambda_name="lam")

        return X_bar[n_controls-n_pfs:n_controls]
    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.algos.perturbations import yaml_import
        model = yaml_import('examples/models/compat/rbc.yaml', compiler=None)


        from trash.dolo.numeric.perturbations import solve_decision_rule
        from trash.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
Пример #4
0
 def dynamic_residuals(X, return_dr=False):
     x = X[:, 0]
     dx = X[:, 1:]
     d = {}
     for i in range(n_pfs):
         p = pf_parms[i]
         v = pf_vars[i]
         d[p] = x[i]
         d[v] = x[i]
         for j in range(n_states):
             d[pf_dparms[i][j]] = dx[i, j]
     new_model.set_calibration(d)
     if return_dr:
         dr = approximate_controls(new_model, order=2, lambda_name="lam")
         return dr
     else:
         [X_bar, X_s, X_ss, X_sss] = approximate_controls(new_model, order=3, return_dr=False, lambda_name="lam")
         crit = numpy.column_stack([X_bar[n_controls - n_pfs : n_controls], X_s[n_controls - n_pfs : n_controls, :]])
         return crit
Пример #5
0
def solve_model_around_risky_ss(model,
                                verbose=False,
                                return_dr=True,
                                initial_sol=None):

    if initial_sol == None:
        if model.model_spec == 'portfolios':
            print(
                'This is a portfolio model ! Converting to deterministic one.')
            from dolo.algos.portfolio_perturbation import portfolios_to_deterministic
            model = portfolios_to_deterministic(model, ['x_1', 'x_2'])
            model.check()
        from trash.dolo.numeric.perturbations_to_states import approximate_controls
        perturb_sol = approximate_controls(model, order=1, return_dr=False)
        [X_bar, X_s] = perturb_sol
    else:
        perturb_sol = initial_sol
        [X_bar, X_s] = perturb_sol

    # reduce X_s to the real controls  (remove auxiliary variables

    X_s = X_s[:len(model.symbols['controls']), :]
    X_bar = X_bar[:len(model.symbols['controls'])]
    X_bar = numpy.array(X_bar)

    if abs(X_s.imag).max() < 1e-10:
        X_s = X_s.real
    else:
        raise (Exception('Complex decision rule'))

    print('Perturbation solution found')

    #print model.parameters
    #exit()

    X_bar_1 = X_bar
    X_s_1 = X_s

    #model = yaml_import(filename)

    #from dolo.symbolic.symbolic import Parameter

    [S_bar, X_bar, X_s, P] = solve_risky_ss(model.model,
                                            X_bar,
                                            X_s,
                                            verbose=verbose)

    if return_dr:
        cdr = CDR([S_bar, X_bar, X_s])
        #        cdr.P = P
        return cdr
    return [S_bar, X_bar, X_s, P]
Пример #6
0
def solve_model_around_risky_ss(model, verbose=False, return_dr=True, initial_sol=None):



    if initial_sol == None:
        if model.model_type == 'portfolios':
            print('This is a portfolio model ! Converting to deterministic one.')
            from dolo.algos.portfolio_perturbation import portfolios_to_deterministic
            model = portfolios_to_deterministic(model,['x_1','x_2'])
            model.check()
        from trash.dolo.numeric.perturbations_to_states import approximate_controls
        perturb_sol = approximate_controls(model, order = 1, return_dr=False)
        [X_bar,X_s] =  perturb_sol
    else:
        perturb_sol = initial_sol
        [X_bar,X_s] =  perturb_sol


    # reduce X_s to the real controls  (remove auxiliary variables

    X_s = X_s[:len(model.symbols['controls']),:]
    X_bar = X_bar[:len(model.symbols['controls'])]
    X_bar = numpy.array(X_bar)

    if  abs(X_s.imag).max() < 1e-10:
        X_s = X_s.real
    else:
        raise( Exception('Complex decision rule') )

    print('Perturbation solution found')

    #print model.parameters
    #exit()

    X_bar_1 = X_bar
    X_s_1 = X_s

    #model = yaml_import(filename)

    #from dolo.symbolic.symbolic import Parameter

    [S_bar, X_bar, X_s, P] = solve_risky_ss(model.model, X_bar, X_s, verbose=verbose)

    if return_dr:
        cdr = CDR([S_bar, X_bar, X_s])
#        cdr.P = P
        return cdr
    return [S_bar, X_bar, X_s, P]
Пример #7
0
def solve_portfolio_model(model, pf_names, order=2, lambda_name='lam', guess=None):

    from dolo.compiler.compiler_python import GModel
    if isinstance(model, GModel):
        model = model.model

    pf_model = model

    from dolo import Variable, Parameter, Equation
    import re

    n_states = len(pf_model.symbols_s['states'])
    states = pf_model.symbols_s['states']
    steady_states = [Parameter(v.name+'_bar') for v in pf_model.symbols_s['states']]
    n_pfs = len(pf_names)

    pf_vars = [Variable(v) for v in pf_names]
    res_vars = [Variable('res_'+str(i)) for i in range(n_pfs)]


    pf_parms = [Parameter('K_'+str(i)) for i in range(n_pfs)]
    pf_dparms = [[Parameter('K_'+str(i)+'_'+str(j)) for j in range(n_states)] for i in range(n_pfs)]

    from sympy import Matrix

    # creation of the new model

    import copy

    new_model = copy.copy(pf_model)

    new_model.symbols_s['controls'] += res_vars
    for v in res_vars + pf_vars:
        new_model.calibration_s[v] = 0


    new_model.symbols_s['parameters'].extend(steady_states)
    for p in pf_parms + Matrix(pf_dparms)[:]:
        new_model.symbols_s['parameters'].append(p)
        new_model.calibration_s[p] = 0

    compregex = re.compile('(.*)<=(.*)<=(.*)')

    to_be_added_1 = []
    to_be_added_2 = []

    expressions = Matrix(pf_parms) + Matrix(pf_dparms)*( Matrix(states) - Matrix(steady_states))

    for n,eq in enumerate(new_model.equations_groups['arbitrage']):
        if 'complementarity' in eq.tags:
            tg = eq.tags['complementarity']
            [lhs,mhs,rhs] = compregex.match(tg).groups()
            mhs = new_model.eval_string(mhs)
        else:
            mhs = None
        if mhs in pf_vars:
            i = pf_vars.index(mhs)
            neq = Equation(mhs, expressions[i])
            neq.tag(**eq.tags)
            eq_res = Equation(eq.gap, res_vars[i])
            eq_res.tag(eq_type='arbitrage')
            to_be_added_2.append(eq_res)
            new_model.equations_groups['arbitrage'][n] = neq
            to_be_added_1.append(neq)

    # new_model.equations_groups['arbitrage'].extend(to_be_added_1)
    new_model.equations_groups['arbitrage'].extend(to_be_added_2)
    new_model.update()

    print("number of equations {}".format(len(new_model.equations)))
    print("number of arbitrage equations {}".format( len(new_model.equations_groups['arbitrage'])) )

    print('parameters_ordering')
    print("number of parameters {}".format(new_model.symbols['parameters']))
    print("number of parameters {}".format(new_model.parameters))


    # now, we need to solve for the optimal portfolio coefficients
    from trash.dolo.numeric.perturbations_to_states import approximate_controls

    dr = approximate_controls(new_model)
    print('ok')

    import numpy

    n_controls = len(model.symbols_s['controls'])

    def constant_residuals(x, return_dr=False):
        d = {}
        for i in range(n_pfs):
            p = pf_parms[i]
            v = pf_vars[i]
            d[p] = x[i]
            d[v] = x[i]
        new_model.set_calibration(d)
            # new_model.parameters_values[p] = x[i]
            # new_model.init_values[v] = x[i]
        if return_dr:
            dr = approximate_controls(new_model, order=1, return_dr=True, lambda_name='lam')
            return dr
        X_bar, X_s, X_ss = approximate_controls(new_model, order=2, return_dr=False, lambda_name="lam")

        return X_bar[n_controls-n_pfs:n_controls]


    if guess is not None:
        x0 = numpy.array(guess)
    else:
        x0 = numpy.zeros(n_pfs)

    print('Zero order portfolios')
    print('Initial guess: {}'.format(x0))
    print('Initial error: {}'.format( constant_residuals(x0) ))

    portfolios_0 = solver(constant_residuals, x0)
    print('Solution: {}'.format(portfolios_0))
    print('Final error: {}'.format( constant_residuals(portfolios_0) ))

    if order == 1:
        dr = constant_residuals(portfolios_0, return_dr=True)
        return dr

    def dynamic_residuals(X, return_dr=False):
        x = X[:,0]
        dx = X[:,1:]
        d = {}
        for i in range(n_pfs):
            p = pf_parms[i]
            v = pf_vars[i]
            d[p] = x[i]
            d[v] = x[i]
            for j in range(n_states):
                d[pf_dparms[i][j]] = dx[i,j]
        new_model.set_calibration(d)
        if return_dr:
            dr = approximate_controls(new_model, order=2, lambda_name='lam')
            return dr
        else:
            [X_bar, X_s, X_ss, X_sss] = approximate_controls(new_model, order=3, return_dr=False, lambda_name='lam')
            crit = numpy.column_stack([
                X_bar[n_controls-n_pfs:n_controls],
                X_s[n_controls-n_pfs:n_controls,:],
            ])
            return crit



    y0 = numpy.column_stack([x0, numpy.zeros((n_pfs, n_states))])
    print('Initial error:')
    err = (dynamic_residuals(y0))

    print( abs(err).max() )
    portfolios_1 = solver(dynamic_residuals, y0)

    print('First order portfolios : ')
    print(portfolios_1)

    print('Final error:')
    print(dynamic_residuals(portfolios_1))

    dr = dynamic_residuals(portfolios_1, return_dr=True)

    # TODO: remove coefficients of criteria

    return dr
Пример #8
0
        z0 = z1

    print('finished in {} iterations'.format(it))

    return [x0,z0]



from dolo import yaml_import

from trash.dolo.numeric.perturbations_to_states import approximate_controls

model = yaml_import('../../../examples/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.time_iteration import time_iteration
dr_smol = time_iteration(model, smolyak_order=2, maxit=2, polish=True)

smin = dr_smol.bounds[0,:]
smax = dr_smol.bounds[1,:]


dr_x = SmolyakGrid( smin, smax, 3)
Пример #9
0
        print(err, err2)
        x0 = x1
        z0 = z1

    print('finished in {} iterations'.format(it))

    return [x0, z0]


from dolo import yaml_import

from trash.dolo.numeric.perturbations_to_states import approximate_controls

model = yaml_import('../../../examples/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.time_iteration import time_iteration
dr_smol = time_iteration(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
Пример #10
0
 def test_perturbation_3(self):
     from dolo import yaml_import
     from dolo.algos.perturbations import approximate_controls
     model = yaml_import('examples/models/compat/rbc.yaml')
     dr = approximate_controls(model,order=3)
Пример #11
0
 def test_perturbation_1_old(self):
     from dolo import yaml_import
     model = yaml_import('examples/models/rbc.yaml')
     from dolo.algos.perturbations import approximate_controls
     dr = approximate_controls(model, order=1)
Пример #12
0
 def test_perturbation_1_old(self):
     from dolo import yaml_import
     model = yaml_import('examples/models/rbc.yaml')
     from dolo.algos.perturbations import approximate_controls
     dr = approximate_controls(model,order=1)