v4: more changes related to new filter

more variables returned by the smoother saved in oo_
    DiffuseKalmanSmoother1_Z.m isn't finished yet


git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1689 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
michel 2008-02-04 17:52:16 +00:00
parent 290722e3fd
commit 2f3767d317
4 changed files with 141 additions and 33 deletions

View File

@ -1,4 +1,4 @@
function [alphahat,etahat,a, aK] = DiffuseKalmanSmoother1_Z(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
function [alphahat,etahat,a,aK,P,PK,d] = DiffuseKalmanSmoother1_Z(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
% function [alphahat,etahat,a, aK] = DiffuseKalmanSmoother1(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
% Computes the diffuse kalman smoother without measurement error, in the case of a non-singular var-cov matrix
@ -20,7 +20,14 @@ function [alphahat,etahat,a, aK] = DiffuseKalmanSmoother1_Z(T,Z,R,Q,Pinf1,Pstar1
% etahat: smoothed shocks
% a: matrix of one step ahead filtered state variables
% aK: 3D array of k step ahead filtered state variables
% (meaningless for periods 1:d)
% 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)
% d: number of periods where filter remains in diffuse part
% (should be equal to the order of integration of the model)
%
% SPECIAL REQUIREMENTS
% See "Filtering and Smoothing of State Vector for Diffuse State Space
% Models", S.J. Koopman and J. Durbin (2003, in Journal of Time Series
@ -44,7 +51,8 @@ spinf = size(Pinf1);
spstar = size(Pstar1);
v = zeros(pp,smpl);
a = zeros(mm,smpl+1);
aK = zeros(nk,mm,smpl+1);
aK = zeros(nk,mm,smpl+nk);
PK = zeros(nk,mm,mm,smpl+nk);
iF = zeros(pp,pp,smpl);
Fstar = zeros(pp,pp,smpl);
iFinf = zeros(pp,pp,smpl);
@ -77,6 +85,7 @@ while rank(Pinf(:,:,t+1),crit1) & t<smpl
Kinf(:,:,t) = T*Pinf(:,:,t)*Z'*iFinf(:,:,t);
a(:,t+1) = T*a(:,t) + Kinf(:,:,t)*v(:,t);
aK(1,:,t+1) = a(:,t+1);
% isn't a meaningless as long as we are in the diffuse part? MJ
for jnk=2:nk,
aK(jnk,:,t+jnk) = T^(jnk-1)*a(:,t+1);
end
@ -106,12 +115,16 @@ while notsteady & t<smpl
iF(:,:,t) = inv(F);
K(:,:,t) = T*P(:,:,t)*Z'*iF(:,:,t);
L(:,:,t) = T-K(:,:,t)*Z;
a(:,t+1) = T*a(:,t) + K(:,:,t)*v(:,t);
aK(1,:,t+1) = a(:,t+1);
for jnk=2:nk,
aK(jnk,:,t+jnk) = T^(jnk-1)*a(:,t+1);
a(:,t+1) = T*a(:,t) + K(:,:,t)*v(:,t);
af = a(:,t);
Pf = P(:,:,t);
for jnk=1:nk,
af = T*af;
Pf = T*Pf*T' + QQ;
aK(jnk,:,t+jnk) = af;
PK(jnk,:,:,t+jnk) = Pf;
end
P(:,:,t+1) = T*P(:,:,t)*transpose(T)-T*P(:,:,t)*Z'*K(:,:,t)' + QQ;
P(:,:,t+1) = T*P(:,:,t)*T'-T*P(:,:,t)*Z'*K(:,:,t)' + QQ;
notsteady = ~(max(max(abs(P(:,:,t+1)-P(:,:,t))))<crit);
end
K_s = K(:,:,t);
@ -127,9 +140,13 @@ while t<smpl
t=t+1;
v(:,t) = Y(:,t) - Z*a(:,t);
a(:,t+1) = T*a(:,t) + K_s*v(:,t);
aK(1,:,t+1) = a(:,t+1);
for jnk=2:nk,
aK(jnk,:,t+jnk) = T^(jnk-1)*a(:,t+1);
af = a(:,t);
Pf = P(:,:,t);
for jnk=1:nk,
af = T*af;
Pf = T*Pf*T' + QQ;
aK(jnk,:,t+jnk) = af;
PK(jnk,:,:,t+jnk) = Pf;
end
end
t = smpl+1;

View File

@ -1,6 +1,6 @@
function [alphahat,etahat,a1, aK] = DiffuseKalmanSmoother3_Z(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
function [alphahat,etahat,a1,P,aK,PK,d,decomp] = DiffuseKalmanSmoother3_Z(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
% function [alphahat,etahat,a1, aK] = DiffuseKalmanSmoother3(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
% function [alphahat,etahat,a1,P,aK,PK,d,decomp_filt] = DiffuseKalmanSmoother3(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
% Computes the diffuse kalman smoother without measurement error, in the case of a singular var-cov matrix.
% Univariate treatment of multivariate time series.
%
@ -21,7 +21,15 @@ function [alphahat,etahat,a1, aK] = DiffuseKalmanSmoother3_Z(T,Z,R,Q,Pinf1,Pstar
% etahat: smoothed shocks
% a1: matrix of one step ahead filtered state variables
% aK: 3D array of k step ahead filtered state variables
% (meaningless for periods 1:d)
% 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)
% d: number of periods where filter remains in diffuse part
% (should be equal to the order of integration of the model)
% decomp: decomposition of the effect of shocks on filtered values
%
% SPECIAL REQUIREMENTS
% See "Filtering and Smoothing of State Vector for Diffuse State Space
% Models", S.J. Koopman and J. Durbin (2003, in Journal of Time Series
@ -67,7 +75,9 @@ Linf = zeros(mm,mm,pp,smpl_diff);
L0 = zeros(mm,mm,pp,smpl_diff);
Kstar = zeros(mm,pp,smpl_diff);
P = zeros(mm,mm,smpl+1);
P1 = P;
P1 = P;
aK = zeros(nk,mm,smpl+nk);
PK = zeros(nk,mm,mm,smpl+nk);
Pstar = zeros(spstar(1),spstar(2),smpl_diff+1); Pstar(:,:,1) = Pstar1;
Pinf = zeros(spinf(1),spinf(2),smpl_diff+1); Pinf(:,:,1) = Pinf1;
Pstar1 = Pstar;
@ -75,7 +85,7 @@ Pinf1 = Pinf;
crit = options_.kalman_tol;
crit1 = 1.e-6;
steady = smpl;
rr = size(Q,1);
rr = size(Q,1); % number of structural shocks
QQ = R*Q*transpose(R);
QRt = Q*transpose(R);
alphahat = zeros(mm,smpl);
@ -180,18 +190,25 @@ while notsteady & t<smpl
end
end
a(:,t+1) = T*a(:,t);
af = a(:,t);
Pf = P(:,:,t);
for jnk=1:nk,
aK(jnk,:,t+jnk) = T^jnk*a(:,t);
af = T*af;
Pf = T*Pf*T' + QQ;
aK(jnk,:,t+jnk) = af;
PK(jnk,:,:,t+jnk) = Pf;
end
P(:,:,t+1) = T*P(:,:,t)*T' + QQ;
notsteady = ~(max(max(abs(P(:,:,t+1)-P(:,:,t))))<crit);
end
P_s=tril(P(:,:,t))+tril(P(:,:,t),-1)';
P1_s=tril(P1(:,:,t))+tril(P1(:,:,t),-1)';
Fi_s = Fi(:,t);
Ki_s = Ki(:,:,t);
L_s =Li(:,:,:,t);
if t<smpl
P = cat(3,P(:,:,1:t),repmat(P_s,[1 1 smpl-t]));
P1 = cat(3,P1(:,:,1:t),repmat(P1_s,[1 1 smpl-t]));
Fi = cat(2,Fi(:,1:t),repmat(Fi_s,[1 1 smpl-t]));
Li = cat(4,Li(:,:,:,1:t),repmat(L_s,[1 1 smpl-t]));
Ki = cat(3,Ki(:,:,1:t),repmat(Ki_s,[1 1 smpl-t]));
@ -207,8 +224,13 @@ while t<smpl
end
end
a(:,t+1) = T*a(:,t);
af = a(:,t);
Pf = P(:,:,t);
for jnk=1:nk,
aK(jnk,:,t+jnk) = T^jnk*a(:,t);
af = T*af;
Pf = T*Pf*T' + QQ;
aK(jnk,:,t+jnk) = af;
PK(jnk,:,:,t+jnk) = Pf;
end
end
a1(:,t+1) = a(:,t+1);
@ -270,3 +292,29 @@ else
end
a=a(:,1:end-1);
if nargout > 7
decomp = zeros(nk,mm,rr,smpl+nk);
ZRQinv = inv(Z*QQ*Z');
for t = d:smpl
ri_d = zeros(mm,1);
for i=pp:-1:1
if Fi(i,t) > crit
ri_d = Z(i,:)'/Fi(i,t)*v(i,t)+Li(:,:,i,t)'*ri_d;
end
end
% calculate eta_tm1t
eta_tm1t = QRt*ri_d;
% calculate decomposition
Ttok = eye(mm,mm);
for h = 1:nk
for j=1:rr
eta=zeros(rr,1);
eta(j) = eta_tm1t(j);
decomp(h,:,j,t+h) = Ttok*P1(:,:,t)*Z'*ZRQinv*Z*R*eta;
end
Ttok = T*Ttok;
end
end
end

View File

@ -1,4 +1,4 @@
function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R] = DsgeSmoother(xparam1,gend,Y)
function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R,P,PK,d,decomp] = DsgeSmoother(xparam1,gend,Y)
% Estimation of the smoothed variables and innovations.
%
% INPUTS
@ -14,9 +14,17 @@ function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R] = Dsge
% 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.
% o T and R [double] Matrices defining the state equation (T is the (m*m) transition matrix).
% 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)
% d: number of periods where filter remains in diffuse part
% (should be equal to the order of integration of the model)
%
% ALGORITHM
% Metropolis-Hastings.
% Diffuse Kalman filter (Durbin and Koopman)
%
% SPECIAL REQUIREMENTS
% None.
@ -27,8 +35,18 @@ function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R] = Dsge
global bayestopt_ M_ oo_ estim_params_ options_
alphahat = [];
epsilonhat = [];
etahat = [];
epsilonhat = [];
ahat = [];
SteadyState = [];
trend_coeff = [];
aK = [];
T = [];
R = [];
P = [];
PK = [];
d = [];
decomp = [];
nobs = size(options_.varobs,1);
smpl = size(Y,2);
@ -168,15 +186,31 @@ function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R] = Dsge
end
elseif options_.kalman_algo == 3
[alphahat,etahat,ahat,aK] = DiffuseKalmanSmoother3(T,R,Q,Pinf,Pstar,Y,trend,nobs,np,smpl,mf);
elseif options_.kalman_algo == 4
elseif options_.kalman_algo == 4 | options_.kalman_algo == 5
data1 = Y - trend;
[alphahat,etahat,ahat,aK] = DiffuseKalmanSmoother1_Z(ST,Z,R1,Q,Pinf,Pstar,data1,nobs,np,smpl);
alphahat = QT*alphahat;
ahat = QT*ahat;
elseif options_.kalman_algo == 5
data1 = Y - trend;
[alphahat,etahat,ahat,aK] = DiffuseKalmanSmoother3_Z(ST,Z,R1,Q,Pinf,Pstar,data1,nobs,np,smpl);
if options_.kalman_algo == 4
[alphahat,etahat,ahat,P,aK,PK,d] = DiffuseKalmanSmoother1_Z(ST, ...
Z,R1,Q,Pinf,Pstar,data1,nobs,np,smpl);
else
[alphahat,etahat,ahat,P,aK,PK,d,decomp] = DiffuseKalmanSmoother3_Z(ST, ...
Z,R1,Q,Pinf,Pstar,data1,nobs,np,smpl);
end
alphahat = QT*alphahat;
ahat = QT*ahat;
if options_.nk > 0
nk = options_.nk;
for jnk=1:nk
aK(jnk,:,:) = QT*squeeze(aK(jnk,:,:));
for i=1:size(PK,4)
PK(jnk,:,:,i) = QT*squeeze(PK(jnk,:,:,i))*QT';
end
for i=1:size(decomp,4)
decomp(jnk,:,:,i) = QT*squeeze(decomp(jnk,:,:,i));
end
end
for i=1:size(P,4)
P(:,:,i) = QT*squeeze(P(:,:,i))*QT';
end
end
end
end

View File

@ -880,11 +880,20 @@ if (any(bayestopt_.pshape >0 ) & options_.mh_replic) | ...
% return
end
if ~((any(bayestopt_.pshape > 0) & options_.mh_replic) | (any(bayestopt_.pshape ...
> 0) & options_.load_mh_file)) | ~options_.smoother
if (~((any(bayestopt_.pshape > 0) & options_.mh_replic) | (any(bayestopt_.pshape ...
> 0) & options_.load_mh_file)) ...
| ~options_.smoother ) & M_.endo_nbr^2*gend < 1e6 % to be fixed
%% ML estimation, or posterior mode without metropolis-hastings or metropolis without bayesian smooth variable
options_.lik_algo = 2;
[atT,innov,measurement_error,filtered_state_vector,ys,trend_coeff] = DsgeSmoother(xparam1,gend,data);
[atT,innov,measurement_error,filtered_state_vector,ys,trend_coeff,aK,T,R,P,PK,d,decomp] = DsgeSmoother(xparam1,gend,data);
oo_.Smoother.SteadyState = ys;
oo_.Smoother.TrendCoeffs = trend_coeff;
oo_.Smoother.integration_order = d;
oo_.Smoother.variance = P;
if options_.nk ~= 0
oo_.FilteredVariablesKStepAhead = aK(options_.filter_step_ahead,:,:);
oo_.FilteredVariablesKStepAheadVariances = PK(options_.filter_step_ahead,:,:,:);
oo_.FilteredVariablesShockDecomposition = decomp(options_.filter_step_ahead,:,:,:);
end
for i=1:M_.endo_nbr
eval(['oo_.SmoothedVariables.' deblank(M_.endo_names(dr.order_var(i),:)) ' = atT(i,:)'';']);
eval(['oo_.FilteredVariables.' deblank(M_.endo_names(dr.order_var(i),:)) ' = filtered_state_vector(i,:)'';']);