auxiliary_particle_filter.m 6.35 KB
Newer Older
1
function [LIK,lik] = auxiliary_particle_filter(ReducedForm,Y,start,ParticleOptions,ThreadsOptions)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
2

Stéphane Adjemian's avatar
Stéphane Adjemian committed
3
% Evaluates the likelihood of a nonlinear model with the auxiliary particle filter
4
5
6
% allowing eventually resampling.
%
% Copyright (C) 2011-2015 Dynare Team
7
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
8
% This file is part of Dynare (particles module).
9
10
11
12
13
14
%
% 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.
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
15
% Dynare particles module is distributed in the hope that it will be useful,
16
17
18
19
20
21
% 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
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
22

23
persistent init_flag mf0 mf1 number_of_particles
24
persistent sample_size number_of_observed_variables number_of_structural_innovations
25
26
27
28
29
30
31

% Set default
if isempty(start)
    start = 1;
end

% Set flag for prunning
32
pruning = ParticleOptions.pruning;
33
34
35
36
37
38
39
40
41
42
43
44
45

% Get steady state and mean.
steadystate = ReducedForm.steadystate;
constant = ReducedForm.constant;
state_variables_steady_state = ReducedForm.state_variables_steady_state;

% Set persistent variables.
if isempty(init_flag)
    mf0 = ReducedForm.mf0;
    mf1 = ReducedForm.mf1;
    sample_size = size(Y,2);
    number_of_observed_variables = length(mf1);
    number_of_structural_innovations = length(ReducedForm.Q);
46
    number_of_particles = ParticleOptions.number_of_particles;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    init_flag = 1;
end

% 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;

% Get covariance matrices
Q = ReducedForm.Q;
H = ReducedForm.H;

% Get initial condition for the state vector.
StateVectorMean = ReducedForm.StateVectorMean;
64
StateVectorVarianceSquareRoot = chol(ReducedForm.StateVectorVariance)';
65
66
67
68
69
70
71
state_variance_rank = size(StateVectorVarianceSquareRoot,2);
Q_lower_triangular_cholesky = chol(Q)';

% Set seed for randn().
set_dynare_seed('default');

% Initialization of the likelihood.
72
const_lik = log(2*pi)*number_of_observed_variables+log(det(H));
73
74
75
76
77
78
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);
79
%StateVectors = bsxfun(@plus,zeros(state_variance_rank,number_of_particles),StateVectorMean);
80
81
82
if pruning
    StateVectors_ = StateVectors;
end
83

84
85
86
% Uncomment for building the mean average predictions based on a sparse
% grids of structural shocks. Otherwise, all shocks are set to 0 in the
% prediction.
87
88
89
90
91
92
93
% if ParticleOptions.proposal_approximation.cubature
%     [nodes,nodes_weights] = spherical_radial_sigma_points(number_of_structural_innovations) ;
%     nodes_weights = ones(size(nodes,1),1)*nodes_weights ;
% elseif ParticleOptions.proposal_approximation.unscented
%     [nodes,nodes_weights,nodes_weights_c] = unscented_sigma_points(number_of_structural_innovations,ParticleOptions);
% else
%     error('Estimation: This approximation for the proposal is not implemented or unknown!')
Stéphane Adjemian's avatar
Stéphane Adjemian committed
94
% end
95
% nodes = (Q_lower_triangular_cholesky*(nodes'))' ;
96
97

nodes = zeros(1,number_of_structural_innovations) ;
Stéphane Adjemian's avatar
Stéphane Adjemian committed
98
nodes_weights = ones(number_of_structural_innovations,1) ;
99

100
101
for t=1:sample_size
    yhat = bsxfun(@minus,StateVectors,state_variables_steady_state);
102
103
104
105
106
    if pruning
        yhat_ = bsxfun(@minus,StateVectors_,state_variables_steady_state);
        tmp = 0 ;
        tmp_ = 0 ;
        for i=1:size(nodes)
107
            [tmp1, tmp1_] = local_state_space_iteration_2(yhat,nodes(i,:)'*ones(1,number_of_particles),ghx,ghu,constant,ghxx,ghuu,ghxu,yhat_,steadystate,ThreadsOptions.local_state_space_iteration_2);
108
109
110
111
112
113
            tmp = tmp + nodes_weights(i)*tmp1 ;
            tmp_ = tmp_ + nodes_weights(i)*tmp1_ ;
        end
    else
        tmp = 0 ;
        for i=1:size(nodes)
114
            tmp = tmp + nodes_weights(i)*local_state_space_iteration_2(yhat,nodes(i,:)'*ones(1,number_of_particles),ghx,ghu,constant,ghxx,ghuu,ghxu,ThreadsOptions.local_state_space_iteration_2);
115
        end
116
117
    end
    PredictionError = bsxfun(@minus,Y(:,t),tmp(mf1,:));
118
119
120
121
122
123
    %tau_tilde = weights.*(exp(-.5*(const_lik+sum(PredictionError.*(H\PredictionError),1))) + 1e-99) ;
    % Replace Gaussian density with a Student density with 3 degrees of
    % freedom for fat tails.
    z = sum(PredictionError.*(H\PredictionError),1) ;
    tau_tilde = weights.*(tpdf(z,3*ones(size(z)))+1e-99) ;
    tau_tilde = tau_tilde/sum(tau_tilde) ;
124
    indx = resample(0,tau_tilde',ParticleOptions);
125
    if pruning
126
        yhat_ = yhat_(:,indx) ;
127
    end
128
    yhat = yhat(:,indx) ;
Stéphane Adjemian's avatar
Stéphane Adjemian committed
129
    weights_stage_1 = weights(indx)./tau_tilde(indx) ;
130
131
    epsilon = Q_lower_triangular_cholesky*randn(number_of_structural_innovations,number_of_particles);
    if pruning
132
        [tmp, tmp_] = local_state_space_iteration_2(yhat,epsilon,ghx,ghu,constant,ghxx,ghuu,ghxu,yhat_,steadystate,ThreadsOptions.local_state_space_iteration_2);
133
134
        StateVectors_ = tmp_(mf0,:);
    else
135
        tmp = local_state_space_iteration_2(yhat,epsilon,ghx,ghu,constant,ghxx,ghuu,ghxu,ThreadsOptions.local_state_space_iteration_2);
136
137
138
    end
    StateVectors = tmp(mf0,:);
    PredictionError = bsxfun(@minus,Y(:,t),tmp(mf1,:));
139
    weights_stage_2 = weights_stage_1.*(exp(-.5*(const_lik+sum(PredictionError.*(H\PredictionError),1))) + 1e-99) ;
Stéphane Adjemian's avatar
Stéphane Adjemian committed
140
    lik(t) = log(mean(weights_stage_2)) ;
141
142
143
144
145
146
147
148
149
150
151
    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)';
            StateVectors_ = temp(:,number_of_state_variables+1:2*number_of_state_variables)';
        else
            StateVectors = resample(StateVectors',weights',ParticleOptions)';
        end
        weights = ones(1,number_of_particles)/number_of_particles;
    end
152
153
end

154
%plot(lik) ;
155
LIK = -sum(lik(start:end));