From bd42665121b975aa05f613f31a9cb824a2fee8b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Karam=C3=A9?= <frederic.karame@univ-lemans.fr> Date: Thu, 23 May 2024 15:30:16 +0200 Subject: [PATCH] Modifications for the online filter at order 3. --- .../nonlinear-filters/online_auxiliary_filter.m | 16 +++++++++------- .../solve_model_for_online_filter.m | 5 +++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/matlab/nonlinear-filters/online_auxiliary_filter.m b/matlab/nonlinear-filters/online_auxiliary_filter.m index 7495d4bc66..62634eeccf 100644 --- a/matlab/nonlinear-filters/online_auxiliary_filter.m +++ b/matlab/nonlinear-filters/online_auxiliary_filter.m @@ -143,9 +143,9 @@ for t=1:sample_size ghxx = ReducedForm.ghxx; ghuu = ReducedForm.ghuu; ghxu = ReducedForm.ghxu; - ghs2 = ReducedForm.ghs2; - if (order == 3) + if (order==3) % Set local state space model (third order approximation). + ghs2 = ReducedForm.ghs2; ghxxx = ReducedForm.ghxxx; ghuuu = ReducedForm.ghuuu; ghxxu = ReducedForm.ghxxu; @@ -171,7 +171,7 @@ for t=1:sample_size else if pruning yhat_ = bsxfun(@minus,StateVectors_(:,i),state_variables_steady_state_); - if order == 2 + if order <= 2 tmp = local_state_space_iteration_2(yhat, zeros(number_of_structural_innovations, 1), ghx, ghu, constant, ghxx, ghuu, ghxu, yhat_, steadystate, options_.threads.local_state_space_iteration_2); elseif order == 3 tmp = local_state_space_iteration_3(yhat_, zeros(number_of_structural_innovations, 1), ghx, ghu, ghxx, ghuu, ghxu, ghs2, ghxxx, ghuuu, ghxxu, ghxuu, ghxss, ghuss, steadystate, options_.threads.local_state_space_iteration_3, pruning); @@ -179,7 +179,7 @@ for t=1:sample_size error('Pruning is not available for orders > 3'); end else - if order == 2 + if order <= 2 tmp = local_state_space_iteration_2(yhat, zeros(number_of_structural_innovations, 1), ghx, ghu, constant, ghxx, ghuu, ghxu, options_.threads.local_state_space_iteration_2); elseif order == 3 tmp = local_state_space_iteration_3(yhat, zeros(number_of_structural_innovations, 1), ghx, ghu, ghxx, ghuu, ghxu, ghs2, ghxxx, ghuuu, ghxxu, ghxuu, ghxss, ghuss, steadystate, options_.threads.local_state_space_iteration_3, pruning); @@ -192,6 +192,8 @@ for t=1:sample_size % Replace Gaussian density with a Student density with 3 degrees of freedom for fat tails. z = sum(PredictionError.*(ReducedForm.H\PredictionError), 1) ; tau_tilde(i) = weights(i).*(tpdf(z, 3*ones(size(z)))+1e-99) ; + else + tau_tilde(i) = 0 ; end end % particles selection @@ -232,9 +234,9 @@ for t=1:sample_size ghxx = ReducedForm.ghxx; ghuu = ReducedForm.ghuu; ghxu = ReducedForm.ghxu; - ghs2 = ReducedForm.ghs2; if (order == 3) % Set local state space model (third order approximation). + ghs2 = ReducedForm.ghs2; ghxxx = ReducedForm.ghxxx; ghuuu = ReducedForm.ghuuu; ghxxu = ReducedForm.ghxxu; @@ -267,7 +269,7 @@ for t=1:sample_size else if pruning yhat_ = bsxfun(@minus,StateVectors_(:,i), state_variables_steady_state_); - if order == 2 + if order <= 2 [tmp, tmp_] = local_state_space_iteration_2(yhat, epsilon, ghx, ghu, constant, ghxx, ghuu, ghxu, yhat_, steadystate, options_.threads.local_state_space_iteration_2); elseif order == 3 [tmp, tmp_] = local_state_space_iteration_3(yhat_, epsilon, ghx, ghu, ghxx, ghuu, ghxu, ghs2, ghxxx, ghuuu, ghxxu, ghxuu, ghxss, ghuss, steadystate, options_.threads.local_state_space_iteration_3, pruning); @@ -276,7 +278,7 @@ for t=1:sample_size end StateVectors_(:,i) = tmp_(mf0_,:); else - if order == 2 + if order <= 2 tmp = local_state_space_iteration_2(yhat, epsilon, ghx, ghu, constant, ghxx, ghuu, ghxu, options_.threads.local_state_space_iteration_2); elseif order == 3 tmp = local_state_space_iteration_3(yhat, epsilon, ghx, ghu, ghxx, ghuu, ghxu, ghs2, ghxxx, ghuuu, ghxxu, ghxuu, ghxss, ghuss, steadystate, options_.threads.local_state_space_iteration_3, pruning); diff --git a/matlab/nonlinear-filters/solve_model_for_online_filter.m b/matlab/nonlinear-filters/solve_model_for_online_filter.m index f4a3e6af56..c7aa6785a9 100644 --- a/matlab/nonlinear-filters/solve_model_for_online_filter.m +++ b/matlab/nonlinear-filters/solve_model_for_online_filter.m @@ -125,8 +125,8 @@ M_.H = H; %------------------------------------------------------------------------------ warning('off', 'MATLAB:nearlySingularMatrix') -[~, ~, ~, info, oo_.dr, M_.params] = ... - dynare_resolve(M_, options_, oo_.dr, oo_.steady_state, oo_.exo_steady_state, oo_.exo_det_steady_state, 'restrict'); +[oo_.dr, info, M_.params] = ... + resol(0,M_, options_, oo_.dr, oo_.steady_state, oo_.exo_steady_state, oo_.exo_det_steady_state); warning('on', 'MATLAB:nearlySingularMatrix') if info(1)~=0 @@ -174,6 +174,7 @@ if nargout>4 ReducedForm.ghuu = zeros(size(restrict_variables_idx,1),n_shocks^2); ReducedForm.ghxu = zeros(size(restrict_variables_idx,1),n_states*n_shocks); ReducedForm.constant = ReducedForm.steadystate; +% ReducedForm.ghs2 = dr.ghs2(restrict_variables_idx,:); end ReducedForm.state_variables_steady_state = dr.ys(dr.order_var(state_variables_idx)); ReducedForm.Q = Q; -- GitLab