2019-12-21 10:37:48 +01:00
|
|
|
function [LIK,lik] = auxiliary_particle_filter(ReducedForm,Y,start,ParticleOptions,ThreadsOptions, DynareOptions, Model)
|
2014-12-12 18:09:04 +01:00
|
|
|
|
2017-05-18 23:59:10 +02:00
|
|
|
% Evaluates the likelihood of a nonlinear model with the auxiliary particle filter
|
2015-10-20 15:12:56 +02:00
|
|
|
% allowing eventually resampling.
|
|
|
|
%
|
2022-04-13 14:47:52 +02:00
|
|
|
% Copyright © 2011-2022 Dynare Team
|
2014-11-10 19:00:16 +01:00
|
|
|
%
|
2014-12-12 18:09:04 +01:00
|
|
|
% This file is part of Dynare (particles module).
|
2014-11-10 19:00:16 +01:00
|
|
|
%
|
|
|
|
% Dynare is free software: you can redistribute it and/or modify
|
|
|
|
% it under the terms of the GNU General Public License as published by
|
|
|
|
% the Free Software Foundation, either version 3 of the License, or
|
|
|
|
% (at your option) any later version.
|
|
|
|
%
|
2014-12-12 18:09:04 +01:00
|
|
|
% Dynare particles module is distributed in the hope that it will be useful,
|
2014-11-10 19:00:16 +01:00
|
|
|
% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
% GNU General Public License for more details.
|
|
|
|
%
|
|
|
|
% You should have received a copy of the GNU General Public License
|
2021-06-09 17:21:49 +02:00
|
|
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2014-12-12 18:09:04 +01:00
|
|
|
|
2014-11-10 19:00:16 +01:00
|
|
|
% Set default
|
|
|
|
if isempty(start)
|
|
|
|
start = 1;
|
|
|
|
end
|
2022-09-19 15:16:29 +02:00
|
|
|
% Get perturbation order
|
|
|
|
order = DynareOptions.order;
|
2014-11-10 19:00:16 +01:00
|
|
|
|
|
|
|
% Set flag for prunning
|
2014-12-12 19:14:35 +01:00
|
|
|
pruning = ParticleOptions.pruning;
|
2014-11-10 19:00:16 +01:00
|
|
|
|
|
|
|
% Get steady state and mean.
|
|
|
|
steadystate = ReducedForm.steadystate;
|
|
|
|
constant = ReducedForm.constant;
|
|
|
|
state_variables_steady_state = ReducedForm.state_variables_steady_state;
|
|
|
|
|
2019-12-21 10:37:48 +01:00
|
|
|
mf0 = ReducedForm.mf0;
|
|
|
|
mf1 = ReducedForm.mf1;
|
|
|
|
sample_size = size(Y,2);
|
2022-09-19 15:16:29 +02:00
|
|
|
number_of_state_variables = length(mf0);
|
2019-12-21 10:37:48 +01:00
|
|
|
number_of_observed_variables = length(mf1);
|
|
|
|
number_of_structural_innovations = length(ReducedForm.Q);
|
|
|
|
number_of_particles = ParticleOptions.number_of_particles;
|
|
|
|
|
|
|
|
if ReducedForm.use_k_order_solver
|
|
|
|
dr = ReducedForm.dr;
|
2022-02-04 11:29:31 +01:00
|
|
|
udr = ReducedForm.udr;
|
2019-12-21 10:37:48 +01:00
|
|
|
else
|
|
|
|
% Set local state space model (first order approximation).
|
|
|
|
ghx = ReducedForm.ghx;
|
|
|
|
ghu = ReducedForm.ghu;
|
|
|
|
% Set local state space model (second order approximation).
|
|
|
|
ghxx = ReducedForm.ghxx;
|
|
|
|
ghuu = ReducedForm.ghuu;
|
|
|
|
ghxu = ReducedForm.ghxu;
|
2023-07-01 16:43:48 +02:00
|
|
|
ghs2 = ReducedForm.ghs2;
|
2022-09-19 15:43:10 +02:00
|
|
|
if (order == 3)
|
|
|
|
% Set local state space model (third order approximation).
|
|
|
|
ghxxx = ReducedForm.ghxxx;
|
|
|
|
ghuuu = ReducedForm.ghuuu;
|
|
|
|
ghxxu = ReducedForm.ghxxu;
|
|
|
|
ghxuu = ReducedForm.ghxuu;
|
|
|
|
ghxss = ReducedForm.ghxss;
|
|
|
|
ghuss = ReducedForm.ghuss;
|
|
|
|
end
|
2014-11-10 19:00:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
% Get covariance matrices
|
|
|
|
Q = ReducedForm.Q;
|
|
|
|
H = ReducedForm.H;
|
|
|
|
|
|
|
|
% Get initial condition for the state vector.
|
|
|
|
StateVectorMean = ReducedForm.StateVectorMean;
|
2015-10-20 15:12:56 +02:00
|
|
|
StateVectorVarianceSquareRoot = chol(ReducedForm.StateVectorVariance)';
|
2014-11-10 19:00:16 +01:00
|
|
|
state_variance_rank = size(StateVectorVarianceSquareRoot,2);
|
|
|
|
Q_lower_triangular_cholesky = chol(Q)';
|
|
|
|
|
|
|
|
% Set seed for randn().
|
2023-09-25 13:25:39 +02:00
|
|
|
DynareOptions=set_dynare_seed_local_options(DynareOptions,'default');
|
2014-11-10 19:00:16 +01:00
|
|
|
|
|
|
|
% Initialization of the likelihood.
|
2015-10-20 15:12:56 +02:00
|
|
|
const_lik = log(2*pi)*number_of_observed_variables+log(det(H));
|
2014-11-10 19:00:16 +01:00
|
|
|
lik = NaN(sample_size,1);
|
|
|
|
LIK = NaN;
|
|
|
|
|
|
|
|
% Initialization of the weights across particles.
|
|
|
|
weights = ones(1,number_of_particles)/number_of_particles ;
|
|
|
|
StateVectors = bsxfun(@plus,StateVectorVarianceSquareRoot*randn(state_variance_rank,number_of_particles),StateVectorMean);
|
2015-10-20 15:12:56 +02:00
|
|
|
%StateVectors = bsxfun(@plus,zeros(state_variance_rank,number_of_particles),StateVectorMean);
|
2014-11-10 19:00:16 +01:00
|
|
|
if pruning
|
2022-09-19 15:16:29 +02:00
|
|
|
if order == 2
|
|
|
|
StateVectors_ = StateVectors;
|
|
|
|
state_variables_steady_state_ = state_variables_steady_state;
|
|
|
|
mf0_ = mf0;
|
2022-09-19 15:43:10 +02:00
|
|
|
elseif order == 3
|
2023-07-01 16:43:48 +02:00
|
|
|
StateVectors_ = repmat(StateVectors,3,1);
|
|
|
|
state_variables_steady_state_ = repmat(state_variables_steady_state,3,1);
|
|
|
|
mf0_ = repmat(mf0,1,3);
|
|
|
|
mask2 = number_of_state_variables+1:2*number_of_state_variables;
|
|
|
|
mask3 = 2*number_of_state_variables+1:3*number_of_state_variables;
|
|
|
|
mf0_(mask2) = mf0_(mask2)+size(ghx,1);
|
|
|
|
mf0_(mask3) = mf0_(mask3)+2*size(ghx,1);
|
2022-09-19 15:16:29 +02:00
|
|
|
else
|
2022-09-19 15:43:10 +02:00
|
|
|
error('Pruning is not available for orders > 3');
|
2022-09-19 15:16:29 +02:00
|
|
|
end
|
2014-11-10 19:00:16 +01:00
|
|
|
end
|
2014-12-18 14:29:00 +01:00
|
|
|
|
2014-11-10 19:00:16 +01:00
|
|
|
for t=1:sample_size
|
|
|
|
yhat = bsxfun(@minus,StateVectors,state_variables_steady_state);
|
2015-01-07 14:22:12 +01:00
|
|
|
if pruning
|
2022-09-19 15:16:29 +02:00
|
|
|
yhat_ = bsxfun(@minus,StateVectors_,state_variables_steady_state_);
|
|
|
|
if order == 2
|
|
|
|
[tmp, tmp_] = local_state_space_iteration_2(yhat,zeros(number_of_structural_innovations,number_of_particles),ghx,ghu,constant,ghxx,ghuu,ghxu,yhat_,steadystate,ThreadsOptions.local_state_space_iteration_2);
|
2022-09-19 15:43:10 +02:00
|
|
|
elseif order == 3
|
2023-07-01 16:43:48 +02:00
|
|
|
[tmp, tmp_] = local_state_space_iteration_3(yhat_, zeros(number_of_structural_innovations,number_of_particles), ghx, ghu, ghxx, ghuu, ghxu, ghs2, ghxxx, ghuuu, ghxxu, ghxuu, ghxss, ghuss, steadystate, ThreadsOptions.local_state_space_iteration_3, pruning);
|
2022-09-19 15:16:29 +02:00
|
|
|
else
|
2022-09-19 15:43:10 +02:00
|
|
|
error('Pruning is not available for orders > 3');
|
2015-01-07 14:22:12 +01:00
|
|
|
end
|
|
|
|
else
|
2019-12-21 10:37:48 +01:00
|
|
|
if ReducedForm.use_k_order_solver
|
2022-09-19 15:16:29 +02:00
|
|
|
tmp = local_state_space_iteration_k(yhat, zeros(number_of_structural_innovations,number_of_particles), dr, Model, DynareOptions, udr);
|
2019-12-21 10:37:48 +01:00
|
|
|
else
|
2022-09-19 15:16:29 +02:00
|
|
|
if order == 2
|
|
|
|
tmp = local_state_space_iteration_2(yhat,zeros(number_of_structural_innovations,number_of_particles),ghx,ghu,constant,ghxx,ghuu,ghxu,ThreadsOptions.local_state_space_iteration_2);
|
2022-09-19 15:43:10 +02:00
|
|
|
elseif order == 3
|
2023-07-01 16:43:48 +02:00
|
|
|
tmp = local_state_space_iteration_3(yhat, zeros(number_of_structural_innovations,number_of_particles), ghx, ghu, ghxx, ghuu, ghxu, ghs2, ghxxx, ghuuu, ghxxu, ghxuu, ghxss, ghuss, steadystate, ThreadsOptions.local_state_space_iteration_3, pruning);
|
2022-09-19 15:16:29 +02:00
|
|
|
else
|
2022-09-19 15:43:10 +02:00
|
|
|
error('Order > 3: use_k_order_solver should be set to true');
|
2019-12-21 10:37:48 +01:00
|
|
|
end
|
2015-01-07 14:22:12 +01:00
|
|
|
end
|
2014-11-10 19:00:16 +01:00
|
|
|
end
|
|
|
|
PredictionError = bsxfun(@minus,Y(:,t),tmp(mf1,:));
|
2015-10-20 15:12:56 +02:00
|
|
|
z = sum(PredictionError.*(H\PredictionError),1) ;
|
|
|
|
tau_tilde = weights.*(tpdf(z,3*ones(size(z)))+1e-99) ;
|
|
|
|
tau_tilde = tau_tilde/sum(tau_tilde) ;
|
2014-12-18 14:29:00 +01:00
|
|
|
indx = resample(0,tau_tilde',ParticleOptions);
|
2014-11-10 19:00:16 +01:00
|
|
|
if pruning
|
2014-12-18 14:29:00 +01:00
|
|
|
yhat_ = yhat_(:,indx) ;
|
2014-11-10 19:00:16 +01:00
|
|
|
end
|
2014-12-18 14:29:00 +01:00
|
|
|
yhat = yhat(:,indx) ;
|
2017-05-18 23:59:10 +02:00
|
|
|
weights_stage_1 = weights(indx)./tau_tilde(indx) ;
|
2014-11-10 19:00:16 +01:00
|
|
|
epsilon = Q_lower_triangular_cholesky*randn(number_of_structural_innovations,number_of_particles);
|
|
|
|
if pruning
|
2022-09-19 15:16:29 +02:00
|
|
|
if order == 2
|
|
|
|
[tmp, tmp_] = local_state_space_iteration_2(yhat,epsilon,ghx,ghu,constant,ghxx,ghuu,ghxu,yhat_,steadystate,ThreadsOptions.local_state_space_iteration_2);
|
2022-09-19 15:43:10 +02:00
|
|
|
elseif order == 3
|
2023-07-01 16:43:48 +02:00
|
|
|
[tmp, tmp_] = local_state_space_iteration_3(yhat_, epsilon, ghx, ghu, ghxx, ghuu, ghxu, ghs2, ghxxx, ghuuu, ghxxu, ghxuu, ghxss, ghuss, steadystate, ThreadsOptions.local_state_space_iteration_3, pruning);
|
2022-09-19 15:16:29 +02:00
|
|
|
else
|
2022-09-19 15:43:10 +02:00
|
|
|
error('Pruning is not available for orders > 3');
|
2022-09-19 15:16:29 +02:00
|
|
|
end
|
|
|
|
StateVectors_ = tmp_(mf0_,:);
|
2014-11-10 19:00:16 +01:00
|
|
|
else
|
2019-12-21 10:37:48 +01:00
|
|
|
if ReducedForm.use_k_order_solver
|
2022-02-04 11:29:31 +01:00
|
|
|
tmp = local_state_space_iteration_k(yhat, epsilon, dr, Model, DynareOptions, udr);
|
2019-12-21 10:37:48 +01:00
|
|
|
else
|
2022-09-19 15:16:29 +02:00
|
|
|
if order == 2
|
|
|
|
tmp = local_state_space_iteration_2(yhat, epsilon, ghx, ghu, constant, ghxx, ghuu, ghxu, ThreadsOptions.local_state_space_iteration_2);
|
2022-09-19 15:43:10 +02:00
|
|
|
elseif order == 3
|
2023-07-01 16:43:48 +02:00
|
|
|
tmp = local_state_space_iteration_3(yhat, epsilon, ghx, ghu, ghxx, ghuu, ghxu, ghs2, ghxxx, ghuuu, ghxxu, ghxuu, ghxss, ghuss, steadystate, ThreadsOptions.local_state_space_iteration_3, pruning);
|
2022-09-19 15:16:29 +02:00
|
|
|
else
|
2022-09-19 15:43:10 +02:00
|
|
|
error('Order > 3: use_k_order_solver should be set to true');
|
2022-09-19 15:16:29 +02:00
|
|
|
end
|
2019-12-21 10:37:48 +01:00
|
|
|
end
|
2014-11-10 19:00:16 +01:00
|
|
|
end
|
|
|
|
StateVectors = tmp(mf0,:);
|
|
|
|
PredictionError = bsxfun(@minus,Y(:,t),tmp(mf1,:));
|
2015-10-20 15:12:56 +02:00
|
|
|
weights_stage_2 = weights_stage_1.*(exp(-.5*(const_lik+sum(PredictionError.*(H\PredictionError),1))) + 1e-99) ;
|
2017-05-18 23:59:10 +02:00
|
|
|
lik(t) = log(mean(weights_stage_2)) ;
|
2015-10-20 15:12:56 +02:00
|
|
|
weights = weights_stage_2/sum(weights_stage_2);
|
|
|
|
if (ParticleOptions.resampling.status.generic && neff(weights)<ParticleOptions.resampling.threshold*sample_size) || ParticleOptions.resampling.status.systematic
|
|
|
|
if pruning
|
|
|
|
temp = resample([StateVectors' StateVectors_'],weights',ParticleOptions);
|
|
|
|
StateVectors = temp(:,1:number_of_state_variables)';
|
2022-09-19 15:16:29 +02:00
|
|
|
StateVectors_ = temp(:,number_of_state_variables+1:end)';
|
2015-10-20 15:12:56 +02:00
|
|
|
else
|
|
|
|
StateVectors = resample(StateVectors',weights',ParticleOptions)';
|
|
|
|
end
|
|
|
|
weights = ones(1,number_of_particles)/number_of_particles;
|
|
|
|
end
|
2014-11-10 19:00:16 +01:00
|
|
|
end
|
|
|
|
|
2022-02-04 11:29:31 +01:00
|
|
|
LIK = -sum(lik(start:end));
|