Example #1
0
    def __init__(self, fname, i_options={}, dptype=None, debug=False):

        txt = read_file_or_url(fname)

        try:
            model_data, hmodel_data = yaml.compose_all(txt,
                                                       Loader=yaml.BaseLoader)
        except Exception as ex:
            print(
                "Error while parsing YAML file. Probable YAML syntax error in file : ",
                fname,
            )
            raise ex

        self.data = hmodel_data

        self.__model__ = Model(model_data)

        self.discretization_options = i_options

        # cache for functions
        self.__symbols__ = None
        self.__transition__ = None
        self.__equilibrium__ = None
        self.__projection__ = None
        self.__features__ = None

        self.debug = debug

        self.__set_changed__()

        from dolo.numeric.processes import IIDProcess, ProductProcess

        self.check()
        self.dptype = dptype
Example #2
0
def yaml_import(fname, check=True, check_only=False):

    txt = read_file_or_url(fname)
    txt = txt.replace('^', '**')

    try:
        data = ry.load(txt, ry.RoundTripLoader)
    except Exception as ex:
        print(
            "Error while parsing YAML file. Probable syntax error in your model file : ",
            fname)
        raise ex

    if check:
        from dolo.linter import lint
        output = lint(data, source=fname)
        if len(output) > 0:
            print(output)

    if check_only:
        return output

    data['filename'] = fname

    from dolo.compiler.model import Model

    return Model(data)
Example #3
0
def yaml_import(fname, check=True, check_only=False):

    txt = read_file_or_url(fname)

    try:
        data = yaml.compose(txt)
        # print(data)
        # return data
    except Exception as ex:
        print(
            "Error while parsing YAML file. Probable YAML syntax error in file : ",
            fname,
        )
        raise ex

    # if check:
    #     from dolo.linter import lint
    #     data = ry.load(txt, ry.RoundTripLoader)
    #     output = lint(data, source=fname)
    #     if len(output) > 0:
    #         print(output)

    # if check_only:
    #     return output

    data["filename"] = fname

    from dolo.compiler.model import Model

    return Model(data, check=check)
Example #4
0
def yaml_import(fname, return_symbolic=False, check=True, check_only=False):

    txt = read_file_or_url(fname)

    if check:
        from dolo.linter import lint
        output = lint(txt)
        if len(output)>0:
            print(output)

    if check_only:
        return output

    txt = txt.replace('^', '**')

    return fast_import(txt, return_symbolic=return_symbolic, filename=fname)
Example #5
0
def yaml_import(fname, check=True, check_only=False):

    txt = read_file_or_url(fname)

    if check:
        from dolo.linter import lint
        output = lint(txt)
        if len(output) > 0:
            print(output)

    if check_only:
        return output

    txt = txt.replace('^', '**')

    data = ry.load(txt, ry.RoundTripLoader)
    data['filename'] = fname

    from dolo.compiler.model import Model

    return Model(data)
Example #6
0
    def __init__(self, fname, i_options={}, dptype=None, debug=False):

        txt = read_file_or_url(fname)

        try:
            model_data, hmodel_data = ry.load_all(txt, ry.RoundTripLoader)
        except Exception as ex:
            print(
                "Error while parsing YAML file. Probable YAML syntax error in file : ",
                fname)
            raise ex

        model_data, hmodel_data = ry.load_all(txt, Loader=ry.RoundTripLoader)

        self.__model__ = Model(model_data)
        self.data = hmodel_data

        self.discretization_options = i_options

        # cache for functions
        self.__equilibrium__ = None
        self.__projection__ = None
        self.__features__ = None

        self.debug = debug

        self.check()
        self.__set_changed__()

        from dolo.numeric.processes import IIDProcess, ProductProcess
        if dptype is None and isinstance(
                self.model.exogenous,
                ProductProcess) and (self.model.exogenous.processes[1],
                                     IIDProcess):
            dptype = 'iid'
        else:
            dptype = 'mc'
        self.dptype = dptype
