From 4cf9f7d1a7fa5dac8f91ff3344a6114c7f15959c Mon Sep 17 00:00:00 2001
From: Johannes Pfeifer <jpfeifer@gmx.de>
Date: Tue, 15 Dec 2020 14:38:33 +0100
Subject: [PATCH] stoch_simul.m: reports second moments based on pruned state
 space at order=2 when pruning is specified

Related to https://git.dynare.org/Dynare/dynare/-/merge_requests/1744
---
 doc/manual/source/the-model-file.rst          |  8 +++-
 ...m => disp_th_moments_pruned_state_space.m} |  0
 matlab/stoch_simul.m                          |  6 +--
 tests/Makefile.am                             |  1 +
 tests/moments/example1_order2_pruning.mod     | 46 +++++++++++++++++++
 5 files changed, 56 insertions(+), 5 deletions(-)
 rename matlab/{disp_th_moments_order3.m => disp_th_moments_pruned_state_space.m} (100%)
 create mode 100644 tests/moments/example1_order2_pruning.mod

diff --git a/doc/manual/source/the-model-file.rst b/doc/manual/source/the-model-file.rst
index d5f6e6e01d..1b134750a3 100644
--- a/doc/manual/source/the-model-file.rst
+++ b/doc/manual/source/the-model-file.rst
@@ -3819,7 +3819,7 @@ Computing the stochastic solution
        :mvar:`oo_.conditional_variance_decomposition_ME`).  The
        variance decomposition is only conducted, if theoretical
        moments are requested, *i.e.* using the ``periods=0``-option. 
-       Only available at ``order<3``. In case of ``order=2``, 
+       Only available at ``order<3`` and without ``pruning''. In case of ``order=2``, 
        Dynare provides a second-order accurate
        approximation to the true second moments based on the linear
        terms of the second-order solution (see *Kim, Kim,
@@ -3836,7 +3836,11 @@ Computing the stochastic solution
        algorithm of *Kim, Kim, Schaumburg and Sims (2008)*, while at
        third order its generalization by *Andreasen,
        Fernández-Villaverde and Rubio-Ramírez (2018)* is used.
-       Not available above third order.
+       Not available above third order. When specified, theoretical moments
+       are based on the pruned state space, i.e. the computation of second moments 
+       uses all terms as in *Andreasen, Fernández-Villaverde and Rubio-Ramírez (2018), page 10* 
+       as opposed to simply providing a second-order accurate result based on the 
+       linear solution as in *Kim, Kim, Schaumburg and Sims (2008)*.
 
     .. option:: partial_information
 
diff --git a/matlab/disp_th_moments_order3.m b/matlab/disp_th_moments_pruned_state_space.m
similarity index 100%
rename from matlab/disp_th_moments_order3.m
rename to matlab/disp_th_moments_pruned_state_space.m
diff --git a/matlab/stoch_simul.m b/matlab/stoch_simul.m
index ce517a8af7..47415c8ede 100644
--- a/matlab/stoch_simul.m
+++ b/matlab/stoch_simul.m
@@ -189,11 +189,11 @@ if ~options_.nomoments
     if PI_PCL_solver
         PCL_Part_info_moments(0, PCL_varobs, oo_.dr, i_var);
     elseif options_.periods == 0
-        if options_.order <= 2
+        if options_.order == 1 || (options_.order == 2 && ~options_.pruning)
             oo_=disp_th_moments(oo_.dr,var_list,M_,options_,oo_);
-        elseif options_.order == 3 && options_.pruning  
+        elseif (ismember(options_.order,[2,3])) && options_.pruning  
             % There is no code for theoretical moments at 3rd order without pruning
-            oo_=disp_th_moments_order3(oo_.dr,M_,options_,i_var,oo_);
+            oo_=disp_th_moments_pruned_state_space(oo_.dr,M_,options_,i_var,oo_);
         end
     else
         oo_=disp_moments(oo_.endo_simul,var_list,M_,options_,oo_);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 64a77571eb..3fdbcb9e62 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -3,6 +3,7 @@ MODFILES = \
 	optimizers/fs2000_6.mod \
 	moments/example1_hp_test.mod \
 	moments/fs2000_post_moments.mod \
+	moments/example1_order2_pruning.mod \
 	lmmcp/rbcii.mod \
 	lmmcp/purely_backward.mod \
 	lmmcp/purely_forward.mod \
diff --git a/tests/moments/example1_order2_pruning.mod b/tests/moments/example1_order2_pruning.mod
new file mode 100644
index 0000000000..bd13e0c84a
--- /dev/null
+++ b/tests/moments/example1_order2_pruning.mod
@@ -0,0 +1,46 @@
+// Example 1 from Collard's guide to Dynare
+// tests moments at order=2 with pruning
+var y, k, a, h, b;
+varexo e, u;
+
+parameters beta, rho, alpha, delta, theta, psi, tau;
+
+alpha = 0.36;
+rho   = 0.95;
+tau   = 0.025;
+beta  = 0.99;
+delta = 0.025;
+psi   = 0;
+theta = 2.95;
+
+phi   = 0.1;
+
+model;
+[endogenous='c',name='law of motion of capital']
+c*theta*h^(1+psi)=(1-alpha)*y;
+k = beta*(((exp(b)*c)/(exp(b(+1))*c(+1)))
+    *(exp(b(+1))*alpha*y(+1)+(1-delta)*k));
+y = exp(a)*(k(-1)^alpha)*(h^(1-alpha));
+k = exp(b)*(y-c)+(1-delta)*k(-1);
+a = rho*a(-1)+tau*b(-1) + e;
+b = tau*a(-1)+rho*b(-1) + u;
+end;
+
+initval;
+y = 1.08068253095672;
+c = 0.80359242014163;
+h = 0.29175631001732;
+k = 11.08360443260358;
+a = 0;
+b = 0;
+e = 0;
+u = 0;
+end;
+
+shocks;
+var e; stderr 0.009;
+var u; stderr 0.009;
+var e, u = phi*0.009*0.009;
+end;
+
+stoch_simul(order=2,pruning);
\ No newline at end of file
-- 
GitLab