Allow k order approximation in conditional particle filter (cpf).

Ref. dynare#1673
remove-submodule^2
Stéphane Adjemian (Charybdis) 2020-01-27 10:39:34 +01:00
parent 2250fc39d1
commit 00dbc9f3f0
2 changed files with 134 additions and 163 deletions

View File

@ -1,24 +1,28 @@
function [ProposalStateVector,Weights,flag] = conditional_filter_proposal(ReducedForm,obs,StateVectors,SampleWeights,Q_lower_triangular_cholesky,H_lower_triangular_cholesky,H,ParticleOptions,ThreadsOptions,normconst2) function [ProposalStateVector, Weights, flag] = conditional_filter_proposal(ReducedForm, y, StateVectors, SampleWeights, Q_lower_triangular_cholesky, H_lower_triangular_cholesky, ...
% H, ParticleOptions, ThreadsOptions, DynareOptions, Model)
% Computes the proposal for each past particle using Gaussian approximations % Computes the proposal for each past particle using Gaussian approximations
% for the state errors and the Kalman filter % for the state errors and the Kalman filter
% %
% INPUTS % INPUTS
% reduced_form_model [structure] Matlab's structure describing the reduced form model. % - ReducedForm [structure] Matlab's structure describing the reduced form model.
% reduced_form_model.measurement.H [double] (pp x pp) variance matrix of measurement errors. % - y [double] p×1 vector, current observation (p is the number of observed variables).
% reduced_form_model.state.Q [double] (qq x qq) variance matrix of state errors. % - StateVectors
% reduced_form_model.state.dr [structure] output of resol.m. % - SampleWeights
% Y [double] pp*smpl matrix of (detrended) data, where pp is the maximum number of observed variables. % - Q_lower_triangular_cholesky
% - H_lower_triangular_cholesky
% - H
% - ParticleOptions
% - ThreadsOptions
% - DynareOptions
% - Model
% %
% OUTPUTS % OUTPUTS
% LIK [double] scalar, likelihood % - ProposalStateVector
% lik [double] vector, density of observations in each period. % - Weights
% % - flag
% REFERENCES
% % Copyright © 2012-2020 Dynare Team
% NOTES
% The vector "lik" is used to evaluate the jacobian of the likelihood.
% Copyright (C) 2012-2017 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -34,124 +38,107 @@ function [ProposalStateVector,Weights,flag] = conditional_filter_proposal(Reduce
% %
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
%
% AUTHOR(S) frederic DOT karame AT univ DASH lemans DOT fr
% stephane DOT adjemian AT univ DASH lemans DOT fr
persistent init_flag2 mf0 mf1 flag = false;
persistent number_of_state_variables number_of_observed_variables
persistent number_of_structural_innovations
flag=0 ; if ReducedForm.use_k_order_solver
% Set local state space model (first-order approximation). dr = ReducedForm.dr;
ghx = ReducedForm.ghx; else
ghu = ReducedForm.ghu; % Set local state space model (first-order approximation).
% Set local state space model (second-order approximation). ghx = ReducedForm.ghx;
ghxx = ReducedForm.ghxx; ghu = ReducedForm.ghu;
ghuu = ReducedForm.ghuu; % Set local state space model (second-order approximation).
ghxu = ReducedForm.ghxu; ghxx = ReducedForm.ghxx;
ghuu = ReducedForm.ghuu;
if any(any(isnan(ghx))) || any(any(isnan(ghu))) || any(any(isnan(ghxx))) || any(any(isnan(ghuu))) || any(any(isnan(ghxu))) || ... ghxu = ReducedForm.ghxu;
any(any(isinf(ghx))) || any(any(isinf(ghu))) || any(any(isinf(ghxx))) || any(any(isinf(ghuu))) || any(any(isinf(ghxu))) ...
any(any(abs(ghx)>1e4)) || any(any(abs(ghu)>1e4)) || any(any(abs(ghxx)>1e4)) || any(any(abs(ghuu)>1e4)) || any(any(abs(ghxu)>1e4))
ghx
ghu
ghxx
ghuu
ghxu
end end
constant = ReducedForm.constant; constant = ReducedForm.constant;
state_variables_steady_state = ReducedForm.state_variables_steady_state; state_variables_steady_state = ReducedForm.state_variables_steady_state;
% Set persistent variables. mf0 = ReducedForm.mf0;
if isempty(init_flag2) mf1 = ReducedForm.mf1;
mf0 = ReducedForm.mf0; number_of_state_variables = length(mf0);
mf1 = ReducedForm.mf1; number_of_observed_variables = length(mf1);
number_of_state_variables = length(mf0); number_of_structural_innovations = length(ReducedForm.Q);
number_of_observed_variables = length(mf1);
number_of_structural_innovations = length(ReducedForm.Q);
init_flag2 = 1;
end
if ParticleOptions.proposal_approximation.montecarlo if ParticleOptions.proposal_approximation.montecarlo
nodes = randn(ParticleOptions.number_of_particles/10,number_of_structural_innovations) ; nodes = randn(ParticleOptions.number_of_particles/10, number_of_structural_innovations);
weights = 1/ParticleOptions.number_of_particles ; weights = 1.0/ParticleOptions.number_of_particles;
weights_c = weights ; weights_c = weights;
elseif ParticleOptions.proposal_approximation.cubature elseif ParticleOptions.proposal_approximation.cubature
[nodes,weights] = spherical_radial_sigma_points(number_of_structural_innovations); [nodes, weights] = spherical_radial_sigma_points(number_of_structural_innovations);
weights_c = weights ; weights_c = weights;
elseif ParticleOptions.proposal_approximation.unscented elseif ParticleOptions.proposal_approximation.unscented
[nodes,weights,weights_c] = unscented_sigma_points(number_of_structural_innovations,ParticleOptions); [nodes, weights, weights_c] = unscented_sigma_points(number_of_structural_innovations, ParticleOptions);
else else
error('Estimation: This approximation for the proposal is not implemented or unknown!') error('Estimation: This approximation for the proposal is not implemented or unknown!')
end end
epsilon = Q_lower_triangular_cholesky*(nodes') ; epsilon = Q_lower_triangular_cholesky*nodes';
yhat = repmat(StateVectors-state_variables_steady_state,1,size(epsilon,2)) ; yhat = repmat(StateVectors-state_variables_steady_state, 1, size(epsilon, 2));
tmp = local_state_space_iteration_2(yhat,epsilon,ghx,ghu,constant,ghxx,ghuu,ghxu,ThreadsOptions.local_state_space_iteration_2); if ReducedForm.use_k_order_solver
tmp = local_state_space_iteration_k(yhat, epsilon, dr, Model, DynareOptions);
else
tmp = local_state_space_iteration_2(yhat, epsilon, ghx, ghu, constant, ghxx, ghuu, ghxu, ThreadsOptions.local_state_space_iteration_2);
end
PredictedStateMean = tmp(mf0,:)*weights ; PredictedStateMean = tmp(mf0,:)*weights;
PredictedObservedMean = tmp(mf1,:)*weights; PredictedObservedMean = tmp(mf1,:)*weights;
if ParticleOptions.proposal_approximation.cubature || ParticleOptions.proposal_approximation.montecarlo if ParticleOptions.proposal_approximation.cubature || ParticleOptions.proposal_approximation.montecarlo
PredictedStateMean = sum(PredictedStateMean,2) ; PredictedStateMean = sum(PredictedStateMean, 2);
PredictedObservedMean = sum(PredictedObservedMean,2) ; PredictedObservedMean = sum(PredictedObservedMean, 2);
dState = bsxfun(@minus,tmp(mf0,:),PredictedStateMean)'.*sqrt(weights) ; dState = bsxfun(@minus, tmp(mf0,:), PredictedStateMean)'.*sqrt(weights);
dObserved = bsxfun(@minus,tmp(mf1,:),PredictedObservedMean)'.*sqrt(weights); dObserved = bsxfun(@minus, tmp(mf1,:), PredictedObservedMean)'.*sqrt(weights);
PredictedStateVariance = dState*dState'; PredictedStateVariance = dState*dState';
big_mat = [dObserved dState; [H_lower_triangular_cholesky zeros(number_of_observed_variables,number_of_state_variables)] ]; big_mat = [dObserved dState; H_lower_triangular_cholesky zeros(number_of_observed_variables,number_of_state_variables)];
[mat1,mat] = qr2(big_mat,0); [~, mat] = qr2(big_mat,0);
mat = mat'; mat = mat';
clear('mat1'); PredictedObservedVarianceSquareRoot = mat(1:number_of_observed_variables, 1:number_of_observed_variables);
PredictedObservedVarianceSquareRoot = mat(1:number_of_observed_variables,1:number_of_observed_variables);
CovarianceObservedStateSquareRoot = mat(number_of_observed_variables+(1:number_of_state_variables),1:number_of_observed_variables); CovarianceObservedStateSquareRoot = mat(number_of_observed_variables+(1:number_of_state_variables),1:number_of_observed_variables);
StateVectorVarianceSquareRoot = mat(number_of_observed_variables+(1:number_of_state_variables),number_of_observed_variables+(1:number_of_state_variables)); StateVectorVarianceSquareRoot = mat(number_of_observed_variables+(1:number_of_state_variables),number_of_observed_variables+(1:number_of_state_variables));
Error = obs - PredictedObservedMean ; Error = y-PredictedObservedMean;
StateVectorMean = PredictedStateMean + (CovarianceObservedStateSquareRoot/PredictedObservedVarianceSquareRoot)*Error ; StateVectorMean = PredictedStateMean+(CovarianceObservedStateSquareRoot/PredictedObservedVarianceSquareRoot)*Error;
if ParticleOptions.cpf_weights_method.amisanotristani if ParticleOptions.cpf_weights_method.amisanotristani
Weights = SampleWeights.*probability2(zeros(number_of_observed_variables,1),PredictedObservedVarianceSquareRoot,Error) ; Weights = SampleWeights.*probability2(zeros(number_of_observed_variables,1), PredictedObservedVarianceSquareRoot, Error);
% Weights = SampleWeights.*sum(weights*probability2(obs,H_lower_triangular_cholesky,tmp(mf1,:)),1) ; end
end
else else
dState = bsxfun(@minus,tmp(mf0,:),PredictedStateMean); dState = bsxfun(@minus, tmp(mf0,:), PredictedStateMean);
dObserved = bsxfun(@minus,tmp(mf1,:),PredictedObservedMean); dObserved = bsxfun(@minus, tmp(mf1,:), PredictedObservedMean);
PredictedStateVariance = dState*diag(weights_c)*dState'; PredictedStateVariance = dState*diag(weights_c)*dState';
PredictedObservedVariance = dObserved*diag(weights_c)*dObserved' + H; PredictedObservedVariance = dObserved*diag(weights_c)*dObserved'+H;
PredictedStateAndObservedCovariance = dState*diag(weights_c)*dObserved'; PredictedStateAndObservedCovariance = dState*diag(weights_c)*dObserved';
KalmanFilterGain = PredictedStateAndObservedCovariance/PredictedObservedVariance ; KalmanFilterGain = PredictedStateAndObservedCovariance/PredictedObservedVariance;
Error = obs - PredictedObservedMean ; Error = y-PredictedObservedMean;
StateVectorMean = PredictedStateMean + KalmanFilterGain*Error ; StateVectorMean = PredictedStateMean+KalmanFilterGain*Error;
StateVectorVariance = PredictedStateVariance - KalmanFilterGain*PredictedObservedVariance*KalmanFilterGain'; StateVectorVariance = PredictedStateVariance-KalmanFilterGain*PredictedObservedVariance*KalmanFilterGain';
StateVectorVariance = 0.5*(StateVectorVariance+StateVectorVariance'); StateVectorVariance = 0.5*(StateVectorVariance+StateVectorVariance');
%StateVectorVarianceSquareRoot = reduced_rank_cholesky(StateVectorVariance)';%chol(StateVectorVariance + eye(number_of_state_variables)*1e-6)' ; [StateVectorVarianceSquareRoot, p] = chol(StateVectorVariance, 'lower') ;
[StateVectorVarianceSquareRoot,p] = chol(StateVectorVariance,'lower') ;
if p if p
flag=1; flag = true;
ProposalStateVector = zeros(number_of_state_variables,1) ; ProposalStateVector = zeros(number_of_state_variables, 1);
Weights = 0.0 ; Weights = 0.0;
return return
end end
if ParticleOptions.cpf_weights_method.amisanotristani if ParticleOptions.cpf_weights_method.amisanotristani
Weights = SampleWeights.*probability2(zeros(number_of_observed_variables,1),chol(PredictedObservedVariance)',Error) ; Weights = SampleWeights.*probability2(zeros(number_of_observed_variables, 1), chol(PredictedObservedVariance)', Error);
% Weights = SampleWeights.*sum(probability2(obs,H_lower_triangular_cholesky,tmp(mf1,:))*weights) ; end
end
end end
ProposalStateVector = StateVectorVarianceSquareRoot*randn(size(StateVectorVarianceSquareRoot,2),1)+StateVectorMean ; ProposalStateVector = StateVectorVarianceSquareRoot*randn(size(StateVectorVarianceSquareRoot, 2), 1)+StateVectorMean;
if ParticleOptions.cpf_weights_method.murrayjonesparslow if ParticleOptions.cpf_weights_method.murrayjonesparslow
PredictedStateVariance = 0.5*(PredictedStateVariance+PredictedStateVariance'); PredictedStateVariance = 0.5*(PredictedStateVariance+PredictedStateVariance');
%PredictedStateVarianceSquareRoot = reduced_rank_cholesky(PredictedStateVariance)';%chol(PredictedStateVariance + eye(number_of_state_variables)*1e-6)' ; [PredictedStateVarianceSquareRoot, p] = chol(PredictedStateVariance, 'lower');
[PredictedStateVarianceSquareRoot,p] = chol(PredictedStateVariance,'lower') ;
if p if p
flag=1; flag = true;
ProposalStateVector = zeros(number_of_state_variables,1) ; ProposalStateVector = zeros(number_of_state_variables,1);
Weights = 0.0 ; Weights = 0.0;
return return
end end
Prior = probability2(PredictedStateMean,PredictedStateVarianceSquareRoot,ProposalStateVector) ; Prior = probability2(PredictedStateMean, PredictedStateVarianceSquareRoot, ProposalStateVector);
Posterior = probability2(StateVectorMean,StateVectorVarianceSquareRoot,ProposalStateVector) ; Posterior = probability2(StateVectorMean, StateVectorVarianceSquareRoot, ProposalStateVector);
Likelihood = probability2(obs,H_lower_triangular_cholesky,measurement_equations(ProposalStateVector,ReducedForm,ThreadsOptions)) ; Likelihood = probability2(y, H_lower_triangular_cholesky, measurement_equations(ProposalStateVector, ReducedForm, ThreadsOptions, DynareOptions, Model));
Weights = SampleWeights.*Likelihood.*(Prior./Posterior) ; Weights = SampleWeights.*Likelihood.*(Prior./Posterior);
end end

View File

@ -1,7 +1,22 @@
function [LIK,lik] = conditional_particle_filter(ReducedForm,Y,start,ParticleOptions,ThreadsOptions) function [LIK,lik] = conditional_particle_filter(ReducedForm, Y, s, ParticleOptions, ThreadsOptions, DynareOptions, Model)
%
% Evaluates the likelihood of a non-linear model with a particle filter % Evaluates the likelihood of a non-linear model with a particle filter
% - the proposal is built using the Kalman updating step for each particle. %
% INPUTS
% - ReducedForm [structure] Matlab's structure describing the reduced form model.
% - Y [double] p×T matrix of (detrended) data, where p is the number of observed variables.
% - s [integer] scalar, likelihood evaluation starts at s (has to be smaller than T, the sample length provided in Y).
% - ParticlesOptions [struct]
% - ThreadsOptions [struct]
% - DynareOptions [struct]
% - Model [struct]
%
% OUTPUTS
% - LIK [double] scalar, likelihood
% - lik [double] (T-s+1)×1 vector, density of observations in each period.
%
% REMARKS
% - The proposal is built using the Kalman updating step for each particle.
% - we need draws in the errors distributions % - we need draws in the errors distributions
% Whether we use Monte-Carlo draws from a multivariate gaussian distribution % Whether we use Monte-Carlo draws from a multivariate gaussian distribution
% as in Amisano & Tristani (JEDC 2010). % as in Amisano & Tristani (JEDC 2010).
@ -19,25 +34,8 @@ function [LIK,lik] = conditional_particle_filter(ReducedForm,Y,start,ParticleOpt
% - The use of the Kalman updating step may biais the proposal distribution since % - The use of the Kalman updating step may biais the proposal distribution since
% it has been derived in a linear context and is implemented in a nonlinear % it has been derived in a linear context and is implemented in a nonlinear
% context. That is why particle resampling is performed. % context. That is why particle resampling is performed.
%
% INPUTS % Copyright (C) 2009-2020 Dynare Team
% reduced_form_model [structure] Matlab's structure describing the reduced form model.
% reduced_form_model.measurement.H [double] (pp x pp) variance matrix of measurement errors.
% reduced_form_model.state.Q [double] (qq x qq) variance matrix of state errors.
% reduced_form_model.state.dr [structure] output of resol.m.
% Y [double] pp*smpl matrix of (detrended) data, where pp is the maximum number of observed variables.
% start [integer] scalar, likelihood evaluation starts at 'start'.
% smolyak_accuracy [integer] scalar.
%
% OUTPUTS
% LIK [double] scalar, likelihood
% lik [double] vector, density of observations in each period.
%
% REFERENCES
%
% NOTES
% The vector "lik" is used to evaluate the jacobian of the likelihood.
% Copyright (C) 2009-2017 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -54,26 +52,14 @@ function [LIK,lik] = conditional_particle_filter(ReducedForm,Y,start,ParticleOpt
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) frederic DOT karame AT univ DASH lemans DOT fr % Set default for third input argument.
% stephane DOT adjemian AT univ DASH lemans DOT fr if isempty(s)
s = 1;
persistent init_flag mf1
persistent number_of_particles
persistent sample_size number_of_observed_variables
% Set default
if isempty(start)
start = 1;
end end
% Set persistent variables. T = size(Y,2);
if isempty(init_flag) p = length(ReducedForm.mf1);
mf1 = ReducedForm.mf1; n = ParticleOptions.number_of_particles;
sample_size = size(Y,2);
number_of_observed_variables = length(mf1);
init_flag = 1;
number_of_particles = ParticleOptions.number_of_particles ;
end
% Get covariance matrices % Get covariance matrices
Q = ReducedForm.Q; Q = ReducedForm.Q;
@ -88,39 +74,37 @@ end
% Get initial condition for the state vector. % Get initial condition for the state vector.
StateVectorMean = ReducedForm.StateVectorMean; StateVectorMean = ReducedForm.StateVectorMean;
StateVectorVarianceSquareRoot = chol(ReducedForm.StateVectorVariance)'; StateVectorVarianceSquareRoot = chol(ReducedForm.StateVectorVariance)';
state_variance_rank = size(StateVectorVarianceSquareRoot,2); state_variance_rank = size(StateVectorVarianceSquareRoot, 2);
Q_lower_triangular_cholesky = chol(Q)'; Q_lower_triangular_cholesky = chol(Q)';
% Set seed for randn(). % Set seed for randn().
set_dynare_seed('default'); set_dynare_seed('default');
% Initialization of the likelihood. % Initialization of the likelihood.
normconst2 = sqrt( ((2*pi)^number_of_observed_variables)*prod(det(H)) ) ; lik = NaN(T, 1);
lik = NaN(sample_size,1); ks = 0;
LIK = NaN; StateParticles = bsxfun(@plus, StateVectorVarianceSquareRoot*randn(state_variance_rank, n), StateVectorMean);
ks = 0 ; SampleWeights = ones(1, n)/n;
StateParticles = bsxfun(@plus,StateVectorVarianceSquareRoot*randn(state_variance_rank,number_of_particles),StateVectorMean);
SampleWeights = ones(1,number_of_particles)/number_of_particles ; for t=1:T
for t=1:sample_size flags = false(n, 1);
obs=Y(:,t); for i=1:n
flag = zeros(number_of_particles) ; [StateParticles(:,i), SampleWeights(i), flags(i)] = ...
parfor i=1:number_of_particles conditional_filter_proposal(ReducedForm, Y(:,t), StateParticles(:,i), SampleWeights(i), Q_lower_triangular_cholesky, H_lower_triangular_cholesky, H, ParticleOptions, ThreadsOptions, DynareOptions, Model);
[StateParticles(:,i),SampleWeights(i),flag(i)] = ...
conditional_filter_proposal(ReducedForm,obs,StateParticles(:,i),SampleWeights(i),Q_lower_triangular_cholesky,H_lower_triangular_cholesky,H,ParticleOptions,ThreadsOptions,normconst2) ;
end end
if sum(flag)~=0 if any(flags)
LIK=-Inf; LIK = -Inf;
lik(t)=-Inf; lik(t) = -Inf;
return return
end end
SumSampleWeights = sum(SampleWeights) ; SumSampleWeights = sum(SampleWeights);
lik(t) = log(SumSampleWeights) ; lik(t) = log(SumSampleWeights);
SampleWeights = SampleWeights./SumSampleWeights ; SampleWeights = SampleWeights./SumSampleWeights;
if (ParticleOptions.resampling.status.generic && neff(SampleWeights)<ParticleOptions.resampling.threshold*sample_size) || ParticleOptions.resampling.status.systematic if (ParticleOptions.resampling.status.generic && neff(SampleWeights)<ParticleOptions.resampling.threshold*T) || ParticleOptions.resampling.status.systematic
ks = ks + 1 ; ks = ks + 1;
StateParticles = resample(StateParticles',SampleWeights',ParticleOptions)'; StateParticles = resample(StateParticles', SampleWeights', ParticleOptions)';
SampleWeights = ones(1,number_of_particles)/number_of_particles ; SampleWeights = ones(1, n)/n;
end end
end end
LIK = -sum(lik(start:end)); LIK = -sum(lik(s:end));