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).
|
|
|
|
|
% 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)
|
2008-02-04 18:52:16 +01:00
|
|
|
|
%
|
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
|
|
|
|
|
2012-06-08 18:22:34 +02:00
|
|
|
|
% Copyright (C) 2006-2012 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 = [];
|
2010-01-05 11:46:10 +01:00
|
|
|
|
nobs = size(options_.varobs,1);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
smpl = size(Y,2);
|
2005-02-18 20:54:39 +01:00
|
|
|
|
|
2012-06-07 14:15:59 +02:00
|
|
|
|
M_ = set_all_parameters(xparam1,estim_params_,M_);
|
2005-02-18 20:54:39 +01:00
|
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
|
%------------------------------------------------------------------------------
|
|
|
|
|
% 2. call model setup & reduction program
|
|
|
|
|
%------------------------------------------------------------------------------
|
2011-01-26 13:50:11 +01:00
|
|
|
|
oo_.dr.restrict_var_list = bayestopt_.smoother_var_list;
|
|
|
|
|
oo_.dr.restrict_columns = bayestopt_.smoother_restrict_columns;
|
2011-09-19 12:19:04 +02:00
|
|
|
|
[T,R,SteadyState,info,M_,options_,oo_] = dynare_resolve(M_,options_,oo_);
|
2010-03-23 18:17:15 +01:00
|
|
|
|
bayestopt_.mf = bayestopt_.smoother_mf;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
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
|
2005-09-11 11:38:52 +02:00
|
|
|
|
trend_coeff = zeros(nobs,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)
|
|
|
|
|
H = zeros(nobs,nobs);
|
|
|
|
|
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
|
|
|
|
|
Pstar = lyapunov_symm(T,R*Q*transpose(R),options_.qz_criterium,options_.lyapunov_complex_threshold);
|
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
|
2011-01-13 21:50:26 +01:00
|
|
|
|
[Z,ST,R1,QT,Pstar,Pinf] = schur_statespace_transformation(mf,T,R,Q,options_.qz_criterium);
|
2013-06-28 16:26:53 +02:00
|
|
|
|
elseif options_.lik_init == 4 % Start from the solution of the Riccati equation.
|
2011-06-23 23:39:15 +02:00
|
|
|
|
[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
|
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
|
|
|
|
|
Pstar_tmp = lyapunov_symm(T_tmp,Q,options_.lyapunov_fixed_point_tol,options_.lyapunov_complex_threshold, 3, R_tmp);
|
|
|
|
|
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
|
|
|
|
|
Pstar_tmp = lyapunov_symm(T_tmp,Q,options_.lyapunov_fixed_point_tol,options_.lyapunov_complex_threshold, 4, R_tmp);
|
2013-06-28 16:26:53 +02:00
|
|
|
|
else
|
2013-06-28 16:59:47 +02:00
|
|
|
|
Pstar_tmp = lyapunov_symm(T_tmp,R_tmp*Q*R_tmp',options_.qz_criterium,options_.lyapunov_complex_threshold);
|
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;
|
|
|
|
|
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
|
|
|
|
|
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
|
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, ...
|
2011-03-25 21:32:45 +01:00
|
|
|
|
data1,nobs,np,smpl,data_index, ...
|
|
|
|
|
options_.nk,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
|
|
|
|
|
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);
|
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), ...
|
|
|
|
|
Pinf,Pstar,data1,nobs,np,smpl,data_index, ...
|
|
|
|
|
options_.nk,kalman_tol,...
|
|
|
|
|
options_.filter_decomposition);
|
2011-01-13 21:50:26 +01:00
|
|
|
|
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';
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
2011-03-25 21:32:45 +01:00
|
|
|
|
if options_.filter_decomposition
|
|
|
|
|
for i=1:size(decomp,4)
|
|
|
|
|
decomp(jnk,:,:,i) = QT*dynare_squeeze(decomp(jnk,:,:,i));
|
|
|
|
|
end
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2011-01-13 21:50:26 +01:00
|
|
|
|
for i=1:size(P,4)
|
|
|
|
|
P(:,:,i) = QT*dynare_squeeze(P(:,:,i))*QT';
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
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
|
|
|
|
|
k = nobs+(1:np);
|
|
|
|
|
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
|