<commit_before>from dolo.numeric.taylor_expansion import CDR
from dolo.numeric.extern.qz import qzordered
from dolo.compiler.derivatives import get_model_derivatives
from typing import List
from numpy import ndarray

def perturb(model, order=1, return_dr=True, steady_state=None, verbose=True):

    from dolo.numeric.processes import IIDProcess

    # here we should check that exogenous don't appear with time t-1 in transition
    # and with time t+1 in arbitrage.
    assert isinstance(model.exogenous, IIDProcess)

    import numpy

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

    [f, g] = get_model_derivatives(model, order=order, calibration=calibration)
    sigma = model.exogenous.Σ

    problem = PerturbationProblem(f,g,sigma)
    print(len(problem.f))

    pert_sol = state_perturb(problem, verbose=verbose)

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

    n_s = len(model.symbols['states'])
    n_c = len(model.symbols['controls'])

    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<commit_msg>FIX: Perturbation was printing some object length. Delete it<commit_after>from dolo.numeric.taylor_expansion import CDR
from dolo.numeric.extern.qz import qzordered
from dolo.compiler.derivatives import get_model_derivatives
from typing import List
from numpy import ndarray

def perturb(model, order=1, return_dr=True, steady_state=None, verbose=True):

    from dolo.numeric.processes import IIDProcess

    # here we should check that exogenous don't appear with time t-1 in transition
    # and with time t+1 in arbitrage.
    assert isinstance(model.exogenous, IIDProcess)

    import numpy

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

    [f, g] = get_model_derivatives(model, order=order, calibration=calibration)
    sigma = model.exogenous.Σ

    problem = PerturbationProblem(f,g,sigma)

    pert_sol = state_perturb(problem, verbose=verbose)

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

    n_s = len(model.symbols['states'])
    n_c = len(model.symbols['controls'])

    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
{'fraction_non_alphanumeric': 0.09886264216972879, 'fraction_numerical': 0.0058326042578011085, 'mean_word_length': 2.543388429752066, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2352854', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1113, 'n_words': 286}