write dynamic model output

Conflicts:
	preprocessor/DynamicModel.cc
time-shift
Houtan Bastani 2015-07-29 14:59:09 +02:00
parent 114b446bff
commit 8f92b975e4
5 changed files with 127 additions and 45 deletions

View File

@ -72,6 +72,12 @@ immutable DetShocks
value::Float64
end
immutable EquationTag
eq_nbr::Int
name::UTF8String
value::UTF8String
end
type Model
fname::ASCIIString
dname::ASCIIString
@ -104,14 +110,14 @@ type Model
lead_lag_incidence::Matrix{Int}
nnzderivatives::Vector{Int}
static_and_dynamic_models_differ::Bool
equations_tags::Array{ASCIIString,1}
equations_tags::Array{UTF8String,1}
exo_names_orig_ord::Array{Int, 1}
sigma_e::Matrix{Float64}
correlation_matrix::Matrix{Float64}
h::Matrix{Float64}
correlation_matrix_me::Matrix{Float64}
sigma_e_is_diagonal::Bool
params::Vector{Float64}
params::Matrix{Float64}
static::Function
static_params_derivs::Function
dynamic::Function
@ -158,7 +164,7 @@ function dynare_model()
Array(Float64, 0, 0), # h (Cov matrix of the measurement errors)
Array(Float64, 0, 0), # correlation_matrix_me (Cov matrix of the measurement errors)
true, # sigma_e_is_diagonal
Array(Float64, 0), # params
Array(Float64, 0, 0), # params
function()end, # static
function()end, # static_params_derivs
function()end, # dynamic

37
julia/DynareOutput.jl Normal file
View File

@ -0,0 +1,37 @@
module DynareOutput
##
# Copyright (C) 2015 Dynare Team
#
# This file is part of Dynare.
#
# Dynare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Dynare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Dynare. If not, see <http://www.gnu.org/licenses/>.
##
export dynare_output
type Output
dynare_version::ASCIIString
steady_state::Matrix{Float64}
exo_steady_state::Matrix{Float64}
end
function dynare_output()
return Output("", # dynare_version
Array(Float64, 0, 0), # steady_state
Array(Float64, 0, 0) # exo_steady_state
)
end
end

View File

