estimation: add subsamples for std / corr, prior / options statements & simplify code

time-shift
Houtan Bastani 2012-03-26 16:46:44 +02:00
parent 4b7c2e9f8d
commit c5ace037ad
6 changed files with 177 additions and 103 deletions

View File

@ -1693,18 +1693,32 @@ BasicPriorStatement::writePriorIndex(ostream &output, const string &lhs_field) c
}
void
BasicPriorStatement::writeVarianceOption(ostream &output, const string &lhs_field) const
BasicPriorStatement::writeCommonOutput(ostream &output, const string &lhs_field) const
{
if (variance)
{
output << "estimation_info" << lhs_field << "(prior_indx).variance = ";
variance->writeOutput(output);
output << ";" << endl;
}
writeShape(output, lhs_field);
writeCommonOutputHelper(output, "mean", lhs_field);
writeCommonOutputHelper(output, "mode", lhs_field);
writeCommonOutputHelper(output, "stdev", lhs_field);
writeCommonOutputHelper(output, "shape", lhs_field);
writeCommonOutputHelper(output, "shift", lhs_field);
writeCommonOutputHelper(output, "date1", lhs_field);
writeCommonOutputHelper(output, "date2", lhs_field);
writeCommonOutputHelper(output, "domain", lhs_field);
writeCommonOutputHelper(output, "median", lhs_field);
writeCommonOutputHelper(output, "truncate", lhs_field);
writeCommonOutputHelper(output, "interval", lhs_field);
writeVarianceOption(output, lhs_field);
}
void
BasicPriorStatement::writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const
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
BasicPriorStatement::writeCommonOutputHelper(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())
@ -1718,10 +1732,14 @@ BasicPriorStatement::writeOutputHelper(ostream &output, const string &field, con
}
void
BasicPriorStatement::writeShape(ostream &output, const string &lhs_field) const
BasicPriorStatement::writeVarianceOption(ostream &output, const string &lhs_field) const
{
assert(prior_shape != eNoShape);
output << "estimation_info" << lhs_field << "(prior_indx).shape = " << prior_shape << ";" << endl;
if (variance)
{
output << "estimation_info" << lhs_field << "(prior_indx).variance = ";
variance->writeOutput(output);
output << ";" << endl;
}
}
PriorStatement::PriorStatement(const string &name_arg,
@ -1749,19 +1767,8 @@ PriorStatement::writeOutput(ostream &output, const string &basename) const
writePriorIndex(output, lhs_field);
output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name << "'};" << endl
<< "estimation_info" << lhs_field <<"(prior_indx).name = '" << name << "';" << endl;
writeShape(output, lhs_field);
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, "median", lhs_field);
writeOutputHelper(output, "truncate", lhs_field);
writeOutputHelper(output, "interval", lhs_field);
writeVarianceOption(output, lhs_field);
writeCommonOutput(output, lhs_field);
}
StdPriorStatement::StdPriorStatement(const string &name_arg,
@ -1794,17 +1801,7 @@ StdPriorStatement::writeOutput(ostream &output, const string &basename) const
output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name << "'};" << endl;
output << "estimation_info" << lhs_field << "(prior_indx).name = '" << name << "';" << endl;
writeShape(output, lhs_field);
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, "median", lhs_field);
writeOutputHelper(output, "truncate", lhs_field);
writeOutputHelper(output, "interval", lhs_field);
writeVarianceOption(output, lhs_field);
writeCommonOutput(output, lhs_field);
}
CorrPriorStatement::CorrPriorStatement(const string &name_arg1, const string &name_arg2,
@ -1846,17 +1843,7 @@ CorrPriorStatement::writeOutput(ostream &output, const string &basename) const
output << "estimation_info" << lhs_field << "(prior_indx).name1 = '" << name << "';" << endl;
output << "estimation_info" << lhs_field << "(prior_indx).name2 = '" << name1 << "';" << endl;
writeShape(output, lhs_field);
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, "median", lhs_field);
writeOutputHelper(output, "truncate", lhs_field);
writeOutputHelper(output, "interval", lhs_field);
writeVarianceOption(output, lhs_field);
writeCommonOutput(output, lhs_field);
}
BasicOptionsStatement::~BasicOptionsStatement()
@ -1899,7 +1886,17 @@ BasicOptionsStatement::get_base_name(const SymbolType symb_type, string &lhs_fie
}
void
BasicOptionsStatement::writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const
BasicOptionsStatement::writeCommonOutput(ostream &output, const string &lhs_field) const
{
writeCommonOutputHelper(output, "init", lhs_field);
writeCommonOutputHelper(output, "bounds", lhs_field);
writeCommonOutputHelper(output, "jscale", lhs_field);
writeCommonOutputHelper(output, "date1", lhs_field);
writeCommonOutputHelper(output, "date2", lhs_field);
}
void
BasicOptionsStatement::writeCommonOutputHelper(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())
@ -1936,11 +1933,7 @@ OptionsStatement::writeOutput(ostream &output, const string &basename) const
output << "estimation_info" << lhs_field <<"_index(options_indx) = {'" << name << "'};" << endl
<< "estimation_info" << lhs_field << "(options_indx).name = '" << name << "';" << endl;
writeOutputHelper(output, "init", lhs_field);
writeOutputHelper(output, "bounds", lhs_field);
writeOutputHelper(output, "jscale", lhs_field);
writeOutputHelper(output, "date1", lhs_field);
writeOutputHelper(output, "date2", lhs_field);
writeCommonOutput(output, lhs_field);
}
StdOptionsStatement::StdOptionsStatement(const string &name_arg,
@ -1971,11 +1964,7 @@ StdOptionsStatement::writeOutput(ostream &output, const string &basename) const
output << "estimation_info" << lhs_field << "_index(options_indx) = {'" << name << "'};" << endl;
output << "estimation_info" << lhs_field << "(options_indx).name = '" << name << "';" << endl;
writeOutputHelper(output, "init", lhs_field);
writeOutputHelper(output, "bounds", lhs_field);
writeOutputHelper(output, "jscale", lhs_field);
writeOutputHelper(output, "date1", lhs_field);
writeOutputHelper(output, "date2", lhs_field);
writeCommonOutput(output, lhs_field);
}
CorrOptionsStatement::CorrOptionsStatement(const string &name_arg1, const string &name_arg2,
@ -2015,9 +2004,5 @@ CorrOptionsStatement::writeOutput(ostream &output, const string &basename) const
output << "estimation_info" << lhs_field << "(options_indx).name1 = '" << name << "';" << endl;
output << "estimation_info" << lhs_field << "(options_indx).name2 = '" << name1 << "';" << endl;
writeOutputHelper(output, "init", lhs_field);
writeOutputHelper(output, "bounds", lhs_field);
writeOutputHelper(output, "jscale", lhs_field);
writeOutputHelper(output, "date1", lhs_field);
writeOutputHelper(output, "date2", lhs_field);
writeCommonOutput(output, lhs_field);
}

