Commit 4604148d authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Put back pruning in particle filter (implemented in sequential_importance_particle_filter).

parent c7bd0522
...@@ -68,6 +68,9 @@ if isempty(start) ...@@ -68,6 +68,9 @@ if isempty(start)
start = 1; start = 1;
end end
% Set flag for prunning
pruning = DynareOptions.particle.pruning;
% Get steady state and mean. % Get steady state and mean.
steadystate = ReducedForm.steadystate; steadystate = ReducedForm.steadystate;
constant = ReducedForm.constant; constant = ReducedForm.constant;
...@@ -103,7 +106,15 @@ end ...@@ -103,7 +106,15 @@ end
% Get initial condition for the state vector. % Get initial condition for the state vector.
StateVectorMean = ReducedForm.StateVectorMean; StateVectorMean = ReducedForm.StateVectorMean;
StateVectorVarianceSquareRoot = reduced_rank_cholesky(ReducedForm.StateVectorVariance)'; StateVectorVarianceSquareRoot = reduced_rank_cholesky(ReducedForm.StateVectorVariance)';
if pruning
StateVectorMean_ = StateVectorMean;
StateVectorVarianceSquareRoot_ = StateVectorVarianceSquareRoot;
end
% Get the rank of StateVectorVarianceSquareRoot
state_variance_rank = size(StateVectorVarianceSquareRoot,2); state_variance_rank = size(StateVectorVarianceSquareRoot,2);
% Factorize the covariance matrix of the structural innovations
Q_lower_triangular_cholesky = chol(Q)'; Q_lower_triangular_cholesky = chol(Q)';
% Set seed for randn(). % Set seed for randn().
...@@ -114,35 +125,55 @@ const_lik = log(2*pi)*number_of_observed_variables; ...@@ -114,35 +125,55 @@ const_lik = log(2*pi)*number_of_observed_variables;
lik = NaN(sample_size,1); lik = NaN(sample_size,1);
% Initialization of the weights across particles. % Initialization of the weights across particles.
nb_obs_resamp = 0 ;
weights = ones(1,number_of_particles)/number_of_particles ; weights = ones(1,number_of_particles)/number_of_particles ;
StateVectors = bsxfun(@plus,StateVectorVarianceSquareRoot*randn(state_variance_rank,number_of_particles),StateVectorMean); StateVectors = bsxfun(@plus,StateVectorVarianceSquareRoot*randn(state_variance_rank,number_of_particles),StateVectorMean);
if pruning
StateVectors_ = StateVectors;
end
% Loop over observations
for t=1:sample_size for t=1:sample_size
yhat = bsxfun(@minus,StateVectors,state_variables_steady_state); yhat = bsxfun(@minus,StateVectors,state_variables_steady_state);
epsilon = Q_lower_triangular_cholesky*randn(number_of_structural_innovations,number_of_particles); epsilon = Q_lower_triangular_cholesky*randn(number_of_structural_innovations,number_of_particles);
if pruning
yhat_ = bsxfun(@minus,StateVectors_,state_variables_steady_state);
[tmp, tmp_] = local_state_space_iteration_2(yhat,epsilon,ghx,ghu,constant,ghxx,ghuu,ghxu,yhat_,steadystate,DynareOptions.threads.local_state_space_iteration_2);
else
tmp = local_state_space_iteration_2(yhat,epsilon,ghx,ghu,constant,ghxx,ghuu,ghxu,DynareOptions.threads.local_state_space_iteration_2); tmp = local_state_space_iteration_2(yhat,epsilon,ghx,ghu,constant,ghxx,ghuu,ghxu,DynareOptions.threads.local_state_space_iteration_2);
end
PredictedObservedMean = tmp(mf1,:)*transpose(weights); PredictedObservedMean = tmp(mf1,:)*transpose(weights);
PredictionError = bsxfun(@minus,Y(:,t),tmp(mf1,:)); PredictionError = bsxfun(@minus,Y(:,t),tmp(mf1,:));
dPredictedObservedMean = bsxfun(@minus,tmp(mf1,:),PredictedObservedMean); dPredictedObservedMean = bsxfun(@minus,tmp(mf1,:),PredictedObservedMean);
PredictedObservedVariance = dPredictedObservedMean*diag(weights)*dPredictedObservedMean' + H; PredictedObservedVariance = bsxfun(@times,dPredictedObservedMean,weights)*dPredictedObservedMean' + H;
lnw = -.5*(const_lik+log(det(PredictedObservedVariance))+sum(PredictionError.*(PredictedObservedVariance\PredictionError),1)); lnw = -.5*(const_lik+log(det(PredictedObservedVariance))+sum(PredictionError.*(PredictedObservedVariance\PredictionError),1));
dfac = max(lnw); dfac = max(lnw);
wtilde = weights.*exp(lnw-dfac); wtilde = weights.*exp(lnw-dfac);
lik(t) = log(sum(wtilde))+dfac; lik(t) = log(sum(wtilde))+dfac;
weights = wtilde/sum(wtilde); weights = wtilde/sum(wtilde);
if strcmp(DynareOptions.particle.resampling.status,'generic') if (strcmp(DynareOptions.particle.resampling.status,'generic') && neff(weights)<DynareOptions.particle.resampling.neff_threshold*sample_size ) || strcmp(DynareOptions.particle.resampling.status,'systematic')
Neff = 1/(weights*weights'); idx = resample(weights,DynareOptions.particle.resampling.method1,DynareOptions.particle.resampling.method2);
StateVectors = tmp(mf0,idx);
if pruning
StateVectors_ = tmp_(mf0,idx);
end end
if (strcmp(DynareOptions.particle.resampling.status,'generic') && Neff<DynareOptions.particle.resampling.neff_threshold*sample_size ) || strcmp(DynareOptions.particle.resampling.status,'systematic')
nb_obs_resamp = nb_obs_resamp+1 ;
StateVectors = tmp(mf0,resample(weights,DynareOptions.particle.resampling.method1,DynareOptions.particle.resampling.method2));
weights = ones(1,number_of_particles)/number_of_particles; weights = ones(1,number_of_particles)/number_of_particles;
elseif strcmp(DynareOptions.particle.resampling.status,'smoothed') elseif strcmp(DynareOptions.particle.resampling.status,'smoothed')
StateVectors = multivariate_smooth_resampling(weights',tmp(mf0,:)',number_of_particles,DynareOptions.particle.resampling.number_of_partitions)'; StateVectors = multivariate_smooth_resampling(weights',tmp(mf0,:)',number_of_particles,DynareOptions.particle.resampling.number_of_partitions)';
if pruning
StateVectors_ = multivariate_smooth_resampling(weights',tmp_(mf0,:)',number_of_particles,DynareOptions.particle.resampling.number_of_partitions)';
end
weights = ones(1,number_of_particles)/number_of_particles; weights = ones(1,number_of_particles)/number_of_particles;
elseif strcmp(DynareOptions.particle.resampling.status,'none') elseif strcmp(DynareOptions.particle.resampling.status,'none')
StateVectors = tmp(mf0,:); StateVectors = tmp(mf0,:);
if pruning
StateVectors_ = tmp_(mf0,:)
end
end end
end end
LIK = -sum(lik(start:end)); LIK = -sum(lik(start:end));
function n = neff(w)
n = dot(w,w);
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment