Commit 2c04f5c8 authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Merge branch 'master' of kirikou.dynare.org:/srv/d_kirikou/git/dynare

parents 7a875fe5 a8354326
......@@ -1954,6 +1954,12 @@ of the simulation is period @code{0}, then period @code{-1}, and so on.
If your lagged variables are linked by identities, be careful to
satisfy these identities when you set historical initial values.
Variables not initialized in the @code{histval} block are assumed to
have a value of zero at period 0 and before. Note that this behavior
differs from the case where there is no @code{histval} block, where all
variables are initialized at their steady state value at period 0 and
before.
@examplehead
@example
......@@ -3803,7 +3809,7 @@ algorithm. For the time being, @code{mh_replic} should be larger than
@item sub_draws = @var{INTEGER}
@anchor{sub_draws} number of draws from the Metropolis iterations that
are used to compute posterior distirbution of various objects (smoothed
are used to compute posterior distribution of various objects (smoothed
variable, smoothed shocks, forecast, moments, IRF). @code{sub_draws} should be smaller than
the total number of Metropolis draws available. Default:
@code{min(1200,0.25*Total number of draws)}
......
......@@ -28,7 +28,7 @@ function global_initialization()
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global oo_ M_ options_ estim_params_ bayestopt_
global oo_ M_ options_ estim_params_ bayestopt_ estimation_info
estim_params_ = [];
bayestopt_ = [];
......@@ -182,6 +182,35 @@ options_.ramsey_policy = 0;
options_.timeless = 0;
% estimation
estimation_info.prior = struct('name', {}, 'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.structural_innovation.prior = struct('name', {}, 'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.structural_innovation_corr.prior = struct('name', {}, 'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.measurement_error.prior = struct('name', {}, 'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.measurement_error_corr.prior = struct('name', {}, 'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.measurement_error.prior_index = {};
estimation_info.structural_innovation.prior_index = {};
estimation_info.measurement_error_corr.prior_index = {};
estimation_info.structural_innovation_corr.prior_index = {};
estimation_info.measurement_error.options_index = {};
estimation_info.structural_innovation.options_index = {};
estimation_info.measurement_error_corr.options_index = {};
estimation_info.structural_innovation_corr.options_index = {};
options_.initial_period = dynDate(1);
options_.dataset.firstobs = options_.initial_period;
options_.dataset.lastobs = NaN;
options_.dataset.nobs = NaN;
options_.dataset.xls_sheet = NaN;
options_.dataset.xls_range = NaN;
options_.Harvey_scale_factor = 10;
options_.MaxNumberOfBytes = 1e6;
options_.MaximumNumberOfMegaBytes = 111;
......
......@@ -52,7 +52,7 @@ function ms_write_markov_file(fname, options)
%//=====================================================//
fprintf(fh,'//== Number of states for state_variable[%d] ==//\n', ...
i_chain);
n_states = length(options.ms.ms_chain(i_chain).state);
n_states = length(options.ms.ms_chain(i_chain).regime);
fprintf(fh,'%d\n\n',n_states);
%//== 03/15/06: DW TVBVAR code reads the data below and overwrite the prior data read somewhere else if any.
......@@ -64,7 +64,7 @@ function ms_write_markov_file(fname, options)
i_chain);
Alpha = ones(n_states,n_states);
for i_state = 1:n_states
p = 1-1/options.ms.ms_chain(i_chain).state(i_state).duration;
p = 1-1/options.ms.ms_chain(i_chain).regime(i_state).duration;
Alpha(i_state,i_state) = p*(n_states-1)/(1-p);
fprintf(fh,'%22.16f',Alpha(i_state,:));
fprintf(fh,'\n');
......
......@@ -31,7 +31,7 @@ function plot_ms_probabilities(computed_probabilities, options_)
[T,num_grand_regimes] = size(computed_probabilities);
num_chains = length(options_.ms.ms_chain);
for i=1:num_chains
chains(i).num_regimes = length(options_.ms.ms_chain(i).state);
chains(i).num_regimes = length(options_.ms.ms_chain(i).regime);
chains(i).probabilities = zeros([T,chains(i).num_regimes]);
end
......
function y_=simult(ys, dr)
% function y_=simult(ys, dr)
function y_=simult(y0, dr)
% function y_=simult(y0, dr)
% Recursive Monte Carlo simulations
%
% INPUTS
% ys: vector of variables in steady state
% y0: vector of variables in initial period of the simulation
% dr: structure of decisions rules for stochastic simulations
%
% OUTPUTS
......@@ -54,7 +54,7 @@ for i=1:replic
if ~isempty(M_.Sigma_e)
oo_.exo_simul(:,i_exo_var) = randn(options_.periods,nxs)*chol_S;
end
y_ = simult_(ys,dr,oo_.exo_simul,order);
y_ = simult_(y0,dr,oo_.exo_simul,order);
% elimninating initial value
y_ = y_(:,2:end);
if replic > 1
......
......@@ -130,7 +130,15 @@ if options_.periods > 0 && ~PI_PCL_solver
options_ =options_old;
return
end
oo_.endo_simul = simult(oo_.dr.ys,oo_.dr);
% Note that the first column of oo_.endo_simul is preserved by the following
% call to simult; this is important because stoch_simul can be followed by
% forecast (see ticket #157)
if size(oo_.endo_simul,2) == 0
y0 = oo_.dr.ys;
else
y0 = oo_.endo_simul(:,1);
end
oo_.endo_simul = simult(y0,oo_.dr);
dyn2vec;
end
......
......@@ -82,7 +82,12 @@ elseif options_.periods ~= 0
options_ =options_old;
return
end
oo_.endo_simul = simult(repmat(oo_.dr.ys,1,M_.maximum_lag),oo_.dr);
if size(oo_.endo_simul,2) < maximum_lag
y0 = oo_.dr.ys;
else
y0 = oo_.endo_simul(:,1);
end
oo_.endo_simul = simult(y0,oo_.dr);
dyn2vec;
if options_.nomoments == 0
disp_moments(oo_.endo_simul,var_list);
......
......@@ -140,14 +140,15 @@ enum BlockSimulationType
/*! Warning: do not to change existing values for 0 to 4: the values matter for homotopy_setup command */
enum SymbolType
{
eEndogenous = 0, //!< Endogenous
eExogenous = 1, //!< Exogenous
eExogenousDet = 2, //!< Exogenous deterministic
eParameter = 4, //!< Parameter
eModelLocalVariable = 10, //!< Local variable whose scope is model (pound expression)
eModFileLocalVariable = 11, //!< Local variable whose scope is mod file (model excluded)
eExternalFunction = 12, //!< External (user-defined) function
eTrend = 13 //!< Trend variable
eEndogenous = 0, //!< Endogenous
eExogenous = 1, //!< Exogenous
eExogenousDet = 2, //!< Exogenous deterministic
eParameter = 4, //!< Parameter
eModelLocalVariable = 10, //!< Local variable whose scope is model (pound expression)
eModFileLocalVariable = 11, //!< Local variable whose scope is mod file (model excluded)
eExternalFunction = 12, //!< External (user-defined) function
eTrend = 13, //!< Trend variable
eStatementDeclaredVariable = 14 //!< Local variable assigned within a Statement (see subsample statement for example)
};
enum ExpressionType
......
......@@ -135,15 +135,6 @@ StochSimulStatement::checkPass(ModFileStructure &mod_file_struct)
cerr << "ERROR: in 'stoch_simul', you cannot use option 'pruning' with 'k_order_solver' option or with 3rd order approximation" << endl;
exit(EXIT_FAILURE);
}
// Workaround for ticket #157
it = options_list.num_options.find("periods");
if (it != options_list.num_options.end() && atoi(it->second.c_str()) > 0
&& mod_file_struct.histval_present)
{
cerr << "ERROR: the 'periods' option of 'stoch_simul' is not compatible with a 'histval' block" << endl;
exit(EXIT_FAILURE);
}
}
void
......@@ -325,6 +316,30 @@ EstimationStatement::checkPass(ModFileStructure &mod_file_struct)
cerr << "ERROR: An estimation statement cannot take more than one dsge_var option." << endl;
exit(EXIT_FAILURE);
}
if (options_list.string_options.find("datafile") == options_list.string_options.end() &&
!mod_file_struct.estimation_data_statement_present)
{
cerr << "ERROR: The estimation statement requires a data file to be supplied "
<< "either from the data statement or from the deprecated option datafile." << endl;
exit(EXIT_FAILURE);
}
if (options_list.string_options.find("datafile") != options_list.string_options.end())
cerr << "WARNING: The datafile option of estimation has been deprecated. "
<< "Use the data command instead." << endl;
if (options_list.string_options.find("xls_sheet") != options_list.string_options.end())
cerr << "WARNING: The xls_sheet option of estimation has been deprecated. "
<< "Use the data command instead." << endl;
if (options_list.string_options.find("xls_range") != options_list.string_options.end())
cerr << "WARNING: The xls_range option of estimation has been deprecated. "
<< "Use the data command instead." << endl;
if (options_list.num_options.find("first_obs") != options_list.num_options.end())
cerr << "WARNING: The first_obs option of estimation has been deprecated. "
<< "Use the data command instead." << endl;
}
void
......@@ -1335,7 +1350,9 @@ MarkovSwitchingStatement::MarkovSwitchingStatement(const OptionsList &options_li
void
MarkovSwitchingStatement::writeOutput(ostream &output, const string &basename) const
{
OptionsList::num_options_t::const_iterator itChain, itState, itNOS, itDuration;
bool isDurationAVec = true;
string infStr("Inf");
OptionsList::num_options_t::const_iterator itChain, itNOR, itDuration;
itChain = options_list.num_options.find("ms.chain");
if (itChain == options_list.num_options.end())
......@@ -1350,16 +1367,20 @@ MarkovSwitchingStatement::writeOutput(ostream &output, const string &basename) c
cerr << "MarkovSwitchingStatement::writeOutput() Should not arrive here (2). Please report this to the Dynare Team." << endl;
exit(EXIT_FAILURE);
}
else if (atof(itDuration->second.c_str()) || infStr.compare(itDuration->second) == 0)
isDurationAVec = false;
output << "options_.ms.duration = " << itDuration->second << ";" << endl;
itState = options_list.num_options.find("ms.state");
itNOS = options_list.num_options.find("ms.number_of_states");
if (itState != options_list.num_options.end()
&& itNOS == options_list.num_options.end())
output << "options_.ms.ms_chain(" << itChain->second << ").state(" << itState->second << ").duration = " << itDuration->second << ";" << endl;
else if (itState == options_list.num_options.end()
&& itNOS != options_list.num_options.end())
for (int i = 0; i < atoi(itNOS->second.c_str()); i++)
output << "options_.ms.ms_chain(" << itChain->second << ").state(" << i+1 << ").duration = " << itDuration->second << ";" << endl;
itNOR = options_list.num_options.find("ms.number_of_regimes");
if (itNOR != options_list.num_options.end())
for (int i = 0; i < atoi(itNOR->second.c_str()); i++)
{
output << "options_.ms.ms_chain(" << itChain->second << ").regime("
<< i+1 << ").duration = options_.ms.duration";
if (isDurationAVec)
output << "(" << i+1 << ")";
output << ";" << endl;
}
else
{
cerr << "MarkovSwitchingStatement::writeOutput() Should not arrive here (3). Please report this to the Dynare Team." << endl;
......@@ -1431,3 +1452,416 @@ SvarStatement::writeOutput(ostream &output, const string &basename) const
else
output << "'ALL';" << endl;
}
SetTimeStatement::SetTimeStatement(const OptionsList &options_list_arg) :
options_list(options_list_arg)
{
}
void
SetTimeStatement::writeOutput(ostream &output, const string &basename) const
{
options_list.writeOutput(output);
}
EstimationDataStatement::EstimationDataStatement(const OptionsList &options_list_arg) :
options_list(options_list_arg)
{
}
void
EstimationDataStatement::checkPass(ModFileStructure &mod_file_struct)
{
mod_file_struct.estimation_data_statement_present = true;
OptionsList::num_options_t::const_iterator it = options_list.num_options.find("nobs");
if (it != options_list.num_options.end())
if (atoi(it->second.c_str()) <= 0)
{
cerr << "ERROR: The nobs option of the data statement only accepts positive integers." << endl;
exit(EXIT_FAILURE);
}
if (options_list.string_options.find("file") == options_list.string_options.end())
{
cerr << "ERROR: The file option must be passed to the data statement." << endl;
exit(EXIT_FAILURE);
}
}
void
EstimationDataStatement::writeOutput(ostream &output, const string &basename) const
{
options_list.writeOutput(output, "options_.dataset");
if (options_list.date_options.find("first_obs") == options_list.date_options.end())
output << "options_.dataset.firstobs = options_.initial_period;" << endl;
}
BasicPriorStatement::~BasicPriorStatement()
{
}
BasicPriorStatement::BasicPriorStatement(const string &name_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg) :
name(name_arg),
variance(variance_arg),
options_list(options_list_arg),
first_statement_encountered(false)
{
}
void
BasicPriorStatement::checkPass(ModFileStructure &mod_file_struct)
{
if (options_list.num_options.find("shape") == options_list.num_options.end())
{
cerr << "ERROR: You must pass the shape option to the prior statement." << endl;
exit(EXIT_FAILURE);
}
if (options_list.num_options.find("date1") != options_list.num_options.end() ||
options_list.num_options.find("date2") != options_list.num_options.end())
if (options_list.num_options.find("date1") == options_list.num_options.end() ||
options_list.num_options.find("date2") == options_list.num_options.end())
{
cerr << "ERROR: PriorStatement::checkPass(1). Should not arrive here. "
<< "Please inform Dynare Team." << endl;
exit(EXIT_FAILURE);
}
}
void
BasicPriorStatement::get_base_name(const SymbolType symb_type, string &lhs_field) const
{
if (symb_type == eExogenous || symb_type == eExogenousDet)
lhs_field = "structural_innovation";
else
lhs_field = "measurement_error";
}
void
BasicPriorStatement::writePriorIndex(ostream &output, const string &lhs_field) const
{
if (first_statement_encountered)
output << "prior_indx = 1;" << endl;
else
output << "prior_indx = size(estimation_info" << lhs_field << "_index, 2) + 1;" << endl;
}
void
BasicPriorStatement::writeVarianceOption(ostream &output, const string &lhs_field) const
{
if (variance)
{
output << "estimation_info" << lhs_field << "(prior_indx).variance = ";
variance->writeOutput(output);
output << ";" << endl;
}
}
void
BasicPriorStatement::writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const
{
OptionsList::num_options_t::const_iterator itn = options_list.num_options.find(field);
if (itn != options_list.num_options.end())
output << "estimation_info" << lhs_field << "(prior_indx)." << field
<< " = " << itn->second << ";" << endl;
OptionsList::date_options_t::const_iterator itd = options_list.date_options.find(field);
if (itd != options_list.date_options.end())
output << "estimation_info" << lhs_field << "(prior_indx)." << field
<< " = '" << itd->second << "';" << endl;
}
PriorStatement::PriorStatement(const string &name_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg) :
BasicPriorStatement(name_arg, variance_arg, options_list_arg)
{
}
void
PriorStatement::checkPass(ModFileStructure &mod_file_struct)
{
BasicPriorStatement::checkPass(mod_file_struct);
if (!mod_file_struct.prior_statement_present)
first_statement_encountered = true;
mod_file_struct.prior_statement_present = true;
}
void
PriorStatement::writeOutput(ostream &output, const string &basename) const
{
string lhs_field = ".prior";
BasicPriorStatement::writePriorIndex(output, lhs_field);
output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name << "'};" << endl
<< "estimation_info" << lhs_field <<"(prior_indx).name = '" << name << "';" << endl;
writeOutputHelper(output, "mean", lhs_field);
writeOutputHelper(output, "mode", lhs_field);
writeOutputHelper(output, "stdev", lhs_field);
writeOutputHelper(output, "shape", lhs_field);
writeOutputHelper(output, "shift", lhs_field);
writeOutputHelper(output, "date1", lhs_field);
writeOutputHelper(output, "date2", lhs_field);
writeOutputHelper(output, "domain", lhs_field);
writeOutputHelper(output, "interval", lhs_field);
BasicPriorStatement::writeVarianceOption(output, lhs_field);
}
StdPriorStatement::StdPriorStatement(const string &name_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg,
const SymbolTable &symbol_table_arg ) :
BasicPriorStatement(name_arg, variance_arg, options_list_arg),
symbol_table(symbol_table_arg)
{
}
void
StdPriorStatement::checkPass(ModFileStructure &mod_file_struct)
{
BasicPriorStatement::checkPass(mod_file_struct);
if (!mod_file_struct.std_prior_statement_present)
first_statement_encountered = true;
mod_file_struct.std_prior_statement_present = true;
}
void
StdPriorStatement::writeOutput(ostream &output, const string &basename) const
{
string lhs_field;
get_base_name(symbol_table.getType(name), lhs_field);
lhs_field = "." + lhs_field + ".prior";
BasicPriorStatement::writePriorIndex(output, lhs_field);
output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name << "'};" << endl;
output << "estimation_info" << lhs_field << "(prior_indx).name = '" << name << "';" << endl;
writeOutputHelper(output, "mean", lhs_field);
writeOutputHelper(output, "mode", lhs_field);
writeOutputHelper(output, "stdev", lhs_field);
writeOutputHelper(output, "shape", lhs_field);
writeOutputHelper(output, "shift", lhs_field);
writeOutputHelper(output, "domain", lhs_field);
writeOutputHelper(output, "interval", lhs_field);
BasicPriorStatement::writeVarianceOption(output, lhs_field);
}
CorrPriorStatement::CorrPriorStatement(const string &name_arg1, const string &name_arg2,
const expr_t &variance_arg,
const OptionsList &options_list_arg,
const SymbolTable &symbol_table_arg ) :
BasicPriorStatement(name_arg1, variance_arg, options_list_arg),
name1(name_arg2),
symbol_table(symbol_table_arg)
{
}
void
CorrPriorStatement::checkPass(ModFileStructure &mod_file_struct)
{
BasicPriorStatement::checkPass(mod_file_struct);
if (symbol_table.getType(name) != symbol_table.getType(name1))
{
cerr << "ERROR: In the corr(A,B).prior statement, A and B must be of the same type. "
<< "In your case, " << name << " and " << name1 << " are of different "
<< "types." << endl;
exit(EXIT_FAILURE);
}
if (!mod_file_struct.corr_prior_statement_present)
first_statement_encountered = true;
mod_file_struct.corr_prior_statement_present = true;
}
void
CorrPriorStatement::writeOutput(ostream &output, const string &basename) const
{
string lhs_field;
get_base_name(symbol_table.getType(name), lhs_field);
lhs_field = "." + lhs_field + "_corr.prior";
BasicPriorStatement::writePriorIndex(output, lhs_field);
output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name << "_" << name1 << "'};" << endl;
output << "estimation_info" << lhs_field << "(prior_indx).name1 = '" << name << "';" << endl;
output << "estimation_info" << lhs_field << "(prior_indx).name2 = '" << name1 << "';" << endl;
writeOutputHelper(output, "mean", lhs_field);
writeOutputHelper(output, "mode", lhs_field);
writeOutputHelper(output, "stdev", lhs_field);
writeOutputHelper(output, "shape", lhs_field);
writeOutputHelper(output, "shift", lhs_field);
writeOutputHelper(output, "domain", lhs_field);
writeOutputHelper(output, "interval", lhs_field);
BasicPriorStatement::writeVarianceOption(output, lhs_field);
}
BasicOptionsStatement::~BasicOptionsStatement()
{
}
BasicOptionsStatement::BasicOptionsStatement(const string &name_arg,
const OptionsList &options_list_arg) :
name(name_arg),
options_list(options_list_arg),
first_statement_encountered(false)
{
}
void
BasicOptionsStatement::checkPass(ModFileStructure &mod_file_struct)
{
if (options_list.num_options.find("date1") != options_list.num_options.end() ||
options_list.num_options.find("date2") != options_list.num_options.end())
if (options_list.num_options.find("date1") == options_list.num_options.end() ||
options_list.num_options.find("date2") == options_list.num_options.end())
{
cerr << "ERROR: OptionsStatement::checkPass(1). Should not arrive here. "
<< "Please inform Dynare Team." << endl;
exit(EXIT_FAILURE);
}
}
void
BasicOptionsStatement::writeOptionsIndex(ostream &output, const string &lhs_field) const
{
if (first_statement_encountered)
output << "options_indx = 1;" << endl;
else
output << "options_indx = size(estimation_info" << lhs_field << "_index, 2) + 1;" << endl;
}
void
BasicOptionsStatement::get_base_name(const SymbolType symb_type, string &lhs_field) const
{
if (symb_type == eExogenous || symb_type == eExogenousDet)
lhs_field = "structural_innovation";
else
lhs_field = "measurement_error";
}
void
BasicOptionsStatement::writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const
{
OptionsList::num_options_t::const_iterator itn = options_list.num_options.find(field);
if (itn != options_list.num_options.end())
output << "estimation_info" << lhs_field << "(options_indx)." << field
<< " = " << itn->second << ";" << endl;
OptionsList::date_options_t::const_iterator itd = options_list.date_options.find(field);
if (itd != options_list.date_options.end())
output << "estimation_info" << lhs_field << "(options_indx)." << field
<< " = '" << itd->second << "';" << endl;
}
OptionsStatement::OptionsStatement(const string &name_arg,
const OptionsList &options_list_arg) :
BasicOptionsStatement(name_arg, options_list_arg)
{
}
void
OptionsStatement::checkPass(ModFileStructure &mod_file_struct)
{
BasicOptionsStatement::checkPass(mod_file_struct);
if (!mod_file_struct.options_statement_present)
first_statement_encountered = true;
mod_file_struct.options_statement_present = true;
}
void
OptionsStatement::writeOutput(ostream &output, const string &basename) const
{
string lhs_field = ".options";