2010-04-03 08:00:22 +02:00
|
|
|
|
function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R,P,PK,decomp] = DsgeSmoother(xparam1,gend,Y,data_index,missing_value)
|
2007-04-15 16:21:11 +02:00
|
|
|
|
% Estimation of the smoothed variables and innovations.
|
|
|
|
|
%
|
|
|
|
|
% INPUTS
|
2008-03-03 12:10:16 +01:00
|
|
|
|
% 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.
|
2008-11-25 18:31:37 +01:00
|
|
|
|
% o data_index [cell] 1*smpl cell of column vectors of indices.
|
|
|
|
|
% o missing_value 1 if missing values, 0 otherwise
|
2007-04-15 16:21:11 +02:00
|
|
|
|
%
|
|
|
|
|
% 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.
|
2008-03-03 12:10:16 +01:00
|
|
|
|
% o T and R [double] Matrices defining the state equation (T is the (m*m) transition matrix).
|
2014-12-11 20:59:31 +01:00
|
|
|
|
% o P: 3D array of one-step ahead forecast error variance
|
|
|
|
|
% matrices
|
|
|
|
|
% o PK: 4D array of k-step ahead forecast error variance
|
|
|
|
|
% matrices (meaningless for periods 1:d)
|
|
|
|
|
% o decomp 4D array of shock decomposition of k-step ahead
|
|
|
|
|
% filtered variables
|
|
|
|
|
%
|
2007-04-15 16:21:11 +02:00
|
|
|
|
% ALGORITHM
|
2008-02-04 18:52:16 +01:00
|
|
|
|
% Diffuse Kalman filter (Durbin and Koopman)
|
2005-02-18 20:54:39 +01:00
|
|
|
|
%
|
2007-04-15 16:21:11 +02:00
|
|
|
|
% SPECIAL REQUIREMENTS
|
2008-03-03 12:10:16 +01:00
|
|
|
|
% None
|
2008-08-01 14:40:33 +02:00
|
|
|
|
|
2014-12-11 20:59:31 +01:00
|
|
|
|
% Copyright (C) 2006-2014 Dynare Team
|
2008-08-01 14:40:33 +02:00
|
|
|
|
%
|
|
|
|
|
% 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/>.
|
2008-03-03 12:10:16 +01:00
|
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
|
global bayestopt_ M_ oo_ estim_params_ options_
|
2005-02-18 20:54:39 +01:00
|
|
|
|
|
2010-01-05 11:46:10 +01:00
|
|
|
|
alphahat = [];
|
|
|
|
|
etahat = [];
|
|
|
|
|
epsilonhat = [];
|
2009-12-16 18:17:34 +01:00
|
|
|
|
ahat = [];
|
|
|
|
|
SteadyState = [];
|
|
|
|
|
trend_coeff = [];
|
|
|
|
|
aK = [];
|
|
|
|
|
T = [];
|
|
|
|
|
R = [];
|
|
|
|
|
P = [];
|
|
|
|
|
PK = [];
|
|
|
|
|
decomp = [];
|
2014-07-15 11:13:41 +02:00
|
|
|
|
vobs = length(options_.varobs);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
smpl = size(Y,2);
|
2005-02-18 20:54:39 +01:00
|
|
|
|
|
2015-06-09 14:51:32 +02:00
|
|
|
|
if ~isempty(xparam1) %not calibrated model
|
|
|
|
|
M_ = set_all_parameters(xparam1,estim_params_,M_);
|
|
|
|
|
end
|
2005-02-18 20:54:39 +01:00
|
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
|
%------------------------------------------------------------------------------
|
|
|
|
|
% 2. call model setup & reduction program
|
|
|
|
|
%------------------------------------------------------------------------------
|
2015-04-03 17:59:51 +02:00
|
|
|
|
oldoo.restrict_var_list = oo_.dr.restrict_var_list;
|
|
|
|
|
oldoo.restrict_columns = oo_.dr.restrict_columns;
|
|
|
|
|
oo_.dr.restrict_var_list = bayestopt_.smoother_var_list;
|
|
|
|
|
oo_.dr.restrict_columns = bayestopt_.smoother_restrict_columns;
|
|
|
|
|
|
|
|
|
|
[T,R,SteadyState,info,M_,options_,oo_] = dynare_resolve(M_,options_,oo_);
|
|
|
|
|
|
|
|
|
|
oo_.dr.restrict_var_list = oldoo.restrict_var_list;
|
|
|
|
|
oo_.dr.restrict_columns = oldoo.restrict_columns;
|
|
|
|
|
|
2010-03-23 18:17:15 +01:00
|
|
|
|
bayestopt_.mf = bayestopt_.smoother_mf;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
if options_.noconstant
|
2014-07-15 11:13:41 +02:00
|
|
|
|
constant = zeros(vobs,1);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
else
|
2014-01-30 13:00:25 +01:00
|
|
|
|
if options_.loglinear
|
2009-12-16 18:17:34 +01:00
|
|
|
|
constant = log(SteadyState(bayestopt_.mfys));
|
|
|
|
|
else
|
|
|
|
|
constant = SteadyState(bayestopt_.mfys);
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-07-15 11:13:41 +02:00
|
|
|
|
trend_coeff = zeros(vobs,1);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
if bayestopt_.with_trend == 1
|
2014-07-15 11:13:41 +02:00
|
|
|
|
trend_coeff = zeros(vobs,1);
|
2007-09-27 17:35:40 +02:00
|
|
|
|
t = options_.trend_coeffs;
|
2007-10-01 11:39:32 +02:00
|
|
|
|
for i=1:length(t)
|
2009-12-16 18:17:34 +01:00
|
|
|
|
if ~isempty(t{i})
|
|
|
|
|
trend_coeff(i) = evalin('base',t{i});
|
|
|
|
|
end
|
2005-09-11 11:38:52 +02:00
|
|
|
|
end
|
|
|
|
|
trend = constant*ones(1,gend)+trend_coeff*(1:gend);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
else
|
2005-09-11 11:38:52 +02:00
|
|
|
|
trend = constant*ones(1,gend);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
|
|
|
|
start = options_.presample+1;
|
|
|
|
|
np = size(T,1);
|
2010-03-23 18:17:15 +01:00
|
|
|
|
mf = bayestopt_.smoother_mf;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
% ------------------------------------------------------------------------------
|
|
|
|
|
% 3. Initial condition of the Kalman filter
|
|
|
|
|
% ------------------------------------------------------------------------------
|
|
|
|
|
%
|
|
|
|
|
% C'est ici qu'il faut d<>terminer Pinf et Pstar. Si le mod<6F>le est stationnaire,
|
|
|
|
|
% alors il suffit de poser Pstar comme la solution de l'<27>uation de Lyapounov et
|
|
|
|
|
% Pinf=[].
|
|
|
|
|
%
|
|
|
|
|
Q = M_.Sigma_e;
|
|
|
|
|
H = M_.H;
|
|
|
|
|
|
2011-06-23 23:39:15 +02:00
|
|
|
|
if isequal(H,0)
|
2014-07-15 11:13:41 +02:00
|
|
|
|
H = zeros(vobs,vobs);
|
2011-06-23 23:39:15 +02:00
|
|
|
|
end
|
|
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
|
kalman_algo = options_.kalman_algo;
|
2010-01-05 11:46:10 +01:00
|
|
|
|
if options_.lik_init == 1 % Kalman filter
|
2009-12-16 18:17:34 +01:00
|
|
|
|
if kalman_algo ~= 2
|
|
|
|
|
kalman_algo = 1;
|
|
|
|
|
end
|
2014-01-14 17:32:01 +01:00
|
|
|
|
if options_.lyapunov_fp == 1
|
2015-07-22 15:11:39 +02:00
|
|
|
|
Pstar = lyapunov_symm(T,R*Q*R',options_.lyapunov_fixed_point_tol,options_.qz_criterium,options_.lyapunov_complex_threshold, 3, [], options_.debug);
|
2014-01-14 17:32:01 +01:00
|
|
|
|
elseif options_.lyapunov_db == 1
|
|
|
|
|
Pstar = disclyap_fast(T,R*Q*R',options_.lyapunov_doubling_tol);
|
|
|
|
|
elseif options_.lyapunov_srs == 1
|
2015-07-22 15:11:39 +02:00
|
|
|
|
Pstar = lyapunov_symm(T,Q,options_.lyapunov_fixed_point_tol,options_.qz_criterium,options_.lyapunov_complex_threshold, 4, R, options_.debug);
|
2014-01-14 17:32:01 +01:00
|
|
|
|
else
|
2015-07-22 15:11:39 +02:00
|
|
|
|
Pstar = lyapunov_symm(T,R*Q*R',options_.lyapunov_fixed_point_tol,options_.qz_criterium,options_.lyapunov_complex_threshold, [], [], options_.debug);
|
2014-01-14 17:32:01 +01:00
|
|
|
|
end;
|
2010-01-05 11:46:10 +01:00
|
|
|
|
Pinf = [];
|
2013-06-28 16:26:53 +02:00
|
|
|
|
elseif options_.lik_init == 2 % Old Diffuse Kalman filter
|
2009-12-16 18:17:34 +01:00
|
|
|
|
if kalman_algo ~= 2
|
|
|
|
|
kalman_algo = 1;
|
|
|
|
|
end
|
|
|
|
|
Pstar = options_.Harvey_scale_factor*eye(np);
|
2010-01-05 11:46:10 +01:00
|
|
|
|
Pinf = [];
|
2013-06-28 16:26:53 +02:00
|
|
|
|
elseif options_.lik_init == 3 % Diffuse Kalman filter
|
2009-12-16 18:17:34 +01:00
|
|
|
|
if kalman_algo ~= 4
|
|
|
|
|
kalman_algo = 3;
|
|
|
|
|
end
|
2015-10-13 17:26:39 +02:00
|
|
|
|
[Z,ST,R1,QT,Pstar,Pinf] = schur_statespace_transformation(mf,T,R,Q,options_.qz_criterium,oo_.dr.restrict_var_list);
|
|
|
|
|
Pinf = QT*Pinf*QT';
|
|
|
|
|
Pstar = QT*Pstar*QT';
|
2013-06-28 16:26:53 +02:00
|
|
|
|
elseif options_.lik_init == 4 % Start from the solution of the Riccati equation.
|
2014-07-15 11:13:41 +02:00
|
|
|
|
[err, Pstar] = kalman_steady_state(transpose(T),R*Q*transpose(R),transpose(build_selection_matrix(mf,np,vobs)),H);
|
2011-06-23 23:39:15 +02:00
|
|
|
|
mexErrCheck('kalman_steady_state',err);
|
|
|
|
|
Pinf = [];
|
|
|
|
|
if kalman_algo~=2
|
|
|
|
|
kalman_algo = 1;
|
|
|
|
|
end
|
2013-06-28 16:26:53 +02:00
|
|
|
|
elseif options_.lik_init == 5 % Old diffuse Kalman filter only for the non stationary variables
|
|
|
|
|
[eigenvect, eigenv] = eig(T);
|
|
|
|
|
eigenv = diag(eigenv);
|
|
|
|
|
nstable = length(find(abs(abs(eigenv)-1) > 1e-7));
|
|
|
|
|
unstable = find(abs(abs(eigenv)-1) < 1e-7);
|
|
|
|
|
V = eigenvect(:,unstable);
|
|
|
|
|
indx_unstable = find(sum(abs(V),2)>1e-5);
|
|
|
|
|
stable = find(sum(abs(V),2)<1e-5);
|
|
|
|
|
nunit = length(eigenv) - nstable;
|
|
|
|
|
Pstar = options_.Harvey_scale_factor*eye(np);
|
|
|
|
|
if kalman_algo ~= 2
|
|
|
|
|
kalman_algo = 1;
|
|
|
|
|
end
|
|
|
|
|
R_tmp = R(stable, :);
|
|
|
|
|
T_tmp = T(stable,stable);
|
2013-06-28 16:59:47 +02:00
|
|
|
|
if options_.lyapunov_fp == 1
|
2015-07-22 15:11:39 +02:00
|
|
|
|
Pstar_tmp = lyapunov_symm(T_tmp,R_tmp*Q*R_tmp',options_.lyapunov_fixed_point_tol,options_.qz_criterium,options_.lyapunov_complex_threshold, 3, [], options_.debug);
|
2013-06-28 16:59:47 +02:00
|
|
|
|
elseif options_.lyapunov_db == 1
|
|
|
|
|
Pstar_tmp = disclyap_fast(T_tmp,R_tmp*Q*R_tmp',options_.lyapunov_doubling_tol);
|
|
|
|
|
elseif options_.lyapunov_srs == 1
|
2015-07-22 15:11:39 +02:00
|
|
|
|
Pstar_tmp = lyapunov_symm(T_tmp,Q,options_.lyapunov_fixed_point_tol,options_.qz_criterium,options_.lyapunov_complex_threshold, 4, R_tmp, options_.debug);
|
2013-06-28 16:26:53 +02:00
|
|
|
|
else
|
2015-07-22 15:11:39 +02:00
|
|
|
|
Pstar_tmp = lyapunov_symm(T_tmp,R_tmp*Q*R_tmp',options_.lyapunov_fixed_point_tol,options_.qz_criterium,options_.lyapunov_complex_threshold, [], [], options_.debug);
|
2013-06-28 16:26:53 +02:00
|
|
|
|
end
|
|
|
|
|
Pstar(stable, stable) = Pstar_tmp;
|
|
|
|
|
Pinf = [];
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
2010-03-23 18:17:15 +01:00
|
|
|
|
kalman_tol = options_.kalman_tol;
|
2015-04-03 17:48:25 +02:00
|
|
|
|
diffuse_kalman_tol = options_.diffuse_kalman_tol;
|
2010-03-23 18:17:15 +01:00
|
|
|
|
riccati_tol = options_.riccati_tol;
|
|
|
|
|
data1 = Y-trend;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
% -----------------------------------------------------------------------------
|
|
|
|
|
% 4. Kalman smoother
|
|
|
|
|
% -----------------------------------------------------------------------------
|
2011-01-13 21:50:26 +01:00
|
|
|
|
|
|
|
|
|
if ~missing_value
|
|
|
|
|
for i=1:smpl
|
2014-07-15 11:13:41 +02:00
|
|
|
|
data_index{i}=(1:vobs)';
|
2011-01-13 21:50:26 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-10-13 17:26:39 +02:00
|
|
|
|
ST = T;
|
|
|
|
|
R1 = R;
|
|
|
|
|
Z = zeros(vobs,size(T,2));
|
|
|
|
|
for i=1:vobs
|
|
|
|
|
Z(i,mf(i)) = 1;
|
2011-01-13 21:50:26 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if kalman_algo == 1 || kalman_algo == 3
|
2011-03-24 15:45:16 +01:00
|
|
|
|
[alphahat,epsilonhat,etahat,ahat,P,aK,PK,decomp] = missing_DiffuseKalmanSmootherH1_Z(ST, ...
|
2011-01-13 21:50:26 +01:00
|
|
|
|
Z,R1,Q,H,Pinf,Pstar, ...
|
2014-07-15 11:13:41 +02:00
|
|
|
|
data1,vobs,np,smpl,data_index, ...
|
2015-04-03 17:48:25 +02:00
|
|
|
|
options_.nk,kalman_tol,diffuse_kalman_tol,options_.filter_decomposition);
|
2011-11-02 14:02:12 +01:00
|
|
|
|
if isinf(alphahat)
|
2011-01-13 21:50:26 +01:00
|
|
|
|
if kalman_algo == 1
|
2009-12-16 18:17:34 +01:00
|
|
|
|
kalman_algo = 2;
|
2011-01-13 21:50:26 +01:00
|
|
|
|
elseif kalman_algo == 3
|
|
|
|
|
kalman_algo = 4;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
else
|
2011-01-13 21:50:26 +01:00
|
|
|
|
error('This case shouldn''t happen')
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
2011-01-13 21:50:26 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2011-02-04 17:17:48 +01:00
|
|
|
|
|
2011-01-13 21:50:26 +01:00
|
|
|
|
if kalman_algo == 2 || kalman_algo == 4
|
|
|
|
|
if estim_params_.ncn
|
2014-07-15 11:13:41 +02:00
|
|
|
|
ST = [ zeros(vobs,vobs) Z; zeros(np,vobs) T];
|
2011-01-13 21:50:26 +01:00
|
|
|
|
ns = size(Q,1);
|
2014-07-15 11:13:41 +02:00
|
|
|
|
R1 = [ eye(vobs) zeros(vobs, ns); zeros(np,vobs) R];
|
|
|
|
|
Q = [H zeros(vobs,ns); zeros(ns,vobs) Q];
|
|
|
|
|
Z = [eye(vobs) zeros(vobs, np)];
|
2011-01-13 21:50:26 +01:00
|
|
|
|
if kalman_algo == 4
|
2014-07-15 11:13:41 +02:00
|
|
|
|
[Z,ST,R1,QT,Pstar,Pinf] = schur_statespace_transformation((1:vobs)',ST,R1,Q,options_.qz_criterium);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
2011-01-13 21:50:26 +01:00
|
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
2011-03-24 15:45:16 +01:00
|
|
|
|
[alphahat,epsilonhat,etahat,ahat,P,aK,PK,decomp] = missing_DiffuseKalmanSmootherH3_Z(ST, ...
|
2011-03-25 21:32:45 +01:00
|
|
|
|
Z,R1,Q,diag(H), ...
|
2014-07-15 11:13:41 +02:00
|
|
|
|
Pinf,Pstar,data1,vobs,np,smpl,data_index, ...
|
2015-04-03 17:48:25 +02:00
|
|
|
|
options_.nk,kalman_tol,diffuse_kalman_tol, ...
|
2011-03-25 21:32:45 +01:00
|
|
|
|
options_.filter_decomposition);
|
2011-01-13 21:50:26 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if estim_params_.ncn && (kalman_algo == 2 || kalman_algo == 4)
|
|
|
|
|
% extracting measurement errors
|
|
|
|
|
% removing observed variables from the state vector
|
2014-07-15 11:13:41 +02:00
|
|
|
|
k = vobs+(1:np);
|
2011-01-13 21:50:26 +01:00
|
|
|
|
alphahat = alphahat(k,:);
|
|
|
|
|
ahat = ahat(k,:);
|
|
|
|
|
aK = aK(:,k,:,:);
|
|
|
|
|
if ~isempty(PK)
|
|
|
|
|
PK = PK(:,k,k,:);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
2011-01-13 21:50:26 +01:00
|
|
|
|
if ~isempty(decomp)
|
|
|
|
|
decomp = decomp(:,k,:,:);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
2011-01-28 14:01:57 +01:00
|
|
|
|
if ~isempty(P)
|
2011-01-13 21:50:26 +01:00
|
|
|
|
P = P(k,k,:);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
|
|
|
|
end
|