Esempio n. 1
0
    def compute_static_mfile(self, max_order=1):

        print "Computing static .m file at order {0}.".format(max_order)

        DerivativesTree.symbol_type = Variable

        model = self.model
        var_order = model.variables

        # TODO create a log system

        sols = []
        i = 0
        for eq in model.equations:
            i += 1
            l = [tv for tv in eq.atoms() if isinstance(tv, Variable)]
            expr = eq.gap
            for tv in l:
                if tv.lag != 0:
                    expr = expr.subs(tv, tv.P)
            ndt = DerivativesTree(expr)
            ndt.compute_nth_order_children(max_order)
            sols.append(ndt)
        self.static_derivatives = sols

        stat_subs_dict = self.static_substitution_list()

        stat_printer = DicPrinter(stat_subs_dict)

        txt = """function [residual, {gs}] = {fname}_static(y, x, params, it_)
%
% Status : Computes static model for Dynare
%
% Warning : this file is generated automatically by Dynare
%           from model file (.mod)

%
% Model equations
%

    residual = zeros({neq}, 1);
"""
        gs = str.join(", ", [("g" + str(i)) for i in range(1, (max_order + 1))])
        txt = txt.format(gs=gs, fname=model.fname, neq=len(model.equations))

        for i in range(len(sols)):
            ndt = sols[i]
            eq = ndt.expr
            rhs = stat_printer.doprint_matlab(eq)
            txt += "    residual({0}) = {1};\n".format(i + 1, rhs)

        for current_order in range(1, (max_order + 1)):
            if current_order == 1:
                matrix_name = "Jacobian"
            elif current_order == 2:
                matrix_name = "Hessian"
            else:
                matrix_name = "{0}_th order".format(current_order)

            txt += """
    if nargout >= {orderr}
        g{order} = zeros({n_rows}, {n_cols});

%
% {matrix_name} matrix
%
\n""".format(
                order=current_order,
                orderr=current_order + 1,
                n_rows=len(model.equations),
                n_cols=len(var_order) ** current_order,
                matrix_name=matrix_name,
            )
            if current_order == 2:
                txt.format(matrix_name="Hessian")

            # What is the equivalent of NNZ for static files ?
            nnzd = self.NNZStaticDerivatives(current_order)

            if True:
                for n in range(len(sols)):
                    ndt = sols[n]
                    l = ndt.list_nth_order_children(current_order)
                    for nd in l:
                        # here we compute indices where we write the derivatives
                        indices = nd.compute_index_set_matlab(var_order)

                        rhs = stat_printer.doprint_matlab(nd.expr)

                        # rhs = comp.dyn_tabify_expression(nd.expr)
                        i0 = indices[0]
                        indices.remove(i0)
                        txt += "        g{order}({0},{1}) = {2};\n".format(n + 1, i0, rhs, order=current_order)
                        for ind in indices:
                            txt += "        g{order}({0},{1}) = g{order}({0},{2});\n".format(
                                n + 1, ind, i0, order=current_order
                            )
            else:
                print "to be implemented"
            txt += """
    end
"""

        f = file(model.fname + "_static.m", "w")
        f.write(txt)
        f.close()
        return txt