@ -2377,7 +2377,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
}
void
DynamicModel::writeOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const
DynamicModel::writeOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present, bool julia) const
{
/* Writing initialisation for M_.lead_lag_incidence matrix
M_.lead_lag_incidence is a matrix with as many columns as there are
@ -2388,7 +2388,20 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
model at a given period.
*/
output << "M_.lead_lag_incidence = [";
string modstruct;
string outstruct;
if (julia)
{
modstruct = "model.";
outstruct = "output.";
}
else
{
modstruct = "M_.";
outstruct = "oo_.";
}
output << modstruct << "lead_lag_incidence = [";
// Loop on endogenous variables
int nstatic = 0,
nfwrd = 0,
@ -2440,26 +2453,41 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
output << ";";
}
output << "]';" << endl;
output << "M_.nstatic = " << nstatic << ";" << endl
<< "M_.nfwrd = " << nfwrd << ";" << endl
<< "M_.npred = " << npred << ";" << endl
<< "M_.nboth = " << nboth << ";" << endl
<< "M_.nsfwrd = " << nfwrd+nboth << ";" << endl
<< "M_.nspred = " << npred+nboth << ";" << endl
<< "M_.ndynamic = " << npred+nboth+nfwrd << ";" << endl;
output << modstruct << "nstatic = " << nstatic << ";" << endl
<< modstruct << "nfwrd = " << nfwrd << ";" << endl
<< modstruct << "npred = " << npred << ";" << endl
<< modstruct << "nboth = " << nboth << ";" << endl
<< modstruct << "nsfwrd = " << nfwrd+nboth << ";" << endl
<< modstruct << "nspred = " << npred+nboth << ";" << endl
<< modstruct << "ndynamic = " << npred+nboth+nfwrd << ";" << endl;
// Write equation tags
output << "M_.equations_tags = {" << endl;
for (size_t i = 0; i < equation_tags.size(); i++)
output << " " << equation_tags[i].first + 1 << " , '"
<< equation_tags[i].second.first << "' , '"
<< equation_tags[i].second.second << "' ;" << endl;
output << "};" << endl;
if (julia)
{
output << modstruct << "equations_tags = [" << endl;
for (size_t i = 0; i < equation_tags.size(); i++)
output << " EquationTag(" << equation_tags[i].first + 1 << " , \""
<< equation_tags[i].second.first << "\" , \""
<< equation_tags[i].second.second << "\")" << endl;
output << "]" << endl;
}
else
{
output << modstruct << "equations_tags = {" << endl;
for (size_t i = 0; i < equation_tags.size(); i++)
output << " " << equation_tags[i].first + 1 << " , '"
<< equation_tags[i].second.first << "' , '"
<< equation_tags[i].second.second << "' ;" << endl;
output << "};" << endl;
}
/* Say if static and dynamic models differ (because of [static] and [dynamic]
equation tags) */
output << "M_.static_and_dynamic_models_differ = "
<< (static_only_equations.size() > 0 ? "1" : "0")
output << modstruct << "static_and_dynamic_models_differ = "
<< (static_only_equations.size() > 0 ?
(julia ? "true" : "1") :
(julia ? "false" : "0"))
<< ";" << endl;
//In case of sparse model, writes the block_decomposition structure of the model
@ -2703,14 +2731,14 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
output << "block_structure.block(" << block+1 << ").n_backward = " << n_backward << ";\n";
output << "block_structure.block(" << block+1 << ").n_mixed = " << n_mixed << ";\n";
}
output << "M_.block_structure.block = block_structure.block;\n";
output << modstruct << "block_structure.block = block_structure.block;\n";
string cst_s;
int nb_endo = symbol_table.endo_nbr();
output << "M_.block_structure.variable_reordered = [";
output << modstruct << "block_structure.variable_reordered = [";
for (int i = 0; i < nb_endo; i++)
output << " " << variable_reordered[i]+1;
output << "];\n";
output << "M_.block_structure.equation_reordered = [";
output << modstruct << "block_structure.equation_reordered = [";
for (int i = 0; i < nb_endo; i++)
output << " " << equation_reordered[i]+1;
output << "];\n";
@ -2744,8 +2772,8 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
if (prev_lag != -1000000)
output << "];\n";
prev_lag = it->first.first;
output << "M_.block_structure.incidence(" << max_endo_lag+it->first.first+1 << ").lead_lag = " << prev_lag << ";\n";
output << "M_.block_structure.incidence(" << max_endo_lag+it->first.first+1 << ").sparse_IM = [";
output << modstruct << "block_structure.incidence(" << max_endo_lag+it->first.first+1 << ").lead_lag = " << prev_lag << ";\n";
output << modstruct << "block_structure.incidence(" << max_endo_lag+it->first.first+1 << ").sparse_IM = [";
}
output << it->first.second.first+1 << " " << it->first.second.second+1 << ";\n";
}
@ -2763,7 +2791,7 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
n_obs--;
int n = n_obs + n_state;
output << "M_.nobs_non_statevar = " << n_obs << ";" << endl;
output << modstruct << "nobs_non_statevar = " << n_obs << ";" << endl;
int nb_diag = 0;
//map<pair<int,int>, int>::const_iterator row_state_var_incidence_it = row_state_var_incidence.begin();
@ -2850,11 +2878,11 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
i_nz_state_var[lp + i] = lp + nze;
lp += nze;
}
output << "M_.nz_state_var = [";
output << modstruct << "nz_state_var = [";
for (unsigned int i = 0; i < lp; i++)
output << i_nz_state_var[i] << " ";
output << "];" << endl;
output << "M_.n_diag = " << nb_diag << ";" << endl;
output << modstruct << "n_diag = " << nb_diag << ";" << endl;
KF_index_file.write(reinterpret_cast<char *>(&nb_diag), sizeof(nb_diag));
@ -2898,7 +2926,7 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
KF_index_file.write(reinterpret_cast<char *>(&(*it)), sizeof(index_KF));
KF_index_file.close();
}
output << "M_.state_var = [";
output << modstruct << "state_var = [";
for (vector<int>::const_iterator it=state_var.begin(); it != state_var.end(); it++)
output << *it << " ";
@ -2906,30 +2934,32 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
}
// Writing initialization for some other variables
output << "M_.exo_names_orig_ord = [1:" << symbol_table.exo_nbr() << "];" << endl
<< "M_.maximum_lag = " << max_lag << ";" << endl
<< "M_.maximum_lead = " << max_lead << ";" << endl;
output << modstruct << "exo_names_orig_ord = [1:" << symbol_table.exo_nbr() << "];" << endl
<< modstruct << "maximum_lag = " << max_lag << ";" << endl
<< modstruct << "maximum_lead = " << max_lead << ";" << endl;
output << "M_.maximum_endo_lag = " << max_endo_lag << ";" << endl
<< "M_.maximum_endo_lead = " << max_endo_lead << ";" << endl
<< "oo_.steady_state = zeros(" << symbol_table.endo_nbr() << ", 1);" << endl;
output << modstruct << "maximum_endo_lag = " << max_endo_lag << ";" << endl
<< modstruct << "maximum_endo_lead = " << max_endo_lead << ";" << endl
<< outstruct << "steady_state = zeros(" << symbol_table.endo_nbr() << ", 1);" << endl;
output << "M_.maximum_exo_lag = " << max_exo_lag << ";" << endl
<< "M_.maximum_exo_lead = " << max_exo_lead << ";" << endl
<< "oo_.exo_steady_state = zeros(" << symbol_table.exo_nbr() << ", 1);" << endl;
output << modstruct << "maximum_exo_lag = " << max_exo_lag << ";" << endl
<< modstruct << "maximum_exo_lead = " << max_exo_lead << ";" << endl
<< outstruct << "exo_steady_state = zeros(" << symbol_table.exo_nbr() << ", 1);" << endl;
if (symbol_table.exo_det_nbr())
{
output << "M_.maximum_exo_det_lag = " << max_exo_det_lag << ";" << endl
<< "M_.maximum_exo_det_lead = " << max_exo_det_lead << ";" << endl
<< "oo_.exo_det_steady_state = zeros(" << symbol_table.exo_det_nbr() << ", 1);" << endl;
output << modstruct << "maximum_exo_det_lag = " << max_exo_det_lag << ";" << endl
<< modstruct << "maximum_exo_det_lead = " << max_exo_det_lead << ";" << endl
<< outstruct << "exo_det_steady_state = zeros(" << symbol_table.exo_det_nbr() << ", 1);" << endl;
}
output << "M_.params = NaN(" << symbol_table.param_nbr() << ", 1);" << endl;
output << modstruct << "params = " << (julia ? "fill(NaN, " : "NaN(")
<< symbol_table.param_nbr() << ", 1);" << endl;
// Write number of non-zero derivatives
// Use -1 if the derivatives have not been computed
output << "M_.NNZDerivatives = [" << NNZDerivatives[0] << "; ";
output << modstruct << (julia ? "nnzderivatives" : "NNZDerivatives")
<< " = [" << NNZDerivatives[0] << "; ";
if (order > 1)
output << NNZDerivatives[1] << "; ";
else

