DsgeSmoother.m 8.13 KB
Newer Older
1
function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R,P,PK,decomp] = DsgeSmoother(xparam1,gend,Y,data_index,missing_value)
2
3
4
% Estimation of the smoothed variables and innovations. 
% 
% INPUTS 
assia's avatar
assia committed
5
6
7
%   o xparam1       [double]   (p*1) vector of (estimated) parameters. 
%   o gend          [integer]  scalar specifying the number of observations ==> varargin{1}.
%   o data          [double]   (T*n) matrix of data.
8
9
%   o data_index    [cell]      1*smpl cell of column vectors of indices.
%   o missing_value 1 if missing values, 0 otherwise
10
11
12
13
14
15
16
17
18
%  
% OUTPUTS 
%   o alphahat      [double]  (m*T) matrix, smoothed endogenous variables.
%   o etahat        [double]  (r*T) matrix, smoothed structural shocks (r>n is the umber of shocks).
%   o epsilonhat    [double]  (n*T) matrix, smoothed measurement errors.
%   o ahat          [double]  (m*T) matrix, one step ahead filtered (endogenous) variables.
%   o SteadyState   [double]  (m*1) vector specifying the steady state level of each endogenous variable.
%   o trend_coeff   [double]  (n*1) vector, parameters specifying the slope of the trend associated to each observed variable.
%   o aK            [double]  (K,n,T+K) array, k (k=1,...,K) steps ahead filtered (endogenous) variables.
assia's avatar
assia committed
19
20
21
22
23
%   o T and R       [double]  Matrices defining the state equation (T is the (m*m) transition matrix).
%    P:             3D array of one-step ahead forecast error variance
%                   matrices
%    PK:            4D array of k-step ahead forecast error variance
%                   matrices (meaningless for periods 1:d)
24
%    
25
% ALGORITHM 
26
%   Diffuse Kalman filter (Durbin and Koopman)       
michel's avatar
michel committed
27
%
28
% SPECIAL REQUIREMENTS
assia's avatar
assia committed
29
%   None
30

31
% Copyright (C) 2006-2011 Dynare Team
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
%
% This file is part of Dynare.
%
% 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.
%
% Dynare is distributed in the hope that it will be useful,
% 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/>.
assia's avatar
assia committed
47

48
global bayestopt_ M_ oo_ estim_params_ options_
michel's avatar
michel committed
49

50
51
52
alphahat        = [];
etahat  = [];
epsilonhat      = [];
53
54
55
56
57
58
59
60
61
ahat          = [];
SteadyState   = [];
trend_coeff   = [];
aK            = [];
T             = [];
R             = [];
P             = [];
PK            = [];
decomp        = [];
62
nobs            = size(options_.varobs,1);
63
smpl          = size(Y,2);
michel's avatar
michel committed
64

65
set_all_parameters(xparam1);
michel's avatar
michel committed
66

67
68
69
%------------------------------------------------------------------------------
% 2. call model setup & reduction program
%------------------------------------------------------------------------------
70
71
oo_.dr.restrict_var_list = bayestopt_.smoother_var_list;
oo_.dr.restrict_columns = bayestopt_.smoother_restrict_columns;
72
[T,R,SteadyState,info,M_,options_,oo_] = dynare_resolve(M_,options_,oo_);
73
bayestopt_.mf = bayestopt_.smoother_mf;
74
75
76
77
78
79
80
81
82
83
84
if options_.noconstant
    constant = zeros(nobs,1);
else
    if options_.loglinear == 1
        constant = log(SteadyState(bayestopt_.mfys));
    else
        constant = SteadyState(bayestopt_.mfys);
    end
end
trend_coeff = zeros(nobs,1);
if bayestopt_.with_trend == 1
michel's avatar
michel committed
85
    trend_coeff = zeros(nobs,1);
86
    t = options_.trend_coeffs;
87
    for i=1:length(t)
88
89
90
        if ~isempty(t{i})
            trend_coeff(i) = evalin('base',t{i});
        end
michel's avatar
michel committed
91
92
    end
    trend = constant*ones(1,gend)+trend_coeff*(1:gend);
93
else
michel's avatar
michel committed
94
    trend = constant*ones(1,gend);
95
96
97
end
start = options_.presample+1;
np    = size(T,1);
98
mf    = bayestopt_.smoother_mf;
99
100
101
102
103
104
105
106
107
108
109
% ------------------------------------------------------------------------------
%  3. Initial condition of the Kalman filter
% ------------------------------------------------------------------------------
% 
%  C'est ici qu'il faut dterminer Pinf et Pstar. Si le modle est stationnaire,
%  alors il suffit de poser Pstar comme la solution de l'uation de Lyapounov et
%  Pinf=[].
%
Q = M_.Sigma_e;
H = M_.H;

110
111
112
113
if isequal(H,0)
    H = zeros(nobs,nobs);
end

114
kalman_algo = options_.kalman_algo;
115
if options_.lik_init == 1               % Kalman filter
116
117
118
119
    if kalman_algo ~= 2
        kalman_algo = 1;
    end
    Pstar = lyapunov_symm(T,R*Q*transpose(R),options_.qz_criterium,options_.lyapunov_complex_threshold);