Esempio n. 2
0
    def compute_main_file(self, omit_nnz=True):
        model = self.model

        # should be computed somewhere else
        all_symbols = set([])
        for eq in model.equations:
            all_symbols.update(eq.atoms())
        format_dict = dict()
        format_dict["fname"] = model.fname
        format_dict["maximum_lag"] = max([-v.lag for v in all_symbols if isinstance(v, TSymbol)])
        format_dict["maximum_lead"] = max([v.lag for v in all_symbols if isinstance(v, TSymbol)])
        format_dict["maximum_endo_lag"] = max([-v.lag for v in all_symbols if isinstance(v, Variable)])
        format_dict["maximum_endo_lead"] = max([v.lag for v in all_symbols if isinstance(v, Variable)])
        format_dict["maximum_exo_lag"] = max([-v.lag for v in all_symbols if isinstance(v, Shock)])
        format_dict["maximum_exo_lead"] = max([v.lag for v in all_symbols if isinstance(v, Shock)])
        format_dict["endo_nbr"] = len(model.variables)
        format_dict["exo_nbr"] = len(model.shocks)
        format_dict["param_nbr"] = len(model.parameters)

        output = """%
% Status : main Dynare file
%
% Warning : this file is generated automatically by Dolo for Dynare
%           from model file (.mod)

clear all
tic;
global M_ oo_ options_
global ys0_ ex0_ ct_
options_ = [];
M_.fname = '{fname}';
%
% Some global variables initialization
%
global_initialization;
diary off;
warning_old_state = warning;
warning off;
delete {fname}.log;
warning warning_old_state;
logname_ = '{fname}.log';
diary {fname}.log;
options_.model_mode = 0;
erase_compiled_function('{fname}_static');
erase_compiled_function('{fname}_dynamic');

M_.exo_det_nbr = 0;
M_.exo_nbr = {exo_nbr};
M_.endo_nbr = {endo_nbr};
M_.param_nbr = {param_nbr};
M_.Sigma_e = zeros({exo_nbr}, {exo_nbr});
M_.exo_names_orig_ord = [1:{exo_nbr}];
""".format(
            **format_dict
        )

        string_of_names = lambda l: str.join(" , ", ["'%s'" % str(v) for v in l])
        string_of_tex_names = lambda l: str.join(" , ", ["'%s'" % v._latex_() for v in l])
        output += "M_.exo_names = strvcat( {0} );\n".format(string_of_names(model.shocks))
        output += "M_.exo_names_tex = strvcat( {0} );\n".format(string_of_tex_names(model.shocks))
        output += "M_.endo_names = strvcat( {0} );\n".format(string_of_names(model.variables))
        output += "M_.endo_names_tex = strvcat( {0} );\n".format(string_of_tex_names(model.variables))
        output += "M_.param_names = strvcat( {0} );\n".format(string_of_names(model.parameters))
        output += "M_.param_names_tex = strvcat( {0} );\n".format(string_of_tex_names(model.parameters))

        from dolo.misc.matlab import value_to_mat

        lli = value_to_mat(self.lead_lag_incidence_matrix())
        output += "M_.lead_lag_incidence = {0}';\n".format(lli)

        output += """M_.maximum_lag = {maximum_lag};
M_.maximum_lead = {maximum_lead};
M_.maximum_endo_lag = {maximum_endo_lag};
M_.maximum_endo_lead = {maximum_endo_lead};
M_.maximum_exo_lag = {maximum_exo_lag};
M_.maximum_exo_lead = {maximum_exo_lead};
oo_.steady_state = zeros({endo_nbr}, 1);
oo_.exo_steady_state = zeros({exo_nbr}, 1);
M_.params = repmat(NaN,{param_nbr}, 1);
""".format(
            **format_dict
        )

        # Now we add tags for equations
        tags_array_string = []
        for eq in model.equations:
            for k in eq.tags.keys():
                tags_array_string.append("{n}, '{key}', '{value}'".format(n=eq.n, key=k, value=eq.tags[k]))
        output += "M_.equations_tags = {{\n{0}\n}};\n".format(";\n".join(tags_array_string))

        if not omit_nnz:
            # we don't set the number of non zero derivatives yet
            order = max([ndt.depth() for ndt in self.dynamic_derivatives])
            output += "M_.NNZDerivatives = zeros({0}, 1); % parrot mode\n".format(order)
            output += "M_.NNZDerivatives(1) = {0}; % parrot mode\n".format(self.NNZDerivatives(1))
            output += "M_.NNZDerivatives(2) = {0}; % parrot mode\n".format(self.NNZDerivatives(2))
            output += "M_.NNZDerivatives(3) = {0}; % parrot mode\n".format(self.NNZDerivatives(3))

        idp = DicPrinter(self.static_substitution_list(y="oo_.steady_state", params="M_.params"))

        # BUG: how do we treat parameters absent from the model, but present in parameters_values ?

        from dolo.misc.triangular_solver import solve_triangular_system

        [junk, porder] = solve_triangular_system(model.parameters_values)
        porder = [p for p in porder if p in model.parameters]

        d = dict()
        d.update(model.parameters_values)
        d.update(model.init_values)

        [junk, vorder] = solve_triangular_system(model.init_values)
        vorder = [v for v in vorder if p in model.variables]

        for p in porder:
            i = model.parameters.index(p) + 1
            output += "M_.params({0}) = {1};\n".format(i, idp.doprint_matlab(model.parameters_values[p]))
            output += "{0} = M_.params({1});\n".format(p, i)

        output += """%
% INITVAL instructions
%
"""
        # idp = DicPrinter(self.static_substitution_list(y='oo_.steady_state',params='M_.params'))
        output += "options_.initval_file = 0; % parrot mode\n"
        for v in vorder:
            if v in self.model.variables:  # should be removed
                i = model.variables.index(v) + 1
                output += "oo_.steady_state({0}) = {1};\n".format(i, idp.doprint_matlab(model.init_values[v]))
        # we don't allow initialization of shocks to nonzero values
        output += "oo_.exo_steady_state = zeros({0},1);\n".format(len(model.shocks))
        output += """oo_.endo_simul=[oo_.steady_state*ones(1,M_.maximum_lag)];
if M_.exo_nbr > 0;
    oo_.exo_simul = [ones(M_.maximum_lag,1)*oo_.exo_steady_state'];
end;
if M_.exo_det_nbr > 0;
    oo_.exo_det_simul = [ones(M_.maximum_lag,1)*oo_.exo_det_steady_state'];
end;
"""
        # output += '''steady;
        output += """
%
% SHOCKS instructions
%
make_ex_;
M_.exo_det_length = 0; % parrot
"""

        for i in range(model.covariances.shape[0]):
            for j in range(model.covariances.shape[1]):
                expr = model.covariances[i, j]
                if expr != 0:
                    v = str(expr).replace("**", "^")
                    # if (v != 0) and not isinstance(v,sympy.core.numbers.Zero):
                    output += "M_.Sigma_e({0}, {1}) = {2};\n".format(i + 1, j + 1, v)

        # This results from the stoch_simul(
        #        output += '''options_.drop = 200;
        # options_.order = 3;
        # var_list_=[];
        # info = stoch_simul(var_list_);
        #'''
        #
        #        output += '''save('example2_results.mat', 'oo_', 'M_', 'options_');
        # diary off
        #
        # disp(['Total computing time : ' dynsec2hms(toc) ]);'''

        f = file(model.fname + ".m", "w")
        f.write(output)
        f.close()

        return output
