diff --git a/matlab/DsgeSmoother.m b/matlab/DsgeSmoother.m index 09fff8f28917bb1b22e889a257e4d3fa7197c189..1490d848fc810d371f1130b65eab8c333ae26805 100644 --- a/matlab/DsgeSmoother.m +++ b/matlab/DsgeSmoother.m @@ -265,7 +265,7 @@ if kalman_algo == 1 || kalman_algo == 3 a_initial = zeros(np,1); a_initial=set_Kalman_smoother_starting_values(a_initial,M_,oo_,options_); a_initial=T*a_initial; %set state prediction for first Kalman step; - [alphahat,epsilonhat,etahat,ahat,P,aK,PK,decomp,state_uncertainty, aahat, eehat, d, alphahat0, state_uncertainty0] = missing_DiffuseKalmanSmootherH1_Z(a_initial,ST, ... + [alphahat,epsilonhat,etahat,ahat,P,aK,PK,decomp,state_uncertainty, aahat, eehat, d, alphahat0, aalphahat0, state_uncertainty0] = missing_DiffuseKalmanSmootherH1_Z(a_initial,ST, ... Z,R1,Q,H,Pinf,Pstar, ... data1,vobs,np,smpl,data_index, ... options_.nk,kalman_tol,diffuse_kalman_tol,options_.filter_decomposition,options_.smoothed_state_uncertainty,options_.filter_covariance,options_.smoother_redux); @@ -321,7 +321,7 @@ if kalman_algo == 2 || kalman_algo == 4 a_initial = zeros(np,1); a_initial=set_Kalman_smoother_starting_values(a_initial,M_,oo_,options_); a_initial=ST*a_initial; %set state prediction for first Kalman step; - [alphahat,epsilonhat,etahat,ahat,P,aK,PK,decomp,state_uncertainty, aahat, eehat, d, alphahat0, state_uncertainty0, regimes_,TT,RR,CC,TTx,RRx,CCx] = missing_DiffuseKalmanSmootherH3_Z(a_initial,ST, ... + [alphahat,epsilonhat,etahat,ahat,P,aK,PK,decomp,state_uncertainty, aahat, eehat, d, alphahat0, aalphahat0, state_uncertainty0, regimes_,TT,RR,CC,TTx,RRx,CCx] = missing_DiffuseKalmanSmootherH3_Z(a_initial,ST, ... Z,R1,Q,diag(H), ... Pinf,Pstar,data1,vobs,np,smpl,data_index, ... options_.nk,kalman_tol,diffuse_kalman_tol, ... @@ -557,7 +557,9 @@ else static_var_list(static_var_list) = ~ilagged; % reconstruct smoothed variables aaa=zeros(M_.endo_nbr,gend+1); + if ~isempty(alphahat0) aaa(oo_.dr.restrict_var_list,1)=alphahat0; + end aaa(oo_.dr.restrict_var_list,2:end)=alphahat; for k=1:gend aaa(static_var_list,k+1) = C(~ilagged,:)*alphahat(:,k)+D(~ilagged,:)*etahat(:,k); @@ -580,6 +582,9 @@ else if any(ilagged) % bbb=zeros(M_.endo_nbr,gend); % bbb(oo_.dr.restrict_var_list,:)=aahat; + if ~isempty(aalphahat0) + aaa(static_var_list0,d+1) = Tstar(ilagged,:)*aalphahat0+Rstar(ilagged,:)*eehat(:,d+1); + end for k=d+2:gend aaa(static_var_list0,k) = Tstar(ilagged,:)*aahat(:,k-1)+Rstar(ilagged,:)*eehat(:,k); end diff --git a/matlab/missing_DiffuseKalmanSmootherH1_Z.m b/matlab/missing_DiffuseKalmanSmootherH1_Z.m index 3cbc664d6f66564f013756073053f9e2cfc79201..cf1430a6abe9bd6cf8f2a9668a4d9e1f917b5cf8 100644 --- a/matlab/missing_DiffuseKalmanSmootherH1_Z.m +++ b/matlab/missing_DiffuseKalmanSmootherH1_Z.m @@ -1,4 +1,4 @@ -function [alphahat,epsilonhat,etahat,atilde,P,aK,PK,decomp,V,aalphahat,eetahat,d,alphahat0,V0] = missing_DiffuseKalmanSmootherH1_Z(a_initial,T,Z,R,Q,H,Pinf1,Pstar1,Y,pp,mm,smpl,data_index,nk,kalman_tol,diffuse_kalman_tol,decomp_flag,state_uncertainty_flag,filter_covariance_flag,smoother_redux) +function [alphahat,epsilonhat,etahat,atilde,P,aK,PK,decomp,V,aalphahat,eetahat,d,alphahat0,aalphahat0,V0] = missing_DiffuseKalmanSmootherH1_Z(a_initial,T,Z,R,Q,H,Pinf1,Pstar1,Y,pp,mm,smpl,data_index,nk,kalman_tol,diffuse_kalman_tol,decomp_flag,state_uncertainty_flag,filter_covariance_flag,smoother_redux) % function [alphahat,epsilonhat,etahat,atilde,P,aK,PK,decomp,V,aalphahat,eetahat,d] = missing_DiffuseKalmanSmootherH1_Z(a_initial,T,Z,R,Q,H,Pinf1,Pstar1,Y,pp,mm,smpl,data_index,nk,kalman_tol,diffuse_kalman_tol,decomp_flag,state_uncertainty_flag,filter_covariance_flag,smoother_redux) % Computes the diffuse kalman smoother without measurement error, in the case of a non-singular var-cov matrix. @@ -141,6 +141,7 @@ else V=[]; end alphahat0=[]; +aalphahat0=[]; V0=[]; t = 0; @@ -276,6 +277,10 @@ while notsteady && t<smpl eetahat(:,st) = QRt*ri; %DK (2012), eq. 4.63 end end + if t==1 + ri = T'*ri; %compute r_{t-1}, DK (2012), eq. 4.38 with Z=0 + aalphahat0 = P(:,:,1)*ri; %DK (2012), eq. 4.35 + end end a(:,t+1) = T*atilde(:,t); if filter_covariance_flag diff --git a/matlab/missing_DiffuseKalmanSmootherH3_Z.m b/matlab/missing_DiffuseKalmanSmootherH3_Z.m index 179b28377da62a00bf890dbda7ecdeafb5964ef0..a5894922aa5815144f5e6bdc7767a99d1611b16b 100644 --- a/matlab/missing_DiffuseKalmanSmootherH3_Z.m +++ b/matlab/missing_DiffuseKalmanSmootherH3_Z.m @@ -1,4 +1,4 @@ -function [alphahat,epsilonhat,etahat,a,P1,aK,PK,decomp,V, aalphahat,eetahat,d,alphahat0,V0,varargout] = missing_DiffuseKalmanSmootherH3_Z(a_initial,T,Z,R,Q,H,Pinf1,Pstar1,Y,pp,mm,smpl,data_index,nk,kalman_tol,diffuse_kalman_tol,decomp_flag,state_uncertainty_flag, filter_covariance_flag, smoother_redux, occbin_) +function [alphahat,epsilonhat,etahat,a,P1,aK,PK,decomp,V, aalphahat,eetahat,d,alphahat0,aalphahat0,V0,varargout] = missing_DiffuseKalmanSmootherH3_Z(a_initial,T,Z,R,Q,H,Pinf1,Pstar1,Y,pp,mm,smpl,data_index,nk,kalman_tol,diffuse_kalman_tol,decomp_flag,state_uncertainty_flag, filter_covariance_flag, smoother_redux, occbin_) % function [alphahat,epsilonhat,etahat,a,P1,aK,PK,decomp,V, aalphahat,eetahat,d] = missing_DiffuseKalmanSmootherH3_Z(a_initial,T,Z,R,Q,H,Pinf1,Pstar1,Y,pp,mm,smpl,data_index,nk,kalman_tol,diffuse_kalman_tol,decomp_flag,state_uncertainty_flag, filter_covariance_flag, smoother_redux, occbin_) % Computes the diffuse kalman smoother in the case of a singular var-cov matrix. % Univariate treatment of multivariate time series. @@ -150,6 +150,7 @@ else V=[]; end alphahat0=[]; +aalphahat0=[]; V0=[]; if ~occbin_.status @@ -389,8 +390,8 @@ while notsteady && t<smpl if smoother_redux && t>1 aalphahat(:,t-1) = aha(:,1); - eetahat(:,t) = etaha(:,2); end + eetahat(:,t) = etaha(:,2); a(:,t) = ax(:,1); a1(:,t) = a1x(:,2); a1(:,t+1) = ax(:,2); @@ -469,6 +470,9 @@ while notsteady && t<smpl end ri = T'*ri; % KD (2003), eq. (23), equation for r_{t-1,p_{t-1}} end + if t==1 + aalphahat0 = P1(:,:,st)*ri; + end end if isoccbin if isqvec diff --git a/tests/kalman_filter_smoother/fs2000_smoother_redux.mod b/tests/kalman_filter_smoother/fs2000_smoother_redux.mod index 057dd82aeaecbade3b24852f3f0b6683ad0df0c5..ee13d7a2c4401d8edc725c81363fd01c77db13c6 100644 --- a/tests/kalman_filter_smoother/fs2000_smoother_redux.mod +++ b/tests/kalman_filter_smoother/fs2000_smoother_redux.mod @@ -115,12 +115,7 @@ if max(merr1)>1.e-12 error('smoother_redux with kalman_algo=1 does not replicate original smoothed static variables!') end if max(merr1U)>1.e-12 - for k=1:length(vlist1) - merr2U(k)=max(abs(oo0.UpdatedVariables.(vlist1{k})(2:end)-oo1.UpdatedVariables.(vlist1{k})(2:end))); - end - if max(merr2U)>1.e-12 - error('smoother_redux with kalman_algo=1 does not replicate original updated static variables!') - end + error('smoother_redux with kalman_algo=1 does not replicate original updated static variables!') end if max(merr1F)>1.e-12 error('smoother_redux with kalman_algo=1 does not replicate original filtered static variables!') @@ -177,12 +172,7 @@ if max(merr1)>1.e-12 error('smoother_redux with kalman_algo=2 does not replicate original smoothed static variables!') end if max(merr1U)>1.e-12 - for k=1:length(vlist1) - merr2U(k)=max(abs(oo0.UpdatedVariables.(vlist1{k})(2:end)-oo2.UpdatedVariables.(vlist1{k})(2:end))); - end - if max(merr2U)>1.e-12 - error('smoother_redux with kalman_algo=2 does not replicate original updated static variables!') - end + error('smoother_redux with kalman_algo=2 does not replicate original updated static variables!') end if max(merr1F)>1.e-12 error('smoother_redux with kalman_algo=2 does not replicate original filtered static variables!')