From 30cbcdbb55f5ed8acd8785eac7e5d0d7bac803dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien.villemot@ens.fr>
Date: Wed, 28 Dec 2011 11:11:06 +0100
Subject: [PATCH] Store histval information for endogenous in M_.endo_histval

Really closes: #157
---
 matlab/dyn_forecast.m                   |  4 ++--
 matlab/global_initialization.m          |  1 +
 matlab/make_y_.m                        | 11 +++++++----
 matlab/stoch_simul.m                    |  7 ++-----
 matlab/stoch_simul_sparse.m             |  4 ++--
 preprocessor/NumericalInitialization.cc |  7 +++----
 preprocessor/NumericalInitialization.hh |  2 +-
 tests/histval_sto.mod                   |  5 ++++-
 8 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/matlab/dyn_forecast.m b/matlab/dyn_forecast.m
index 235b4d38b3..4343e2c8fc 100644
--- a/matlab/dyn_forecast.m
+++ b/matlab/dyn_forecast.m
@@ -61,10 +61,10 @@ switch task
     if horizon == 0
         horizon = 5;
     end
-    if size(oo_.endo_simul,2) < maximum_lag
+    if isempty(M_.endo_histval)
         y0 = repmat(oo_.steady_state,1,maximum_lag);
     else
-        y0 = oo_.endo_simul(:,1:maximum_lag);
+        y0 = M_.endo_histval;
     end
   case 'smoother'
     horizon = options_.forecast;
diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m
index 87bd4d9acf..7cfb6f5287 100644
--- a/matlab/global_initialization.m
+++ b/matlab/global_initialization.m
@@ -332,6 +332,7 @@ oo_.exo_det_steady_state = [];
 oo_.exo_det_simul = [];
 
 M_.params = [];
+M_.endo_histval = [];
 
 % BVAR
 M_.bvar = [];
diff --git a/matlab/make_y_.m b/matlab/make_y_.m
index 7b6316d1ad..9c1a963ed5 100644
--- a/matlab/make_y_.m
+++ b/matlab/make_y_.m
@@ -35,13 +35,16 @@ if isempty(oo_.steady_state)
     oo_.steady_state = zeros(M_.endo_nbr,1);
 end
 
-if isempty(oo_.endo_simul)
+if isempty(M_.endo_histval)
     if isempty(ys0_)
         oo_.endo_simul = [oo_.steady_state*ones(1,M_.maximum_lag+options_.periods+M_.maximum_lead)];
     else
         oo_.endo_simul = [ys0_*ones(1,M_.maximum_lag) oo_.steady_state*ones(1,options_.periods+M_.maximum_lead)];
     end
-elseif size(oo_.endo_simul,2) < M_.maximum_lag+M_.maximum_lead+options_.periods
-        oo_.endo_simul = [oo_.endo_simul ...
-                          oo_.steady_state*ones(1,M_.maximum_lag+options_.periods+M_.maximum_lead-size(oo_.endo_simul,2),1)];
+else
+    if ~isempty(ys0_)
+        error('histval and endval cannot be used simultaneously')
+    end
+    oo_.endo_simul = [M_.endo_histval ...
+                      oo_.steady_state*ones(1,options_.periods+M_.maximum_lead)];
 end
diff --git a/matlab/stoch_simul.m b/matlab/stoch_simul.m
index ec6d096648..d6eb5d6257 100644
--- a/matlab/stoch_simul.m
+++ b/matlab/stoch_simul.m
@@ -130,13 +130,10 @@ if options_.periods > 0 && ~PI_PCL_solver
         options_ =options_old;
         return
     end
