From 2be333da551c612433f96b9e018fe4c02f6faf9f Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 30 Mar 2012 14:04:06 +0200 Subject: [PATCH 1/7] estimation: change name handling for correlation statements --- matlab/get_existing_subsamples_indx.m | 4 ++-- matlab/get_new_or_existing_ei_index.m | 4 ++-- preprocessor/ComputingTasks.cc | 14 +++++--------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/matlab/get_existing_subsamples_indx.m b/matlab/get_existing_subsamples_indx.m index a8f30ed41..3f6e5b42b 100644 --- a/matlab/get_existing_subsamples_indx.m +++ b/matlab/get_existing_subsamples_indx.m @@ -38,9 +38,9 @@ global estimation_info if isempty(name2) % parameter or std() statement subsamples_indx = find(strcmp(name1, estimation_info.subsamples_index) == 1); else % corr statement - subsamples_indx = find(strcmp([name1 '_' name2], estimation_info.subsamples_index) == 1); + subsamples_indx = find(strcmp([name1 ':' name2], estimation_info.subsamples_index) == 1); if isempty(subsamples_indx) - subsamples_indx = find(strcmp([name2 '_' name1], estimation_info.subsamples_index) == 1); + subsamples_indx = find(strcmp([name2 ':' name1], estimation_info.subsamples_index) == 1); end end diff --git a/matlab/get_new_or_existing_ei_index.m b/matlab/get_new_or_existing_ei_index.m index 7f1c379c0..39bec99a3 100644 --- a/matlab/get_new_or_existing_ei_index.m +++ b/matlab/get_new_or_existing_ei_index.m @@ -44,9 +44,9 @@ end if isempty(name2) % parameter or std() statement indx = eval(['find(strcmp(name1, estimation_info.' substructure_name ') == 1)']); else % corr statement - indx = eval(['find(strcmp([''' name1 '_' name2 '''], estimation_info.' substructure_name ') == 1)']); + indx = eval(['find(strcmp([''' name1 ':' name2 '''], estimation_info.' substructure_name ') == 1)']); if isempty(indx) - indx = eval(['find(strcmp([''' name2 '_' name1 '''], estimation_info.' substructure_name ') == 1)']); + indx = eval(['find(strcmp([''' name2 ':' name1 '''], estimation_info.' substructure_name ') == 1)']); end end diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 3660946e4..34cde5366 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1651,7 +1651,7 @@ SubsamplesStatement::writeOutput(ostream &output, const string &basename) const << name1 << "','" << name2 << "');" << endl << "estimation_info.subsamples_index(subsamples_indx) = {'" << name1; if (!name2.empty()) - output << "_" << name2; + output << ":" << name2; output << "'};" << endl << "estimation_info.subsamples(subsamples_indx).range = {};" << endl; @@ -1689,7 +1689,7 @@ SubsamplesEqualStatement::writeOutput(ostream &output, const string &basename) c << to_name1 << "','" << to_name2 << "');" << endl << "estimation_info.subsamples_index(subsamples_to_indx) = {'" << to_name1; if (!to_name2.empty()) - output << "_" << to_name2; + output << ":" << to_name2; output << "'};" << endl << "subsamples_from_indx = get_existing_subsamples_indx('" << from_name1 << "','" << from_name2 << "');" << endl @@ -1885,13 +1885,12 @@ CorrPriorStatement::writeOutput(ostream &output, const string &basename) const output << "eifind = get_new_or_existing_ei_index('" << lhs_field << "_corr_prior_index', '" << name << "', '" << name1 << "');" << endl << "estimation_info." << lhs_field << "_corr_prior_index(eifind) = {'" - << name << "_" << name1 << "'};" << endl; + << name << ":" << name1 << "'};" << endl; lhs_field = "estimation_info." + lhs_field + "_corr(eifind)"; writePriorOutput(output, lhs_field); +} - output << lhs_field << ".name1 = '" << name << "';" << endl - << lhs_field << ".name2 = '" << name1 << "';" << endl; } BasicOptionsStatement::~BasicOptionsStatement() @@ -2022,11 +2021,8 @@ CorrOptionsStatement::writeOutput(ostream &output, const string &basename) const output << "eifind = get_new_or_existing_ei_index('" << lhs_field << "_corr_options_index', '" << name << "', '" << name1 << "');" << endl << "estimation_info." << lhs_field << "_corr_options_index(eifind) = {'" - << name << "_" << name1 << "'};" << endl; + << name << ":" << name1 << "'};" << endl; lhs_field = "estimation_info." + lhs_field + "_corr(eifind)"; writeOptionsOutput(output, lhs_field, name1); - - output << lhs_field << ".name1 = '" << name << "';" << endl - << lhs_field << ".name2 = '" << name1 << "';" << endl; } From 93db8937ab90175010aa5b8cfc4b3cd0227cb739 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 30 Mar 2012 14:54:08 +0200 Subject: [PATCH 2/7] estimation: reorganize Matlab structure output --- matlab/global_initialization.m | 60 +++++++++++++++++----------------- preprocessor/ComputingTasks.cc | 34 ++++++++++--------- 2 files changed, 49 insertions(+), 45 deletions(-) diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m index aed14ddf9..ae3526db0 100644 --- a/matlab/global_initialization.m +++ b/matlab/global_initialization.m @@ -247,61 +247,61 @@ options_.timeless = 0; % estimation estimation_info.subsamples.range = struct('date1', {}, 'date2', {}); estimation_info.parameters.prior = struct(... - 'shape', {}, 'mean', {}, ... - 'mode', {}, 'stdev', {}, 'date1', {}, ... - 'date2', {}, 'shift', {}, 'variance', {}); + 'domain', {}, 'interval', {}, 'mean', {}, ... + 'median', {}, 'mode', {}, 'shape', {}, ... + 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.parameters.subsample_prior = struct(... - 'shape', {}, 'mean', {}, ... - 'mode', {}, 'stdev', {}, 'date1', {}, ... - 'date2', {}, 'shift', {}, 'variance', {}); + 'domain', {}, 'interval', {}, 'mean', {}, ... + 'median', {}, 'mode', {}, 'shape', {}, ... + 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.parameters.options = struct(... 'init', {}, 'bounds',{}, 'jscale', {}); estimation_info.parameters.subsample_options = struct(... 'init', {}, 'bounds',{}, 'jscale', {}); estimation_info.structural_innovation.prior = struct(... - 'shape', {}, 'mean', {}, ... - 'mode', {}, 'stdev', {}, 'date1', {}, ... - 'date2', {}, 'shift', {}, 'variance', {}); + 'domain', {}, 'interval', {}, 'mean', {}, ... + 'median', {}, 'mode', {}, 'shape', {}, ... + 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.structural_innovation.subsample_prior = struct(... - 'shape', {}, 'mean', {}, ... - 'mode', {}, 'stdev', {}, 'date1', {}, ... - 'date2', {}, 'shift', {}, 'variance', {}); + 'domain', {}, 'interval', {}, 'mean', {}, ... + 'median', {}, 'mode', {}, 'shape', {}, ... + 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.structural_innovation.options = struct(... 'init', {}, 'bounds',{}, 'jscale', {}); estimation_info.structural_innovation.subsample_options = struct(... 'init', {}, 'bounds',{}, 'jscale', {}); estimation_info.structural_innovation_corr.prior = struct(... - 'shape', {}, 'mean', {}, ... - 'mode', {}, 'stdev', {}, 'date1', {}, ... - 'date2', {}, 'shift', {}, 'variance', {}); + 'domain', {}, 'interval', {}, 'mean', {}, ... + 'median', {}, 'mode', {}, 'shape', {}, ... + 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.structural_innovation_corr.subsample_prior = struct(... - 'shape', {}, 'mean', {}, ... - 'mode', {}, 'stdev', {}, 'date1', {}, ... - 'date2', {}, 'shift', {}, 'variance', {}); + 'domain', {}, 'interval', {}, 'mean', {}, ... + 'median', {}, 'mode', {}, 'shape', {}, ... + 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.structural_innovation_corr.options = struct(... 'init', {}, 'bounds',{}, 'jscale', {}); estimation_info.structural_innovation_corr.subsample_options = struct(... 'init', {}, 'bounds',{}, 'jscale', {}); estimation_info.measurement_error.prior = struct(... - 'shape', {}, 'mean', {}, ... - 'mode', {}, 'stdev', {}, 'date1', {}, ... - 'date2', {}, 'shift', {}, 'variance', {}); + 'domain', {}, 'interval', {}, 'mean', {}, ... + 'median', {}, 'mode', {}, 'shape', {}, ... + 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.measurement_error.subsample_prior = struct(... - 'shape', {}, 'mean', {}, ... - 'mode', {}, 'stdev', {}, 'date1', {}, ... - 'date2', {}, 'shift', {}, 'variance', {}); + 'domain', {}, 'interval', {}, 'mean', {}, ... + 'median', {}, 'mode', {}, 'shape', {}, ... + 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.measurement_error.options = struct(... 'init', {}, 'bounds',{}, 'jscale', {}); estimation_info.measurement_error.subsample_options = struct(... 'init', {}, 'bounds',{}, 'jscale', {}); estimation_info.measurement_error_corr.prior = struct(... - 'name1', {}, 'name2', {}, 'shape', {}, 'mean', {}, ... - 'mode', {}, 'stdev', {}, 'date1', {}, ... - 'date2', {}, 'shift', {}, 'variance', {}); + 'domain', {}, 'interval', {}, 'mean', {}, ... + 'median', {}, 'mode', {}, 'shape', {}, ... + 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.measurement_error_corr.subsample_prior = struct(... - 'name1', {}, 'name2', {},'shape', {}, 'mean', {}, ... - 'mode', {}, 'stdev', {}, 'date1', {}, ... - 'date2', {}, 'shift', {}, 'variance', {}); + 'domain', {}, 'interval', {}, 'mean', {}, ... + 'median', {}, 'mode', {}, 'shape', {}, ... + 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.measurement_error_corr.options = struct(... 'init', {}, 'bounds',{}, 'jscale', {}); estimation_info.measurement_error_corr.subsample_options = struct(... diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 34cde5366..09e2d98d3 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1764,33 +1764,37 @@ BasicPriorStatement::get_base_name(const SymbolType symb_type, string &lhs_field void BasicPriorStatement::writeCommonOutput(ostream &output, const string &lhs_field) const { + writeCommonOutputHelper(output, "domain", lhs_field); + writeCommonOutputHelper(output, "interval", lhs_field); + writeCommonOutputHelper(output, "mean", lhs_field); + writeCommonOutputHelper(output, "median", lhs_field); + writeCommonOutputHelper(output, "mode", lhs_field); + assert(prior_shape != eNoShape); output << lhs_field << ".shape = " << prior_shape << ";" << endl; - if (variance) - { - output << lhs_field << ".variance = "; - variance->writeOutput(output); - output << ";" << endl; - } - - writeCommonOutputHelper(output, "mean", lhs_field); - writeCommonOutputHelper(output, "mode", lhs_field); - writeCommonOutputHelper(output, "stdev", lhs_field); - writeCommonOutputHelper(output, "shape", lhs_field); writeCommonOutputHelper(output, "shift", lhs_field); - writeCommonOutputHelper(output, "domain", lhs_field); - writeCommonOutputHelper(output, "median", lhs_field); + writeCommonOutputHelper(output, "stdev", lhs_field); writeCommonOutputHelper(output, "truncate", lhs_field); - writeCommonOutputHelper(output, "interval", lhs_field); + + output << lhs_field << ".variance = "; + if (variance) + variance->writeOutput(output); + else + output << "[]"; + output << ";" << endl; } void BasicPriorStatement::writeCommonOutputHelper(ostream &output, const string &field, const string &lhs_field) const { OptionsList::num_options_t::const_iterator itn = options_list.num_options.find(field); + output << lhs_field << "." << field << " = "; if (itn != options_list.num_options.end()) - output << lhs_field << "." << field << " = " << itn->second << ";" << endl; + output << itn->second; + else + output << "[]"; + output << ";" << endl; } void From 50551cc6b2a40208b2d6d1973886dc0b1ee4d04c Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 30 Mar 2012 14:54:31 +0200 Subject: [PATCH 3/7] estimation: remove unnecessary function --- preprocessor/ComputingTasks.cc | 5 ----- preprocessor/ComputingTasks.hh | 1 - 2 files changed, 6 deletions(-) diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 09e2d98d3..bbf82ac3d 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1677,11 +1677,6 @@ SubsamplesEqualStatement::SubsamplesEqualStatement(const string &to_name1_arg, { } -void -SubsamplesEqualStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) -{ -} - void SubsamplesEqualStatement::writeOutput(ostream &output, const string &basename) const { diff --git a/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh index 494b04690..71a95ebec 100644 --- a/preprocessor/ComputingTasks.hh +++ b/preprocessor/ComputingTasks.hh @@ -608,7 +608,6 @@ public: const string &to_name2_arg, const string &from_name1_arg, const string &from_name2_arg); - virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename) const; }; From 26d245ac4b8ba0e61fdd19ddac1f7d031c6a9abd Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 30 Mar 2012 14:55:25 +0200 Subject: [PATCH 4/7] estimation: introduce prior = prior syntax --- preprocessor/ComputingTasks.cc | 102 +++++++++++++++++++++++++++++++++ preprocessor/ComputingTasks.hh | 27 +++++++++ preprocessor/DynareBison.yy | 62 +++++++++++++++++++- preprocessor/ParsingDriver.cc | 26 +++++++++ preprocessor/ParsingDriver.hh | 5 +- 5 files changed, 220 insertions(+), 2 deletions(-) diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index bbf82ac3d..1a98b5083 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1890,6 +1890,108 @@ CorrPriorStatement::writeOutput(ostream &output, const string &basename) const writePriorOutput(output, lhs_field); } +PriorEqualStatement::PriorEqualStatement(const string &to_declaration_type_arg, + const string &to_name1_arg, + const string &to_name2_arg, + const string &to_subsample_name_arg, + const string &from_declaration_type_arg, + const string &from_name1_arg, + const string &from_name2_arg, + const string &from_subsample_name_arg, + const SymbolTable &symbol_table_arg) : + to_declaration_type(to_declaration_type_arg), + to_name1(to_name1_arg), + to_name2(to_name2_arg), + to_subsample_name(to_subsample_name_arg), + from_declaration_type(from_declaration_type_arg), + from_name1(from_name1_arg), + from_name2(from_name2_arg), + from_subsample_name(from_subsample_name_arg), + symbol_table(symbol_table_arg) +{ +} + +void +PriorEqualStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) +{ + if ((to_declaration_type != "par" && to_declaration_type != "std" && to_declaration_type != "corr") || + (from_declaration_type != "par" && from_declaration_type != "std" && from_declaration_type != "corr")) + { + cerr << "Internal Dynare Error" << endl; + exit(EXIT_FAILURE); + } +} + +void +PriorEqualStatement::get_base_name(const SymbolType symb_type, string &lhs_field) const +{ + if (symb_type == eExogenous || symb_type == eExogenousDet) + lhs_field = "structural_innovation"; + else + lhs_field = "measurement_error"; +} + +void +PriorEqualStatement::writeOutput(ostream &output, const string &basename) const +{ + string lhs_field, rhs_field; + + if (to_declaration_type == "par") + lhs_field = "parameter"; + else + get_base_name(symbol_table.getType(to_name1), lhs_field); + + if (from_declaration_type == "par") + rhs_field = "parameter"; + else + get_base_name(symbol_table.getType(from_name1), rhs_field); + + + if (to_declaration_type == "corr") + lhs_field += "_corr"; + + if (from_declaration_type == "corr") + rhs_field += "_corr"; + + output << "ei_to_ind = get_new_or_existing_ei_index('" << lhs_field << "_prior_index', '" + << to_name1 << "', '" << to_name2<< "');" << endl + << "ei_from_ind = get_new_or_existing_ei_index('" << rhs_field << "_prior_index', '" + << from_name1 << "', '" << from_name2<< "');" << endl + << "estimation_info." << lhs_field << "_prior_index(ei_to_ind) = {'" << to_name1; + + if (to_declaration_type == "corr") + output << ":" << to_name2; + output << "'};" << endl; + + if (to_declaration_type == "par") + lhs_field = "parameters"; + + if (from_declaration_type == "par") + rhs_field = "parameters"; + + lhs_field = "estimation_info." + lhs_field + "(ei_to_ind)"; + rhs_field = "estimation_info." + rhs_field + "(ei_from_ind)"; + + if (to_subsample_name.empty()) + lhs_field += ".prior"; + else + { + output << "subsamples_to_indx = get_existing_subsamples_indx('" << to_name1 << "','" << to_name2 << "');" << endl + << lhs_field << ".range_index = estimation_info.subsamples(subsamples_to_indx).range_index;" << endl + << "ei_to_ss_ind = get_subsamples_range_indx(subsamples_to_indx, '" << to_subsample_name << "');" << endl; + lhs_field += ".subsample_prior(ei_to_ss_ind)"; + } + + if (from_subsample_name.empty()) + rhs_field += ".prior"; + else + { + output << "subsamples_from_indx = get_existing_subsamples_indx('" << from_name1 << "','" << from_name2 << "');" << endl + << "ei_from_ss_ind = get_subsamples_range_indx(subsamples_from_indx, '" << from_subsample_name << "');" << endl; + rhs_field += ".subsample_prior(ei_from_ss_ind)"; + } + + output << lhs_field << " = " << rhs_field << ";" << endl; } BasicOptionsStatement::~BasicOptionsStatement() diff --git a/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh index 71a95ebec..fe93b4081 100644 --- a/preprocessor/ComputingTasks.hh +++ b/preprocessor/ComputingTasks.hh @@ -675,6 +675,33 @@ public: virtual void writeOutput(ostream &output, const string &basename) const; }; +class PriorEqualStatement : public Statement +{ +private: + const string to_declaration_type; + const string to_name1; + const string to_name2; + const string to_subsample_name; + const string from_declaration_type; + const string from_name1; + const string from_name2; + const string from_subsample_name; + const SymbolTable symbol_table; +public: + PriorEqualStatement(const string &to_declaration_type_arg, + const string &to_name1_arg, + const string &to_name2_arg, + const string &to_subsample_name_arg, + const string &from_declaration_type_arg, + const string &from_name1_arg, + const string &from_name2_arg, + const string &from_subsample_name_arg, + const SymbolTable &symbol_table_arg); + void get_base_name(const SymbolType symb_type, string &lhs_field) const; + virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + class BasicOptionsStatement : public Statement { public: diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 4e08e8f2b..3289badb9 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -182,7 +182,7 @@ class ParsingDriver; %type vec_value_1 vec_value signed_inf signed_number_w_inf %type range vec_value_w_inf vec_value_1_w_inf %type change_type_arg -%type change_type_var_list subsamples_eq_opt +%type change_type_var_list subsamples_eq_opt prior_eq_opt %type vec_int_elem vec_int_1 vec_int vec_int_number %type prior_pdf prior_distribution %% @@ -220,6 +220,7 @@ statement : parameters | set_time | data | prior + | prior_eq | subsamples | subsamples_eq | options @@ -1279,6 +1280,65 @@ prior_options : o_shift | o_domain ; +prior_eq : prior_eq_opt EQUAL prior_eq_opt ';' + { + driver.copy_prior($1->at(0), $1->at(1), $1->at(2), $1->at(3), + $3->at(0), $3->at(1), $3->at(2), $3->at(3)); + delete $1; + delete $3; + } + ; + +prior_eq_opt : symbol '.' PRIOR + { + $$ = new vector(); + $$->push_back(new string ("par")); + $$->push_back($1); + $$->push_back(new string ("")); + $$->push_back(new string ("")); + } + | symbol '.' symbol '.' PRIOR + { + $$ = new vector(); + $$->push_back(new string ("par")); + $$->push_back($1); + $$->push_back(new string ("")); + $$->push_back($3); + } + | STD '(' symbol ')' '.' PRIOR + { + $$ = new vector(); + $$->push_back(new string ("std")); + $$->push_back($3); + $$->push_back(new string ("")); + $$->push_back(new string ("")); + } + | STD '(' symbol ')' '.' symbol '.' PRIOR + { + $$ = new vector(); + $$->push_back(new string ("std")); + $$->push_back($3); + $$->push_back(new string ("")); + $$->push_back($6); + } + | CORR '(' symbol COMMA symbol ')' '.' PRIOR + { + $$ = new vector(); + $$->push_back(new string ("corr")); + $$->push_back($3); + $$->push_back($5); + $$->push_back(new string ("")); + } + | CORR '(' symbol COMMA symbol ')' '.' symbol '.' PRIOR + { + $$ = new vector(); + $$->push_back(new string ("corr")); + $$->push_back($3); + $$->push_back($5); + $$->push_back($8); + } + ; + options : symbol '.' OPTIONS '(' options_options_list ')' ';' { driver.set_options($1, new string ("")); } | symbol '.' symbol '.' OPTIONS '(' options_options_list ')' ';' diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index aab17b50c..8e3270850 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1357,6 +1357,32 @@ ParsingDriver::set_prior_variance(expr_t variance) prior_variance = variance; } +void +ParsingDriver::copy_prior(string *to_declaration_type, string *to_name1, string *to_name2, string *to_subsample_name, + string *from_declaration_type, string *from_name1, string *from_name2, string *from_subsample_name) +{ + check_symbol_existence(*to_name1); + check_symbol_existence(*from_name1); + + if (!to_name2->empty()) + check_symbol_existence(*to_name2); + + if (!from_name2->empty()) + check_symbol_existence(*from_name2); + + mod_file->addStatement(new PriorEqualStatement(*to_declaration_type, *to_name1, *to_name2, *to_subsample_name, + *from_declaration_type, *from_name1, *from_name2, *from_subsample_name, + mod_file->symbol_table)); + delete to_declaration_type; + delete to_name1; + delete to_name2; + delete to_subsample_name; + delete from_declaration_type; + delete from_name1; + delete from_name2; + delete from_subsample_name; +} + void ParsingDriver::set_options(string *name, string *subsample_name) { diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 520df7628..a1bbef815 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 Dynare Team + * Copyright (C) 2003-2012 Dynare Team * * This file is part of Dynare. * @@ -392,6 +392,9 @@ public: void set_prior(string *arg1, string *arg2); //! Adds the variance option to its temporary holding place void set_prior_variance(expr_t variance=NULL); + //! Copies the prior from_name to_name + void copy_prior(string *to_declaration_type, string *to_name1, string *to_name2, string *to_subsample_name, + string *from_declaration_type, string *from_name1, string *from_name2, string *from_subsample_name); //! Sets the options for a parameter void set_options(string *arg1, string *arg2); //! Sets the prior for estimated std dev From 36295da47a807800cee45e4c8e3ab482657a8892 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 30 Mar 2012 15:07:05 +0200 Subject: [PATCH 5/7] cosmetic change --- preprocessor/ComputingTasks.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 1a98b5083..cf5855050 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -765,7 +765,7 @@ OptimWeightsStatement::writeOutput(ostream &output, const string &basename) cons const string &name = it->first; const expr_t value = it->second; int id = symbol_table.getTypeSpecificID(name) + 1; - output << "optim_weights_(" << id << "," << id << ") = "; + output << "optim_weights_(" << id << "," << id << ") = "; value->writeOutput(output); output << ";" << endl; output << "obj_var_ = [obj_var_; " << id << "];\n"; @@ -779,7 +779,7 @@ OptimWeightsStatement::writeOutput(ostream &output, const string &basename) cons const expr_t value = it->second; int id1 = symbol_table.getTypeSpecificID(name1) + 1; int id2 = symbol_table.getTypeSpecificID(name2) + 1; - output << "optim_weights_(" << id1 << "," << id2 << ") = "; + output << "optim_weights_(" << id1 << "," << id2 << ") = "; value->writeOutput(output); output << ";" << endl; output << "obj_var_ = [obj_var_; " << id1 << "; " << id2 << "];\n"; From 9741b3b3ec015f76be73cdb25f6049b2d99b3241 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 30 Mar 2012 15:08:48 +0200 Subject: [PATCH 6/7] estimation: reorganize options output --- matlab/global_initialization.m | 20 ++++++++++---------- preprocessor/ComputingTasks.cc | 8 ++++++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m index ae3526db0..f74adc617 100644 --- a/matlab/global_initialization.m +++ b/matlab/global_initialization.m @@ -255,9 +255,9 @@ estimation_info.parameters.subsample_prior = struct(... 'median', {}, 'mode', {}, 'shape', {}, ... 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.parameters.options = struct(... - 'init', {}, 'bounds',{}, 'jscale', {}); + 'bounds',{}, 'init', {}, 'jscale', {}); estimation_info.parameters.subsample_options = struct(... - 'init', {}, 'bounds',{}, 'jscale', {}); + 'bounds',{}, 'init', {}, 'jscale', {}); estimation_info.structural_innovation.prior = struct(... 'domain', {}, 'interval', {}, 'mean', {}, ... 'median', {}, 'mode', {}, 'shape', {}, ... @@ -267,9 +267,9 @@ estimation_info.structural_innovation.subsample_prior = struct(... 'median', {}, 'mode', {}, 'shape', {}, ... 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.structural_innovation.options = struct(... - 'init', {}, 'bounds',{}, 'jscale', {}); + 'bounds',{}, 'init', {}, 'jscale', {}); estimation_info.structural_innovation.subsample_options = struct(... - 'init', {}, 'bounds',{}, 'jscale', {}); + 'bounds',{}, 'init', {}, 'jscale', {}); estimation_info.structural_innovation_corr.prior = struct(... 'domain', {}, 'interval', {}, 'mean', {}, ... 'median', {}, 'mode', {}, 'shape', {}, ... @@ -279,9 +279,9 @@ estimation_info.structural_innovation_corr.subsample_prior = struct(... 'median', {}, 'mode', {}, 'shape', {}, ... 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.structural_innovation_corr.options = struct(... - 'init', {}, 'bounds',{}, 'jscale', {}); + 'bounds',{}, 'init', {}, 'jscale', {}); estimation_info.structural_innovation_corr.subsample_options = struct(... - 'init', {}, 'bounds',{}, 'jscale', {}); + 'bounds',{}, 'init', {}, 'jscale', {}); estimation_info.measurement_error.prior = struct(... 'domain', {}, 'interval', {}, 'mean', {}, ... 'median', {}, 'mode', {}, 'shape', {}, ... @@ -291,9 +291,9 @@ estimation_info.measurement_error.subsample_prior = struct(... 'median', {}, 'mode', {}, 'shape', {}, ... 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.measurement_error.options = struct(... - 'init', {}, 'bounds',{}, 'jscale', {}); + 'bounds',{}, 'init', {}, 'jscale', {}); estimation_info.measurement_error.subsample_options = struct(... - 'init', {}, 'bounds',{}, 'jscale', {}); + 'bounds',{}, 'init', {}, 'jscale', {}); estimation_info.measurement_error_corr.prior = struct(... 'domain', {}, 'interval', {}, 'mean', {}, ... 'median', {}, 'mode', {}, 'shape', {}, ... @@ -303,9 +303,9 @@ estimation_info.measurement_error_corr.subsample_prior = struct(... 'median', {}, 'mode', {}, 'shape', {}, ... 'shift', {}, 'stdev', {}, 'truncate', {}, 'variance', {}); estimation_info.measurement_error_corr.options = struct(... - 'init', {}, 'bounds',{}, 'jscale', {}); + 'bounds',{}, 'init', {}, 'jscale', {}); estimation_info.measurement_error_corr.subsample_options = struct(... - 'init', {}, 'bounds',{}, 'jscale', {}); + 'bounds',{}, 'init', {}, 'jscale', {}); estimation_info.subsamples_index = {}; estimation_info.subsamples.range_index = {}; estimation_info.parameter_prior_index = {}; diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index cf5855050..35eaea639 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -2024,8 +2024,8 @@ BasicOptionsStatement::get_base_name(const SymbolType symb_type, string &lhs_fie void BasicOptionsStatement::writeCommonOutput(ostream &output, const string &lhs_field) const { - writeCommonOutputHelper(output, "init", lhs_field); writeCommonOutputHelper(output, "bounds", lhs_field); + writeCommonOutputHelper(output, "init", lhs_field); writeCommonOutputHelper(output, "jscale", lhs_field); } @@ -2033,8 +2033,12 @@ void BasicOptionsStatement::writeCommonOutputHelper(ostream &output, const string &field, const string &lhs_field) const { OptionsList::num_options_t::const_iterator itn = options_list.num_options.find(field); + output << lhs_field << "." << field << " = "; if (itn != options_list.num_options.end()) - output << lhs_field << "." << field << " = " << itn->second << ";" << endl; + output << itn->second; + else + output << "[]"; + output << ";" << endl; } void From 8d69c8bf2b77319d6d535cbf0c534eeb7c2a7ba3 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 30 Mar 2012 15:36:58 +0200 Subject: [PATCH 7/7] estimation: introduce options = options syntax --- preprocessor/ComputingTasks.cc | 104 +++++++++++++++++++++++++++++++++ preprocessor/ComputingTasks.hh | 27 +++++++++ preprocessor/DynareBison.yy | 62 +++++++++++++++++++- preprocessor/ParsingDriver.cc | 26 +++++++++ preprocessor/ParsingDriver.hh | 3 + 5 files changed, 221 insertions(+), 1 deletion(-) diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 35eaea639..83fc079cd 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -2131,3 +2131,107 @@ CorrOptionsStatement::writeOutput(ostream &output, const string &basename) const lhs_field = "estimation_info." + lhs_field + "_corr(eifind)"; writeOptionsOutput(output, lhs_field, name1); } + +OptionsEqualStatement::OptionsEqualStatement(const string &to_declaration_type_arg, + const string &to_name1_arg, + const string &to_name2_arg, + const string &to_subsample_name_arg, + const string &from_declaration_type_arg, + const string &from_name1_arg, + const string &from_name2_arg, + const string &from_subsample_name_arg, + const SymbolTable &symbol_table_arg) : + to_declaration_type(to_declaration_type_arg), + to_name1(to_name1_arg), + to_name2(to_name2_arg), + to_subsample_name(to_subsample_name_arg), + from_declaration_type(from_declaration_type_arg), + from_name1(from_name1_arg), + from_name2(from_name2_arg), + from_subsample_name(from_subsample_name_arg), + symbol_table(symbol_table_arg) +{ +} + +void +OptionsEqualStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) +{ + if ((to_declaration_type != "par" && to_declaration_type != "std" && to_declaration_type != "corr") || + (from_declaration_type != "par" && from_declaration_type != "std" && from_declaration_type != "corr")) + { + cerr << "Internal Dynare Error" << endl; + exit(EXIT_FAILURE); + } +} + +void +OptionsEqualStatement::get_base_name(const SymbolType symb_type, string &lhs_field) const +{ + if (symb_type == eExogenous || symb_type == eExogenousDet) + lhs_field = "structural_innovation"; + else + lhs_field = "measurement_error"; +} + +void +OptionsEqualStatement::writeOutput(ostream &output, const string &basename) const +{ + string lhs_field, rhs_field; + + if (to_declaration_type == "par") + lhs_field = "parameter"; + else + get_base_name(symbol_table.getType(to_name1), lhs_field); + + if (from_declaration_type == "par") + rhs_field = "parameter"; + else + get_base_name(symbol_table.getType(from_name1), rhs_field); + + + if (to_declaration_type == "corr") + lhs_field += "_corr"; + + if (from_declaration_type == "corr") + rhs_field += "_corr"; + + output << "ei_to_ind = get_new_or_existing_ei_index('" << lhs_field << "_options_index', '" + << to_name1 << "', '" << to_name2<< "');" << endl + << "ei_from_ind = get_new_or_existing_ei_index('" << rhs_field << "_options_index', '" + << from_name1 << "', '" << from_name2<< "');" << endl + << "estimation_info." << lhs_field << "_options_index(ei_to_ind) = {'" << to_name1; + + if (to_declaration_type == "corr") + output << ":" << to_name2; + output << "'};" << endl; + + if (to_declaration_type == "par") + lhs_field = "parameters"; + + if (from_declaration_type == "par") + rhs_field = "parameters"; + + lhs_field = "estimation_info." + lhs_field + "(ei_to_ind)"; + rhs_field = "estimation_info." + rhs_field + "(ei_from_ind)"; + + if (to_subsample_name.empty()) + lhs_field += ".options"; + else + { + output << "subsamples_to_indx = get_existing_subsamples_indx('" << to_name1 << "','" << to_name2 << "');" << endl + << lhs_field << ".range_index = estimation_info.subsamples(subsamples_to_indx).range_index;" << endl + << "ei_to_ss_ind = get_subsamples_range_indx(subsamples_to_indx, '" << to_subsample_name << "');" << endl; + lhs_field += ".subsample_options(ei_to_ss_ind)"; + } + + if (from_subsample_name.empty()) + rhs_field += ".options"; + else + { + output << "subsamples_from_indx = get_existing_subsamples_indx('" << from_name1 << "','" << from_name2 << "');" << endl + << "ei_from_ss_ind = get_subsamples_range_indx(subsamples_from_indx, '" << from_subsample_name << "');" << endl; + rhs_field += ".subsample_options(ei_from_ss_ind)"; + } + + output << lhs_field << " = " << rhs_field << ";" << endl; +} diff --git a/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh index fe93b4081..3e048f7a0 100644 --- a/preprocessor/ComputingTasks.hh +++ b/preprocessor/ComputingTasks.hh @@ -753,4 +753,31 @@ public: virtual void writeOutput(ostream &output, const string &basename) const; }; +class OptionsEqualStatement : public Statement +{ +private: + const string to_declaration_type; + const string to_name1; + const string to_name2; + const string to_subsample_name; + const string from_declaration_type; + const string from_name1; + const string from_name2; + const string from_subsample_name; + const SymbolTable symbol_table; +public: + OptionsEqualStatement(const string &to_declaration_type_arg, + const string &to_name1_arg, + const string &to_name2_arg, + const string &to_subsample_name_arg, + const string &from_declaration_type_arg, + const string &from_name1_arg, + const string &from_name2_arg, + const string &from_subsample_name_arg, + const SymbolTable &symbol_table_arg); + void get_base_name(const SymbolType symb_type, string &lhs_field) const; + virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + #endif diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 3289badb9..f4efacc14 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -182,7 +182,7 @@ class ParsingDriver; %type vec_value_1 vec_value signed_inf signed_number_w_inf %type range vec_value_w_inf vec_value_1_w_inf %type change_type_arg -%type change_type_var_list subsamples_eq_opt prior_eq_opt +%type change_type_var_list subsamples_eq_opt prior_eq_opt options_eq_opt %type vec_int_elem vec_int_1 vec_int vec_int_number %type prior_pdf prior_distribution %% @@ -224,6 +224,7 @@ statement : parameters | subsamples | subsamples_eq | options + | options_eq | varobs | observation_trends | unit_root_vars @@ -1362,6 +1363,65 @@ options_options : o_jscale | o_bounds ; +options_eq : options_eq_opt EQUAL options_eq_opt ';' + { + driver.copy_options($1->at(0), $1->at(1), $1->at(2), $1->at(3), + $3->at(0), $3->at(1), $3->at(2), $3->at(3)); + delete $1; + delete $3; + } + ; + +options_eq_opt : symbol '.' OPTIONS + { + $$ = new vector(); + $$->push_back(new string ("par")); + $$->push_back($1); + $$->push_back(new string ("")); + $$->push_back(new string ("")); + } + | symbol '.' symbol '.' OPTIONS + { + $$ = new vector(); + $$->push_back(new string ("par")); + $$->push_back($1); + $$->push_back(new string ("")); + $$->push_back($3); + } + | STD '(' symbol ')' '.' OPTIONS + { + $$ = new vector(); + $$->push_back(new string ("std")); + $$->push_back($3); + $$->push_back(new string ("")); + $$->push_back(new string ("")); + } + | STD '(' symbol ')' '.' symbol '.' OPTIONS + { + $$ = new vector(); + $$->push_back(new string ("std")); + $$->push_back($3); + $$->push_back(new string ("")); + $$->push_back($6); + } + | CORR '(' symbol COMMA symbol ')' '.' OPTIONS + { + $$ = new vector(); + $$->push_back(new string ("corr")); + $$->push_back($3); + $$->push_back($5); + $$->push_back(new string ("")); + } + | CORR '(' symbol COMMA symbol ')' '.' symbol '.' OPTIONS + { + $$ = new vector(); + $$->push_back(new string ("corr")); + $$->push_back($3); + $$->push_back($5); + $$->push_back($8); + } + ; + estimation : ESTIMATION ';' { driver.run_estimation(); } | ESTIMATION '(' estimation_options_list ')' ';' diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 8e3270850..4f95a9c2b 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1394,6 +1394,32 @@ ParsingDriver::set_options(string *name, string *subsample_name) delete subsample_name; } +void +ParsingDriver::copy_options(string *to_declaration_type, string *to_name1, string *to_name2, string *to_subsample_name, + string *from_declaration_type, string *from_name1, string *from_name2, string *from_subsample_name) +{ + check_symbol_existence(*to_name1); + check_symbol_existence(*from_name1); + + if (!to_name2->empty()) + check_symbol_existence(*to_name2); + + if (!from_name2->empty()) + check_symbol_existence(*from_name2); + + mod_file->addStatement(new OptionsEqualStatement(*to_declaration_type, *to_name1, *to_name2, *to_subsample_name, + *from_declaration_type, *from_name1, *from_name2, *from_subsample_name, + mod_file->symbol_table)); + delete to_declaration_type; + delete to_name1; + delete to_name2; + delete to_subsample_name; + delete from_declaration_type; + delete from_name1; + delete from_name2; + delete from_subsample_name; +} + void ParsingDriver::check_symbol_is_endogenous_or_exogenous(string *name) { diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index a1bbef815..266283a7f 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -397,6 +397,9 @@ public: string *from_declaration_type, string *from_name1, string *from_name2, string *from_subsample_name); //! Sets the options for a parameter void set_options(string *arg1, string *arg2); + //! Copies the options from_name to_name + void copy_options(string *to_declaration_type, string *to_name1, string *to_name2, string *to_subsample_name, + string *from_declaration_type, string *from_name1, string *from_name2, string *from_subsample_name); //! Sets the prior for estimated std dev void set_std_prior(string *arg1, string *arg2); //! Sets the options for estimated std dev