preprocessor: add subsamples statement

issue#70
Houtan Bastani 2011-12-14 13:58:21 +01:00
parent 29d8028fc4
commit 1d68887387
7 changed files with 136 additions and 14 deletions

View File

@ -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

View File

@ -1522,6 +1522,15 @@ BasicPriorStatement::checkPass(ModFileStructure &mod_file_struct)
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
@ -1556,10 +1565,16 @@ BasicPriorStatement::writeVarianceOption(ostream &output, const string &lhs_fiel
void
BasicPriorStatement::writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const
{
OptionsList::num_options_t::const_iterator it = options_list.num_options.find(field);
if (it != options_list.num_options.end())
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
<< " = " << it->second << ";" << endl;
<< " = " << 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,
@ -1592,6 +1607,8 @@ PriorStatement::writeOutput(ostream &output, const string &basename) const
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);

View File

@ -105,7 +105,7 @@ class ParsingDriver;
%token <string_val> INT_NUMBER
%token <string_val> DATE_NUMBER
%token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS
%token KALMAN_ALGO KALMAN_TOL
%token KALMAN_ALGO KALMAN_TOL SUBSAMPLES
%token LABELS LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR
%token MARKOWITZ MARGINAL_DENSITY MAX MAXIT
%token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER MIN MINIMAL_SOLVING_PERIODS
@ -217,6 +217,7 @@ statement : parameters
| set_time
| data
| prior
| subsamples
| varobs
| observation_trends
| unit_root_vars
@ -1197,8 +1198,21 @@ data_options : o_file
| o_xls_range
;
subsamples : symbol '.' SUBSAMPLES '(' subsamples_name_list ')' ';'
{ driver.set_subsamples($1); }
;
subsamples_name_list : subsamples_name_list COMMA o_subsample_name
| o_subsample_name
;
prior : symbol '.' PRIOR '(' prior_options_list ')' ';'
{ driver.set_prior($1); }
| symbol '.' symbol '.' PRIOR '(' prior_options_list ')' ';'
{
driver.add_subsample_range(new string (*$1), $3);
driver.set_prior($1);
}
| STD '(' symbol ')' '.' PRIOR '(' prior_options_list ')' ';'
{ driver.set_std_prior($3); }
| CORR '(' symbol COMMA symbol')' '.' PRIOR '(' prior_options_list ')' ';'
@ -1955,6 +1969,12 @@ o_nograph : NOGRAPH
| GRAPH
{ driver.option_num("nograph", "0"); }
;
o_subsample_name : symbol EQUAL date_number ':' date_number
{
driver.declare_statement_local_variable(new string (*$1));
driver.set_subsample_name_equal_to_date_range($1, $3, $5);
}
;
o_conf_sig : CONF_SIG EQUAL non_negative_number { driver.option_num("conf_sig", $3); };
o_mh_replic : MH_REPLIC EQUAL INT_NUMBER { driver.option_num("mh_replic", $3); };
o_mh_drop : MH_DROP EQUAL non_negative_number { driver.option_num("mh_drop", $3); };

View File

@ -189,6 +189,7 @@ string eofbuff;
/* End of a Dynare block */
<DYNARE_BLOCK>end {BEGIN INITIAL; return token::END;}
<DYNARE_STATEMENT>subsamples {return token::SUBSAMPLES;}
<DYNARE_STATEMENT>prior {return token::PRIOR;}
<INITIAL>std {BEGIN DYNARE_STATEMENT; return token::STD;}
<INITIAL>corr {BEGIN DYNARE_STATEMENT; return token::CORR;}

View File

@ -496,6 +496,7 @@ VariableNode::prepareForDerivation()
non_null_derivatives = datatree.local_variables_table[symb_id]->non_null_derivatives;
break;
case eModFileLocalVariable:
case eStatementDeclaredVariable:
// Such a variable is never derived
break;
case eExternalFunction:
@ -523,6 +524,9 @@ VariableNode::computeDerivative(int deriv_id)
case eModFileLocalVariable:
cerr << "ModFileLocalVariable is not derivable" << endl;
exit(EXIT_FAILURE);
case eStatementDeclaredVariable:
cerr << "eStatementDeclaredVariable is not derivable" << endl;
exit(EXIT_FAILURE);
case eExternalFunction:
cerr << "Impossible case!" << endl;
exit(EXIT_FAILURE);
@ -735,6 +739,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
case eExternalFunction:
case eTrend:
case eStatementDeclaredVariable:
cerr << "Impossible case" << endl;
exit(EXIT_FAILURE);
}
@ -917,6 +922,9 @@ VariableNode::getChainRuleDerivative(int deriv_id, const map<int, expr_t> &recur
case eModFileLocalVariable:
cerr << "ModFileLocalVariable is not derivable" << endl;
exit(EXIT_FAILURE);
case eStatementDeclaredVariable:
cerr << "eStatementDeclaredVariable is not derivable" << endl;
exit(EXIT_FAILURE);
case eExternalFunction:
cerr << "Impossible case!" << endl;
exit(EXIT_FAILURE);

View File

@ -175,6 +175,16 @@ ParsingDriver::declare_parameter(string *name, string *tex_name)
delete tex_name;
}
void
ParsingDriver::declare_statement_local_variable(string *name)
{
if (mod_file->symbol_table.exists(*name))
error("Symbol " + *name + " cannot be assigned within a statement " +
"while being assigned elsewhere in the modfile");
declare_symbol(name, eStatementDeclaredVariable, NULL);
delete name;
}
void
ParsingDriver::declare_optimal_policy_discount_factor_parameter(expr_t exprnode)
{
@ -1224,6 +1234,56 @@ ParsingDriver::estimation_data()
options_list.clear();
}
void
ParsingDriver::set_subsamples(string *name)
{
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;
}
void
ParsingDriver::check_symbol_is_statement_variable(string *name)
{
check_symbol_existence(*name);
int symb_id = mod_file->symbol_table.getID(*name);
if (mod_file->symbol_table.getType(symb_id) != eStatementDeclaredVariable)
error(*name + " is not a variable assigned in a statement");
}
void
ParsingDriver::set_subsample_name_equal_to_date_range(string *name, string *date1, string *date2)
{
check_symbol_is_statement_variable(name);
if (subsample_declaration_map.find(*name) != subsample_declaration_map.end())
error("Symbol " + *name + " may only be assigned once in a SUBSAMPLE statement");
subsample_declaration_map[*name] = make_pair(*date1, *date2);
delete name;
delete date1;
delete date2;
}
void
ParsingDriver::add_subsample_range(string *parameter, string *subsample_name)
{
check_symbol_is_parameter(parameter);
check_symbol_is_statement_variable(subsample_name);
subsample_declarations_t::const_iterator it = subsample_declarations.find(*parameter);
if (it == subsample_declarations.end())
error("A subsample statement has not been issued for " + *parameter);
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 subsample_name;
}
void
ParsingDriver::set_prior(string *name)
{

View File

@ -84,6 +84,9 @@ private:
//! Checks that a given symbol exists and is a parameter, and stops with an error message if it isn't
void check_symbol_is_parameter(string *name);
//! Checks that a given symbol was assigned within a Statement
void check_symbol_is_statement_variable(string *name);
//! Checks that a given symbol exists and is a endogenous or exogenous, and stops with an error message if it isn't
void check_symbol_is_endogenous_or_exogenous(string *name);
@ -186,6 +189,12 @@ private:
vector<int> declared_nonstationary_vars;
//! Temporary storage for a variance declared in the prior statement
expr_t prior_variance;
//! 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;
subsample_declarations_t subsample_declarations;
//! reset the values for temporary storage
void reset_current_external_function_options();
//! Adds a model lagged variable to ModelTree and VariableTable
@ -239,6 +248,14 @@ public:
void declare_exogenous_det(string *name, string *tex_name = NULL);
//! Declares a parameter
void declare_parameter(string *name, string *tex_name = NULL);
//! Declares a statement local variable
void declare_statement_local_variable(string *name);
//! Completes a subsample statement
void set_subsamples(string *name);
//! 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);
//! 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
@ -375,8 +392,6 @@ public:
void set_std_prior(string *arg);
//! Sets the prior for estimated correlation
void set_corr_prior(string *arg1, string *arg2);
//! Sets the subsamples for a parameter
void set_subsamples(string *arg);
//! Runs estimation process
void run_estimation();
//! Runs dynare_sensitivy()