From 77342bcae5433a5614c3109e11ff6e687343e387 Mon Sep 17 00:00:00 2001
From: Johannes Pfeifer <jpfeifer@gmx.de>
Date: Tue, 26 Jan 2021 09:14:34 +0100
Subject: [PATCH] Port state variance initialization fix from
 https://git.dynare.org/Dynare/dynare/-/commit/dab5d38068db477493eb5e7213c82d3e3a8b543c

(cherry picked from commit 60f71f27205dfd2002cb3d37177eeab5e9362384)
---
 src/solve_model_for_online_filter.m | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/solve_model_for_online_filter.m b/src/solve_model_for_online_filter.m
index fbfcb05..7f046ac 100644
--- a/src/solve_model_for_online_filter.m
+++ b/src/solve_model_for_online_filter.m
@@ -185,15 +185,22 @@ if setinitialcondition
     switch DynareOptions.particle.initialization
       case 1% Initial state vector covariance is the ergodic variance associated to the first order Taylor-approximation of the model.
         StateVectorMean = ReducedForm.state_variables_steady_state;%.constant(mf0);
-        StateVectorVariance = lyapunov_symm(ReducedForm.ghx(mf0,:),ReducedForm.ghu(mf0,:)*ReducedForm.Q*ReducedForm.ghu(mf0,:)',DynareOptions.lyapunov_fixed_point_tol,DynareOptions.qz_criterium,DynareOptions.lyapunov_complex_threshold);
+        [A,B] = kalman_transition_matrix(dr,dr.restrict_var_list,dr.restrict_columns,Model.exo_nbr);
+        StateVectorVariance2 = lyapunov_symm(ReducedForm.ghx(mf0,:),ReducedForm.ghu(mf0,:)*ReducedForm.Q*ReducedForm.ghu(mf0,:)',DynareOptions.lyapunov_fixed_point_tol,DynareOptions.qz_criterium,DynareOptions.lyapunov_complex_threshold);
+        StateVectorVariance = lyapunov_symm(A, B*ReducedForm.Q*B', DynareOptions.lyapunov_fixed_point_tol, ...
+                                        DynareOptions.qz_criterium, DynareOptions.lyapunov_complex_threshold, [], DynareOptions.debug);
+        StateVectorVariance = StateVectorVariance(mf0,mf0);
       case 2% Initial state vector covariance is a monte-carlo based estimate of the ergodic variance (consistent with a k-order Taylor-approximation of the model).
         StateVectorMean = ReducedForm.state_variables_steady_state;%.constant(mf0);
         old_DynareOptionsperiods = DynareOptions.periods;
         DynareOptions.periods = 5000;
-        y_ = simult(oo_.steady_state, dr, Model, DynareOptions, DynareResults);
-        y_ = y_(state_variables_idx,2001:5000);
+        old_DynareOptionspruning =  DynareOptions.pruning;
+        DynareOptions.pruning = DynareOptions.particle.pruning;
+        y_ = simult(dr.ys, dr, Model, DynareOptions, DynareResults);
+        y_ = y_(dr.order_var(state_variables_idx),2001:DynareOptions.periods);
         StateVectorVariance = cov(y_');
         DynareOptions.periods = old_DynareOptionsperiods;
+        DynareOptions.pruning = old_DynareOptionspruning;
         clear('old_DynareOptionsperiods','y_');
       case 3% Initial state vector covariance is a diagonal matrix.
         StateVectorMean = ReducedForm.state_variables_steady_state;%.constant(mf0);
-- 
GitLab