Merge branch 'master' of ssh://kirikou.dynare.org/srv/d_kirikou/git/dynare

time-shift
Michel Juillard 2010-03-15 12:05:33 +01:00
commit 195fdece71
4 changed files with 67 additions and 16 deletions

View File

@ -2656,6 +2656,20 @@ DynamicModel::computeParamsDerivatives()
continue;
jacobian_params_derivatives[make_pair(eq, make_pair(var, param))] = d2;
}
for (second_derivatives_type::const_iterator it2 = second_derivatives.begin();
it2 != second_derivatives.end(); it2++)
{
int eq = it2->first.first;
int var1 = it2->first.second.first;
int var2 = it2->first.second.second;
NodeID d1 = it2->second;
NodeID d2 = d1->getDerivative(param);
if (d2 == Zero)
continue;
hessian_params_derivatives[make_pair(eq, make_pair(var1, make_pair(var2, param)))] = d2;
}
}
}
@ -2678,7 +2692,8 @@ void
DynamicModel::writeParamsDerivativesFile(const string &basename) const
{
if (!residuals_params_derivatives.size()
&& !jacobian_params_derivatives.size())
&& !jacobian_params_derivatives.size()
&& !hessian_params_derivatives.size())
return;
string filename = basename + "_params_derivs.m";
@ -2735,6 +2750,29 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const
paramsDerivsFile << ";" << endl;
}
// Write hessian derivatives
paramsDerivsFile << "gp = zeros(" << equation_number() << ", " << dynJacobianColsNbr << ", "
<< dynJacobianColsNbr << ", " << symbol_table.param_nbr() << ");" << endl;
for (third_derivatives_type::const_iterator it = hessian_params_derivatives.begin();
it != hessian_params_derivatives.end(); it++)
{
int eq = it->first.first;
int var1 = it->first.second.first;
int var2 = it->first.second.second.first;
int param = it->first.second.second.second;
NodeID d2 = it->second;
int var1_col = getDynJacobianCol(var1) + 1;
int var2_col = getDynJacobianCol(var2) + 1;
int param_col = symbol_table.getTypeSpecificID(getSymbIDByDerivID(param)) + 1;
paramsDerivsFile << "hp(" << eq+1 << ", " << var1_col << ", " << var2_col << ", " << param_col << ") = ";
d2->writeOutput(paramsDerivsFile, oMatlabDynamicModel, params_derivs_temporary_terms);
paramsDerivsFile << ";" << endl;
}
paramsDerivsFile.close();
}

View File

@ -71,6 +71,13 @@ private:
*/
second_derivatives_type jacobian_params_derivatives;
//! Derivatives of the hessian w.r. to parameters
/*! First index is equation number, first and second are endo/exo/exo_det variable, and third is parameter.
Only non-null derivatives are stored in the map.
Variable and parameter indices are those of the getDerivID() method.
*/
third_derivatives_type hessian_params_derivatives;
//! Temporary terms for the file containing parameters dervicatives
temporary_terms_type params_derivs_temporary_terms;

View File

@ -416,7 +416,7 @@ expression : '(' expression ')'
| MIN '(' expression COMMA expression ')'
{ $$ = driver.add_min($3, $5); }
| symbol { driver.push_external_function_arg_vector_onto_stack(); } '(' comma_expression ')'
{ $$ = driver.add_model_var_or_external_function($1,false); }
{ $$ = driver.add_model_var_or_external_function($1, false); }
| NORMCDF '(' expression COMMA expression COMMA expression ')'
{ $$ = driver.add_normcdf($3, $5, $7); }
| NORMCDF '(' expression ')'
@ -572,7 +572,7 @@ hand_side : '(' hand_side ')'
| MIN '(' hand_side COMMA hand_side ')'
{ $$ = driver.add_min($3, $5); }
| symbol { driver.push_external_function_arg_vector_onto_stack(); } '(' comma_hand_side ')'
{ $$ = driver.add_model_var_or_external_function($1,true); }
{ $$ = driver.add_model_var_or_external_function($1, true); }
| NORMCDF '(' hand_side COMMA hand_side COMMA hand_side ')'
{ $$ = driver.add_normcdf($3, $5, $7); }
| NORMCDF '(' hand_side ')'

View File

@ -1723,9 +1723,19 @@ ParsingDriver::add_model_var_or_external_function(string *function_name, bool in
if (mod_file->symbol_table.exists(*function_name))
{
if (mod_file->symbol_table.getType(*function_name) != eExternalFunction)
{ // e.g. model_var(lag) => ADD MODEL VARIABLE WITH LEAD (NumConstNode)/LAG (UnaryOpNode)
if ((int)stack_external_function_args.top().size() == 1)
{
if (!in_model_block)
{
if ((int)stack_external_function_args.top().size() > 0)
error("A variable cannot take arguments.");
else
return add_expression_variable(function_name);
}
else
{ // e.g. model_var(lag) => ADD MODEL VARIABLE WITH LEAD (NumConstNode)/LAG (UnaryOpNode)
if ((int)stack_external_function_args.top().size() != 1)
error("A model variable is being treated as if it were a function (i.e., has received more than one argument).");
NumConstNode *numNode = dynamic_cast<NumConstNode *>(stack_external_function_args.top().front());
UnaryOpNode *unaryNode = dynamic_cast<UnaryOpNode *>(stack_external_function_args.top().front());
@ -1741,15 +1751,13 @@ ParsingDriver::add_model_var_or_external_function(string *function_name, bool in
model_var_arg_dbl = numNode->eval(ectmp);
}
else
{
if (unaryNode->get_op_code() != oUminus)
error("A model variable is being treated as if it were a function (i.e., takes an argument that is not an integer).");
else
{
model_var_arg = (int)unaryNode->eval(ectmp);
model_var_arg_dbl = unaryNode->eval(ectmp);
}
}
if (unaryNode->get_op_code() != oUminus)
error("A model variable is being treated as if it were a function (i.e., takes an argument that is not an integer).");
else
{
model_var_arg = (int)unaryNode->eval(ectmp);
model_var_arg_dbl = unaryNode->eval(ectmp);
}
if ((double) model_var_arg != model_var_arg_dbl) //make 100% sure int cast didn't lose info
error("A model variable is being treated as if it were a function (i.e., takes an argument that is not an integer).");
@ -1759,8 +1767,6 @@ ParsingDriver::add_model_var_or_external_function(string *function_name, bool in
delete function_name;
return nid;
}
else
error("A model variable is being treated as if it were a function (i.e., has received more than one argument).");
}
else
{ // e.g. this function has already been referenced (either ad hoc or through the external_function() statement