120
    Pinf        = [];
121
122
123
124
125
elseif options_.lik_init == 2 % Old Diffuse Kalman filter
    if kalman_algo ~= 2
        kalman_algo = 1;
    end
    Pstar = options_.Harvey_scale_factor*eye(np);
126
    Pinf        = [];
127
128
129
130
elseif options_.lik_init == 3 % Diffuse Kalman filter
    if kalman_algo ~= 4
        kalman_algo = 3;
    end
131
    [Z,ST,R1,QT,Pstar,Pinf] = schur_statespace_transformation(mf,T,R,Q,options_.qz_criterium);
132
133
134
135
136
137
138
139
elseif options_.lik_init == 4
    % Start from the solution of the Riccati equation.
    [err, Pstar] = kalman_steady_state(transpose(T),R*Q*transpose(R),transpose(build_selection_matrix(mf,np,nobs)),H);
    mexErrCheck('kalman_steady_state',err);
    Pinf  = [];
    if kalman_algo~=2
        kalman_algo = 1;
    end
140
end
141
142
143
kalman_tol = options_.kalman_tol;
riccati_tol = options_.riccati_tol;
data1 = Y-trend;
144
145
146
% -----------------------------------------------------------------------------
%  4. Kalman smoother
% -----------------------------------------------------------------------------
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

if ~missing_value
    for i=1:smpl
        data_index{i}=(1:nobs)';
    end
end

if kalman_algo == 1 || kalman_algo == 2
    ST = T;
    R1 = R;
    Z = zeros(nobs,size(T,2));
    for i=1:nobs
        Z(i,mf(i)) = 1;
    end
end

if kalman_algo == 1 || kalman_algo == 3
164
    [alphahat,epsilonhat,etahat,ahat,P,aK,PK,decomp] = missing_DiffuseKalmanSmootherH1_Z(ST, ...
165
                                                      Z,R1,Q,H,Pinf,Pstar, ...
166
167
                                                      data1,nobs,np,smpl,data_index, ...
                                                      options_.nk,kalman_tol,options_.filter_decomposition);
168
    if isequal(alphahat,0)
169
        if kalman_algo == 1
170
            kalman_algo = 2;
171
172
        elseif kalman_algo == 3
            kalman_algo = 4;
173
        else
174
            error('This case shouldn''t happen')
175
        end
176
177
    end
end
178

179
180
181
182
183
184
185
186
187
if kalman_algo == 2 || kalman_algo == 4
    if estim_params_.ncn
        ST = [ zeros(nobs,nobs) Z; zeros(np,nobs) T];
        ns = size(Q,1);
        R1 = [ eye(nobs) zeros(nobs, ns); zeros(np,nobs) R];
        Q = [H zeros(nobs,ns); zeros(ns,nobs) Q]; 
        Z = [eye(nobs) zeros(nobs, np)];
        if kalman_algo == 4
            [Z,ST,R1,QT,Pstar,Pinf] = schur_statespace_transformation((1:nobs)',ST,R1,Q,options_.qz_criterium);
188
        end
189
        
190
    end
191
    [alphahat,epsilonhat,etahat,ahat,P,aK,PK,decomp] = missing_DiffuseKalmanSmootherH3_Z(ST, ...
192
193
194
195
                                                      Z,R1,Q,diag(H), ...
                                                      Pinf,Pstar,data1,nobs,np,smpl,data_index, ...
                                                      options_.nk,kalman_tol,...
                                                      options_.filter_decomposition);
196
197
198
199
200
201
202
203
204
205
end

if kalman_algo == 3 || kalman_algo == 4
    alphahat = QT*alphahat;
    ahat = QT*ahat;
    nk = options_.nk;
    for jnk=1:nk
        aK(jnk,:,:) = QT*dynare_squeeze(aK(jnk,:,:));
        for i=1:size(PK,4)
            PK(jnk,:,:,i) = QT*dynare_squeeze(PK(jnk,:,:,i))*QT';
206
        end
207
208
209
210
        if options_.filter_decomposition
            for i=1:size(decomp,4)
                decomp(jnk,:,:,i) = QT*dynare_squeeze(decomp(jnk,:,:,i));
            end
211
212
        end
    end
213
214
    for i=1:size(P,4)
        P(:,:,i) = QT*dynare_squeeze(P(:,:,i))*QT';
215
    end
216
217
218
219
220
221
222
223
224
225
226
end

if estim_params_.ncn && (kalman_algo == 2 || kalman_algo == 4)
    % extracting measurement errors
    % removing observed variables from the state vector
    k = nobs+(1:np);
    alphahat = alphahat(k,:);
    ahat = ahat(k,:);
    aK = aK(:,k,:,:);
    if ~isempty(PK)
        PK = PK(:,k,k,:);
227
    end
228
229
    if ~isempty(decomp)
        decomp = decomp(:,k,:,:);
230
    end
231
    if ~isempty(P)
232
        P = P(k,k,:);
233
234
    end
end