View File

@ -597,7 +597,8 @@ protected:
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 writeOutputHelper(ostream &output, const string &field, 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;
};
@ -655,7 +656,8 @@ protected:
void get_base_name(const SymbolType symb_type, string &lhs_field) const;
virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
void writeOptionsIndex(ostream &output, const string &lhs_field) const;
void writeOutputHelper(ostream &output, const string &field, 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;
};
class OptionsStatement : public BasicOptionsStatement

View File

@ -182,7 +182,7 @@ class ParsingDriver;
%type <string_val> vec_value_1 vec_value signed_inf signed_number_w_inf
%type <string_val> range vec_value_w_inf vec_value_1_w_inf
%type <symbol_type_val> change_type_arg
%type <vector_string_val> change_type_var_list
%type <vector_string_val> change_type_var_list subsamples_eq_opt
%type <vector_int_val> vec_int_elem vec_int_1 vec_int vec_int_number
%type <prior_distributions_val> prior_pdf prior_distribution
%%
@ -1213,14 +1213,38 @@ data_options : o_file
| o_xls_range
;
subsamples : symbol '.' SUBSAMPLES '(' subsamples_name_list ')' ';'
{ driver.set_subsamples($1); }
subsamples : subsamples_eq_opt '(' subsamples_name_list ')' ';'
{ driver.set_subsamples($1->at(0), $1->at(1)); }
;
subsamples_eq : symbol '.' SUBSAMPLES EQUAL symbol '.' SUBSAMPLES ';'
{ driver.copy_subsamples($1, $5); }
subsamples_eq : subsamples_eq_opt EQUAL subsamples_eq_opt ';'
{
driver.copy_subsamples($1->at(0), $1->at(1), $3->at(0), $3->at(1));
delete $1;
delete $3;
}
;
subsamples_eq_opt : symbol '.' SUBSAMPLES
{
$$ = new vector<string *>();
$$->push_back($1);
$$->push_back(new string (""));
}
| STD '(' symbol ')' '.'SUBSAMPLES
{
$$ = new vector<string *>();
$$->push_back($3);
$$->push_back(new string (""));
}
| CORR '(' symbol COMMA symbol ')' '.' SUBSAMPLES
{
$$ = new vector<string *>();
$$->push_back($3);
$$->push_back($5);
}
;
subsamples_name_list : subsamples_name_list COMMA o_subsample_name
| o_subsample_name
;
@ -1229,13 +1253,23 @@ prior : symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNo
{ driver.set_prior($1); }
| symbol '.' symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';'
{
driver.add_subsample_range(new string (*$1), $3);
driver.add_subsample_range(new string (*$1), new string (""), $3);
driver.set_prior($1);
}
| STD '(' symbol ')' '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';'
{ driver.set_std_prior($3); }
| CORR '(' symbol COMMA symbol')' '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';'
| STD '(' symbol ')' '.' symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';'
{
driver.add_subsample_range(new string (*$3), new string (""), $6);
driver.set_std_prior($3);
}
| CORR '(' symbol COMMA symbol ')' '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';'
{ driver.set_corr_prior($3, $5); }
| CORR '(' symbol COMMA symbol ')' '.' symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';'
{
driver.add_subsample_range(new string (*$3), new string (*$5), $8);
driver.set_corr_prior($3, $5);
}
;
prior_options_list : prior_options_list COMMA prior_options
@ -1258,13 +1292,23 @@ options : symbol '.' OPTIONS '(' options_options_list ')' ';'
{ driver.set_options($1); }
| symbol '.' symbol '.' OPTIONS '(' options_options_list ')' ';'
{
driver.add_subsample_range(new string (*$1), $3);
driver.add_subsample_range(new string (*$1), new string (""), $3);
driver.set_options($1);
}
| STD '(' symbol ')' '.' OPTIONS '(' options_options_list ')' ';'
{ driver.set_std_options($3); }
| CORR '(' symbol COMMA symbol')' '.' OPTIONS '(' options_options_list ')' ';'
| STD '(' symbol ')' '.' symbol '.' OPTIONS '(' options_options_list ')' ';'
{
driver.add_subsample_range(new string (*$3), new string (""), $6);
driver.set_std_options($3);
}
| CORR '(' symbol COMMA symbol ')' '.' OPTIONS '(' options_options_list ')' ';'
{ driver.set_corr_options($3, $5); }
| CORR '(' symbol COMMA symbol ')' '.' symbol '.' OPTIONS '(' options_options_list ')' ';'
{
driver.add_subsample_range(new string (*$3), new string (*$5), $8);
driver.set_corr_options($3, $5);
}
;
options_options_list : options_options_list COMMA options_options

