Esempio n. 1
0
    def __update_from_symbolic__(self):

        import numpy
        # updates calibration according to the symbolic definitions

        system = self.symbolic.calibration_dict

        from dolo.compiler.triangular_solver import solve_triangular_system
        self.calibration_dict = solve_triangular_system( system )
        from dolo.compiler.misc import CalibrationDict, calibration_to_vector
        calib = calibration_to_vector(self.symbols, self.calibration_dict)
        self.calibration = CalibrationDict(self.symbols, calib)
        from .symbolic_eval import NumericEval
        evaluator = NumericEval(self.calibration_dict)

        # read symbolic structure
        self.options = evaluator.eval(self.symbolic.options)

        distribution = evaluator.eval(self.symbolic.distribution)
        discrete_transition = evaluator.eval(self.symbolic.discrete_transition)


        covariances = distribution
        if distribution is None:
            self.covariances = None
        else:
            self.covariances = numpy.atleast_2d(numpy.array(covariances, dtype=float))

        markov_chain = discrete_transition
        if markov_chain is None:
            self.markov_chain = None
        else:
            self.markov_chain = [numpy.atleast_2d(numpy.array(tab, dtype=float)) for tab in markov_chain]
Esempio n. 2
0
    def __update_from_symbolic__(self):

        import numpy
        # updates calibration according to the symbolic definitions

        system = self.symbolic.calibration_dict

        from dolo.compiler.triangular_solver import solve_triangular_system

        self.calibration_dict = solve_triangular_system( system )

        from dolo.compiler.misc import calibration_to_vector

        self.calibration = calibration_to_vector(self.symbols, self.calibration_dict)

        from symbolic_eval import NumericEval
        evaluator = NumericEval(self.calibration_dict)

        # read symbolic structure
        covariances = evaluator.eval(self.symbolic.covariances)
        if covariances is None:
            self.covariances = covariances
        else:
            self.covariances = numpy.atleast_2d(numpy.array(covariances, dtype=float))

        markov_chain = evaluator.eval(self.symbolic.markov_chain)
        if markov_chain is None:
            self.markov_chain = None
        else:
            self.markov_chain = [numpy.atleast_2d(numpy.array(tab, dtype=float)) for tab in markov_chain]

        self.options = evaluator.eval(self.symbolic.options)
Esempio n. 3
0
def calibration_to_vector(symbols, calibration_dict):

    from dolo.compiler.triangular_solver import solve_triangular_system

    sol = solve_triangular_system(calibration_dict)

    calibration = OrderedDict()
    for group in symbols:
        calibration[group] = numpy.array([sol[s] for s in symbols[group]],
                                         dtype=float)

    return calibration
Esempio n. 4
0
def calibration_to_vector(symbols, calibration_dict):

    from dolo.compiler.triangular_solver import solve_triangular_system
    from numpy import nan

    sol = solve_triangular_system(calibration_dict)

    calibration  = OrderedDict()
    for group in symbols:
        t = numpy.array([sol.get(s, nan) for s in symbols[group]], dtype=float)
        calibration[group] = t

    return calibration
Esempio n. 5
0
def calibration_to_vector(symbols, calibration_dict):

    from dolo.compiler.triangular_solver import solve_triangular_system
    from numpy import nan

    sol = solve_triangular_system(calibration_dict)

    calibration = dict()
    for group in symbols:
        t = numpy.array([sol.get(s, nan) for s in symbols[group]], dtype=float)
        calibration[group] = t

    return calibration
Esempio n. 6
0
    def test_solve_symbolic_system(self):
        from dolo.compiler.triangular_solver import solve_triangular_system

        [w, x, y, z, t] = vars = ['w', 'x', 'y', 'z', 't']

        eqs = ['x + y + z + t', 'y + z', 'z', '1', '1']

        sdict = {s: eqs[i] for i, s in enumerate(vars)}

        solution = solve_triangular_system(sdict)

        assert (solution['z'] == 1)
        assert (solution['t'] == 1)
        assert (solution['y'] == 1)
        assert (solution['x'] == 2)
        assert (solution['w'] == 5)
Esempio n. 7
0
    def __update_from_symbolic__(self):

        import numpy
        # updates calibration according to the symbolic definitions

        system = self.symbolic.calibration_dict

        from dolo.compiler.triangular_solver import solve_triangular_system
        self.calibration_dict = solve_triangular_system(system)

        from dolo.compiler.misc import CalibrationDict, calibration_to_vector
        calib = calibration_to_vector(self.symbols, self.calibration_dict)
        self.calibration = CalibrationDict(self.symbols, calib)

        from .symbolic_eval import NumericEval
        evaluator = NumericEval(self.calibration_dict)
        # read symbolic structure
        self.options = evaluator.eval(self.symbolic.options)
        self.exogenous = self.get_exogenous()
        self.domain = self.get_domain()
Esempio n. 8
0
    def __update_from_symbolic__(self):

        import numpy
        # updates calibration according to the symbolic definitions

        system = self.symbolic.calibration_dict

        from dolo.compiler.triangular_solver import solve_triangular_system
        self.calibration_dict = solve_triangular_system( system )

        from dolo.compiler.misc import CalibrationDict, calibration_to_vector
        calib = calibration_to_vector(self.symbols, self.calibration_dict)
        self.calibration = CalibrationDict(self.symbols, calib)

        from .symbolic_eval import NumericEval
        evaluator = NumericEval(self.calibration_dict)
        # read symbolic structure
        self.options = evaluator.eval(self.symbolic.options)
        #
        # distribution = evaluator.eval(self.symbolic.options.get('distribution'))
        # discrete_transition = evaluator.eval(self.symbolic.options.get('discrete_transition'))

        distribution = self.options.get('distribution')
        discrete_transition = self.options.get('discrete_transition')

        self.distribution = distribution
        self.discrete_transition = discrete_transition

        if distribution is None:
            self.covariances = None
        else:
            self.covariances = numpy.atleast_2d(numpy.array(distribution.sigma, dtype=float))

        markov_chain = discrete_transition

        if markov_chain is None:
            self.markov_chain = None
        else:
            markov_chain = [markov_chain.P, markov_chain.Q]
            self.markov_chain = [numpy.atleast_2d(numpy.array(tab, dtype=float)) for tab in markov_chain]
Esempio n. 9
0
    def test_solve_symbolic_system(self):
        from dolo.compiler.triangular_solver import solve_triangular_system

        [w,x,y,z,t] = vars = ['w', 'x', 'y', 'z', 't']

        eqs = [
            'x + y + z + t',
            'y + z',
            'z',
            '1',
            '1'
        ]

        sdict = {s:eqs[i] for i,s in enumerate(vars) }

        solution = solve_triangular_system(sdict)

        assert(solution['z']==1)
        assert(solution['t']==1)
        assert(solution['y']==1)
        assert(solution['x']==2)
        assert(solution['w']==5)
Esempio n. 10
0
    def get_calibration(self):

        symbols = self.symbols
        calibration = self.data.get("calibration", {})
        definitions = self.definitions

        initial_values = {
            'exogenous': 0,
            'expectations': 0,
            'values': 0,
            'controls': float('nan'),
            'states': float('nan')
        }

        # variables defined by a model equation default to using these definitions
        initialized_from_model = {
            'values': 'value',
            'expectations': 'expectation',
            'direct_responses': 'direct_response'
        }

        for k, v in definitions.items():
            if k not in calibration:
                calibration[k] = v

        for symbol_group in symbols:
            if symbol_group not in initialized_from_model.keys():
                if symbol_group in initial_values:
                    default = initial_values[symbol_group]
                else:
                    default = float('nan')
                for s in symbols[symbol_group]:
                    if s not in calibration:
                        calibration[s] = default

        from dolo.compiler.triangular_solver import solve_triangular_system
        return solve_triangular_system(calibration)
Esempio n. 11
0
    def get_calibration(self):

        symbols = self.symbols
        calibration = self.data.get("calibration", {})
        definitions = self.definitions

        initial_values = {
            'exogenous': 0,
            'expectations': 0,
            'values': 0,
            'controls': float('nan'),
            'states': float('nan')
        }

        # variables defined by a model equation default to using these definitions
        initialized_from_model = {
            'values': 'value',
            'expectations': 'expectation',
            'direct_responses': 'direct_response'
        }

        for k, v in definitions.items():
            if k not in calibration:
                calibration[k] = v

        for symbol_group in symbols:
            if symbol_group not in initialized_from_model.keys():
                if symbol_group in initial_values:
                    default = initial_values[symbol_group]
                else:
                    default = float('nan')
                for s in symbols[symbol_group]:
                    if s not in calibration:
                        calibration[s] = default

        from dolo.compiler.triangular_solver import solve_triangular_system
        return solve_triangular_system(calibration)
Esempio n. 12
0
    def __get_calibration__(self):

        calibration = self.data.get("calibration", {})
        from dolo.compiler.triangular_solver import solve_triangular_system
        return solve_triangular_system(calibration)
Esempio n. 13
0
    # from dolo.compiler.import_dynare import import_dynare
#     model = import_dynare("/home/pablo/Downloads/EA_QUEST3.mod", output_type='json')
#     txt = json.dumps(model)
#     model['calibration']
#     with open("EA_QUEST3.json",'w') as f:
#         json.dump(model,f)


with timeit('Load json'):
    with open("EA_QUEST3.json") as f:
        model = json.load(f)


with timeit("Solve triangular system"):
    calibration = model['calibration']
    sol = solve_triangular_system(calibration)


with timeit("Parse and normalize equations"):
    equations = model['equations']

    compat = lambda s: s.replace("^", "**").replace('==','=').replace('=','==')

    equations = [compat(eq) for eq in equations]
    equations = ['{} - ({})'.format(*str.split(eq,'==')) for eq in equations]
    equations = [to_expr(e) for e in equations]

    all_symbols = sum(model['symbols'].values(), [])
    sds = StandardizeDatesSimple(all_symbols)
    equations_normalized = [sds.visit(e) for e in equations]
    equations_normalized_strings = [to_source(e) for e in equations_normalized]