| 1 |
/* |
| 2 |
Tests that local_state_space_iteration_2 and local_state_space_iteration_k (for k=2) 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=2,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=2, 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 |
state_idx = oo_.dr.order_var((M_.nstatic+1):(M_.nstatic+M_.npred+M_.nboth)); |
| 33 |
yhat = chol(oo_.var(state_idx,state_idx))*randn(M_.npred+M_.nboth, nparticles); |
| 34 |
epsilon = chol(M_.Sigma_e)*randn(M_.exo_nbr, nparticles); |
| 35 |
|
| 36 |
dr = oo_.dr; |
| 37 |
|
| 38 |
|
| 39 |
// “rf” stands for “Reduced Form” |
| 40 |
rf_ghx = dr.ghx(dr.restrict_var_list, :); |
| 41 |
rf_ghu = dr.ghu(dr.restrict_var_list, :); |
| 42 |
rf_constant = dr.ys(dr.order_var)+0.5*dr.ghs2; |
| 43 |
rf_constant = rf_constant(dr.restrict_var_list, :); |
| 44 |
rf_ghxx = dr.ghxx(dr.restrict_var_list, :); |
| 45 |
rf_ghuu = dr.ghuu(dr.restrict_var_list, :); |
| 46 |
rf_ghxu = dr.ghxu(dr.restrict_var_list, :); |
| 47 |
|
| 48 |
tStart1 = tic; for i=1:nsims, ynext1 = local_state_space_iteration_2(yhat, epsilon, rf_ghx, rf_ghu, rf_constant, rf_ghxx, rf_ghuu, rf_ghxu, options_.threads.local_state_space_iteration_2); end, tElapsed1 = toc(tStart1); |
| 49 |
|
| 50 |
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); |
| 51 |
|
| 52 |
if max(max(abs(ynext1-ynext2))) > 1e-14 |
| 53 |
error('Inconsistency between local_state_space_iteration_2 and local_state_space_iteration_k') |
| 54 |
end |
| 55 |
|
| 56 |
if tElapsed1<tElapsed2 |
| 57 |
skipline() |
| 58 |
dprintf('local_state_space_iteration_2 is %5.2f times faster than local_state_space_iteration_k', tElapsed2/tElapsed1) |
| 59 |
skipline() |
| 60 |
else |
| 61 |
skipline() |
| 62 |
dprintf('local_state_space_iteration_2 is %5.2f times slower than local_state_space_iteration_k', tElapsed1/tElapsed2) |
| 63 |
skipline() |
| 64 |
end |