Esempio n. 1
0
def solve_model_around_risky_ss(model,
                                verbose=False,
                                return_dr=True,
                                initial_sol=None):

    #model = yaml_import(filename)

    if initial_sol == None:
        if 'model_type' in model and model['model_type'] == 'portfolios':
            print(
                'This is a portfolio model ! Converting to deterministic one.')
            from portfolio_perturbation import portfolios_to_deterministic
            model = portfolios_to_deterministic(model, ['x_1', 'x_2'])
            model.check()
        from dolo.numeric.perturbations_to_states import approximate_controls
        perturb_sol = approximate_controls(model,
                                           order=1,
                                           return_dr=False,
                                           substitute_auxiliary=True)
        [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['variables_groups']['controls']), :]
    X_bar = X_bar[:len(model['variables_groups']['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, 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]
Esempio n. 2
0
def approximate_controls(model,
                         order=1,
                         lambda_name=None,
                         return_dr=True,
                         steady_state=None,
                         verbose=True):

    assert (model.model_type == 'dtcscc')
    import numpy

    [f_fun, g_fun] = model_to_fg(model, order=order)

    parms = model.calibration['parameters']
    distrib = model.get_distribution()
    sigma = distrib.sigma

    if steady_state is None:
        calibration = model.calibration
    else:
        calibration = steady_state

    states_ss = calibration['states']
    controls_ss = calibration['controls']
    shocks_ss = calibration['shocks']

    f_args_ss = numpy.concatenate(
        [states_ss, controls_ss, states_ss, controls_ss])
    g_args_ss = numpy.concatenate([states_ss, controls_ss, shocks_ss])

    f = f_fun(f_args_ss, parms, order=order)
    g = g_fun(g_args_ss, parms, order=order)

    if lambda_name:
        epsilon = 0.001
        sigma_index = [p.name for p in model.parameters].index(lambda_name)
        pert_parms = parms.copy()
        pert_parms[sigma_index] += epsilon

        g_pert = g_fun(g_args_ss, pert_parms)
        sig2 = (g_pert[0] - g[0]) / epsilon * 2
        sig2_s = (g_pert[1] - g[1]) / epsilon * 2
        pert_sol = state_perturb(f,
                                 g,
                                 sigma,
                                 sigma2_correction=[sig2, sig2_s],
                                 verbose=verbose)

    else:
        g = g_fun(g_args_ss, parms, order=order)
        pert_sol = state_perturb(f, g, sigma, verbose=verbose)

    n_s = len(states_ss)
    n_c = len(controls_ss)

    if order == 1:
        if return_dr:
            S_bar = numpy.array(states_ss)
            X_bar = numpy.array(controls_ss)
            # add transitions of states to the d.r.

            X_s = pert_sol[0]
            A = g[1][:, :n_s] + numpy.dot(g[1][:, n_s:n_s + n_c], X_s)
            B = g[1][:, n_s + n_c:]
            dr = CDR([S_bar, X_bar, X_s])
            dr.A = A
            dr.B = B
            dr.sigma = sigma
            return dr

        return [controls_ss] + pert_sol

    if order == 2:
        [[X_s, X_ss], [X_tt]] = pert_sol
        X_bar = controls_ss + X_tt / 2
        if return_dr:
            S_bar = states_ss
            S_bar = numpy.array(S_bar)
            X_bar = numpy.array(X_bar)
            dr = CDR([S_bar, X_bar, X_s, X_ss])
            A = g[1][:, :n_s] + numpy.dot(g[1][:, n_s:n_s + n_c], X_s)
            B = g[1][:, n_s + n_c:]
            dr.sigma = sigma
            dr.A = A
            dr.B = B
            return dr
        return [X_bar, X_s, X_ss]

    if order == 3:
        [[X_s, X_ss, X_sss], [X_tt, X_stt]] = pert_sol
        X_bar = controls_ss + X_tt / 2
        X_s = X_s + X_stt / 2
        if return_dr:
            S_bar = states_ss
            dr = CDR([S_bar, X_bar, X_s, X_ss, X_sss])
            dr.sigma = sigma
            return dr
        return [X_bar, X_s, X_ss, X_sss]
def approximate_controls(model, order=1, lambda_name=None, return_dr=True, steady_state=None, verbose=True, eigmax=1.0):

    assert(model.model_type=='dtcscc')

    [f_fun, g_fun] = model_to_fg(model, order=order)


    import numpy

    states = model.symbols['states']
    controls = model.symbols['controls']

    parms = model.calibration['parameters']
    sigma = model.covariances

    if steady_state is None:
        calibration = model.calibration
    else:
        calibration = steady_state

    states_ss = calibration['states']
    controls_ss = calibration['controls']
    shocks_ss = calibration['shocks']

    f_args_ss = numpy.concatenate( [states_ss, controls_ss, states_ss, controls_ss] )
    g_args_ss = numpy.concatenate( [states_ss, controls_ss, shocks_ss] )


    f = f_fun( f_args_ss, parms, order=order)
    g = g_fun( g_args_ss, parms, order=order)


    if lambda_name:
        epsilon = 0.001
        sigma_index = [p.name for p in model.parameters].index(lambda_name)
        pert_parms = parms.copy()
        pert_parms[sigma_index] += epsilon

        g_pert = g_fun(g_args_ss, pert_parms)
        sig2 = (g_pert[0] - g[0])/epsilon*2
        sig2_s = (g_pert[1] - g[1])/epsilon*2
        pert_sol = state_perturb(f, g, sigma, sigma2_correction = [sig2, sig2_s], eigmax=eigmax, verbose=verbose)

    else:
        g = g_fun( g_args_ss, parms, order=order)
        pert_sol = state_perturb(f, g, sigma, eigmax=eigmax, verbose=verbose )


    n_s = len(states_ss)
    n_c = len(controls_ss)

    if order == 1:
        if return_dr:
            S_bar = numpy.array( states_ss )
            X_bar = numpy.array( controls_ss )
            # add transitions of states to the d.r.



            X_s = pert_sol[0]
            A = g[1][:,:n_s] + numpy.dot( g[1][:,n_s:n_s+n_c], X_s )
            B = g[1][:,n_s+n_c:]
            dr = CDR([S_bar, X_bar, X_s])
            dr.A = A
            dr.B = B
            dr.sigma = sigma
            return dr

        return [controls_ss] + pert_sol

    if order == 2:
        [[X_s,X_ss],[X_tt]] = pert_sol
        X_bar = controls_ss + X_tt/2
        if return_dr:
            S_bar = states_ss
            S_bar = numpy.array(S_bar)
            X_bar = numpy.array(X_bar)
            dr = CDR([S_bar, X_bar, X_s, X_ss])
            A = g[1][:,:n_s] + numpy.dot( g[1][:,n_s:n_s+n_c], X_s )
            B = g[1][:,n_s+n_c:]
            dr.sigma = sigma
            dr.A = A
            dr.B = B
            return dr
        return [X_bar, X_s, X_ss]


    if order == 3:
        [[X_s,X_ss,X_sss],[X_tt, X_stt]] = pert_sol
        X_bar = controls_ss + X_tt/2
        X_s = X_s + X_stt/2
        if return_dr:
            S_bar = states_ss
            dr = CDR([S_bar, X_bar, X_s, X_ss, X_sss])
            dr.sigma = sigma
            return dr
        return [X_bar, X_s, X_ss, X_sss]
def approximate_controls(model, order=1, lambda_name=None, substitute_auxiliary=False, return_dr=True, solve_systems=False):

    [gm, g_fun, f_fun] = interim_gm(model, substitute_auxiliary, solve_systems, order)

    # get steady_state
    import numpy
    [y0,x,parms] = model.read_calibration()
    parms = numpy.array(parms)

    y = y0
    #y = model.solve_for_steady_state(y0)

    sigma = numpy.array( model.read_covariances() ).astype(float)
    states_ss = numpy.array([y[model.variables.index(v)] for v in gm['states']]).astype(float)
    controls_ss = numpy.array([y[model.variables.index(v)] for v in gm['controls']]).astype(float)
    shocks_ss = x

    f_args_ss = numpy.concatenate( [states_ss, controls_ss, states_ss, controls_ss] )
    g_args_ss = numpy.concatenate( [states_ss, controls_ss, shocks_ss] )

    f = f_fun( f_args_ss, parms)
    g = g_fun( g_args_ss, parms)

    if lambda_name:
        epsilon = 0.001
        sigma_index = [p.name for p in model.parameters].index(lambda_name)
        pert_parms = parms.copy()
        pert_parms[sigma_index] += epsilon

        g_pert = g_fun( g_args_ss, pert_parms)
        sig2 = (g_pert[0] - g[0])/epsilon*2
        sig2_s = (g_pert[1] - g[1])/epsilon*2
        pert_sol = state_perturb(f, g, sigma, sigma2_correction = [sig2, sig2_s] )

    else:
        g = g_fun( g_args_ss, parms)
        pert_sol = state_perturb(f, g, sigma )

    n_s = len(states_ss)
    n_c = len(controls_ss)

    if order == 1:
        if return_dr:
            S_bar = numpy.array( states_ss )
            X_bar = numpy.array( controls_ss )
            # add transitions of states to the d.r.



            X_s = pert_sol[0]
            A = g[1][:,:n_s] + numpy.dot( g[1][:,n_s:n_s+n_c], X_s )
            B = g[1][:,n_s+n_c:]
            dr = CDR([S_bar, X_bar, X_s])
            dr.A = A
            dr.B = B
            dr.sigma = sigma
            return dr
        return [controls_ss] + pert_sol

    if order == 2:
        [[X_s,X_ss],[X_tt]] = pert_sol
        X_bar = controls_ss + X_tt/2
        if return_dr:
            S_bar = states_ss
            S_bar = numpy.array(S_bar)
            X_bar = numpy.array(X_bar)
            dr = CDR([S_bar, X_bar, X_s, X_ss])
            A = g[1][:,:n_s] + numpy.dot( g[1][:,n_s:n_s+n_c], X_s )
            B = g[1][:,n_s+n_c:]
            dr.sigma = sigma
            dr.A = A
            dr.B = B
            return dr
        return [X_bar, X_s, X_ss]


    if order == 3:
        [[X_s,X_ss,X_sss],[X_tt, X_stt]] = pert_sol
        X_bar = controls_ss + X_tt/2
        X_s = X_s + X_stt/2
        if return_dr:
            S_bar = states_ss
            dr = CDR([S_bar, X_bar, X_s, X_ss, X_sss])
            dr.sigma = sigma
            return dr
        return [X_bar, X_s, X_ss, X_sss]
Esempio n. 5
0
def approximate_controls(model,
                         order=1,
                         lambda_name=None,
                         return_dr=True,
                         verbose=True):

    from dolo.compiler.converter import model_to_fg
    [g_fun, f_fun] = model_to_fg(model, order=order)

    # get steady_state
    import numpy

    states = model.symbols_s['states']
    controls = model.symbols_s['controls']

    parms = model.calibration['parameters']
    sigma = model.calibration['covariances']
    states_ss = model.calibration['states']
    controls_ss = model.calibration['controls']
    shocks_ss = model.calibration['shocks']

    f_args_ss = numpy.concatenate(
        [states_ss, controls_ss, states_ss, controls_ss])
    g_args_ss = numpy.concatenate([states_ss, controls_ss, shocks_ss])

    f = f_fun(f_args_ss, parms)
    g = g_fun(g_args_ss, parms)

    if lambda_name:
        epsilon = 0.001
        sigma_index = [p.name for p in model.parameters].index(lambda_name)
        pert_parms = parms.copy()
        pert_parms[sigma_index] += epsilon

        g_pert = g_fun(g_args_ss, pert_parms)
        sig2 = (g_pert[0] - g[0]) / epsilon * 2
        sig2_s = (g_pert[1] - g[1]) / epsilon * 2
        pert_sol = state_perturb(f, g, sigma, sigma2_correction=[sig2, sig2_s])

    else:
        g = g_fun(g_args_ss, parms)
        pert_sol = state_perturb(f, g, sigma)

    n_s = len(states_ss)
    n_c = len(controls_ss)

    if order == 1:
        if return_dr:
            S_bar = numpy.array(states_ss)
            X_bar = numpy.array(controls_ss)
            # add transitions of states to the d.r.

            X_s = pert_sol[0]
            A = g[1][:, :n_s] + numpy.dot(g[1][:, n_s:n_s + n_c], X_s)
            B = g[1][:, n_s + n_c:]
            dr = CDR([S_bar, X_bar, X_s])
            dr.A = A
            dr.B = B
            dr.sigma = sigma
            return dr
        return [controls_ss] + pert_sol

    if order == 2:
        [[X_s, X_ss], [X_tt]] = pert_sol
        X_bar = controls_ss + X_tt / 2
        if return_dr:
            S_bar = states_ss
            S_bar = numpy.array(S_bar)
            X_bar = numpy.array(X_bar)
            dr = CDR([S_bar, X_bar, X_s, X_ss])
            A = g[1][:, :n_s] + numpy.dot(g[1][:, n_s:n_s + n_c], X_s)
            B = g[1][:, n_s + n_c:]
            dr.sigma = sigma
            dr.A = A
            dr.B = B
            return dr
        return [X_bar, X_s, X_ss]

    if order == 3:
        [[X_s, X_ss, X_sss], [X_tt, X_stt]] = pert_sol
        X_bar = controls_ss + X_tt / 2
        X_s = X_s + X_stt / 2
        if return_dr:
            S_bar = states_ss
            dr = CDR([S_bar, X_bar, X_s, X_ss, X_sss])
            dr.sigma = sigma
            return dr
        return [X_bar, X_s, X_ss, X_sss]
def approximate_controls(cmodel, order=1, lambda_name=None, return_dr=True):

    from dolo.symbolic.model import SModel
    if not isinstance(cmodel, SModel):
        model = cmodel.model
    else:
        model = cmodel

    from dolo.compiler.compiler_functions import model_to_fg
    [g_fun, f_fun] = model_to_fg(model, order=order)

    # get steady_state
    import numpy



    states = model.symbols_s['states']
    controls = model.symbols_s['controls']

    parms = model.calibration['parameters']
    sigma = model.calibration['covariances']
    states_ss = model.calibration['states']
    controls_ss = model.calibration['controls']
    shocks_ss = model.calibration['shocks']

    f_args_ss = numpy.concatenate( [states_ss, controls_ss, states_ss, controls_ss] )
    g_args_ss = numpy.concatenate( [states_ss, controls_ss, shocks_ss] )

    f = f_fun( f_args_ss, parms)
    g = g_fun( g_args_ss, parms)

    if lambda_name:
        epsilon = 0.001
        sigma_index = [p.name for p in model.parameters].index(lambda_name)
        pert_parms = parms.copy()
        pert_parms[sigma_index] += epsilon

        g_pert = g_fun(g_args_ss, pert_parms)
        sig2 = (g_pert[0] - g[0])/epsilon*2
        sig2_s = (g_pert[1] - g[1])/epsilon*2
        pert_sol = state_perturb(f, g, sigma, sigma2_correction = [sig2, sig2_s] )

    else:
        g = g_fun( g_args_ss, parms)
        pert_sol = state_perturb(f, g, sigma )

    n_s = len(states_ss)
    n_c = len(controls_ss)

    if order == 1:
        if return_dr:
            S_bar = numpy.array( states_ss )
            X_bar = numpy.array( controls_ss )
            # add transitions of states to the d.r.



            X_s = pert_sol[0]
            A = g[1][:,:n_s] + numpy.dot( g[1][:,n_s:n_s+n_c], X_s )
            B = g[1][:,n_s+n_c:]
            dr = CDR([S_bar, X_bar, X_s])
            dr.A = A
            dr.B = B
            dr.sigma = sigma
            return dr
        return [controls_ss] + pert_sol

    if order == 2:
        [[X_s,X_ss],[X_tt]] = pert_sol
        X_bar = controls_ss + X_tt/2
        if return_dr:
            S_bar = states_ss
            S_bar = numpy.array(S_bar)
            X_bar = numpy.array(X_bar)
            dr = CDR([S_bar, X_bar, X_s, X_ss])
            A = g[1][:,:n_s] + numpy.dot( g[1][:,n_s:n_s+n_c], X_s )
            B = g[1][:,n_s+n_c:]
            dr.sigma = sigma
            dr.A = A
            dr.B = B
            return dr
        return [X_bar, X_s, X_ss]


    if order == 3:
        [[X_s,X_ss,X_sss],[X_tt, X_stt]] = pert_sol
        X_bar = controls_ss + X_tt/2
        X_s = X_s + X_stt/2
        if return_dr:
            S_bar = states_ss
            dr = CDR([S_bar, X_bar, X_s, X_ss, X_sss])
            dr.sigma = sigma
            return dr
        return [X_bar, X_s, X_ss, X_sss]
Esempio n. 7
0
def approximate_controls(model, return_dr=True):

    # get steady_state
    import numpy

    p = model.calibration['parameters']
    sigma = model.calibration['covariances']
    s = model.calibration['states'][:, None]
    x = model.calibration['controls'][:, None]
    e = model.calibration['shocks'][:, None]

    from numpy.linalg import solve

    g = model.functions['transition']
    f = model.functions['arbitrage']

    l = g(s, x, e, p, derivs=True)
    [junk, g_s, g_x, g_e] = [el[..., 0] for el in l]

    if model.model_type == "fg2":
        l = f(s, x, e, s, x, p, derivs=True)
        [res, f_s, f_x, f_e, f_S, f_X] = [el[..., 0] for el in l]
    else:
        l = f(s, x, s, x, p, derivs=True)
        [res, f_s, f_x, f_S, f_X] = [el[..., 0] for el in l]

    n_s = g_s.shape[0]  # number of controls
    n_x = g_x.shape[1]  # number of states
    n_e = g_e.shape[1]

    n_v = n_s + n_x

    A = row_stack([
        column_stack([eye(n_s), zeros((n_s, n_x))]),
        column_stack([-f_S, -f_X])
    ])

    B = row_stack([column_stack([g_s, g_x]), column_stack([f_s, f_x])])

    from dolo.numeric.extern.qz import qzordered
    [S, T, Q, Z, eigval] = qzordered(A, B, n_s)
    Q = Q.real  # is it really necessary ?
    Z = Z.real

    Z11 = Z[:n_s, :n_s]
    Z12 = Z[:n_s, n_s:]
    Z21 = Z[n_s:, :n_s]
    Z22 = Z[n_s:, n_s:]
    S11 = S[:n_s, :n_s]
    T11 = T[:n_s, :n_s]

    # first order solution
    C = solve(Z11.T, Z21.T).T
    P = np.dot(solve(S11.T, Z11.T).T, solve(Z11.T, T11.T).T)
    Q = g_e

    s = s.ravel()
    x = x.ravel()

    A = g_s + dot(g_x, C)
    B = g_e

    dr = CDR([s, x, C])
    dr.A = A
    dr.B = B
    dr.sigma = sigma

    return dr