View File

@ -248,6 +248,7 @@ string eofbuff;
<DYNARE_STATEMENT>relative_irf {return token::RELATIVE_IRF;}
<DYNARE_STATEMENT>tex {return token::TEX;}
<DYNARE_STATEMENT>nomoments {return token::NOMOMENTS;}
<DYNARE_STATEMENT>std {return token::STD;}
<DYNARE_STATEMENT>corr {return token::CORR;}
<DYNARE_STATEMENT>nocorr {return token::NOCORR;}
<DYNARE_STATEMENT>optim {return token::OPTIM;}

View File

@ -1236,30 +1236,60 @@ ParsingDriver::estimation_data()
}
void
ParsingDriver::copy_subsamples(string *to_parameter, string *from_parameter)
ParsingDriver::set_subsamples(string *name1, string *name2)
{
check_symbol_is_parameter(to_parameter);
check_symbol_is_parameter(from_parameter);
if (subsample_declarations.find(*to_parameter) != subsample_declarations.end())
error("Parameter " + *to_parameter + " has more than one subsample statement." +
"You may only have one subsample statement per parameter.");
if (subsample_declarations.find(*from_parameter) == subsample_declarations.end())
error("Parameter " + *from_parameter + " does not have an associated subsample statement.");
subsample_declarations[*to_parameter] = subsample_declarations[*from_parameter];
delete to_parameter;
delete from_parameter;
check_symbol_existence(*name1);
if (!name2->empty())
check_symbol_existence(*name2);
if (subsample_declarations.find(make_pair(*name1,*name2)) != subsample_declarations.end())
{
string err = *name1;
if (!name2->empty())
err.append(",").append(*name2);
error(err + " has more than one subsample statement." +
"You may only have one subsample statement per variable.");
}
subsample_declarations[make_pair(*name1, *name2)] = subsample_declaration_map;
subsample_declaration_map.clear();
delete name1;
delete name2;
}
void
ParsingDriver::set_subsamples(string *name)
ParsingDriver::copy_subsamples(string *to_name1, string *to_name2, string *from_name1, string *from_name2)
{
check_symbol_is_parameter(name);
if (subsample_declarations.find(*name) != subsample_declarations.end())
error("Parameter " + *name + " has more than one subsample statement." +
"You may only have one subsample statement per parameter.");
subsample_declarations[*name] = subsample_declaration_map;
subsample_declaration_map.clear();
delete name;
check_symbol_existence(*to_name1);
check_symbol_existence(*from_name1);
if (!to_name2->empty())
check_symbol_existence(*to_name2);
if (!from_name2->empty())
check_symbol_existence(*from_name2);
if (subsample_declarations.find(make_pair(*to_name1,*to_name2)) != subsample_declarations.end())
{
string err = *to_name1;
if (!to_name2->empty())
err.append(",").append(*to_name2);
error(err + " has more than one subsample statement." +
"You may only have one subsample statement per symbol (or pair thereof).");
}
if (subsample_declarations.find(make_pair(*from_name1,*from_name2)) == subsample_declarations.end())
{
string err = *from_name1;
if (!from_name2->empty())
err.append(",").append(*from_name2);
error(err + " does not have an associated subsample statement.");
}
subsample_declarations[make_pair(*to_name1, *to_name2)] =
subsample_declarations[make_pair(*from_name1, *from_name2)];
delete to_name1;
delete to_name2;
delete from_name1;
delete from_name2;
}
void
@ -1284,19 +1314,31 @@ ParsingDriver::set_subsample_name_equal_to_date_range(string *name, string *date
}
void
ParsingDriver::add_subsample_range(string *parameter, string *subsample_name)
ParsingDriver::add_subsample_range(string *name1, string *name2, string *subsample_name)
{
check_symbol_is_parameter(parameter);
check_symbol_existence(*name1);
if (!name2->empty())
check_symbol_existence(*name2);
check_symbol_is_statement_variable(subsample_name);
subsample_declarations_t::const_iterator it = subsample_declarations.find(*parameter);
subsample_declarations_t::const_iterator it = subsample_declarations.find(make_pair(*name1, *name2));
if (it == subsample_declarations.end())
error("A subsample statement has not been issued for " + *parameter);
{
string err = *name1;
if (!name2->empty())
err.append(",").append(*name2);
error("A subsample statement has not been issued for " + err);
}
subsample_declaration_map_t tmp_map = it->second;
if (tmp_map.find(*subsample_name) == tmp_map.end())
error("The subsample name " + *subsample_name + " was not previously declared in a subsample statement.");
option_date("date1", tmp_map[*subsample_name].first);
option_date("date2", tmp_map[*subsample_name].second);
delete parameter;
delete name1;
delete name2;
delete subsample_name;
}