-    % Note that the first column of oo_.endo_simul is preserved by the following
-    % call to simult; this is important because stoch_simul can be followed by
-    % forecast (see ticket #157)
-    if size(oo_.endo_simul,2) == 0
+    if isempty(M_.endo_histval)
         y0 = oo_.dr.ys;
     else
-        y0 = oo_.endo_simul(:,1);
+        y0 = M_.endo_histval;
     end
     oo_.endo_simul = simult(y0,oo_.dr);
     dyn2vec;
diff --git a/matlab/stoch_simul_sparse.m b/matlab/stoch_simul_sparse.m
index 29488b9bbb..2136961b16 100644
--- a/matlab/stoch_simul_sparse.m
+++ b/matlab/stoch_simul_sparse.m
@@ -82,10 +82,10 @@ elseif options_.periods ~= 0
         options_ =options_old;
         return
     end
-    if size(oo_.endo_simul,2) < maximum_lag
+    if isempty(M_.endo_histval)
         y0 = oo_.dr.ys;
     else
-        y0 = oo_.endo_simul(:,1);
+        y0 = M_.endo_histval;
     end
     oo_.endo_simul = simult(y0,oo_.dr);
     dyn2vec;
diff --git a/preprocessor/NumericalInitialization.cc b/preprocessor/NumericalInitialization.cc
index 6b08a74eb4..7a67c56f53 100644
--- a/preprocessor/NumericalInitialization.cc
+++ b/preprocessor/NumericalInitialization.cc
@@ -132,8 +132,7 @@ InitValStatement::writeOutput(ostream &output, const string &basename) const
 void
 InitValStatement::writeOutputPostInit(ostream &output) const
 {
-  output << "oo_.endo_simul=[oo_.steady_state*ones(1,M_.maximum_lag)];" << endl
-         << "if M_.exo_nbr > 0;" << endl
+  output << "if M_.exo_nbr > 0;" << endl
          << "\too_.exo_simul = [ones(M_.maximum_lag,1)*oo_.exo_steady_state'];" << endl
          <<"end;" << endl
          << "if M_.exo_det_nbr > 0;" << endl
@@ -189,7 +188,7 @@ HistValStatement::writeOutput(ostream &output, const string &basename) const
   output << "%" << endl
          << "% HISTVAL instructions" << endl
          << "%" << endl
-         << "oo_.endo_simul = zeros(M_.endo_nbr,M_.maximum_lag);" << endl;
+         << "M_.endo_histval = zeros(M_.endo_nbr,M_.maximum_lag);" << endl;
 
   for (hist_values_t::const_iterator it = hist_values.begin();
        it != hist_values.end(); it++)
@@ -225,7 +224,7 @@ HistValStatement::writeOutput(ostream &output, const string &basename) const
       int tsid = symbol_table.getTypeSpecificID(symb_id) + 1;
 
       if (type == eEndogenous)
-        output << "oo_.endo_simul( " << tsid << ", M_.maximum_lag + " << lag << ") = ";
+        output << "M_.endo_histval( " << tsid << ", M_.maximum_lag + " << lag << ") = ";
       else if (type == eExogenous)
         output << "oo_.exo_simul( M_.maximum_lag + " << lag << ", " << tsid << " ) = ";
       else if (type != eExogenousDet)
diff --git a/preprocessor/NumericalInitialization.hh b/preprocessor/NumericalInitialization.hh
index b9639af910..d791acace2 100644
--- a/preprocessor/NumericalInitialization.hh
+++ b/preprocessor/NumericalInitialization.hh
@@ -71,7 +71,7 @@ public:
   InitValStatement(const init_values_t &init_values_arg,
                    const SymbolTable &symbol_table_arg);
   virtual void writeOutput(ostream &output, const string &basename) const;
-  //! Writes initializations for oo_.endo_simul, oo_.exo_simul and oo_.exo_det_simul
+  //! Writes initializations for oo_.exo_simul and oo_.exo_det_simul
   void writeOutputPostInit(ostream &output) const;
 };
 
diff --git a/tests/histval_sto.mod b/tests/histval_sto.mod
index 830e47fa1e..db4ab3c616 100644
--- a/tests/histval_sto.mod
+++ b/tests/histval_sto.mod
@@ -49,4 +49,7 @@ a(-1) = 0.3;
 u(-1) = 0.1;
 end;
 
-stoch_simul(nograph);
+stoch_simul(nograph, periods = 200);
+
+forecast;
+
-- 
GitLab