0001 function [LIK,lik] = sequential_importance_particle_filter(ReducedForm,Y,start,DynareOptions)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 persistent init_flag
0062 persistent mf0 mf1
0063 persistent number_of_particles
0064 persistent sample_size number_of_observed_variables number_of_structural_innovations
0065
0066
0067 if isempty(start)
0068 start = 1;
0069 end
0070
0071
0072 steadystate = ReducedForm.steadystate;
0073 constant = ReducedForm.constant;
0074 state_variables_steady_state = ReducedForm.state_variables_steady_state;
0075
0076
0077 if isempty(init_flag)
0078 mf0 = ReducedForm.mf0;
0079 mf1 = ReducedForm.mf1;
0080 sample_size = size(Y,2);
0081 number_of_observed_variables = length(mf1);
0082 number_of_structural_innovations = length(ReducedForm.Q);
0083 number_of_particles = DynareOptions.particle.number_of_particles;
0084 init_flag = 1;
0085 end
0086
0087
0088 ghx = ReducedForm.ghx;
0089 ghu = ReducedForm.ghu;
0090
0091
0092 ghxx = ReducedForm.ghxx;
0093 ghuu = ReducedForm.ghuu;
0094 ghxu = ReducedForm.ghxu;
0095
0096
0097 Q = ReducedForm.Q;
0098 H = ReducedForm.H;
0099 if isempty(H)
0100 H = 0;
0101 end
0102
0103
0104 StateVectorMean = ReducedForm.StateVectorMean;
0105 StateVectorVarianceSquareRoot = reduced_rank_cholesky(ReducedForm.StateVectorVariance)';
0106 state_variance_rank = size(StateVectorVarianceSquareRoot,2);
0107 Q_lower_triangular_cholesky = chol(Q)';
0108
0109
0110 set_dynare_seed('default');
0111
0112
0113 const_lik = log(2*pi)*number_of_observed_variables;
0114 lik = NaN(sample_size,1);
0115
0116
0117 nb_obs_resamp = 0 ;
0118 weights = ones(1,number_of_particles)/number_of_particles ;
0119 StateVectors = bsxfun(@plus,StateVectorVarianceSquareRoot*randn(state_variance_rank,number_of_particles),StateVectorMean);
0120 for t=1:sample_size
0121 yhat = bsxfun(@minus,StateVectors,state_variables_steady_state);
0122 epsilon = Q_lower_triangular_cholesky*randn(number_of_structural_innovations,number_of_particles);
0123 tmp = local_state_space_iteration_2(yhat,epsilon,ghx,ghu,constant,ghxx,ghuu,ghxu,DynareOptions.threads.local_state_space_iteration_2);
0124 PredictedObservedMean = mean(tmp(mf1,:),2);
0125 PredictionError = bsxfun(@minus,Y(:,t),tmp(mf1,:));
0126 dPredictedObservedMean = bsxfun(@minus,tmp(mf1,:),PredictedObservedMean);
0127 PredictedObservedVariance = (dPredictedObservedMean*dPredictedObservedMean')/number_of_particles+H;
0128 lnw = -.5*(const_lik+log(det(PredictedObservedVariance))+sum(PredictionError.*(PredictedObservedVariance\PredictionError),1));
0129 dfac = max(lnw);
0130 wtilde = weights.*exp(lnw-dfac);
0131 lik(t) = log(sum(wtilde))+dfac;
0132 weights = wtilde/sum(wtilde);
0133 if strcmp(DynareOptions.particle.resampling.status,'generic')
0134 Neff = 1/(weights*weights');
0135 end
0136 if (strcmp(DynareOptions.particle.resampling.status,'generic') && Neff<DynareOptions.particle.resampling.neff_threshold*sample_size ) || strcmp(DynareOptions.particle.resampling.status,'systematic')
0137 nb_obs_resamp = nb_obs_resamp+1 ;
0138 StateVectors = tmp(mf0,resample(weights,DynareOptions.particle.resampling.method1,DynareOptions.particle.resampling.method2));
0139 weights = ones(1,number_of_particles)/number_of_particles;
0140 elseif strcmp(DynareOptions.particle.resampling.status,'smoothed')
0141 StateVectors = multivariate_smooth_resampling(weights',tmp(mf0,:)',number_of_particles,DynareOptions.particle.resampling.number_of_partitions)';
0142 weights = ones(1,number_of_particles)/number_of_particles;
0143 elseif strcmp(DynareOptions.particle.resampling.status,'none')
0144 StateVectors = tmp(mf0,:);
0145 end
0146 end
0147
0148 LIK = -sum(lik(start:end));