Example #7
0
def yaml_import(fname, txt=None, return_symbolic=False):

    if txt is None:

        txt = read_file_or_url(fname)

    txt = txt.replace('^', '**')

    import yaml

    try:
        data = yaml.safe_load(txt)
    except Exception as e:
        raise e

    if 'symbols' not in data:
        if 'declarations' in data:
            data['symbols'] = data['declarations']
            # TODO: raise an error/warning here
        else:
            raise Exception("Missing section: 'symbols'.")

    if 'model_type' not in data:
        if 'markov_states' in data['symbols']:
            model_type = 'dtmscc'
        elif 'states' in data['symbols']:
            model_type = 'dtcscc'
        elif 'variables' in data['symbols']:
            model_type = 'dynare'
        else:
            raise Exception("'model_type' was not defined and couldn't be guessed.")
        print("Model type detected as '{}'".format(model_type))
    else:
        model_type = data['model_type']


    if 'name' not in data:
        data['name'] = 'anonymous'
        print("Missing model name. Set as '{}'".format(data['name']))



    if 'auxiliary' in data['symbols']:
        aux = data['symbols'].pop('auxiliary')
        data['symbols']['auxiliaries'] = aux

    # check equations
    if 'equations' not in data:
        raise Exception("Missing section: 'equations'.")

    if 'calibration' not in data:
        raise Exception("Missing section: 'calibration'.")

    options = data.get('options')

    if 'steady_state' in data['calibration']:
        oldstyle = data['calibration']
        covs = oldstyle['covariances']
        steady = oldstyle['steady_state']
        params = oldstyle['parameters']
        pp = dict()
        pp.update(steady)
        pp.update(params)
        data['calibration'] = pp
        data['covariances'] = eval(
            "numpy.array({}, dtype='object')".format(covs)
        )

    # model specific

    if model_type in ('dtcscc', 'dynare'):
        if 'distribution' not in data:
            if 'covariances' in data:
                data['distribution'] = {'Normal':data['covariances']}
            else:
                n_s = len(data['symbols']['shocks'])
                zero_matrix = [ [0.0]*n_s ]*n_s
                data['distribution'] = {'Normal': [zero_matrix]}
                # raise Exception(
                #     "Missing section (model type {}): 'distribution'.".format(model_type)
                # )

    if model_type == 'dtmscc':
        if 'discrete_transition' not in data:
            if 'markov_chain' not in data:
                raise Exception(
                    "Missing section (model {}): 'discrete_transition'.".format(model_type)
                )
            else:
                mc = data['markov_chain']
                if isinstance(mc, list):
                    data['discrete_transition'] = {'MarkovChain': mc}
                else:
                    data['discrete_transition'] = mc

        # data['markov_chain'] = data['distribution']['MarkovChain']


    model_name = data['name']
    symbols = data['symbols']
    symbolic_equations = data['equations']
    symbolic_calibration = data['calibration']

    # all symbols are initialized to nan
    # except shocks and markov_states which are initialized to 0
    initial_values = {
        'shocks': 0,
        'markov_states': 0,
        'controls': float('nan'),
        'states': float('nan')
    }

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

    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 symbolic_calibration:
                    symbolic_calibration[s] = default

    if model_type != 'dynare':
        # add the calibration of variables defined in the model not in calibration
        all_variables = sum( [symbols[group] for group in symbols if group!= 'parameters'], [])
        from dolo.compiler.function_compiler_ast import timeshift
        print(all_variables)
        for eq_group in symbolic_equations.keys():
            if eq_group in initialized_from_model.values():
                for eq in symbolic_equations[eq_group]:
                    lhs, rhs = str.split(eq, '=')
                    lhs, rhs = [str.strip(e) for e in [lhs, rhs]]
                    rhs_ss = timeshift(rhs, all_variables, 'S')
                    if lhs not in symbolic_calibration.keys():
                        print((lhs, rhs_ss))
                        symbolic_calibration[lhs] = rhs_ss


    # read covariance matrix

    symbolic_covariances = data.get('covariances')

    if symbolic_covariances is not None:
        try:
            tl = numpy.array(symbolic_covariances, dtype='object')
        except:
            msg = "Incorrect covariances matrix: {}.".format(
                symbolic_covariances
            )
            raise Exception(msg)
        try:
            assert(tl.ndim == 2)
            assert(tl.shape[0] == tl.shape[1])
        except:
            msg = """Covariances matrix should be square.\
            Found {} matrix""".format(tl.shape)
            raise Exception(msg)
        symbolic_covariances = tl

    symbolic_distribution = data.get('distribution')
    symbolic_discrete_transition = data.get('discrete_transition')

    definitions = data.get('definitions', {})


    options = data.get('options')

    infos = dict()
    infos['filename'] = fname
    infos['name'] = model_name
    infos['type'] = model_type

    from dolo.compiler.model_symbolic import SymbolicModel
    smodel = SymbolicModel(model_name, model_type, symbols, symbolic_equations,
                           symbolic_calibration,
                           discrete_transition=symbolic_discrete_transition,
                           distribution=symbolic_distribution,
                           options=options, definitions=definitions)

    if return_symbolic:
        return smodel

    if model_type in ('dtcscc','dtmscc'):
        from dolo.compiler.model_numeric import NumericModel
        model = NumericModel(smodel, infos=infos)
    else:
        from dolo.compiler.model_dynare import DynareModel
        model = DynareModel(smodel, infos=infos)
    return model
