var_model: allow non model-block variables in VAR

issue#70
Houtan Bastani 2017-05-04 15:50:33 +02:00
parent d7336eb317
commit a2771a62ad
8 changed files with 42 additions and 21 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2007-2016 Dynare Team
* Copyright (C) 2007-2017 Dynare Team
*
* This file is part of Dynare.
*
@ -152,7 +152,8 @@ enum SymbolType
eTrend = 13, //!< Trend variable
eStatementDeclaredVariable = 14, //!< Local variable assigned within a Statement (see subsample statement for example)
eLogTrend = 15, //!< Log-trend variable
eUnusedEndogenous = 16
eUnusedEndogenous = 16,
eEndogenousVAR = 17 //!< Variables declared in a var_model statement
};
enum ExpressionType

View File

@ -173,12 +173,10 @@ PriorPosteriorFunctionStatement::writeOutput(ostream &output, const string &base
VarModelStatement::VarModelStatement(const SymbolList &symbol_list_arg,
const OptionsList &options_list_arg,
const string &name_arg,
const SymbolTable &symbol_table_arg) :
const string &name_arg) :
symbol_list(symbol_list_arg),
options_list(options_list_arg),
name(name_arg),
symbol_table(symbol_table_arg)
name(name_arg)
{
}
@ -191,14 +189,6 @@ VarModelStatement::getVarModelNameAndVarList(map<string, pair<SymbolList, int> >
void
VarModelStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
{
vector<string> symbols = symbol_list.get_symbols();
for (vector<string>::const_iterator it = symbols.begin(); it != symbols.end(); it++)
if (symbol_table.getType(*it) != eEndogenous)
{
cerr << "ERROR: You can only run VARs on endogenous variables." << endl;
exit(EXIT_FAILURE);
}
OptionsList::num_options_t::const_iterator it = options_list.num_options.find("var.order");
if (it == options_list.num_options.end())
{

View File

@ -116,12 +116,10 @@ private:
const SymbolList symbol_list;
const OptionsList options_list;
const string &name;
const SymbolTable &symbol_table;
public:
VarModelStatement(const SymbolList &symbol_list_arg,
const OptionsList &options_list_arg,
const string &name_arg,
const SymbolTable &symbol_table_arg);
const string &name_arg);
void getVarModelNameAndVarList(map<string, pair<SymbolList, int> > &var_model_info);
virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const;

View File

@ -3240,8 +3240,8 @@ DynamicModel::addEquationsForVar(map<string, pair<SymbolList, int> > var_model_i
map<string, int>::const_iterator it1 = model_endos_and_lags.find(it->first);
if (it1 == model_endos_and_lags.end())
{
cerr << "ERROR: Variable used in var that is not used in the model: " << it->first << endl;
exit(EXIT_FAILURE);
cerr << "WARNING: Variable used in var that is not used in the model: " << it->first << endl;
// exit(EXIT_FAILURE);
}
else
if (it->second < it1->second)

View File

@ -349,7 +349,15 @@ var : VAR var_list ';'
{ driver.end_nonstationary_var(true, $6); }
;
var_model : VAR_MODEL '(' var_model_options_list ')' symbol_list ';' { driver.var_model(); } ;
var_model : VAR_MODEL '(' var_model_options_list ')' var_symbol_list ';' { driver.var_model(); } ;
var_symbol_list : var_symbol_list symbol
{ driver.declare_var_endogenous($2); }
| var_symbol_list COMMA symbol
{ driver.declare_var_endogenous($3); }
| symbol
{ driver.declare_var_endogenous($1); }
;
var_model_options_list : var_model_options_list COMMA var_model_options
| var_model_options

View File

@ -581,6 +581,7 @@ VariableNode::prepareForDerivation()
// Such a variable is never derived
break;
case eExternalFunction:
case eEndogenousVAR:
cerr << "VariableNode::prepareForDerivation: impossible case" << endl;
exit(EXIT_FAILURE);
}
@ -613,6 +614,7 @@ VariableNode::computeDerivative(int deriv_id)
cerr << "eUnusedEndogenous is not derivable" << endl;
exit(EXIT_FAILURE);
case eExternalFunction:
case eEndogenousVAR:
cerr << "Impossible case!" << endl;
exit(EXIT_FAILURE);
}
@ -865,6 +867,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
case eLogTrend:
case eStatementDeclaredVariable:
case eUnusedEndogenous:
case eEndogenousVAR:
cerr << "Impossible case" << endl;
exit(EXIT_FAILURE);
}
@ -1071,6 +1074,7 @@ VariableNode::getChainRuleDerivative(int deriv_id, const map<int, expr_t> &recur
cerr << "eUnusedEndogenous is not derivable" << endl;
exit(EXIT_FAILURE);
case eExternalFunction:
case eEndogenousVAR:
cerr << "Impossible case!" << endl;
exit(EXIT_FAILURE);
}
@ -1108,6 +1112,7 @@ VariableNode::computeXrefs(EquationInfo &ei) const
case eStatementDeclaredVariable:
case eUnusedEndogenous:
case eExternalFunction:
case eEndogenousVAR:
break;
}
}

View File

@ -195,6 +195,23 @@ ParsingDriver::declare_endogenous(string *name, string *tex_name, vector<pair<st
}
}
void
ParsingDriver::declare_var_endogenous(string *name)
{
if (mod_file->symbol_table.exists(*name))
{
SymbolType type = mod_file->symbol_table.getType(*name);
if (type != eEndogenous && type != eExogenous && type != eExogenousDet)
error("Symbol " + *name + " used in a VAR must be either endogenous or " +
"exogenous if it is also used elsewhere in the .mod file");
add_in_symbol_list(name);
return;
}
declare_symbol(name, eEndogenousVAR, NULL, NULL);
add_in_symbol_list(name);
}
void
ParsingDriver::declare_exogenous(string *name, string *tex_name, vector<pair<string *, string *> *> *partition_value)
{
@ -1286,7 +1303,7 @@ ParsingDriver::var_model()
if (it == options_list.string_options.end())
error("You must pass the model_name option to the var_model statement.");
const string *name = new string(it->second);
mod_file->addStatement(new VarModelStatement(symbol_list, options_list, *name, mod_file->symbol_table));
mod_file->addStatement(new VarModelStatement(symbol_list, options_list, *name));
symbol_list.clear();
options_list.clear();
}

View File

@ -301,6 +301,8 @@ public:
void declare_exogenous_det(string *name, string *tex_name = NULL, vector<pair<string *, string *> *> *partition_value = NULL);
//! Declares a parameter
void declare_parameter(string *name, string *tex_name = NULL, vector<pair<string *, string *> *> *partition_value = NULL);
//! Declares a VAR variable and adds to symbol_list
void declare_var_endogenous(string *name);
//! Declares a statement local variable
void declare_statement_local_variable(string *name);
//! Completes a subsample statement