View File

@ -215,7 +215,7 @@ public:
void computingPass(bool jacobianExo, bool hessian, bool thirdDerivatives, bool paramsDerivatives,
const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool use_dll, bool bytecode);
//! Writes model initialization and lead/lag incidence matrix to output
void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const;
void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present, bool julia) const;
//! Adds informations for simulation in a binary file
void Write_Inf_To_Bin_File_Block(const string &dynamic_basename, const string &bin_basename,

View File

@ -735,7 +735,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo
if (dynamic_model.equation_number() > 0)
{
dynamic_model.writeOutput(mOutputFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present);
dynamic_model.writeOutput(mOutputFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present, false);
if (!no_static)
static_model.writeOutput(mOutputFile, block);
}
@ -1083,11 +1083,17 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output)
<< "##" << endl
<< "using DynareModel" << endl
<< "using DynareOptions" << endl
<< "using DynareOutput" << endl
<< "using Utils" << endl
<< "using " << basename << "Static" << endl
<< "using " << basename << "Dynamic" << endl << endl
<< "export model" << endl;
// Write Output
jlOutputFile << endl
<< "output = dynare_output()" << endl
<< "output.dynare_version = \"" << PACKAGE_VERSION << "\"" << endl;
// Write Options
jlOutputFile << endl
<< "options = dynare_options()" << endl
@ -1124,6 +1130,9 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output)
if (dynamic_model.equation_number() > 0)
{
dynamic_model.writeOutput(jlOutputFile, basename, false, false, false,
mod_file_struct.order_option,
mod_file_struct.estimation_present, true);
if (!no_static)
{
static_model.writeStaticFile(basename, false, false, false, true);