From ac2e5cfd5264ef60df9a7875734a37ac2d808031 Mon Sep 17 00:00:00 2001 From: ferhat Date: Thu, 19 Nov 2015 17:03:05 +0100 Subject: [PATCH 1/2] - In extended path - fix an error with results print out - fix an error withe exogenous initialization --- mex/sources/bytecode/ErrorHandling.hh | 6 +-- mex/sources/bytecode/Interpreter.cc | 56 +++++++++++++++++---------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/mex/sources/bytecode/ErrorHandling.hh b/mex/sources/bytecode/ErrorHandling.hh index c3012bc11..1e537cb39 100644 --- a/mex/sources/bytecode/ErrorHandling.hh +++ b/mex/sources/bytecode/ErrorHandling.hh @@ -438,7 +438,7 @@ public: switch (variable_type) { case eEndogenous: - if (variable_num < nb_endo) + if (variable_num <= nb_endo) { for (unsigned int i = 0; i < endo_name_length; i++) if (P_endo_names[CHAR_LENGTH*(variable_num+i*nb_endo)] != ' ') @@ -449,7 +449,7 @@ public: break; case eExogenous: case eExogenousDet: - if (variable_num < nb_exo) + if (variable_num <= nb_exo) { for (unsigned int i = 0; i < exo_name_length; i++) if (P_exo_names[CHAR_LENGTH*(variable_num+i*nb_exo)] != ' ') @@ -459,7 +459,7 @@ public: mexPrintf("=> Unknown exogenous variable # %d", variable_num); break; case eParameter: - if (variable_num < nb_param) + if (variable_num <= nb_param) { for (unsigned int i = 0; i < param_name_length; i++) if (P_param_names[CHAR_LENGTH*(variable_num+i*nb_param)] != ' ') diff --git a/mex/sources/bytecode/Interpreter.cc b/mex/sources/bytecode/Interpreter.cc index b72272da5..f85c9e709 100644 --- a/mex/sources/bytecode/Interpreter.cc +++ b/mex/sources/bytecode/Interpreter.cc @@ -77,7 +77,7 @@ Interpreter::Interpreter(double *params_arg, double *y_arg, double *ya_arg, doub GlobalTemporaryTerms = GlobalTemporaryTerms_arg; print_error = print_error_arg; //steady_state = steady_state_arg; - //print_it = print_it_arg; + print_it = print_it_arg; } @@ -850,16 +850,18 @@ Interpreter::extended_path(string file_name, string bin_basename, bool evaluate, int endo_name_length_l = endo_name_length; for (int j = 0; j < col_x* nb_row_x; j++) - x_save[j] = x[j]; + { + x_save[j] = x[j]; + x[j] = 0; + } + for (int j = 0; j < col_x; j++) + x[y_kmin + j * nb_row_x] = x_save[1 + y_kmin + j * nb_row_x]; for (int i = 0; i < (y_size*(periods + y_kmax + y_kmin)); i++) y_save[i] = y[i]; if (endo_name_length_l < 8) endo_name_length_l = 8; - //mexPrintf("endo_name_length = %d\n",endo_name_length_l); + bool old_print_it = print_it; print_it = false; - //print_it = true; - mexPrintf("\nExtended Path simulation:\n"); - mexPrintf("-------------------------\n"); ostringstream res1; res1 << std::scientific << 2.54656875434865131; int real_max_length = res1.str().length(); @@ -868,16 +870,24 @@ Interpreter::extended_path(string file_name, string bin_basename, bool evaluate, string line; line.insert(line.begin(),table_length,'-'); line.insert(line.length(),"\n"); - mexPrintf(line.c_str()); - string title = "|" + elastic("date",date_length+2, false) + "|" + elastic("variable",endo_name_length_l+2, false) + "|" + elastic("max. value",real_max_length+2, false) + "| iter. |" + elastic("cvg",5, false) + "|\n"; - mexPrintf(title.c_str()); - mexPrintf(line.c_str()); + if (old_print_it) + { + mexPrintf("\nExtended Path simulation:\n"); + mexPrintf("-------------------------\n"); + mexPrintf(line.c_str()); + string title = "|" + elastic("date",date_length+2, false) + "|" + elastic("variable",endo_name_length_l+2, false) + "|" + elastic("max. value",real_max_length+2, false) + "| iter. |" + elastic("cvg",5, false) + "|\n"; + mexPrintf(title.c_str()); + mexPrintf(line.c_str()); + } for (int t = 0; t < nb_periods; t++) { nb_blocks = 0; previous_block_exogenous.clear(); - mexPrintf("|%s|",elastic(dates[t], date_length+2, false).c_str()); - mexEvalString("drawnow;"); + if (old_print_it) + { + mexPrintf("|%s|",elastic(dates[t], date_length+2, false).c_str()); + mexEvalString("drawnow;"); + } for (vector::const_iterator it = sextended_path.begin(); it != sextended_path.end(); it++) x[y_kmin + (it->exo_num - 1) * (periods + y_kmax + y_kmin)] = it->value[t]; it_code = Init_Code; @@ -897,18 +907,22 @@ Interpreter::extended_path(string file_name, string bin_basename, bool evaluate, for (int j = 0; j < col_x; j++) { x_save[t + y_kmin + j * nb_row_x] = x[y_kmin + j * nb_row_x]; - x[y_kmin + j * nb_row_x] = 0; + x[y_kmin + j * nb_row_x] = x_save[t + 1 + y_kmin + j * nb_row_x]; } - ostringstream res, res1; - for (unsigned int i = 0; i < endo_name_length; i++) - if (P_endo_names[CHAR_LENGTH*(max_res_idx+i*y_size)] != ' ') - res << P_endo_names[CHAR_LENGTH*(max_res_idx+i*y_size)]; - res1 << std::scientific << max_res; - mexPrintf("%s|%s| %4d | x |\n",elastic(res.str(),endo_name_length_l+2, true).c_str(), elastic(res1.str(), real_max_length+2, false).c_str(), iter); - mexPrintf(line.c_str()); - mexEvalString("drawnow;"); + if (old_print_it) + { + ostringstream res, res1; + for (unsigned int i = 0; i < endo_name_length; i++) + if (P_endo_names[CHAR_LENGTH*(max_res_idx+i*y_size)] != ' ') + res << P_endo_names[CHAR_LENGTH*(max_res_idx+i*y_size)]; + res1 << std::scientific << max_res; + mexPrintf("%s|%s| %4d | x |\n",elastic(res.str(),endo_name_length_l+2, true).c_str(), elastic(res1.str(), real_max_length+2, false).c_str(), iter); + mexPrintf(line.c_str()); + mexEvalString("drawnow;"); + } } + print_it = old_print_it; for (int j = 0; j < y_size; j++) { for(int k = nb_periods; k < periods; k++) From e555c8181d5e9cf3747a85af2ec39bfc84c467a9 Mon Sep 17 00:00:00 2001 From: ferhat Date: Thu, 19 Nov 2015 17:04:56 +0100 Subject: [PATCH 2/2] Corrections in the way to handle the exogenous --- .../det_cond_forecast.m | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/matlab/perfect-foresight-models/det_cond_forecast.m b/matlab/perfect-foresight-models/det_cond_forecast.m index bc4314904..52c22221c 100644 --- a/matlab/perfect-foresight-models/det_cond_forecast.m +++ b/matlab/perfect-foresight-models/det_cond_forecast.m @@ -33,7 +33,6 @@ global options_ oo_ M_ pp = 2; initial_conditions = oo_.steady_state; verbosity = options_.verbosity; -options_.verbosity = 0; if options_.periods == 0 options_.periods = 25; end; @@ -106,7 +105,14 @@ else sym_dset = dset(dates(-range(1)):dates(range(range.ndat))); periods = options_.periods + M_.maximum_lag + M_.maximum_lead; - oo_.exo_simul = repmat(oo_.exo_steady_state',max(range.ndat + 1, periods),1); + if isfield(oo_, 'exo_simul') + if size(oo_.exo_simul, 1) ~= max(range.ndat + 1, periods) + oo_.exo_simul = repmat(oo_.exo_steady_state',max(range.ndat + 1, periods),1); + end + else + oo_.exo_simul = repmat(oo_.exo_steady_state',max(range.ndat + 1, periods),1); + end + oo_.endo_simul = repmat(oo_.steady_state, 1, max(range.ndat + 1, periods)); for i = 1:sym_dset.vobs @@ -175,6 +181,15 @@ else end end data_set = [dset(dset.dates(1):(plan.date(1)-1)) ; data_set]; + for i=1:M_.exo_nbr + pos = find(strcmp(strtrim(M_.exo_names(i,:)),dset.name)); + if isempty(pos) + data_set{strtrim(M_.exo_names(i,:))} = dseries(exo(1+M_.maximum_lag:end,i), plan.date(1), strtrim(M_.exo_names(i,:))); + else + data_set{strtrim(M_.exo_names(i,:))}(plan.date(1):plan.date(1)+ (size(exo, 1) - M_.maximum_lag)) = exo(1+M_.maximum_lag:end,i); + end + end + data_set = merge(dset(dset.dates(1):(plan.date(1)-1)), data_set); return; union_names = union(data_set.name, dset.name); dif = setdiff(union_names, data_set.name);