Esempio n. 3
0
    def compute_dynamic_mfile(self, max_order=2):

        print "Computing dynamic .m file at order {0}.".format(max_order)

        DerivativesTree.symbol_type = TSymbol

        model = self.model

        var_order = model.dyn_var_order + model.shocks

        # TODO create a log system
        t = []
        t.append(time.time())
        sols = []
        i = 0
        for eq in model.equations:
            i += 1
            ndt = DerivativesTree(eq.gap)
            ndt.compute_nth_order_children(max_order)
            sols.append(ndt)

        t.append(time.time())

        self.dynamic_derivatives = sols

        dyn_subs_dict = self.dynamic_substitution_list()
        dyn_printer = DicPrinter(dyn_subs_dict)

        txt = """function [residual, {gs}] = {fname}_dynamic(y, x, params, it_)
%
% Status : Computes dynamic model for Dynare
%
% Warning : this file is generated automatically by Dynare
%           from model file (.mod)

%
% Model equations
%

    residual = zeros({neq}, 1);
"""
        gs = str.join(", ", [("g" + str(i)) for i in range(1, (max_order + 1))])
        txt = txt.format(gs=gs, fname=model.fname, neq=len(model.equations))

        t.append(time.time())
        for i in range(len(sols)):
            ndt = sols[i]
            eq = ndt.expr
            rhs = dyn_printer.doprint_matlab(eq)
            txt += "    residual({0}) = {1};\n".format(i + 1, rhs)
        t.append(time.time())

        for current_order in range(1, (max_order + 1)):
            if current_order == 1:
                matrix_name = "Jacobian"
            elif current_order == 2:
                matrix_name = "Hessian"
            else:
                matrix_name = "{0}_th order".format(current_order)

            txt += """
    if nargout >= {orderr}
%
% {matrix_name} matrix
%

""".format(
                orderr=current_order + 1, matrix_name=matrix_name
            )
            if current_order == 2:
                txt.format(matrix_name="Hessian")
            elif current_order == 1:
                txt.format(matrix_name="Jacobian")
            t.append(time.time())

            nnzd = self.NNZDerivatives(current_order)
            if True:  # we write full matrices ...
                txt += "        v{order} = zeros({nnzd}, 3);\n".format(order=current_order, nnzd=nnzd)
                i = 0
                for n in range(len(sols)):
                    ndt = sols[n]
                    l = ndt.list_nth_order_children(current_order)
                    for nd in l:
                        i += 1
                        # here we compute indices where we write the derivatives
                        indices = nd.compute_index_set_matlab(var_order)
                        rhs = dyn_printer.doprint_matlab(nd.expr)
                        i0 = indices[0]
                        indices.remove(i0)
                        i_ref = i
                        txt += "        v{order}({i},:) = [{i_eq}, {i_col}, {value}] ;\n".format(
                            order=current_order, i=i, i_eq=n + 1, i_col=i0, value=rhs
                        )
                        for ind in indices:
                            i += 1
                            txt += "        v{order}({i},:) = [{i_eq}, {i_col}, v{order}({i_ref},3)];\n".format(
                                order=current_order, i=i, i_eq=n + 1, i_col=ind, i_ref=i_ref
                            )
                txt += "        g{order} = sparse(v{order}(:,1),v{order}(:,2),v{order}(:,3),{n_rows},{n_cols});\n".format(
                    order=current_order, n_rows=len(model.equations), n_cols=len(var_order) ** current_order
                )
            else:  # ... or sparse matrices
                print "to be implemented"

            txt += """
    end
"""
        t.append(time.time())
        f = file(model.fname + "_dynamic.m", "w")
        f.write(txt)
        f.close()
        return txt
