preprocessor: enumerate prior distributions

issue#70
Houtan Bastani 2011-12-29 18:43:21 +01:00
parent 6b056640e1
commit d2c03a206b
6 changed files with 83 additions and 60 deletions

View File

@ -234,6 +234,18 @@ enum external_function_type
ExternalFunctionSecondDerivative
};
enum PriorDistributions
{
eNoShape = 0,
eBeta = 1,
eGamma = 2,
eNormal = 3,
eInvGamma = 4,
eInvGamma1 = 4,
eUniform = 5,
eInvGamma2 = 6
};
struct Block_contain_type
{
int Equation, Variable, Own_Derivative;

View File

@ -446,7 +446,7 @@ EstimatedParamsStatement::checkPass(ModFileStructure &mod_file_struct)
mod_file_struct.dsge_prior_weight_in_estimated_params = true;
// Handle case of degenerate beta prior
if (it->prior == "1") //BETA_PDF is associated with "1" in DynareBison.yy
if (it->prior == eBeta)
try
{
if (it->mean->eval(eval_context_t()) == 0.5
@ -1655,9 +1655,11 @@ BasicPriorStatement::~BasicPriorStatement()
}
BasicPriorStatement::BasicPriorStatement(const string &name_arg,
const PriorDistributions &prior_shape_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg) :
name(name_arg),
prior_shape(prior_shape_arg),
variance(variance_arg),
options_list(options_list_arg),
first_statement_encountered(false)
@ -1667,7 +1669,7 @@ BasicPriorStatement::BasicPriorStatement(const string &name_arg,
void
BasicPriorStatement::checkPass(ModFileStructure &mod_file_struct)
{
if (options_list.num_options.find("shape") == options_list.num_options.end())
if (options_list.string_options.find("shape") == options_list.string_options.end())
{
cerr << "ERROR: You must pass the shape option to the prior statement." << endl;
exit(EXIT_FAILURE);
@ -1715,7 +1717,6 @@ 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 itn = options_list.num_options.find(field);
if (itn != options_list.num_options.end())
output << "estimation_info" << lhs_field << "(prior_indx)." << field
@ -1727,10 +1728,18 @@ BasicPriorStatement::writeOutputHelper(ostream &output, const string &field, con
<< " = '" << itd->second << "';" << endl;
}
void
BasicPriorStatement::writeShape(ostream &output, const string &lhs_field) const
{
assert(prior_shape != eNoShape);
output << "estimation_info" << lhs_field << "(prior_indx).shape = " << prior_shape;
}
PriorStatement::PriorStatement(const string &name_arg,
const PriorDistributions &prior_shape_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg) :
BasicPriorStatement(name_arg, variance_arg, options_list_arg)
BasicPriorStatement(name_arg, prior_shape_arg, variance_arg, options_list_arg)
{
}
@ -1751,7 +1760,7 @@ PriorStatement::writeOutput(ostream &output, const string &basename) const
writePriorIndex(output, lhs_field);
output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name << "'};" << endl
<< "estimation_info" << lhs_field <<"(prior_indx).name = '" << name << "';" << endl;
writeShape(output, lhs_field);
writeOutputHelper(output, "mean", lhs_field);
writeOutputHelper(output, "mode", lhs_field);
writeOutputHelper(output, "stdev", lhs_field);
@ -1765,10 +1774,11 @@ PriorStatement::writeOutput(ostream &output, const string &basename) const
}
StdPriorStatement::StdPriorStatement(const string &name_arg,
const PriorDistributions &prior_shape_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg,
const SymbolTable &symbol_table_arg ) :
BasicPriorStatement(name_arg, variance_arg, options_list_arg),
BasicPriorStatement(name_arg, prior_shape_arg, variance_arg, options_list_arg),
symbol_table(symbol_table_arg)
{
}
@ -1793,6 +1803,7 @@ StdPriorStatement::writeOutput(ostream &output, const string &basename) const
output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name << "'};" << endl;
output << "estimation_info" << lhs_field << "(prior_indx).name = '" << name << "';" << endl;
writeShape(output, lhs_field);
writeOutputHelper(output, "mean", lhs_field);
writeOutputHelper(output, "mode", lhs_field);
writeOutputHelper(output, "stdev", lhs_field);
@ -1804,10 +1815,11 @@ StdPriorStatement::writeOutput(ostream &output, const string &basename) const
}
CorrPriorStatement::CorrPriorStatement(const string &name_arg1, const string &name_arg2,
const PriorDistributions &prior_shape_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg,
const SymbolTable &symbol_table_arg ) :
BasicPriorStatement(name_arg1, variance_arg, options_list_arg),
BasicPriorStatement(name_arg1, prior_shape_arg, variance_arg, options_list_arg),
name1(name_arg2),
symbol_table(symbol_table_arg)
{
@ -1841,6 +1853,7 @@ CorrPriorStatement::writeOutput(ostream &output, const string &basename) const
output << "estimation_info" << lhs_field << "(prior_indx).name1 = '" << name << "';" << endl;
output << "estimation_info" << lhs_field << "(prior_indx).name2 = '" << name1 << "';" << endl;
writeShape(output, lhs_field);
writeOutputHelper(output, "mean", lhs_field);
writeOutputHelper(output, "mode", lhs_field);
writeOutputHelper(output, "stdev", lhs_field);

View File

@ -251,7 +251,8 @@ class EstimationParams
{
public:
int type;
string name, name2, prior;
string name, name2;
PriorDistributions prior;
expr_t init_val, low_bound, up_bound, mean, std, p3, p4, jscale;
void
@ -260,7 +261,7 @@ public:
type = 0;
name = "";
name2 = "";
prior = "NaN";
prior = eNoShape;
init_val = datatree.NaN;
low_bound = datatree.MinusInfinity;
up_bound = datatree.Infinity;
@ -583,10 +584,12 @@ public:
virtual ~BasicPriorStatement();
protected:
const string name;
const PriorDistributions prior_shape;
const expr_t variance;
const OptionsList options_list;
bool first_statement_encountered;
BasicPriorStatement(const string &name_arg,
const PriorDistributions &prior_shape_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg);
virtual void checkPass(ModFileStructure &mod_file_struct);
@ -594,12 +597,14 @@ protected:
void writePriorIndex(ostream &output, const string &lhs_field) const;
void writeVarianceOption(ostream &output, const string &lhs_field) const;
void writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const;
void writeShape(ostream &output, const string &lhs_field) const;
};
class PriorStatement : public BasicPriorStatement
{
public:
PriorStatement(const string &name_arg,
const PriorDistributions &prior_shape_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg);
virtual void checkPass(ModFileStructure &mod_file_struct);
@ -612,6 +617,7 @@ private:
const SymbolTable symbol_table;
public:
StdPriorStatement(const string &name_arg,
const PriorDistributions &prior_shape_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg,
const SymbolTable &symbol_table_arg);
@ -627,6 +633,7 @@ private:
public:
CorrPriorStatement(const string &name_arg1,
const string &name_arg2,
const PriorDistributions &prior_shape_arg,
const expr_t &variance_arg,
const OptionsList &options_list_arg,
const SymbolTable &symbol_table_arg);

View File

@ -33,6 +33,7 @@ using namespace std;
class ParsingDriver;
#include "ExprNode.hh"
#include "CodeInterpreter.hh"
/* Little hack: we redefine the macro which computes the locations, because
we need to access the location from within the parsing driver for error
@ -75,6 +76,7 @@ class ParsingDriver;
SymbolType symbol_type_val;
vector<string *> *vector_string_val;
vector<int> *vector_int_val;
PriorDistributions prior_distributions_val;
};
%{
@ -175,13 +177,13 @@ class ParsingDriver;
%type <node_val> expression expression_or_empty
%type <node_val> equation hand_side
%type <string_val> non_negative_number signed_number signed_integer date_number
%type <string_val> filename symbol prior_distribution vec_of_vec_value vec_value_list
%type <string_val> filename symbol vec_of_vec_value vec_value_list
%type <string_val> vec_value_1 vec_value signed_inf signed_number_w_inf
%type <string_val> range prior_pdf_string vec_value_w_inf vec_value_1_w_inf
%type <string_val> range vec_value_w_inf vec_value_1_w_inf
%type <symbol_type_val> change_type_arg
%type <vector_string_val> change_type_var_list
%type <vector_int_val> vec_int_elem vec_int_1 vec_int vec_int_number
%type <prior_distributions_val> prior_pdf
%%
%start statement_list;
@ -1030,24 +1032,21 @@ estimated_elem1 : STDERR symbol
}
;
estimated_elem2 : prior_pdf_string COMMA estimated_elem3
estimated_elem2 : prior_pdf COMMA estimated_elem3
{
driver.estim_params.prior = *$1;
delete $1;
driver.estim_params.prior = $1;
}
| expression_or_empty COMMA prior_pdf_string COMMA estimated_elem3
| expression_or_empty COMMA prior_pdf COMMA estimated_elem3
{
driver.estim_params.init_val = $1;
driver.estim_params.prior = *$3;
delete $3;
driver.estim_params.prior = $3;
}
| expression_or_empty COMMA expression_or_empty COMMA expression_or_empty COMMA prior_pdf_string COMMA estimated_elem3
| expression_or_empty COMMA expression_or_empty COMMA expression_or_empty COMMA prior_pdf COMMA estimated_elem3
{
driver.estim_params.init_val = $1;
driver.estim_params.low_bound = $3;
driver.estim_params.up_bound = $5;
driver.estim_params.prior = *$7;
delete $7;
driver.estim_params.prior = $7;
}
| expression
{
@ -1160,37 +1159,21 @@ estimated_bounds_elem : STDERR symbol COMMA expression COMMA expression ';'
}
;
prior_distribution : BETA
{ $$ = new string("1"); }
| GAMMA
{ $$ = new string("2"); }
| NORMAL
{ $$ = new string("3"); }
| INV_GAMMA
{ $$ = new string("4"); }
| INV_GAMMA1
{ $$ = new string("4"); }
| UNIFORM
{ $$ = new string("5"); }
| INV_GAMMA2
{ $$ = new string("6"); }
;
prior_pdf_string : BETA_PDF
{ $$ = new string("1"); }
| GAMMA_PDF
{ $$ = new string("2"); }
| NORMAL_PDF
{ $$ = new string("3"); }
| INV_GAMMA_PDF
{ $$ = new string("4"); }
| INV_GAMMA1_PDF
{ $$ = new string("4"); }
| UNIFORM_PDF
{ $$ = new string("5"); }
| INV_GAMMA2_PDF
{ $$ = new string("6"); }
;
prior_pdf : BETA_PDF
{ $$ = eBeta; }
| GAMMA_PDF
{ $$ = eGamma; }
| NORMAL_PDF
{ $$ = eNormal; }
| INV_GAMMA_PDF
{ $$ = eInvGamma; }
| INV_GAMMA1_PDF
{ $$ = eInvGamma1; }
| UNIFORM_PDF
{ $$ = eUniform; }
| INV_GAMMA2_PDF
{ $$ = eInvGamma2; }
;
set_time : SET_TIME '(' date_number ')' ';'
{ driver.set_time($3); }
@ -1224,16 +1207,16 @@ subsamples_name_list : subsamples_name_list COMMA o_subsample_name
| o_subsample_name
;
prior : symbol '.' PRIOR { driver.set_prior_variance(); } '(' prior_options_list ')' ';'
prior : symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';'
{ driver.set_prior($1); }
| symbol '.' symbol '.' PRIOR { driver.set_prior_variance(); } '(' prior_options_list ')' ';'
| symbol '.' symbol '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';'
{
driver.add_subsample_range(new string (*$1), $3);
driver.set_prior($1);
}
| STD '(' symbol ')' '.' PRIOR { driver.set_prior_variance(); } '(' prior_options_list ')' ';'
| STD '(' symbol ')' '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';'
{ driver.set_std_prior($3); }
| CORR '(' symbol COMMA symbol')' '.' PRIOR { driver.set_prior_variance(); } '(' prior_options_list ')' ';'
| CORR '(' symbol COMMA symbol')' '.' PRIOR { driver.set_prior_variance(); driver.prior_shape = eNoShape; } '(' prior_options_list ')' ';'
{ driver.set_corr_prior($3, $5); }
;
@ -1992,7 +1975,7 @@ o_last_obs : LAST_OBS EQUAL date_number
{ driver.option_date("last_obs", $3); }
;
o_shift : SHIFT EQUAL signed_number { driver.option_num("shift", $3); };
o_shape : SHAPE EQUAL prior_distribution { driver.option_num("shape", $3); };
o_shape : SHAPE EQUAL prior_pdf { driver.prior_shape = $3; };
o_mode : MODE EQUAL signed_number { driver.option_num("mode", $3); };
o_mean : MEAN EQUAL signed_number { driver.option_num("mean", $3); };
o_stdev : STDEV EQUAL non_negative_number { driver.option_num("stdev", $3); };

View File

@ -1303,9 +1303,10 @@ void
ParsingDriver::set_prior(string *name)
{
check_symbol_is_parameter(name);
mod_file->addStatement(new PriorStatement(*name, prior_variance, options_list));
mod_file->addStatement(new PriorStatement(*name, prior_shape, prior_variance, options_list));
options_list.clear();
set_prior_variance();
prior_shape = eNoShape;
delete name;
}
@ -1344,9 +1345,11 @@ void
ParsingDriver::set_std_prior(string *name)
{
check_symbol_is_endogenous_or_exogenous(name);
mod_file->addStatement(new StdPriorStatement(*name, prior_variance, options_list, mod_file->symbol_table));
mod_file->addStatement(new StdPriorStatement(*name, prior_shape, prior_variance,
options_list, mod_file->symbol_table));
options_list.clear();
set_prior_variance();
prior_shape = eNoShape;
delete name;
}
@ -1364,9 +1367,11 @@ ParsingDriver::set_corr_prior(string *name1, string *name2)
{
check_symbol_is_endogenous_or_exogenous(name1);
check_symbol_is_endogenous_or_exogenous(name2);
mod_file->addStatement(new CorrPriorStatement(*name1, *name2, prior_variance, options_list, mod_file->symbol_table));
mod_file->addStatement(new CorrPriorStatement(*name1, *name2, prior_shape, prior_variance,
options_list, mod_file->symbol_table));
options_list.clear();
set_prior_variance();
prior_shape = eNoShape;
delete name1;
delete name2;
}

View File

@ -217,6 +217,9 @@ public:
//! Estimation parameters
EstimationParams estim_params;
//! Temporary storage for the prior shape
PriorDistributions prior_shape;
//! Error handler with explicit location
void error(const Dynare::parser::location_type &l, const string &m) __attribute__ ((noreturn));
//! Error handler using saved location