View File

@ -192,8 +192,8 @@ private:
//! Temporary storage for declaring subsamples: map<statement_local_var, <date1, date2 >
typedef map<string, pair<string, string> > subsample_declaration_map_t;
subsample_declaration_map_t subsample_declaration_map;
//! Temporary storage for subsample statement: map<parameter, subsample_declaration_map >
typedef map<string, subsample_declaration_map_t > subsample_declarations_t;
//! Temporary storage for subsample statement: map<pair<var_name1, var_name2>>, subsample_declaration_map >
typedef map<pair<string, string >, subsample_declaration_map_t > subsample_declarations_t;
subsample_declarations_t subsample_declarations;
//! reset the values for temporary storage
void reset_current_external_function_options();
@ -254,13 +254,13 @@ public:
//! Declares a statement local variable
void declare_statement_local_variable(string *name);
//! Completes a subsample statement
void set_subsamples(string *name);
void set_subsamples(string *name1, string *name2);
//! Declares a subsample, assigning the value to name
void set_subsample_name_equal_to_date_range(string *name, string *date1, string *date2);
//! Adds a subsample range to the list of options for the prior statement
void add_subsample_range(string *parameter, string *subsample_name);
//! Copies the set of subsamples from_parameter to_parameter
void copy_subsamples(string *to_parameter, string *from_parameter);
void add_subsample_range(string *name1, string *name2, string *subsample_name);
//! Copies the set of subsamples from_name to_name
void copy_subsamples(string *to_name1, string *to_name2, string *from_name1, string *from_name2);
//! Declares declare_optimal_policy_discount_factor as a parameter and initializes it to exprnode
void declare_optimal_policy_discount_factor_parameter(expr_t exprnode);
//! Adds a predetermined_variable