Esempio n. 4
0
    def compute_static_pfile(self, max_order):

        DerivativesTree.symbol_type = Variable

        model = self.model
        var_order = model.variables

        # TODO create a log system

        sols = []
        i = 0
        for eq in model.equations:
            i += 1
            l = [tv for tv in eq.atoms() if isinstance(tv, Variable)]
            expr = eq.gap
            for tv in l:
                if tv.lag != 0:
                    expr = expr.subs(tv, tv.P)
            ndt = DerivativesTree(expr)
            ndt.compute_nth_order_children(max_order)
            sols.append(ndt)
        self.static_derivatives = sols

        stat_subs_dict = self.static_substitution_list(ind_0=0, brackets=True)

        stat_printer = DicPrinter(stat_subs_dict)

        txt = """def static_gaps(y, x, params):
#
# Status : Computes static model for Python
#
# Warning : this file is generated automatically by Dynare
#           from model file (.mod)
#
#
# Model equations
#
    import numpy as np
    from numpy import exp,log, sin, cos, tan, sinh, cosh, tanh
    from numpy import arcsin as asin
    from numpy import arccos as acos
    from numpy import arctan as atan

    it_ = 1 # should remove this !

    g = []

    residual = np.zeros({neq})
"""
        gs = str.join(", ", [("g" + str(i)) for i in range(1, (max_order + 1))])
        txt = txt.format(gs=gs, fname=model.fname, neq=len(model.equations))

        for i in range(len(sols)):
            ndt = sols[i]
            eq = ndt.expr
            rhs = stat_printer.doprint_matlab(eq)
            txt += "    residual[{0}] = {1}\n".format(i, rhs)

        txt += "    g.append(residual)\n"

        for current_order in range(1, (max_order + 1)):
            if current_order == 1:
                matrix_name = "Jacobian"
            elif current_order == 2:
                matrix_name = "Hessian"
            else:
                matrix_name = "{0}_th order".format(current_order)

            txt += """
    g{order} = np.zeros(({n_rows}, {n_cols}))
#    {matrix_name} matrix\n""".format(
                order=current_order,
                orderr=current_order + 1,
                n_rows=len(model.equations),
                n_cols=len(var_order) ** current_order,
                matrix_name=matrix_name,
            )
            for n in range(len(sols)):
                ndt = sols[n]
                l = ndt.list_nth_order_children(current_order)
                for nd in l:
                    # here we compute indices where we write the derivatives
                    indices = nd.compute_index_set_matlab(var_order)

                    rhs = stat_printer.doprint_matlab(nd.expr)

                    # rhs = comp.dyn_tabify_expression(nd.expr)
                    i0 = indices[0]
                    indices.remove(i0)
                    txt += "    g{order}[{0},{1}] = {2}\n".format(n, i0 - 1, rhs, order=current_order)
                    for ind in indices:
                        txt += "    g{order}[{0},{1}] = g{order}[{0},{2}]\n".format(
                            n, ind - 1, i0 - 1, order=current_order
                        )
            txt += "    g.append(g{order})\n".format(order=current_order)

        txt += "    return g\n"
        txt = txt.replace("^", "**")
        exec(txt)
        return static_gaps
Esempio n. 5
0
    def compute_dynamic_pfile(self, max_order=1, compact_order=True, with_parameters=False):

        if with_parameters:
            DerivativesTree.symbol_type = sympy.Symbol
        else:
            DerivativesTree.symbol_type = TSymbol

        model = self.model

        if compact_order:
            var_order = model.dyn_var_order + model.shocks
        else:
            var_order = [v(1) for v in model.variables]
            var_order += model.variables
            var_order += [v(-1) for v in model.variables]
            var_order += model.shocks
        if with_parameters:
            var_order += model.parameters

        # TODO create a log system

        sols = []
        i = 0
        for eq in model.equations:
            i += 1
            ndt = DerivativesTree(eq.gap)
            ndt.compute_nth_order_children(max_order)
            sols.append(ndt)

        self.dynamic_derivatives = sols

        dyn_subs_dict = self.dynamic_substitution_list(brackets=True, compact=compact_order)
        dyn_printer = DicPrinter(dyn_subs_dict)

        txt = """def dynamic_gaps(y, x, params):
#
# Status : Computes dynamic model for Dynare
#
# Warning : this file is generated automatically by Dynare
#           from model file (.mod)

#
# Model equations
#
    it_ = 0

    import numpy as np
    from numpy import exp, log
    from numpy import arctan as atan

    g = []

    residual = np.zeros({neq});
"""
        gs = str.join(", ", [("g" + str(i)) for i in range(1, (max_order + 1))])
        txt = txt.format(gs=gs, fname=model.fname, neq=len(model.equations))

        for i in range(len(sols)):
            ndt = sols[i]
            eq = ndt.expr
            rhs = dyn_printer.doprint_matlab(eq)
            txt += "    residual[{0}] = {1};\n".format(i, rhs)

        txt += "    g.append(residual)\n"

        for current_order in range(1, (max_order + 1)):
            if current_order == 1:
                matrix_name = "Jacobian"
            elif current_order == 2:
                matrix_name = "Hessian"
            else:
                matrix_name = "{0}_th order".format(current_order)

            txt += """
#
# {matrix_name} matrix
#

""".format(
                orderr=current_order + 1, matrix_name=matrix_name
            )
            if current_order == 2:
                txt.format(matrix_name="Hessian")
            elif current_order == 1:
                txt.format(matrix_name="Jacobian")

            # nnzd = self.NNZDerivatives(current_order)

            n_cols = (len(var_order),) * current_order
            n_cols = ",".join([str(s) for s in n_cols])
            txt += "    g{order} = np.zeros( ({n_eq}, {n_cols}) );\n".format(
                order=current_order, n_eq=len(model.equations), n_cols=n_cols
            )
            for n in range(len(sols)):
                ndt = sols[n]
                l = ndt.list_nth_order_children(current_order)
                for nd in l:
                    # here we compute indices where we write the derivatives
                    indices = nd.compute_index_set(var_order)
                    rhs = dyn_printer.doprint_numpy(nd.expr)
                    i0 = indices[0]
                    i_col_s = ",".join([str(nn) for nn in i0])
                    indices.remove(i0)

                    i_col_s_ref = i_col_s
                    txt += "    g{order}[{i_eq},{i_col}] = {value}\n".format(
                        order=current_order, i_eq=n, i_col=i_col_s, value=rhs
                    )
                    for ind in indices:
                        i += 1
                        i_col_s = ",".join([str(nn) for nn in ind])
                        txt += "    g{order}[{i_eq},{i_col}] = g{order}[{i_eq},{i_col_ref}] \n".format(
                            order=current_order, i_eq=n, i_col=i_col_s, i_col_ref=i_col_s_ref
                        )

            txt += "    g.append(g{order})\n".format(order=current_order)
        txt += "    return g\n"
        txt = txt.replace("^", "**")

        fun = code_to_function(txt, "dynamic_gaps")
        return fun