Example #8
0
def yaml_import(fname, txt=None, return_symbolic=False):

    if txt is None:

        txt = read_file_or_url(fname)

    txt = txt.replace('^', '**')

    import yaml

    try:
        data = yaml.safe_load(txt)
    except Exception as e:
        raise e

    if 'symbols' not in data:
        if 'declarations' in data:
            data['symbols'] = data['declarations']
            # TODO: raise an error/warning here
        else:
            raise Exception("Missing section: 'symbols'.")

    if 'model_type' not in data:
        if 'markov_states' in data['symbols']:
            model_type = 'dtmscc'
        elif 'states' in data['symbols']:
            model_type = 'dtcscc'
        elif 'variables' in data['symbols']:
            model_type = 'dynare'
        else:
            raise Exception("'model_type' was not defined and couldn't be guessed.")
        print("Model type detected as '{}'".format(model_type))
    else:
        model_type = data['model_type']


    if 'name' not in data:
        data['name'] = 'anonymous'
        print("Missing model name. Set as '{}'".format(data['name']))



    if 'auxiliary' in data['symbols']:
        aux = data['symbols'].pop('auxiliary')
        data['symbols']['auxiliaries'] = aux

    # check equations
    if 'equations' not in data:
        raise Exception("Missing section: 'equations'.")

    if 'calibration' not in data:
        raise Exception("Missing section: 'calibration'.")

    options = data.get('options')

    if 'steady_state' in data['calibration']:
        oldstyle = data['calibration']
        covs = oldstyle['covariances']
        steady = oldstyle['steady_state']
        params = oldstyle['parameters']
        pp = dict()
        pp.update(steady)
        pp.update(params)
        data['calibration'] = pp
        data['covariances'] = eval(
            "numpy.array({}, dtype='object')".format(covs)
        )

    # model specific

    if model_type in ('dtcscc', 'dynare'):
        if 'distribution' not in data:
            if 'covariances' in data:
                data['distribution'] = {'Normal':data['covariances']}
            else:
                raise Exception(
                    "Missing section (model type {}): 'distribution'.".format(model_type)
                )

    if model_type == 'dtmscc':
        if 'discrete_transition' not in data:
            if 'markov_chain' not in data:
                raise Exception(
                    "Missing section (model {}): 'discrete_transition'.".format(model_type)
                )
            else:
                mc = data['markov_chain']
                if isinstance(mc, list):
                    data['discrete_transition'] = {'MarkovChain': mc}
                else:
                    data['discrete_transition'] = mc

        # data['markov_chain'] = data['distribution']['MarkovChain']


    model_name = data['name']
    symbols = data['symbols']
    symbolic_equations = data['equations']
    symbolic_calibration = data['calibration']

    # all symbols are initialized to nan
    # except shocks and markov_states which are initialized to 0
    initial_values = {
        'shocks': 0,
        'markov_states': 0,
        'controls': float('nan'),
        'states': float('nan')
    }

    for symbol_group in symbols:
        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 symbolic_calibration:
                symbolic_calibration[s] = default


    # read covariance matrix

    symbolic_covariances = data.get('covariances')

    if symbolic_covariances is not None:
        try:
            tl = numpy.array(symbolic_covariances, dtype='object')
        except:
            msg = "Incorrect covariances matrix: {}.".format(
                symbolic_covariances
            )
            raise Exception(msg)
        try:
            assert(tl.ndim == 2)
            assert(tl.shape[0] == tl.shape[1])
        except:
            msg = """Covariances matrix should be square.\
            Found {} matrix""".format(tl.shape)
            raise Exception(msg)
        symbolic_covariances = tl

    symbolic_distribution = data.get('distribution')
    symbolic_discrete_transition = data.get('discrete_transition')

    definitions = data.get('definitions', {})


    options = data.get('options')

    infos = dict()
    infos['filename'] = fname
    infos['name'] = model_name
    infos['type'] = model_type

    from dolo.compiler.model_symbolic import SymbolicModel
    smodel = SymbolicModel(model_name, model_type, symbols, symbolic_equations,
                           symbolic_calibration,
                           discrete_transition=symbolic_discrete_transition,
                           distribution=symbolic_distribution,
                           options=options, definitions=definitions)

    if return_symbolic:
        return smodel

    if model_type in ('dtcscc','dtmscc'):
        from dolo.compiler.model_numeric import NumericModel
        model = NumericModel(smodel, infos=infos)
    else:
        from dolo.compiler.model_dynare import DynareModel
        model = DynareModel(smodel, infos=infos)
    return model