| 1 |
/* |
| 2 |
Tests that local_state_space_iteration_3 and local_state_space_iteration_k (for k=3) return the same results |
| 3 |
|
| 4 |
This file must be run after first_spec.mod (both are based on the same model). |
| 5 |
*/ |
| 6 |
|
| 7 |
@#include "first_spec_common.inc" |
| 8 |
|
| 9 |
varobs q ca; |
| 10 |
|
| 11 |
shocks; |
| 12 |
var eeps = 0.04^2; |
| 13 |
var nnu = 0.03^2; |
| 14 |
var q = 0.01^2; |
| 15 |
var ca = 0.01^2; |
| 16 |
end; |
| 17 |
|
| 18 |
// Initialize various structures |
| 19 |
estimation(datafile='my_data.mat',order=3,mode_compute=0,mh_replic=0,filter_algorithm=sis,nonlinear_filter_initialization=2 |
| 20 |
,cova_compute=0 %tell program that no covariance matrix was computed |
| 21 |
); |
| 22 |
|
| 23 |
stoch_simul(order=3, periods=200, irf=0, k_order_solver); |
| 24 |
|
| 25 |
// Really perform the test |
| 26 |
|
| 27 |
nparticles = options_.particle.number_of_particles; |
| 28 |
nsims = 1e6/nparticles; |
| 29 |
|
| 30 |
/* We generate particles using realistic distributions (though this is not |
| 31 |
strictly needed) */ |
| 32 |
nstates = M_.npred + M_.nboth; |
| 33 |
state_idx = oo_.dr.order_var((M_.nstatic+1):(M_.nstatic+nstates)); |
| 34 |
yhat = chol(oo_.var(state_idx,state_idx))*randn(nstates, nparticles); |
| 35 |
epsilon = chol(M_.Sigma_e)*randn(M_.exo_nbr, nparticles); |
| 36 |
|
| 37 |
dr = oo_.dr; |
| 38 |
|
| 39 |
|
| 40 |
// “rf” stands for “Reduced Form” |
| 41 |
rf_ghx = dr.ghx(dr.restrict_var_list, :); |
| 42 |
rf_ghu = dr.ghu(dr.restrict_var_list, :); |
| 43 |
rf_ss = dr.ys(dr.order_var); |
| 44 |
rf_ss = rf_ss(dr.restrict_var_list, :); |
| 45 |
rf_constant = dr.ys(dr.order_var)+0.5*dr.ghs2; |
| 46 |
rf_constant = rf_constant(dr.restrict_var_list, :); |
| 47 |
rf_ghxx = dr.ghxx(dr.restrict_var_list, :); |
| 48 |
rf_ghuu = dr.ghuu(dr.restrict_var_list, :); |
| 49 |
rf_ghxu = dr.ghxu(dr.restrict_var_list, :); |
| 50 |
rf_ghs2 = dr.ghs2(dr.restrict_var_list, :); |
| 51 |
rf_ghxxx = dr.ghxxx(dr.restrict_var_list, :); |
| 52 |
rf_ghuuu = dr.ghuuu(dr.restrict_var_list, :); |
| 53 |
rf_ghxxu = dr.ghxxu(dr.restrict_var_list, :); |
| 54 |
rf_ghxuu = dr.ghxuu(dr.restrict_var_list, :); |
| 55 |
rf_ghxss = dr.ghxss(dr.restrict_var_list, :); |
| 56 |
rf_ghuss = dr.ghuss(dr.restrict_var_list, :); |
| 57 |
|
| 58 |
% Without pruning |
| 59 |
tStart1 = tic; for i=1:nsims, ynext1 = local_state_space_iteration_3(yhat, epsilon, rf_ghx, rf_ghu, rf_ghxx, rf_ghuu, rf_ghxu, rf_ghs2, rf_ghxxx, rf_ghuuu, rf_ghxxu, rf_ghxuu, rf_ghxss, rf_ghuss, rf_ss, options_.threads.local_state_space_iteration_3, false); end, tElapsed1 = toc(tStart1); |
| 60 |
tStart2 = tic; [udr] = folded_to_unfolded_dr(dr, M_, options_); for i=1:nsims, ynext2 = local_state_space_iteration_k(yhat, epsilon, dr, M_, options_, udr); end, tElapsed2 = toc(tStart2); |
| 61 |
|
| 62 |
if max(max(abs(ynext1-ynext2))) > 1e-10 |
| 63 |
error('Without pruning: Inconsistency between local_state_space_iteration_3 and local_state_space_iteration_k') |
| 64 |
end |
| 65 |
|
| 66 |
if tElapsed1<tElapsed2 |
| 67 |
skipline() |
| 68 |
dprintf('Without pruning: local_state_space_iteration_3 is %5.2f times faster than local_state_space_iteration_k', tElapsed2/tElapsed1) |
| 69 |
skipline() |
| 70 |
else |
| 71 |
skipline() |
| 72 |
dprintf('Without pruning: local_state_space_iteration_3 is %5.2f times slower than local_state_space_iteration_k', tElapsed1/tElapsed2) |
| 73 |
skipline() |
| 74 |
end |
| 75 |
|
| 76 |
% With pruning |
| 77 |
yhat1_KKSS = chol(oo_.var(state_idx,state_idx))*randn(nstates, nparticles); |
| 78 |
yhat2_KKSS = chol(oo_.var(state_idx,state_idx))*randn(nstates, nparticles); |
| 79 |
yhat1_AVR = yhat1_KKSS; |
| 80 |
yhat2_AVR = yhat2_KKSS-yhat1_AVR; |
| 81 |
yhat3_AVR = chol(oo_.var(state_idx,state_idx))*randn(nstates, nparticles); |
| 82 |
yhat_AVR = [yhat1_AVR;yhat2_AVR;yhat3_AVR]; |
| 83 |
nstatesandobs = size(rf_ghx,1); |
| 84 |
|
| 85 |
[ynext3,ynext3_lat] = local_state_space_iteration_3(yhat_AVR, epsilon, rf_ghx, rf_ghu, rf_ghxx, rf_ghuu, rf_ghxu, rf_ghs2, rf_ghxxx, rf_ghuuu, rf_ghxxu, rf_ghxuu, rf_ghxss, rf_ghuss, rf_ss, options_.threads.local_state_space_iteration_3, true); |
| 86 |
[ynext2,ynext2_lat] = local_state_space_iteration_2(yhat2_KKSS, epsilon, rf_ghx, rf_ghu, rf_constant, rf_ghxx, rf_ghuu, rf_ghxu, yhat1_KKSS, rf_ss, options_.threads.local_state_space_iteration_2); |
| 87 |
if max(max(abs(ynext3_lat(1:nstatesandobs,:)-ynext2_lat))) > 1e-14 |
| 88 |
error('With pruning: inconsistency between local_state_space_iteration_2 and local_state_space_iteration_3 on the 1st-order pruned variable') |
| 89 |
end |
| 90 |
ynext3_2nd = ynext3_lat(1:nstatesandobs,:)+ynext3_lat(nstatesandobs+1:2*nstatesandobs,:)-rf_ss; |
| 91 |
if max(abs(ynext3_2nd(:)-ynext2(:))) > 1e-14 |
| 92 |
error('With pruning: inconsistency between local_state_space_iteration_2 and local_state_space_iteration_3 on the 2nd-order pruned variable') |
| 93 |
end |