Esempio n. 6
0
    def compute_static_mfile(self, max_order=1):

        print "Computing static .m file at order {0}.".format(max_order)

        DerivativesTree.symbol_type = Variable

        model = self.model
        var_order = model.variables

        # TODO create a log system

        sols = []
        i = 0
        for eq in model.equations:
            i += 1
            l = [tv for tv in eq.atoms() if isinstance(tv, Variable)]
            expr = eq.gap
            for tv in l:
                if tv.lag != 0:
                    expr = expr.subs(tv, tv.P)
            ndt = DerivativesTree(expr)
            ndt.compute_nth_order_children(max_order)
            sols.append(ndt)
        self.static_derivatives = sols

        stat_subs_dict = self.static_substitution_list()

        stat_printer = DicPrinter(stat_subs_dict)

        txt = """function [residual, {gs}] = {fname}_static(y, x, params, it_)
%
% Status : Computes static model for Dynare
%
% Warning : this file is generated automatically by Dynare
%           from model file (.mod)

%
% Model equations
%

    residual = zeros({neq}, 1);
"""
        gs = str.join(', ',
                      [('g' + str(i)) for i in range(1, (max_order + 1))])
        txt = txt.format(gs=gs, fname=model.fname, neq=len(model.equations))

        for i in range(len(sols)):
            ndt = sols[i]
            eq = ndt.expr
            rhs = stat_printer.doprint_matlab(eq)
            txt += '    residual({0}) = {1};\n'.format(i + 1, rhs)

        for current_order in range(1, (max_order + 1)):
            if current_order == 1:
                matrix_name = "Jacobian"
            elif current_order == 2:
                matrix_name = "Hessian"
            else:
                matrix_name = "{0}_th order".format(current_order)

            txt += """
    if nargout >= {orderr}
        g{order} = zeros({n_rows}, {n_cols});

%
% {matrix_name} matrix
%
\n""".format(order=current_order,
             orderr=current_order + 1,
             n_rows=len(model.equations),
             n_cols=len(var_order)**current_order,
             matrix_name=matrix_name)
            if current_order == 2:
                txt.format(matrix_name="Hessian")

            # What is the equivalent of NNZ for static files ?
            nnzd = self.NNZStaticDerivatives(current_order)

            if True:
                for n in range(len(sols)):
                    ndt = sols[n]
                    l = ndt.list_nth_order_children(current_order)
                    for nd in l:
                        # here we compute indices where we write the derivatives
                        indices = nd.compute_index_set_matlab(var_order)

                        rhs = stat_printer.doprint_matlab(nd.expr)

                        #rhs = comp.dyn_tabify_expression(nd.expr)
                        i0 = indices[0]
                        indices.remove(i0)
                        txt += '        g{order}({0},{1}) = {2};\n'.format(
                            n + 1, i0, rhs, order=current_order)
                        for ind in indices:
                            txt += '        g{order}({0},{1}) = g{order}({0},{2});\n'.format(
                                n + 1, ind, i0, order=current_order)
            else:
                print 'to be implemented'
            txt += """
    end
"""

        f = file(model.fname + '_static.m', 'w')
        f.write(txt)
        f.close()
        return txt
Esempio n. 7
0
    def compute_dynamic_mfile(self, max_order=2):

        print "Computing dynamic .m file at order {0}.".format(max_order)

        DerivativesTree.symbol_type = TSymbol

        model = self.model

        var_order = model.dyn_var_order + model.shocks

        # TODO create a log system
        t = []
        t.append(time.time())
        sols = []
        i = 0
        for eq in model.equations:
            i += 1
            ndt = DerivativesTree(eq.gap)
            ndt.compute_nth_order_children(max_order)
            sols.append(ndt)

        t.append(time.time())

        self.dynamic_derivatives = sols

        dyn_subs_dict = self.dynamic_substitution_list()
        dyn_printer = DicPrinter(dyn_subs_dict)

        txt = """function [residual, {gs}] = {fname}_dynamic(y, x, params, it_)
%
% Status : Computes dynamic model for Dynare
%
% Warning : this file is generated automatically by Dynare
%           from model file (.mod)

%
% Model equations
%

    residual = zeros({neq}, 1);
"""
        gs = str.join(', ',
                      [('g' + str(i)) for i in range(1, (max_order + 1))])
        txt = txt.format(gs=gs, fname=model.fname, neq=len(model.equations))

        t.append(time.time())
        for i in range(len(sols)):
            ndt = sols[i]
            eq = ndt.expr
            rhs = dyn_printer.doprint_matlab(eq)
            txt += '    residual({0}) = {1};\n'.format(i + 1, rhs)
        t.append(time.time())

        for current_order in range(1, (max_order + 1)):
            if current_order == 1:
                matrix_name = "Jacobian"
            elif current_order == 2:
                matrix_name = "Hessian"
            else:
                matrix_name = "{0}_th order".format(current_order)

            txt += """
    if nargout >= {orderr}
%
% {matrix_name} matrix
%

""".format(orderr=current_order + 1, matrix_name=matrix_name)
            if current_order == 2:
                txt.format(matrix_name="Hessian")
            elif current_order == 1:
                txt.format(matrix_name="Jacobian")
            t.append(time.time())

            nnzd = self.NNZDerivatives(current_order)
            if True:  # we write full matrices ...
                txt += "        v{order} = zeros({nnzd}, 3);\n".format(
                    order=current_order, nnzd=nnzd)
                i = 0
                for n in range(len(sols)):
                    ndt = sols[n]
                    l = ndt.list_nth_order_children(current_order)
                    for nd in l:
                        i += 1
                        # here we compute indices where we write the derivatives
                        indices = nd.compute_index_set_matlab(var_order)
                        rhs = dyn_printer.doprint_matlab(nd.expr)
                        i0 = indices[0]
                        indices.remove(i0)
                        i_ref = i
                        txt += '        v{order}({i},:) = [{i_eq}, {i_col}, {value}] ;\n'.format(
                            order=current_order,
                            i=i,
                            i_eq=n + 1,
                            i_col=i0,
                            value=rhs)
                        for ind in indices:
                            i += 1
                            txt += '        v{order}({i},:) = [{i_eq}, {i_col}, v{order}({i_ref},3)];\n'.format(
                                order=current_order,
                                i=i,
                                i_eq=n + 1,
                                i_col=ind,
                                i_ref=i_ref)
                txt += '        g{order} = sparse(v{order}(:,1),v{order}(:,2),v{order}(:,3),{n_rows},{n_cols});\n'.format(
                    order=current_order,
                    n_rows=len(model.equations),
                    n_cols=len(var_order)**current_order)
            else:  # ... or sparse matrices
                print 'to be implemented'

            txt += """
    end
"""
        t.append(time.time())
        f = file(model.fname + '_dynamic.m', 'w')
        f.write(txt)
        f.close()
        return txt
