Commit 4e180bd3 authored by michel's avatar michel
Browse files

v4 parser.src: added deterministic shocks

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@722 ac1d8469-bf42-47a9-8791-bf33cf982152
parent 475372a5
......@@ -20,7 +20,6 @@
#endif
%}
/* %pure_parser */
%token POUND_SIGN
%token AR AUTOCORR
%token BAYESIAN_IRF BETA_PDF
%token CALIB CALIB_VAR CHECK CONF_SIG CORR COVAR
......@@ -39,7 +38,7 @@
%token PARAMETERS PERIODS PREFILTER PRESAMPLE PRINT PRIOR_TRUNC FILTER_STEP_AHEAD
%token QZ_CRITERIUM
%token RELATIVE_IRF REPLIC RESOL RPLOT
%token SHOCKS SHOCK_SIZE SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED SMOOTHER SOLVE_ALGO STDERR STEADY STOCH_SIMUL
%token SHOCKS SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED SMOOTHER SOLVE_ALGO STDERR STEADY STOCH_SIMUL
%token TEX TEX_NAME
%token UNIFORM_PDF UNIT_ROOT_VARS USE_DLL
%token VALUES VAR VAREXO VAREXO_DET VAROBS
......@@ -360,7 +359,7 @@
{$$ = _parser->add_sqrt($3);}
;
pound_expression: POUND_SIGN NAME
pound_expression: '#' NAME
{$$ = _parser->add_local_parameter($2);}
EQUAL hand_side ';'
{$$ = _parser->init_local_parameter($3,$5);}
......@@ -373,7 +372,11 @@
;
shocks
: SHOCKS ';' {_parser->begin_shocks();} shock_list END
: SHOCKS ';' {_parser->begin_shocks();} shock_list END {_parser->end_shocks();}
;
mshocks
: MSHOCKS ';' {_parser->begin_mshocks();} shock_list END {_parser->end_shocks();}
;
shock_list
......
......@@ -124,7 +124,6 @@ int sigma_e = 0;
<DYNARE_STATEMENT>filter_step_ahead {return FILTER_STEP_AHEAD;}
<DYNARE_STATEMENT>relative_irf {return RELATIVE_IRF;}
<DYNARE_STATEMENT>tex {return TEX;}
<DYNARE_STATEMENT>shock_size {return SHOCK_SIZE;}
<DYNARE_STATEMENT>moments {return MOMENTS;}
<DYNARE_STATEMENT>nomoments {return NOMOMENTS;}
<DYNARE_STATEMENT>corr {return CORR;}
......@@ -240,9 +239,10 @@ int sigma_e = 0;
_scanner->do_as_is(yytext);
}
}
<DYNARE_BLOCK># {return POUND_SIGN;}
<INITIAL>. {BEGIN NATIVE; _scanner->do_as_is(yytext);}
/* NATIVE Block */
<NATIVE>.* {BEGIN INITIAL;_scanner->do_as_is(yytext);_scanner->do_as_is("\n");}
<*>. {return yytext[0];}
......@@ -80,7 +80,9 @@ dynare::Objects* dynare::parser::add_variable(Objects* var)
{
//cout << "add_variable1 : " << var->symbol << endl;
var = get_symbol(var);
if((var->type == eEndogenous) || (var->type == eExogenous))
if((var->type == eEndogenous)
|| (var->type == eExogenous)
|| (var->type == eExogenousDet))
variable_table.AddVariable(var->symbol,0);
//cout << "add_model_token : " << var->ID << endl;
NodeID id = model_tree.AddTerminal(var->symbol);
......@@ -309,15 +311,33 @@ void dynare::parser::begin_shocks(void)
{
shocks.BeginShocks();
}
void dynare::parser::begin_mshocks(void)
{
shocks.BeginMShocks();
}
void dynare::parser::end_shocks(void)
{
shocks.EndShocks();
}
void dynare::parser::add_det_shock(Objects* var)
{
if (!symbol_table.Exist(var->symbol))
{
string msg = "Unknown symbol : "+var->symbol;
error(msg.c_str());
}
int id = symbol_table.getID(var->symbol);
shocks.AddShock(eDeterministic, id);
if (!symbol_table.Exist(var->symbol))
{
string msg = "Unknown symbol : "+var->symbol;
error(msg.c_str());
}
int id = symbol_table.getID(var->symbol);
switch (symbol_table.getType(var->symbol))
{
case eExogenous:
shocks.AddDetShockExo(id);
return;
case eExogenousDet:
shocks.AddDetShockExoDet(id);
return;
default:
error("Shocks can only be applied to exogenous variables");
}
}
void dynare::parser::add_stderr_shock(Objects* var, Objects* value)
{
......@@ -327,7 +347,7 @@ void dynare::parser::add_stderr_shock(Objects* var, Objects* value)
error(msg.c_str());
}
int id = symbol_table.getID(var->symbol);
shocks.AddShock(eSTDerror, id, 0, value->symbol);
shocks.AddSTDShock(id, value->symbol);
}
void dynare::parser::add_var_shock(Objects* var, Objects* value)
{
......@@ -337,7 +357,7 @@ void dynare::parser::add_var_shock(Objects* var, Objects* value)
error(msg.c_str());
}
int id = symbol_table.getID(var->symbol);
shocks.AddShock(eVariance, id, 0, value->symbol);
shocks.AddVARShock(id, value->symbol);
}
void dynare::parser::add_covar_shock(Objects* var1, Objects* var2, Objects* value)
{
......@@ -353,7 +373,7 @@ void dynare::parser::add_covar_shock(Objects* var1, Objects* var2, Objects* valu
}
int id1 = symbol_table.getID(var1->symbol);
int id2 = symbol_table.getID(var2->symbol);
shocks.AddShock(eCovariance, id1, id2, value->symbol);
shocks.AddCOVAShock(id1, id2, value->symbol);
}
void dynare::parser::add_correl_shock(Objects* var1, Objects* var2, Objects* value)
{
......@@ -369,7 +389,7 @@ void dynare::parser::add_correl_shock(Objects* var1, Objects* var2, Objects* val
}
int id1 = symbol_table.getID(var1->symbol);
int id2 = symbol_table.getID(var2->symbol);
shocks.AddShock(eCorrelation, id1, id2, value->symbol);
shocks.AddCORRShock(id1, id2, value->symbol);
}
void dynare::parser::add_period(Objects* p1, Objects* p2)
{
......
......@@ -13,6 +13,7 @@ int ModelParameters::eq_nbr = 0;
int ModelParameters::exo_nbr = 0;
int ModelParameters::var_exo_nbr = 0;
int ModelParameters::exo_det_nbr = 0;
int ModelParameters::var_exo_det_nbr = 0;
int ModelParameters::endo_nbr = 0;
int ModelParameters::var_endo_nbr = 0;
int ModelParameters::parameter_nbr = 0;
......
......@@ -18,6 +18,7 @@ using namespace std;
#include "DynareBison.h"
#include "NumericalConstants.h"
#include "ModelTree.h"
#include "ModelParameters.h"
//------------------------------------------------------------------------------
ostringstream ModelTree::output;
//------------------------------------------------------------------------------
......@@ -1283,7 +1284,8 @@ inline string ModelTree::getArgument(NodeID id, Type type, EquationType iEquatio
}
else if (type == eExogenousDet)
{
argument << "exedet_" << lpar << idx << rpar;
idx += ModelParameters::exo_nbr;
argument << "x" << lpar << idx << rpar;
}
}
else
......@@ -1324,30 +1326,30 @@ inline string ModelTree::getArgument(NodeID id, Type type, EquationType iEquatio
}
else if (type == eExogenousDet)
{
int idx = VariableTable::getSymbolID((int) id)+offset;
int idx = VariableTable::getSymbolID((int) id)+ModelParameters::exo_nbr+offset;
int lag = VariableTable::getLag((int) id);
if (offset == 1)
{
if (lag != 0)
{
argument << "exdet_" << lpar << "it_ + " << lag
argument << "x" << lpar << "it_ + " << lag
<< ", " << idx << rpar;
}
else
{
argument << "exdet_" << lpar << "it_, " << idx << rpar;
argument << "x" << lpar << "it_, " << idx << rpar;
}
}
else
{
if (lag != 0)
{
argument << "exdet_" << lpar << "it_ + " << lag
argument << "x" << lpar << "it_ + " << lag
<< "+" << idx << "*nb_row_xd" << rpar;
}
else
{
argument << "exdet_" << lpar << "it_+" << idx << "*nb_row_xd" << rpar;
argument << "x" << lpar << "it_+" << idx << "*nb_row_xd" << rpar;
}
}
......@@ -1457,13 +1459,13 @@ void ModelTree::ModelInitialization(void)
{
output << "M_.maximum_exo_det_lag = " << ModelParameters::max_exo_det_lag << ";\n";
output << "M_.maximum_exo_det_lead = " << ModelParameters::max_exo_det_lead<< ";\n";
output << "oo_.exo_det_steadystate = zeros(" << ModelParameters::exo_det_nbr << ", 1);\n";
output << "oo_.exo_det_steady_state = zeros(" << ModelParameters::exo_det_nbr << ", 1);\n";
}
if (ModelParameters::recur_nbr)
{
output << "M_.maximum_recur_lag = " << ModelParameters::max_recur_lag << ";\n";
output << "M_.maximum_recur_lead = " << ModelParameters::max_recur_lead<< ";\n";
output << "oo_.recur_steadystate = zeros(" << ModelParameters::recur_nbr << ", 1);\n";
output << "oo_.recur_steady_state = zeros(" << ModelParameters::recur_nbr << ", 1);\n";
}
if (ModelParameters::parameter_nbr)
{
......
......@@ -105,9 +105,12 @@ void NumericalInitialization::SetInit (string name, string expression)
{
*output << "oo_.exo_steady_state( " << id+1 << " ) = " << expression << ";\n";
}
else if (type == eExogenousDet)
{
*output << "oo_.exo_det_steady_state( " << id+1 << " ) = " << expression << ";\n";
}
// Testing if symbol is a variable (Exogenousous deterministic or recursive)
else if ((type != eExogenousDet) ||
(type != eRecursiveVariable))
else if ( type != eRecursiveVariable )
{
cout << "Error : Non-variable symbol used in INITVAL: " << name << endl;
}
......@@ -119,6 +122,9 @@ void NumericalInitialization::EndInitval(void)
*output << "if M_.exo_nbr > 0;\n";
*output << "\too_.exo_simul = [ones(M_.maximum_lag,1)*oo_.exo_steady_state'];\n";
*output <<"end;\n";
*output << "if M_.exo_det_nbr > 0;\n";
*output << "\too_.exo_det_simul = [ones(M_.maximum_lag,1)*oo_.exo_det_steady_state'];\n";
*output <<"end;\n";
}
//------------------------------------------------------------------------------
void NumericalInitialization::BeginEndval (void)
......@@ -134,10 +140,12 @@ void NumericalInitialization::EndEndval (void)
{
*output << "oo_.y_simul = [oo_.y_simul oo_.steady_state*ones(1,M_.maximum_lead+M_.maximum_lead)];\n";
*output << "if M_.exo_nbr > 0;\n";
*output << "\too_.exo_steady_state = [ones(M_.maximum_lag,1)*ex0_';ones(M_.maximum_lead+M_.maximum_lead,1)*oo_.exo_steady_state'];\n";
*output << "end;\n";
*output << "if M_.exo_nbr > 0;\n";
*output << "\too_.exo_simul = [ones(M_.maximum_lag,1)*oo_.exo_steady_state'];\n";
*output <<"end;\n";
*output << "if M_.exo_det_nbr > 0;\n";
*output << "\too_.exo_det_simul = [ones(M_.maximum_lag,1)*oo_.exo_det_steady_state'];\n";
*output <<"end;\n";
}
//------------------------------------------------------------------------------
void NumericalInitialization::BeginHistval (void)
......
......@@ -9,6 +9,10 @@ using namespace std;
//------------------------------------------------------------------------------
#include "Shocks.h"
#include "ModelParameters.h"
static int mshock_flag = 0;
static int exo_det_length = 0;
//------------------------------------------------------------------------------
//ostringstream Shocks::output;
//------------------------------------------------------------------------------
......@@ -29,86 +33,128 @@ void Shocks::setOutput(ostringstream* iOutput)
//------------------------------------------------------------------------------
void Shocks::BeginShocks(void)
{
// Writing a Matlab comment
*output << "%\n% (M)SHOCKS instructions \n%\n";
//Writing intstruction that initialize a shocks
*output << "make_ex_;\n";
mshock_flag = 0;
// Writing a Matlab comment
*output << "%\n% SHOCKS instructions \n%\n";
//Writing intstruction that initialize a shocks
*output << "make_ex_;\n";
}
//------------------------------------------------------------------------------
void Shocks::BeginMShocks(void)
{
mshock_flag = 1;
// Writing a Matlab comment
*output << "%\n% MSHOCKS instructions \n%\n";
//Writing intstruction that initialize a shocks
*output << "make_ex_;\n";
}
void Shocks::EndShocks(void)
{
*output << "M_.exo_det_length = " << exo_det_length << ";\n";
}
//------------------------------------------------------------------------------
void Shocks::AddShock(shocktype type, int id1, int id2, string value)
void Shocks::AddDetShockExo(int id1)
{
Shock shock;
switch (type)
if (mPeriod1.size() != mPeriod2.size() ||
mPeriod1.size() != mValues.size())
{
string msg = "in shocks statement, number of periods and values dont agree";
(* error) (msg.c_str());
}
for (unsigned int i = 0; i < mPeriod1.size(); i++)
{
string period1 = mPeriod1[i];
string period2 = mPeriod2[i];
if (period1 == period2)
{
case eDeterministic :
if (mPeriod1.size() != mPeriod2.size() ||
mPeriod1.size() != mValues.size())
{
string msg = "in shocks statement, number of periods and values dont agree";
(* error) (msg.c_str());
}
for (unsigned int i = 0; i < mPeriod1.size(); i++)
{
string period1 = mPeriod1[i];
string period2 = mPeriod2[i];
if (ModelParameters::exo_det_nbr > 0)
if (period1 == period2)
{
*output << "ex_det_(M_.maximum_lag + " << period1 <<
", " << id1+1 << ") = " << mValues[i] << ";\n";
}
else
{
*output << "ex_det_(" << period1 << ":" << period2 <<
", " << id1+1 << ") = repmat(" << mValues[i] << ", " <<
period2 << "-" << period1 << "+1, 1);\n";
}
if (ModelParameters::exo_det_nbr == 0)
if (period1 == period2)
{
*output << "oo_.exo_simul(M_.maximum_lag+" << period1 << ", " << id1+1 <<
") = " << mValues[i] << ";\n";
}
else
{
*output << "oo_.exo_simul(" << period1 << ":" << period2 << ", " <<
id1+1 << ") = repmat(" << mValues[i] << ", " << period2 << "-" <<
period1 << "+1,1);\n";
}
}
break;
case eSTDerror :
*output << "M_.Sigma_e(" << id1+1 << ", " << id1+1 << ") = " << value << "^2;\n";
break;
case eVariance :
*output << "M_.Sigma_e(" << id1+1 << ", " << id1+1 << ") = " << value << ";\n";
break;
case eCovariance :
*output << "M_.Sigma_e(" << id1+1 << ", " << id2+1 << ") = " << value <<
"; M_.Sigma_e(" << id2+1 << ", " << id1+1 << ") = M_.Sigma_e(" <<
id1+1 << ", " << id2+1 << ");\n";
break;
case eCorrelation :
*output << "M_.Sigma_e(" << id1+1 << ", " << id2+1 << ") = " <<
value << "*sqrt(M_.Sigma_e(" << id1 << ", " << id1+1 << ")*M_.Sigma_e(" <<
id2 << ", " << id2+1 << "); M_.Sigma_e(" << id2+1 << ", " <<
id1 << ") = M_.Sigma_e(" << id1+1 << ", " << id2+1 << ");\n";
break;
default :
;
}
mPeriod1.clear();
mPeriod2.clear();
mValues.clear();
/*
sets a shock
for type == 0, set type, id1, shock_elem will be set by another method
for type == 1 or 2 set type, id1 and value
for type == 3 or 4 set type, id1, id2 and value
*/
*output << "set_shocks(" << mshock_flag << "," << period1
<< ", " << id1+1 << ", " << mValues[i]
<< ");\n";
}
else
{
*output << "set_shocks(" << mshock_flag << "," << period1
<< ":" << period2 << ", " << id1+1
<< ", " << mValues[i] << ");\n";
}
}
mPeriod1.clear();
mPeriod2.clear();
mValues.clear();
}
void Shocks::AddDetShockExoDet(int id1)
{
if (mPeriod1.size() != mPeriod2.size() ||
mPeriod1.size() != mValues.size())
{
string msg = "in shocks statement, number of periods and values dont agree";
(* error) (msg.c_str());
}
for (unsigned int i = 0; i < mPeriod1.size(); i++)
{
string period1 = mPeriod1[i];
string period2 = mPeriod2[i];
if (period1 == period2)
{
*output << "set_shocks(" << mshock_flag + 2 << "," << period1
<< ", " << id1+1 << ", " << mValues[i]
<< ");\n";
}
else
{
*output << "set_shocks(" << mshock_flag+2 << "," << period1
<< ":" << period2 << ", " << id1+1
<< ", " << mValues[i] << ");\n";
}
int p2_int = atoi(period2.c_str());
if (p2_int > exo_det_length)
{
exo_det_length = p2_int;
}
}
mPeriod1.clear();
mPeriod2.clear();
mValues.clear();
}
void Shocks::AddSTDShock(int id1, std::string value)
{
*output << "M_.Sigma_e(" << id1+1 << ", " << id1+1 << ") = " << value << "^2;\n";
mPeriod1.clear();
mPeriod2.clear();
mValues.clear();
}
void Shocks::AddVARShock(int id1, std::string value)
{
*output << "M_.Sigma_e(" << id1+1 << ", " << id1+1 << ") = " << value << ";\n";
mPeriod1.clear();
mPeriod2.clear();
mValues.clear();
}
void Shocks::AddCOVAShock(int id1, int id2 , std::string value)
{
*output << "M_.Sigma_e(" << id1+1 << ", " << id2+1 << ") = " << value <<
"; M_.Sigma_e(" << id2+1 << ", " << id1+1 << ") = M_.Sigma_e(" <<
id1+1 << ", " << id2+1 << ");\n";
mPeriod1.clear();
mPeriod2.clear();
mValues.clear();
}
void Shocks::AddCORRShock(int id1, int id2 , std::string value)
{
*output << "M_.Sigma_e(" << id1+1 << ", " << id2+1 << ") = " <<
value << "*sqrt(M_.Sigma_e(" << id1 << ", " << id1+1 << ")*M_.Sigma_e(" <<
id2 << ", " << id2+1 << "); M_.Sigma_e(" << id2+1 << ", " <<
id1 << ") = M_.Sigma_e(" << id1+1 << ", " << id2+1 << ");\n";
mPeriod1.clear();
mPeriod2.clear();
mValues.clear();
}
/*
sets a shock
for type == 0, set type, id1, shock_elem will be set by another method
for type == 1 or 2 set type, id1 and value
for type == 3 or 4 set type, id1, id2 and value
*/
//------------------------------------------------------------------------------
/*
string Shocks::get(void)
......
......@@ -61,6 +61,7 @@ int VariableTable::AddVariable(string iName, int iLag)
Type type = getType(lVariableID);
if (type == eEndogenous) ModelParameters::var_endo_nbr++;
if (type == eExogenous) ModelParameters::var_exo_nbr++;
if (type == eExogenousDet) ModelParameters::var_exo_det_nbr++;
// Setting Maximum and minimum lags
if (ModelParameters::max_lead < iLag)
{
......
......@@ -177,6 +177,8 @@ namespace dynare
void begin_histval(void);
/*! Write begining of a shock block */
void begin_shocks(void);
void begin_mshocks(void);
void end_shocks(void);
/*! Adds a deterministic chock */
void add_det_shock(Objects* var);
/*! Adds a std error chock */
......
......@@ -28,6 +28,7 @@ class ModelParameters
static int var_exo_nbr;
/*! Number of deterministic Exogenous variables */
static int exo_det_nbr;
static int var_exo_det_nbr;
/*! Number of declared Endogenous variables */
static int endo_nbr;
/*! Number of Endogenous variables that apear in model equations*/
......
......@@ -11,16 +11,7 @@
#include <sstream>
#include <list>
#include <vector>
//------------------------------------------------------------------------------
/*! Shock type enum */
enum shocktype
{
eDeterministic = 0, //!< Deterministic shock
eSTDerror = 1, //!< STD error shock
eVariance = 2, //!< Variance shock
eCovariance = 3, //!< Covariance shock
eCorrelation = 4 //!< Corelated shock
};
//------------------------------------------------------------------------------
/*!
\class Shocks
......@@ -29,56 +20,63 @@ enum shocktype
class Shocks
{
private :
/*!
\class ShockElement
\brief Shock element strcuture
*/
struct ShockElement{
std::string period1;
std::string period2;
std::string value;
};
/*!
int mshock_flag;
/*!
\class ShockElement
\brief Shock element strcuture
*/
struct ShockElement{
std::string period1;
std::string period2;
std::string value;
};
/*!
\class ShockElement
\brief Shock Structure
*/
struct Shock{
int id1;
int id2;
shocktype type;
std::list<ShockElement> shock_elems;
std::string value;
};
/*! Output string of this class */
std::ostringstream *output;
/*! Vector of begin period range */
std::vector<std::string> mPeriod1;
/*! vector of end period range */
std::vector<std::string> mPeriod2;
/*! vector of shock values */
std::vector<std::string> mValues;
public :
/*! Constructor */
Shocks();
/*! Destructor */
~Shocks();
/*! Pointer to error function of parser class */
void (* error) (const char* m);
/*!
Set output reference
\param iOutput : reference to an ostringstream
*/
void setOutput(std::ostringstream* iOutput);
/*! Initialize shocks (or mshocks, for multiplicative shocks) block */
void BeginShocks(void);
/*! Sets a shock */
void AddShock(shocktype type, int id1, int id2 = 0, std::string value = "");
/*! Adds a period rage */
void AddPeriod(std::string p1, std::string p2);
/*! Adds a period */
void AddPeriod(std::string p1);
/*! Adds a value */
void AddValue(std::string value);
*/
struct Shock{
int id1;
int id2;
std::list<ShockElement> shock_elems;
std::string value;
};
/*! Output string of this class */
std::ostringstream *output;
/*! Vector of begin period range */
std::vector<std::string> mPeriod1;
/*! vector of end period range */
std::vector<std::string> mPeriod2;
/*! vector of shock values */
std::vector<std::string> mValues;
public :