removed changes linked with transforming variables to avoid log or

power of negative numbers
issue#70
Michel Juillard 2013-04-09 16:35:57 +02:00
parent 91f3f578a0
commit 24cac29cdf
10 changed files with 2 additions and 244 deletions

View File

@ -4052,38 +4052,6 @@ DynamicModel::substituteExpectation(bool partial_information_model)
}
}
void
DynamicModel::substituteLogPow(void)
{
ExprNode::subst_table_t subst_table;
vector<BinaryOpNode *> neweqs1, neweqs2;
// Substitute in model local variables
for (map<int, expr_t>::iterator it = local_variables_table.begin();
it != local_variables_table.end(); it++)
it->second = it->second->substituteLogPow(subst_table, neweqs1, neweqs2);
// Substitute in equations
for (int i = 0; i < (int) equations.size(); i++)
{
BinaryOpNode *substeq = dynamic_cast<BinaryOpNode *>(equations[i]->substituteLogPow(subst_table, neweqs1, neweqs2));
assert(substeq != NULL);
equations[i] = substeq;
}
// Add new equations
for (int i = 0; i < (int) neweqs1.size(); i++)
addEquation(neweqs1[i]);
// Add the new set of equations at the *beginning* of aux_equations
copy(neweqs2.rbegin(), neweqs2.rend(), front_inserter(aux_equations));
if (subst_table.size() > 0)
{
cout << "Adding auxiliary variables for log and pow expressions: added " << neweqs1.size() << " auxiliary variables and equations." << endl;
}
}
void
DynamicModel::transformPredeterminedVariables()
{

View File

@ -258,9 +258,6 @@ public:
//! Transforms the model by removing all oExpectation
void substituteExpectation(bool partial_information_model);
//! Transform the model by add auxiliary variables for log and pow expressions
void substituteLogPow(void);
//! Transforms the model by decreasing the lead/lag of predetermined variables in model equations by one
void transformPredeterminedVariables();

View File

@ -102,7 +102,7 @@ class ParsingDriver;
%token <string_val> FLOAT_NUMBER
%token DEFAULT FIXED_POINT
%token FORECAST K_ORDER_SOLVER INSTRUMENTS PRIOR SHIFT MEAN STDEV VARIANCE MODE INTERVAL SHAPE DOMAINN
%token GAMMA_PDF GRAPH GRAPH_FORMAT CONDITIONAL_VARIANCE_DECOMPOSITION NOCHECK TRANSFORM_LOGPOW STD
%token GAMMA_PDF GRAPH GRAPH_FORMAT CONDITIONAL_VARIANCE_DECOMPOSITION NOCHECK STD
%token HISTVAL HOMOTOPY_SETUP HOMOTOPY_MODE HOMOTOPY_STEPS HOMOTOPY_FORCE_CONTINUE HP_FILTER HP_NGRID
%token IDENTIFICATION INF_CONSTANT INITVAL INITVAL_FILE BOUNDS JSCALE INIT
%token <string_val> INT_NUMBER
@ -875,7 +875,6 @@ steady_options : o_solve_algo
| o_markowitz
| o_maxit
| o_nocheck
| TRANSFORM_LOGPOW { driver.transform_logpow(); };
;
check : CHECK ';'

View File

@ -450,7 +450,6 @@ string eofbuff;
<DYNARE_STATEMENT>homotopy_steps {return token::HOMOTOPY_STEPS; }
<DYNARE_STATEMENT>homotopy_force_continue {return token::HOMOTOPY_FORCE_CONTINUE;}
<DYNARE_STATEMENT>nocheck {return token::NOCHECK; }
<DYNARE_STATEMENT>transform_logpow {return token::TRANSFORM_LOGPOW; }
<DYNARE_STATEMENT>controlled_varexo {return token::CONTROLLED_VAREXO; }
<DYNARE_STATEMENT>parameter_set {return token::PARAMETER_SET; }

View File

@ -430,12 +430,6 @@ NumConstNode::substituteExpectation(subst_table_t &subst_table, vector<BinaryOpN
return const_cast<NumConstNode *>(this);
}
expr_t
NumConstNode::substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const
{
return const_cast<NumConstNode *>(this);
}
bool
NumConstNode::isNumConstNodeEqualTo(double value) const
{
@ -1218,12 +1212,6 @@ VariableNode::substituteExpectation(subst_table_t &subst_table, vector<BinaryOpN
return const_cast<VariableNode *>(this);
}
expr_t
VariableNode::substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const
{
return const_cast<VariableNode *>(this);
}
bool
VariableNode::isNumConstNodeEqualTo(double value) const
{
@ -2295,92 +2283,6 @@ UnaryOpNode::substituteExpectation(subst_table_t &subst_table, vector<BinaryOpNo
}
}
expr_t
UnaryOpNode::substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const
{
if (op_code==oLog && this->containsEndogenous() )
{
subst_table_t::iterator it = subst_table.find(const_cast<UnaryOpNode *>(this));
if (it != subst_table.end())
return const_cast<VariableNode *>(it->second);
//take care of any nested log expressions by calling arg->substituteLogPow(.), then decreaseLeadsLags for this oExpectation operator
//arg(lag-period) (holds entire subtree of arg(lag-period)
expr_t substexpr = arg->substituteLogPow(subst_table, neweqs1, neweqs2);
assert(substexpr != NULL);
int k = substexpr->maxLead();
if (k)
substexpr = substexpr->decreaseLeadsLags(k);
it = subst_table.find(substexpr);
if (it != subst_table.end())
return (it->second)->decreaseLeadsLags(-k);
//Arriving here, we need to create an auxiliary variable for the argument of the log expression:
//AUX_LOG_(arg.idx)
int symb_id = datatree.symbol_table.addLogAuxiliaryVar(arg->idx);
expr_t newAuxE = datatree.AddVariable(symb_id, 0);
assert(dynamic_cast<VariableNode *>(newAuxE) != NULL);
subst_table[substexpr] = dynamic_cast<VariableNode *>(newAuxE);
// auxiliary equation with the exponential of the auxiliary variable
expr_t lhs = datatree.AddExp(newAuxE);
neweqs1.push_back(dynamic_cast<BinaryOpNode *>(datatree.AddEqual(lhs, substexpr)));
// definition of the auxiliary variable to be used in initval and steadystate files
expr_t definition = datatree.AddLog(substexpr);
neweqs2.push_back(dynamic_cast<BinaryOpNode *>(datatree.AddEqual(newAuxE,definition)));
return newAuxE->decreaseLeadsLags(-k);
}
else if (op_code==oSqrt && this->containsEndogenous() )
{
subst_table_t::iterator it = subst_table.find(const_cast<UnaryOpNode *>(this));
if (it != subst_table.end())
{
// expression to be used instead of sqrt
expr_t constant = datatree.AddNonNegativeConstant("0.5");
return datatree.AddExp(datatree.AddTimes(constant,const_cast<VariableNode *>(it->second)));
}
//take care of any nested log expressions by calling arg->substituteLogPow(.), then decreaseLeadsLags for this oExpectation operator
//arg(lag-period) (holds entire subtree of arg(lag-period)
expr_t substexpr = arg->substituteLogPow(subst_table, neweqs1, neweqs2);
assert(substexpr != NULL);
int k = substexpr->maxLead();
if (k)
substexpr = substexpr->decreaseLeadsLags(k);
it = subst_table.find(substexpr);
if (it != subst_table.end())
{
// expression to be used instead of sqrt
expr_t constant = datatree.AddNonNegativeConstant("0.5");
return datatree.AddExp(datatree.AddTimes(constant,(it->second)->decreaseLeadsLags(-k)));
}
//Arriving here, we need to create an auxiliary variable for the argument of the log expression:
//AUX_LOG_(arg.idx)
int symb_id = datatree.symbol_table.addPowAuxiliaryVar(arg->idx);
expr_t newAuxE = datatree.AddVariable(symb_id, 0);
assert(dynamic_cast<VariableNode *>(newAuxE) != NULL);
subst_table[substexpr] = dynamic_cast<VariableNode *>(newAuxE);
// auxiliary equation with the exponential of the auxiliary variable
expr_t lhs = datatree.AddExp(newAuxE);
neweqs1.push_back(dynamic_cast<BinaryOpNode *>(datatree.AddEqual(lhs, substexpr)));
// definition of the auxiliary variable to be used in initval and steadystate files
expr_t definition = datatree.AddLog(substexpr);
neweqs2.push_back(dynamic_cast<BinaryOpNode *>(datatree.AddEqual(newAuxE,definition)));
// expression to be used instead of sqrt
expr_t constant = datatree.AddNonNegativeConstant("0.5");
return datatree.AddExp(datatree.AddTimes(constant,newAuxE->decreaseLeadsLags(-k)));
}
else
{
expr_t argsubst = arg->substituteLogPow(subst_table, neweqs1, neweqs2);
return buildSimilarUnaryOpNode(argsubst, datatree);
}
}
bool
UnaryOpNode::isNumConstNodeEqualTo(double value) const
{
@ -3606,68 +3508,6 @@ BinaryOpNode::substituteExpectation(subst_table_t &subst_table, vector<BinaryOpN
return buildSimilarBinaryOpNode(arg1subst, arg2subst, datatree);
}
expr_t
BinaryOpNode::substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const
{
if (op_code==oPower && this->containsEndogenous() )
{
NumConstNode *arg2_node = dynamic_cast<NumConstNode *>(arg2);
if (arg2_node != NULL)
{
// the power exponent is a numerical constant
double arg2_val = arg2_node->eval(eval_context_t());
if (arg2_val == round(arg2_val))
// the power exponent is an integer, no transformation
{
expr_t arg1subst = arg1->substituteLogPow(subst_table, neweqs1, neweqs2);
expr_t arg2subst = arg2->substituteLogPow(subst_table, neweqs1, neweqs2);
return buildSimilarBinaryOpNode(arg1subst, arg2subst, datatree);
}
}
subst_table_t::iterator it = subst_table.find(const_cast<BinaryOpNode *>(this));
if (it != subst_table.end())
return datatree.AddExp(datatree.AddTimes(arg2,const_cast<VariableNode *>(it->second)));
//take care of any nested pow expressions by calling arg->substituteLogPow(.),
//arg(lag-period) (holds entire subtree of arg(lag-period)
expr_t arg1substexpr = arg1->substituteLogPow(subst_table, neweqs1, neweqs2);
assert(arg1substexpr != NULL);
expr_t arg2substexpr = arg2->substituteLogPow(subst_table, neweqs1, neweqs2);
assert(arg2substexpr != NULL);
// look for identical expression at the current period
int k = arg1substexpr->maxLead();
if (k)
arg1substexpr = arg1substexpr->decreaseLeadsLags(k);
it = subst_table.find(arg1substexpr);
// if it exists return it with the appropriate lead/lag
if (it != subst_table.end())
return datatree.AddExp(datatree.AddTimes(arg2substexpr,(it->second)->decreaseLeadsLags(-k)));
// if not, create an auxiliary variable for the argument of the power expression:
//AUX_POW_(arg.idx)
int symb_id = datatree.symbol_table.addPowAuxiliaryVar(arg1->idx);
expr_t newAuxE = datatree.AddVariable(symb_id, 0);
assert(dynamic_cast<VariableNode *>(newAuxE) != NULL);
subst_table[arg1substexpr] = dynamic_cast<VariableNode *>(newAuxE);
// auxiliary equation with the exponential of the auxiliary variable
expr_t lhs = datatree.AddExp(newAuxE);
neweqs1.push_back(dynamic_cast<BinaryOpNode *>(datatree.AddEqual(lhs, arg1substexpr)));
// definition of the auxiliary variable to be used in initval and steadystate files
expr_t definition = datatree.AddLog(arg1substexpr);
neweqs2.push_back(dynamic_cast<BinaryOpNode *>(datatree.AddEqual(newAuxE,definition)));
// expression to be used instead of power
return datatree.AddExp(datatree.AddTimes(arg2substexpr,newAuxE->decreaseLeadsLags(-k)));
}
else
{
expr_t arg1subst = arg1->substituteLogPow(subst_table, neweqs1, neweqs2);
expr_t arg2subst = arg2->substituteLogPow(subst_table, neweqs1, neweqs2);
return buildSimilarBinaryOpNode(arg1subst, arg2subst, datatree);
}
}
expr_t
BinaryOpNode::addMultipliersToConstraints(int i)
{
@ -4270,15 +4110,6 @@ TrinaryOpNode::substituteExpectation(subst_table_t &subst_table, vector<BinaryOp
return buildSimilarTrinaryOpNode(arg1subst, arg2subst, arg3subst, datatree);
}
expr_t
TrinaryOpNode::substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const
{
expr_t arg1subst = arg1->substituteLogPow(subst_table, neweqs1, neweqs2);
expr_t arg2subst = arg2->substituteLogPow(subst_table, neweqs1, neweqs2);
expr_t arg3subst = arg3->substituteLogPow(subst_table, neweqs1, neweqs2);
return buildSimilarTrinaryOpNode(arg1subst, arg2subst, arg3subst, datatree);
}
bool
TrinaryOpNode::isNumConstNodeEqualTo(double value) const
{
@ -4829,15 +4660,6 @@ ExternalFunctionNode::substituteExpectation(subst_table_t &subst_table, vector<B
return buildSimilarExternalFunctionNode(arguments_subst, datatree);
}
expr_t
ExternalFunctionNode::substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const
{
vector<expr_t> arguments_subst;
for (vector<expr_t>::const_iterator it = arguments.begin(); it != arguments.end(); it++)
arguments_subst.push_back((*it)->substituteLogPow(subst_table, neweqs1, neweqs2));
return buildSimilarExternalFunctionNode(arguments_subst, datatree);
}
expr_t
ExternalFunctionNode::buildSimilarExternalFunctionNode(vector<expr_t> &alt_args, DataTree &alt_datatree) const
{

View File

@ -357,13 +357,6 @@ public:
*/
virtual expr_t substituteExpectation(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool partial_information_model) const = 0;
//! Constructs a new expression where log expression are replaced by an auxiliary variable and pow argument by an exponential expression
/*!
\param[in,out] subst_table Map used to store expressions that have already be substituted and their corresponding variable, in order to avoid creating two auxiliary variables for the same sub-expr.
\param[out] neweqs Equations to be added to the model to match the creation of auxiliary variables.
*/
virtual expr_t substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const = 0;
virtual expr_t decreaseLeadsLagsPredeterminedVariables() const = 0;
//! Return true if the nodeID is a numerical constant equal to value and false otherwise
@ -447,7 +440,6 @@ public:
virtual expr_t substituteExoLead(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool deterministic_model) const;
virtual expr_t substituteExoLag(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs) const;
virtual expr_t substituteExpectation(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool partial_information_model) const;
virtual expr_t substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const;
virtual expr_t decreaseLeadsLagsPredeterminedVariables() const;
virtual bool isNumConstNodeEqualTo(double value) const;
virtual bool containsEndogenous(void) const;
@ -508,7 +500,6 @@ public:
virtual expr_t substituteExoLead(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool deterministic_model) const;
virtual expr_t substituteExoLag(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs) const;
virtual expr_t substituteExpectation(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool partial_information_model) const;
virtual expr_t substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const;
virtual expr_t decreaseLeadsLagsPredeterminedVariables() const;
virtual bool isNumConstNodeEqualTo(double value) const;
virtual bool containsEndogenous(void) const;
@ -584,7 +575,6 @@ public:
virtual expr_t substituteExoLead(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool deterministic_model) const;
virtual expr_t substituteExoLag(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs) const;
virtual expr_t substituteExpectation(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool partial_information_model) const;
virtual expr_t substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const;
virtual expr_t decreaseLeadsLagsPredeterminedVariables() const;
virtual bool isNumConstNodeEqualTo(double value) const;
virtual bool containsEndogenous(void) const;
@ -673,7 +663,6 @@ public:
virtual expr_t substituteExoLead(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool deterministic_model) const;
virtual expr_t substituteExoLag(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs) const;
virtual expr_t substituteExpectation(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool partial_information_model) const;
virtual expr_t substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const;
virtual expr_t decreaseLeadsLagsPredeterminedVariables() const;
virtual bool isNumConstNodeEqualTo(double value) const;
virtual bool containsEndogenous(void) const;
@ -742,7 +731,6 @@ public:
virtual expr_t substituteExoLead(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool deterministic_model) const;
virtual expr_t substituteExoLag(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs) const;
virtual expr_t substituteExpectation(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool partial_information_model) const;
virtual expr_t substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const;
virtual expr_t decreaseLeadsLagsPredeterminedVariables() const;
virtual bool isNumConstNodeEqualTo(double value) const;
virtual bool containsEndogenous(void) const;
@ -814,7 +802,6 @@ public:
virtual expr_t substituteExoLead(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool deterministic_model) const;
virtual expr_t substituteExoLag(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs) const;
virtual expr_t substituteExpectation(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs, bool partial_information_model) const;
virtual expr_t substituteLogPow(subst_table_t &subst_table, vector<BinaryOpNode *> &neweqs1, vector<BinaryOpNode *> &neweqs2) const;
virtual expr_t buildSimilarExternalFunctionNode(vector<expr_t> &alt_args, DataTree &alt_datatree) const;
virtual expr_t decreaseLeadsLagsPredeterminedVariables() const;
virtual bool isNumConstNodeEqualTo(double value) const;

View File

@ -38,7 +38,7 @@ ModFile::ModFile(WarningConsolidation &warnings_arg)
static_model(symbol_table, num_constants, external_functions_table),
steady_state_model(symbol_table, num_constants, external_functions_table, static_model),
linear(false), block(false), byte_code(false), use_dll(false), no_static(false),
nonstationary_variables(false), transform_logpow(false), ramsey_policy_orig_eqn_nbr(0),
nonstationary_variables(false), ramsey_policy_orig_eqn_nbr(0),
warnings(warnings_arg)
{
}
@ -247,9 +247,6 @@ ModFile::transformPass()
// Create auxiliary vars for Expectation operator
dynamic_model.substituteExpectation(mod_file_struct.partial_information);
if (transform_logpow)
dynamic_model.substituteLogPow();
if (nonstationary_variables)
{
dynamic_model.detrendEquations();

View File

@ -78,9 +78,6 @@ public:
//! Are nonstationary variables present ?
bool nonstationary_variables;
//! Is the log and power functions should be transformed ?
bool transform_logpow;
//! Global evaluation context
/*! Filled using initval blocks and parameters initializations */
eval_context_t global_eval_context;

View File

@ -512,12 +512,6 @@ ParsingDriver::mfs(string *value)
delete value;
}
void
ParsingDriver::transform_logpow()
{
mod_file->transform_logpow = true;
}
void
ParsingDriver::end_initval(bool all_values_required)
{

View File

@ -247,8 +247,6 @@ public:
void cutoff(string *value);
//! mfs option of model block
void mfs(string *value);
//! transform_logpow for model
void transform_logpow(void);
//! Sets the FILENAME for the initial value in initval
void initval_file(string *filename);
//! Declares an endogenous variable