Esempio n. 8
0
    def compute_main_file(self, omit_nnz=True):
        model = self.model

        # should be computed somewhere else
        all_symbols = set([])
        for eq in model.equations:
            all_symbols.update(eq.atoms())
        format_dict = dict()
        format_dict['fname'] = model.fname
        format_dict['maximum_lag'] = max(
            [-v.lag for v in all_symbols if isinstance(v, TSymbol)])
        format_dict['maximum_lead'] = max(
            [v.lag for v in all_symbols if isinstance(v, TSymbol)])
        format_dict['maximum_endo_lag'] = max(
            [-v.lag for v in all_symbols if isinstance(v, Variable)])
        format_dict['maximum_endo_lead'] = max(
            [v.lag for v in all_symbols if isinstance(v, Variable)])
        format_dict['maximum_exo_lag'] = max(
            [-v.lag for v in all_symbols if isinstance(v, Shock)])
        format_dict['maximum_exo_lead'] = max(
            [v.lag for v in all_symbols if isinstance(v, Shock)])
        format_dict['endo_nbr'] = len(model.variables)
        format_dict['exo_nbr'] = len(model.shocks)
        format_dict['param_nbr'] = len(model.parameters)

        output = """%
% Status : main Dynare file
%
% Warning : this file is generated automatically by Dolo for Dynare
%           from model file (.mod)

clear all
tic;
global M_ oo_ options_
global ys0_ ex0_ ct_
options_ = [];
M_.fname = '{fname}';
%
% Some global variables initialization
%
global_initialization;
diary off;
warning_old_state = warning;
warning off;
delete {fname}.log;
warning warning_old_state;
logname_ = '{fname}.log';
diary {fname}.log;
options_.model_mode = 0;
erase_compiled_function('{fname}_static');
erase_compiled_function('{fname}_dynamic');

M_.exo_det_nbr = 0;
M_.exo_nbr = {exo_nbr};
M_.endo_nbr = {endo_nbr};
M_.param_nbr = {param_nbr};
M_.Sigma_e = zeros({exo_nbr}, {exo_nbr});
M_.exo_names_orig_ord = [1:{exo_nbr}];
""".format(**format_dict)

        string_of_names = lambda l: str.join(' , ',
                                             ["'%s'" % str(v) for v in l])
        string_of_tex_names = lambda l: str.join(
            ' , ', ["'%s'" % v._latex_() for v in l])
        output += "M_.exo_names = strvcat( {0} );\n".format(
            string_of_names(model.shocks))
        output += "M_.exo_names_tex = strvcat( {0} );\n".format(
            string_of_tex_names(model.shocks))
        output += "M_.endo_names = strvcat( {0} );\n".format(
            string_of_names(model.variables))
        output += "M_.endo_names_tex = strvcat( {0} );\n".format(
            string_of_tex_names(model.variables))
        output += "M_.param_names = strvcat( {0} );\n".format(
            string_of_names(model.parameters))
        output += "M_.param_names_tex = strvcat( {0} );\n".format(
            string_of_tex_names(model.parameters))

        from dolo.misc.matlab import value_to_mat
        lli = value_to_mat(self.lead_lag_incidence_matrix())
        output += "M_.lead_lag_incidence = {0}';\n".format(lli)

        output += """M_.maximum_lag = {maximum_lag};
M_.maximum_lead = {maximum_lead};
M_.maximum_endo_lag = {maximum_endo_lag};
M_.maximum_endo_lead = {maximum_endo_lead};
M_.maximum_exo_lag = {maximum_exo_lag};
M_.maximum_exo_lead = {maximum_exo_lead};
oo_.steady_state = zeros({endo_nbr}, 1);
oo_.exo_steady_state = zeros({exo_nbr}, 1);
M_.params = repmat(NaN,{param_nbr}, 1);
""".format(**format_dict)

        # Now we add tags for equations
        tags_array_string = []
        for eq in model.equations:
            for k in eq.tags.keys():
                tags_array_string.append("{n}, '{key}', '{value}'".format(
                    n=eq.n, key=k, value=eq.tags[k]))
        output += "M_.equations_tags = {{\n{0}\n}};\n".format(
            ";\n".join(tags_array_string))

        if not omit_nnz:
            # we don't set the number of non zero derivatives yet
            order = max([ndt.depth() for ndt in self.dynamic_derivatives])
            output += "M_.NNZDerivatives = zeros({0}, 1); % parrot mode\n".format(
                order)
            output += "M_.NNZDerivatives(1) = {0}; % parrot mode\n".format(
                self.NNZDerivatives(1))
            output += "M_.NNZDerivatives(2) = {0}; % parrot mode\n".format(
                self.NNZDerivatives(2))
            output += "M_.NNZDerivatives(3) = {0}; % parrot mode\n".format(
                self.NNZDerivatives(3))

        idp = DicPrinter(
            self.static_substitution_list(y='oo_.steady_state',
                                          params='M_.params'))

        # BUG: how do we treat parameters absent from the model, but present in parameters_values ?

        from dolo.misc.triangular_solver import solve_triangular_system

        [junk, porder] = solve_triangular_system(model.parameters_values)
        porder = [p for p in porder if p in model.parameters]

        d = dict()
        d.update(model.parameters_values)
        d.update(model.init_values)

        [junk, vorder] = solve_triangular_system(model.init_values)
        vorder = [v for v in vorder if p in model.variables]

        for p in porder:
            i = model.parameters.index(p) + 1
            output += "M_.params({0}) = {1};\n".format(
                i, idp.doprint_matlab(model.parameters_values[p]))
            output += "{0} = M_.params({1});\n".format(p, i)

        output += '''%
% INITVAL instructions
%
'''
        #idp = DicPrinter(self.static_substitution_list(y='oo_.steady_state',params='M_.params'))
        output += "options_.initval_file = 0; % parrot mode\n"
        for v in vorder:
            if v in self.model.variables:  # should be removed
                i = model.variables.index(v) + 1
                output += "oo_.steady_state({0}) = {1};\n".format(
                    i, idp.doprint_matlab(model.init_values[v]))
        # we don't allow initialization of shocks to nonzero values
        output += "oo_.exo_steady_state = zeros({0},1);\n".format(
            len(model.shocks))
        output += '''oo_.endo_simul=[oo_.steady_state*ones(1,M_.maximum_lag)];
if M_.exo_nbr > 0;
    oo_.exo_simul = [ones(M_.maximum_lag,1)*oo_.exo_steady_state'];
end;
if M_.exo_det_nbr > 0;
    oo_.exo_det_simul = [ones(M_.maximum_lag,1)*oo_.exo_det_steady_state'];
end;
'''
        #output += '''steady;
        output += """
%
% SHOCKS instructions
%
make_ex_;
M_.exo_det_length = 0; % parrot
"""

        for i in range(model.covariances.shape[0]):
            for j in range(model.covariances.shape[1]):
                expr = model.covariances[i, j]
                if expr != 0:
                    v = str(expr).replace("**", "^")
                    #if (v != 0) and not isinstance(v,sympy.core.numbers.Zero):
                    output += "M_.Sigma_e({0}, {1}) = {2};\n".format(
                        i + 1, j + 1, v)

        # This results from the stoch_simul(


#        output += '''options_.drop = 200;
#options_.order = 3;
#var_list_=[];
#info = stoch_simul(var_list_);
#'''
#
#        output += '''save('example2_results.mat', 'oo_', 'M_', 'options_');
#diary off
#
#disp(['Total computing time : ' dynsec2hms(toc) ]);'''

        f = file(model.fname + '.m', 'w')
        f.write(output)
        f.close()

        return output