Commit d54e4c4b authored by Houtan Bastani's avatar Houtan Bastani
Browse files

estimation: rewrite prior output

parent 791d0ff0
function subsamples_indx = get_new_or_existing_subsamples_indx(name1, name2)
% function subsamples_indx = get_new_or_existing_subsamples_indx(name1, name2)
function indx = get_new_or_existing_ei_index(substructure_name, name1, name2)
% function indx = get_new_or_existing_ei_index(substructure_name, name1, name2)
%
% Returns the new estimation_info.subsamples_index index
% Returns the new estimation_info.substructure_name index
% for the name1 & name2 pair
%
% INPUTS
% substructure_name [string] the name of the substructure in which to look
% name1 [string] the variable for which the subsample was declared
% name2 [string] used only in case of corr(name1,name2).subsamples()
%
% OUTPUTS
% subsamples_indx [integer] new index in the estimation_info.subsamples
% structure associated with the name pair
% indx [integer] new index in the
% estimation_info.substructure_name
% associated with the name pair
%
% SPECIAL REQUIREMENTS
% none
......@@ -34,24 +36,24 @@ function subsamples_indx = get_new_or_existing_subsamples_indx(name1, name2)
global estimation_info
if isempty(estimation_info.subsamples_index)
subsamples_indx = 1;
if eval(['isempty(estimation_info.' substructure_name ')'])
indx = 1;
return;
end
if isempty(name2) % parameter or std() statement
subsamples_indx = find(strcmp(name1, estimation_info.subsamples_index) == 1);
indx = eval(['find(strcmp(name1, estimation_info.' substructure_name ') == 1)']);
else % corr statement
subsamples_indx = find(strcmp([name1 '_' name2], estimation_info.subsamples_index) == 1);
if isempty(subsamples_indx)
subsamples_indx = find(strcmp([name2 '_' name1], estimation_info.subsamples_index) == 1);
indx = eval(['find(strcmp([''' name1 '_' name2 '''], estimation_info.' substructure_name ') == 1)']);
if isempty(indx)
indx = eval(['find(strcmp([''' name2 '_' name1 '''], estimation_info.' substructure_name ') == 1)']);
end
end
if isempty(subsamples_indx)
subsamples_indx = size(estimation_info.subsamples_index, 2) + 1;
if isempty(indx)
indx = eval(['size(estimation_info.' substructure_name ', 2) + 1']);
end
if size(subsamples_indx,2) > 1
if size(indx, 2) > 1
error(['Error: ' name1 ' ' name2 'found more than once in estimation_info.subsamples_index']);
end
......@@ -244,33 +244,68 @@ options_.timeless = 0;
% estimation
estimation_info.subsamples.range = struct('date1', {}, 'date2', {});
estimation_info.parameters.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.parameters.prior = struct(...
'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.parameters.subsample_prior = struct(...
'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.structural_innovation.prior = struct(...
'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.structural_innovation.subsample_prior = struct(...
'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.structural_innovation_corr.prior = struct(...
'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.structural_innovation_corr.subsample_prior = struct(...
'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.measurement_error.prior = struct(...
'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.measurement_error.subsample_prior = struct(...
'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.measurement_error_corr.prior = struct(...
'name1', {}, 'name2', {}, 'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.measurement_error_corr.subsample_prior = struct(...
'name1', {}, 'name2', {},'shape', {}, 'mean', {}, ...
'mode', {}, 'stdev', {}, 'date1', {}, ...
'date2', {}, 'shift', {}, 'variance', {});
estimation_info.subsamples_index = {};
estimation_info.subsamples.range_index = {};
estimation_info.parameters.prior_index = {};
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.parameters.options_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 = {};
estimation_info.parameter_prior_index = {};
estimation_info.parameters.range_index = {};
estimation_info.measurement_error_prior_index = {};
estimation_info.measurement_error.range_index = {};
estimation_info.structural_innovation_prior_index = {};
estimation_info.structural_innovation.range_index = {};
estimation_info.measurement_error_corr_prior_index = {};
estimation_info.measurement_error_corr.range_index = {};
estimation_info.structural_innovation_corr_prior_index = {};
estimation_info.structural_innovation_corr.range_index = {};
estimation_info.parameters_options_index = {};
estimation_info.parameters.range_index = {};
estimation_info.measurement_error_options_index = {};
estimation_info.measurement_error.range_index = {};
estimation_info.structural_innovation_options_index = {};
estimation_info.structural_innovation.range_index = {};
estimation_info.measurement_error_corr_options_index = {};
estimation_info.measurement_error_corr.range_index = {};
estimation_info.structural_innovation_corr_options_index = {};
estimation_info.structural_innovation_corr.range_index = {};
options_.initial_period = dynDate(1);
options_.dataset.firstobs = options_.initial_period;
options_.dataset.lastobs = NaN;
......
......@@ -1647,7 +1647,8 @@ SubsamplesStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsoli
void
SubsamplesStatement::writeOutput(ostream &output, const string &basename) const
{
output << "subsamples_indx = get_new_or_existing_subsamples_indx('" << name1 << "','" << name2 << "');" << endl
output << "subsamples_indx = get_new_or_existing_ei_index('subsamples_index', '"
<< name1 << "','" << name2 << "');" << endl
<< "estimation_info.subsamples_index(subsamples_indx) = {'" << name1;
if (!name2.empty())
output << "_" << name2;
......@@ -1684,7 +1685,8 @@ SubsamplesEqualStatement::checkPass(ModFileStructure &mod_file_struct, WarningCo
void
SubsamplesEqualStatement::writeOutput(ostream &output, const string &basename) const
{
output << "subsamples_to_indx = get_new_or_existing_subsamples_indx('" << to_name1 << "','" << to_name2 << "');" << endl
output << "subsamples_to_indx = get_new_or_existing_ei_index('subsamples_index', '"
<< to_name1 << "','" << to_name2 << "');" << endl
<< "estimation_info.subsamples_index(subsamples_to_indx) = {'" << to_name1;
if (!to_name2.empty())
output << "_" << to_name2;
......@@ -1708,8 +1710,7 @@ BasicPriorStatement::BasicPriorStatement(const string &name_arg,
subsample_name(subsample_name_arg),
prior_shape(prior_shape_arg),
variance(variance_arg),
options_list(options_list_arg),
first_statement_encountered(false)
options_list(options_list_arg)
{
}
......@@ -1760,19 +1761,19 @@ BasicPriorStatement::get_base_name(const SymbolType symb_type, string &lhs_field
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::writeCommonOutput(ostream &output, const string &lhs_field) const
{
writeShape(output, lhs_field);
assert(prior_shape != eNoShape);
output << lhs_field << ".shape = " << prior_shape << ";" << endl;
if (variance)
{
output << lhs_field << ".variance = ";
variance->writeOutput(output);
output << ";" << endl;
}
writeCommonOutputHelper(output, "mean", lhs_field);
writeCommonOutputHelper(output, "mode", lhs_field);
writeCommonOutputHelper(output, "stdev", lhs_field);
......@@ -1782,14 +1783,6 @@ BasicPriorStatement::writeCommonOutput(ostream &output, const string &lhs_field)
writeCommonOutputHelper(output, "median", lhs_field);
writeCommonOutputHelper(output, "truncate", lhs_field);
writeCommonOutputHelper(output, "interval", lhs_field);
writeVarianceOption(output, lhs_field);
}
void
BasicPriorStatement::writeShape(ostream &output, const string &lhs_field) const
{
assert(prior_shape != eNoShape);
output << "estimation_info" << lhs_field << "(prior_indx).shape = " << prior_shape << ";" << endl;
}
void
......@@ -1797,43 +1790,22 @@ BasicPriorStatement::writeCommonOutputHelper(ostream &output, const string &fiel
{
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;
output << lhs_field << "." << field << " = " << itn->second << ";" << endl;
}
void
BasicPriorStatement::writeVarianceOption(ostream &output, const string &lhs_field) const
BasicPriorStatement::writePriorOutput(ostream &output, string &lhs_field) const
{
if (variance)
if (subsample_name.empty())
lhs_field += ".prior(1)";
else
{
output << "estimation_info" << lhs_field << "(prior_indx).variance = ";
variance->writeOutput(output);
output << ";" << endl;
output << "subsamples_indx = get_existing_subsamples_indx('" << name << "','');" << endl
<< lhs_field << ".range_index = estimation_info.subsamples(subsamples_indx).range_index;" << endl
<< "eisind = get_subsamples_range_indx(subsamples_indx, '" << subsample_name << "');" << endl;
lhs_field += ".subsample_prior(eisind)";
}
}
void
BasicPriorStatement::writeSubsampleName(ostream &output) const
{
if (!subsample_name.empty())
output << ":" << subsample_name;
}
void
BasicPriorStatement::writeSubsampleInfo(ostream &output, const string &lhs_field, const string name1, const string name2) const
{
if (subsample_name.empty())
return;
output << "subsamples_indx = get_existing_subsamples_indx('" << name1 << "', '" << name2 << "');" << endl;
output << "range_indx = get_subsamples_range_indx(subsamples_indx, '" << subsample_name << "');" << endl;
output << "estimation_info" << lhs_field << "(prior_indx).date1 = estimation_info.subsamples(subsamples_indx).range(range_indx).date1;" << endl;
output << "estimation_info" << lhs_field << "(prior_indx).date2 = estimation_info.subsamples(subsamples_indx).range(range_indx).date2;" << endl;
writeCommonOutput(output, lhs_field);
}
PriorStatement::PriorStatement(const string &name_arg,
......@@ -1845,28 +1817,14 @@ PriorStatement::PriorStatement(const string &name_arg,
{
}
void
PriorStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
{
BasicPriorStatement::checkPass(mod_file_struct, warnings);
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 = ".parameters.prior";
writePriorIndex(output, lhs_field);
output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name;
writeSubsampleName(output);
output << "'};" << endl
<< "estimation_info" << lhs_field <<"(prior_indx).name = '" << name << "';" << endl;
writeCommonOutput(output, lhs_field);
writeSubsampleInfo(output, lhs_field, name, "");
string lhs_field = "estimation_info.parameters(eifind)";
output << "eifind = get_new_or_existing_ei_index('parameter_prior_index', '"
<< name << "', '');" << endl
<< "estimation_info.parameter_prior_index(eifind) = {'" << name << "'};" << endl;
writePriorOutput(output, lhs_field);
}
StdPriorStatement::StdPriorStatement(const string &name_arg,
......@@ -1880,30 +1838,17 @@ StdPriorStatement::StdPriorStatement(const string &name_arg,
{
}
void
StdPriorStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
{
BasicPriorStatement::checkPass(mod_file_struct, warnings);
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";
output << "eifind = get_new_or_existing_ei_index('" << lhs_field << "_prior_index', '"
<< name << "', '');" << endl
<< "estimation_info." << lhs_field << "_prior_index(eifind) = {'" << name << "'};" << endl;
writePriorIndex(output, lhs_field);
output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name;
writeSubsampleName(output);
output << "'};" << endl
<< "estimation_info" << lhs_field << "(prior_indx).name = '" << name << "';" << endl;
writeCommonOutput(output, lhs_field);
writeSubsampleInfo(output, lhs_field, name, "");
lhs_field = "estimation_info." + lhs_field + "(eifind)";
writePriorOutput(output, lhs_field);
}
CorrPriorStatement::CorrPriorStatement(const string &name_arg1, const string &name_arg2,
......@@ -1929,9 +1874,6 @@ CorrPriorStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolid
<< "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
......@@ -1939,17 +1881,17 @@ 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";
writePriorIndex(output, lhs_field);
output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name << "_" << name1;
writeSubsampleName(output);
output << "'};" << endl
<< "estimation_info" << lhs_field << "(prior_indx).name1 = '" << name << "';" << endl
<< "estimation_info" << lhs_field << "(prior_indx).name2 = '" << name1 << "';" << endl;
output << "eifind = get_new_or_existing_ei_index('" << lhs_field << "_prior_index', '"
<< name << "', '" << name1 << "');" << endl
<< "estimation_info." << lhs_field << "_prior_index(eifind) = {'"
<< name << "_" << name1 << "'};" << endl;
writeCommonOutput(output, lhs_field);
writeSubsampleInfo(output, lhs_field, name, name1);
lhs_field = "estimation_info." + lhs_field + "_corr(eifind)";
writePriorOutput(output, lhs_field);
output << lhs_field << ".name1 = '" << name << "';" << endl
<< lhs_field << ".name2 = '" << name1 << "';" << endl;
}
BasicOptionsStatement::~BasicOptionsStatement()
......@@ -2118,7 +2060,6 @@ CorrOptionsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsol
}
if (!mod_file_struct.corr_options_statement_present)
first_statement_encountered = true;
mod_file_struct.corr_prior_statement_present = true;
}
void
......
......@@ -622,7 +622,6 @@ protected:
const PriorDistributions prior_shape;
const expr_t variance;
const OptionsList options_list;
bool first_statement_encountered;
BasicPriorStatement(const string &name_arg,
const string &subsample_name_arg,
const PriorDistributions &prior_shape_arg,
......@@ -630,13 +629,9 @@ protected:
const OptionsList &options_list_arg);
virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
void get_base_name(const SymbolType symb_type, string &lhs_field) const;
void writePriorIndex(ostream &output, const string &lhs_field) const;
void writeVarianceOption(ostream &output, const string &lhs_field) const;
void writeCommonOutput(ostream &output, const string &lhs_field) const;
void writeCommonOutputHelper(ostream &output, const string &field, const string &lhs_field) const;
void writeShape(ostream &output, const string &lhs_field) const;
void writeSubsampleName(ostream &output) const;
void writeSubsampleInfo(ostream &output, const string &lhs_field, const string name1, const string name2) const;
void writePriorOutput(ostream &output, string &lhs_field) const;
};
class PriorStatement : public BasicPriorStatement
......@@ -647,7 +642,6 @@ public:
const PriorDistributions &prior_shape_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg);
virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
virtual void writeOutput(ostream &output, const string &basename) const;
};
......@@ -662,7 +656,6 @@ public:
const expr_t &variance_arg,
const OptionsList &options_list_arg,
const SymbolTable &symbol_table_arg);
virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
virtual void writeOutput(ostream &output, const string &basename) const;
};
......
......@@ -45,9 +45,6 @@ ModFileStructure::ModFileStructure() :
dsge_var_estimated(false),
bayesian_irf_present(false),
estimation_data_statement_present(false),
prior_statement_present(false),
std_prior_statement_present(false),
corr_prior_statement_present(false),
options_statement_present(false),
std_options_statement_present(false),
corr_options_statement_present(false),
......
......@@ -89,12 +89,6 @@ public:
bool bayesian_irf_present;
//! Whether there is a data statement present
bool estimation_data_statement_present;
//! Whether there is a prior statement present
bool prior_statement_present;
//! Whether there is a std prior statement present
bool std_prior_statement_present;
//! Whether there is a corr prior statement present
bool corr_prior_statement_present;
//! Whether there is a options statement present
bool options_statement_present;
//! Whether there is a std options statement present
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment