Add diffuse Kalman filter unit tests

time-shift
Johannes Pfeifer 2016-06-16 18:57:35 +02:00 committed by Stéphane Adjemian (Hermes)
parent 06ff0c7bb6
commit a7e3ecea63
7 changed files with 245 additions and 0 deletions

View File

@ -220,6 +220,10 @@ MODFILES = \
kalman/likelihood_from_dynare/fs2000_corr_ME_missing.mod \
kalman/likelihood_from_dynare/fs2000_uncorr_ME.mod \
kalman/likelihood_from_dynare/fs2000_uncorr_ME_missing.mod \
kalman/likelihood_from_dynare/fs2000ns_corr_ME.mod \
kalman/likelihood_from_dynare/fs2000ns_corr_ME_missing.mod \
kalman/likelihood_from_dynare/fs2000ns_uncorr_ME.mod \
kalman/likelihood_from_dynare/fs2000ns_uncorr_ME_missing.mod \
second_order/burnside_1.mod \
kalman_filter_smoother/compare_results_simulation/fs2000_ML.mod \
kalman_filter_smoother/compare_results_simulation/fs2000_ML_loglinear.mod \
@ -443,6 +447,7 @@ observation_trends_and_prefiltering/ML/Trend_loglinear_no_prefilter_first_obs.m.
observation_trends_and_prefiltering/ML/Trend_loglinear_no_prefilter_first_obs.o.trs: observation_trends_and_prefiltering/ML/Trend_loglinear_no_prefilter.o.trs
observation_trends_and_prefiltering/ML/Trend_loglinear_no_prefilter.m.trs: observation_trends_and_prefiltering/ML/Trend_no_prefilter.m.trs
observation_trends_and_prefiltering/ML/Trend_loglinear_no_prefilter.o.trs: observation_trends_and_prefiltering/ML/Trend_no_prefilter.o.trs
kalman/likelihood_from_dynare/fs2000_corr_ME.m.trs: kalman/likelihood_from_dynare/fs2000_uncorr_ME.m.trs
kalman/likelihood_from_dynare/fs2000_corr_ME.o.trs: kalman/likelihood_from_dynare/fs2000_uncorr_ME.o.trs
kalman/likelihood_from_dynare/fs2000_uncorr_ME_missing.m.trs: kalman/likelihood_from_dynare/fs2000_uncorr_ME.m.trs
@ -450,6 +455,13 @@ kalman/likelihood_from_dynare/fs2000_uncorr_ME_missing.o.trs: kalman/likelihood_
kalman/likelihood_from_dynare/fs2000_corr_ME_missing.m.trs: kalman/likelihood_from_dynare/fs2000_uncorr_ME.m.trs
kalman/likelihood_from_dynare/fs2000_corr_ME_missing.o.trs: kalman/likelihood_from_dynare/fs2000_uncorr_ME.o.trs
kalman/likelihood_from_dynare/fs2000ns_corr_ME.m.trs: kalman/likelihood_from_dynare/fs2000ns_uncorr_ME.m.trs
kalman/likelihood_from_dynare/fs2000ns_corr_ME.o.trs: kalman/likelihood_from_dynare/fs2000ns_uncorr_ME.o.trs
kalman/likelihood_from_dynare/fs2000ns_uncorr_ME_missing.m.trs: kalman/likelihood_from_dynare/fs2000ns_uncorr_ME.m.trs
kalman/likelihood_from_dynare/fs2000ns_uncorr_ME_missing.o.trs: kalman/likelihood_from_dynare/fs2000ns_uncorr_ME.o.trs
kalman/likelihood_from_dynare/fs2000ns_corr_ME_missing.m.trs: kalman/likelihood_from_dynare/fs2000ns_uncorr_ME.m.trs
kalman/likelihood_from_dynare/fs2000ns_corr_ME_missing.o.trs: kalman/likelihood_from_dynare/fs2000ns_uncorr_ME.o.trs
lmmcp/sw_newton.m.trs: lmmcp/sw_lmmcp.m.trs
lmmcp/sw_newton.o.trs: lmmcp/sw_lmmcp.o.trs
@ -542,6 +554,8 @@ EXTRA_DIST = \
kalman_filter_smoother/compare_results_simulation/fsdat_simul_logged.m \
kalman/likelihood_from_dynare/fs2000_model.inc \
kalman/likelihood_from_dynare/fs2000_estimation_check.inc \
kalman/likelihood_from_dynare/fs2000ns_model.inc \
kalman/likelihood_from_dynare/fs2000ns_estimation_check.inc \
identification/kim/kim2_steadystate.m \
identification/as2007/as2007_steadystate.m \
estimation/fsdat_simul.m \

View File

@ -0,0 +1,18 @@
@#include "fs2000ns_model.inc"
estimated_params;
alp, 0.33;
gam, 0.0;
del, 0.02;
stderr e_a, 0.014;
stderr e_m, 0.005;
corr e_m, e_a, 0;
stderr P_obs, 0.05;
stderr Y_obs, 0.05;
corr Y_obs, P_obs,0.5;
end;
@#define mode_file_name="fs2000ns_corr_ME_mode"
@#define data_file_name="fs_ns_dat_simul_corr_ME"
@#include "fs2000ns_estimation_check.inc"

View File

@ -0,0 +1,19 @@
@#include "fs2000ns_model.inc"
estimated_params;
alp, 0.33;
gam, 0.0;
del, 0.02;
stderr e_a, 0.014;
stderr e_m, 0.005;
corr e_m, e_a, 0;
stderr P_obs, 0.05;
stderr Y_obs, 0.05;
corr Y_obs, P_obs,0.5;
end;
@#define mode_file_name="fs2000ns_corr_ME_missing_mode"
@#define data_file_name="fs_ns_dat_simul_corr_ME_missing"
@#include "fs2000ns_estimation_check.inc"

View File

@ -0,0 +1,36 @@
%%get mode
estimation(diffuse_filter,kalman_algo=3,mode_compute=4,order=1,datafile=@{data_file_name},smoother,filter_decomposition,forecast = 8,filtered_vars,filter_step_ahead=[1,3],irf=20) m P c e W R k d y gy_obs;
fval_algo_0=oo_.likelihood_at_initial_parameters;
%%Diffuse Multivariate Kalman Filter
options_.lik_init=1;
estimation(kalman_algo=3,diffuse_filter,mode_file=@{mode_file_name},mode_compute=0,order=1,datafile=@{data_file_name},smoother,filter_decomposition,forecast = 8,filtered_vars,filter_step_ahead=[1,3],irf=20) m P c e W R k d y gy_obs;
fval_algo_2=oo_.likelihood_at_initial_parameters;
SmoothedMeasurementErrors(:,:,1)=cell2mat(struct2cell(oo_.SmoothedMeasurementErrors));
SmoothedShocks(:,:,1)=cell2mat(struct2cell(oo_.SmoothedShocks));
SmoothedVariables(:,:,1)=cell2mat(struct2cell(oo_.SmoothedVariables));
%%Diffuse univariate Kalman Filter
options_.lik_init=1;
estimation(kalman_algo=4,diffuse_filter,mode_file=@{mode_file_name},mode_compute=0,order=1,datafile=@{data_file_name},smoother,filter_decomposition,forecast = 8,filtered_vars,filter_step_ahead=[1,3],irf=20) m P c e W R k d y gy_obs;
fval_algo_4=oo_.likelihood_at_initial_parameters;
SmoothedMeasurementErrors(:,:,2)=cell2mat(struct2cell(oo_.SmoothedMeasurementErrors));
SmoothedShocks(:,:,2)=cell2mat(struct2cell(oo_.SmoothedShocks));
SmoothedVariables(:,:,2)=cell2mat(struct2cell(oo_.SmoothedVariables));
if max(max(abs(SmoothedMeasurementErrors-repmat(SmoothedMeasurementErrors(:,:,1),1,1,2))))>1e-8
error('SmoothedMeasurementErrors do not match')
end
if max(max(abs(SmoothedShocks-repmat(SmoothedShocks(:,:,1),1,1,2))))>1e-8
error('SmoothedShocks do not match')
end
if max(max(abs(SmoothedVariables-repmat(SmoothedVariables(:,:,1),1,1,2))))>1e-8
error('SmoothedVariables do not match')
end
if max(abs(fval_algo_2-fval_algo_4))>1e-6
fprintf('\nLikelihood difference: %4.3f\n',max(abs(fval_algo_2-fval_algo_4)))
error('Likelihoods do not match')
end

View File

@ -0,0 +1,104 @@
/*
* This file is based on the cash in advance model described
* Frank Schorfheide (2000): "Loss function-based evaluation of DSGE models",
* Journal of Applied Econometrics, 15(6), 645-670.
*
* The equations are taken from J. Nason and T. Cogley (1994): "Testing the
* implications of long-run neutrality for monetary business cycle models",
* Journal of Applied Econometrics, 9, S37-S70.
* Note that there is an initial minus sign missing in equation (A1), p. S63.
*
* This implementation was written by Michel Juillard. Please note that the
* following copyright notice only applies to this Dynare implementation of the
* model.
*/
/*
* Copyright (C) 2004-2013 Dynare Team
*
* 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/>.
*/
var m P c e W R k d n l gy_obs gp_obs y dA Y_obs P_obs;
varexo e_a e_m;
parameters alp bet gam mst rho psi del theta;
alp = 0.33;
bet = 0.99;
gam = 0;
mst = 1;
rho = 0.7;
psi = 0.787;
del = 0.02;
theta=0;
model;
dA = exp(gam+e_a);
log(m) = (1-rho)*log(mst) + rho*log(m(-1))+e_m;
-P/(c(+1)*P(+1)*m)+bet*P(+1)*(alp*exp(-alp*(gam+log(e(+1))))*k^(alp-1)*n(+1)^(1-alp)+(1-del)*exp(-(gam+log(e(+1)))))/(c(+2)*P(+2)*m(+1))=0;
W = l/n;
-(psi/(1-psi))*(c*P/(1-n))+l/n = 0;
R = P*(1-alp)*exp(-alp*(gam+e_a))*k(-1)^alp*n^(-alp)/W;
1/(c*P)-bet*P*(1-alp)*exp(-alp*(gam+e_a))*k(-1)^alp*n^(1-alp)/(m*l*c(+1)*P(+1)) = 0;
c+k = exp(-alp*(gam+e_a))*k(-1)^alp*n^(1-alp)+(1-del)*exp(-(gam+e_a))*k(-1);
P*c = m;
m-1+d = l;
e = exp(e_a);
y = k(-1)^alp*n^(1-alp)*exp(-alp*(gam+e_a));
gy_obs = dA*y/y(-1);
gp_obs = (P/P(-1))*m(-1)/dA;
Y_obs/Y_obs(-1) = gy_obs;
P_obs/P_obs(-1) = gp_obs;
end;
steady_state_model;
dA = exp(gam);
gst = 1/dA;
m = mst;
khst = ( (1-gst*bet*(1-del)) / (alp*gst^alp*bet) )^(1/(alp-1));
xist = ( ((khst*gst)^alp - (1-gst*(1-del))*khst)/mst )^(-1);
nust = psi*mst^2/( (1-alp)*(1-psi)*bet*gst^alp*khst^alp );
n = xist/(nust+xist);
P = xist + nust;
k = khst*n;
l = psi*mst*n/( (1-psi)*(1-n) );
c = mst/P;
d = l - mst + 1;
y = k^alp*n^(1-alp)*gst^alp;
R = mst/bet;
W = l/n;
ist = y-c;
q = 1 - d;
e = 1;
gp_obs = m/dA;
gy_obs = dA;
Y_obs = gy_obs;
P_obs = gp_obs;
end;
varobs P_obs Y_obs;
shocks;
var e_a; stderr 0.014;
var e_m; stderr 0.005;
end;
options_.TeX=1;
options_.debug=1;

View File

@ -0,0 +1,36 @@
@#include "fs2000ns_model.inc"
stoch_simul(periods=200, order=1,irf=0);
temp=oo_.endo_simul;
%add measurement error
oo_.endo_simul(strmatch('Y_obs',M_.endo_names,'exact'),:)=oo_.endo_simul(strmatch('Y_obs',M_.endo_names,'exact'),:)+0.05*randn(1,size(oo_.endo_simul,2));
oo_.endo_simul(strmatch('P_obs',M_.endo_names,'exact'),:)=oo_.endo_simul(strmatch('P_obs',M_.endo_names,'exact'),:)+0.05*randn(1,size(oo_.endo_simul,2));
datatomfile('fs_ns_dat_simul_uncorr_ME', char('Y_obs', 'P_obs'));
oo_.endo_simul(strmatch('Y_obs',M_.endo_names,'exact'),[7,199])=NaN;
oo_.endo_simul(strmatch('P_obs',M_.endo_names,'exact'),[151,199])=NaN;
datatomfile('fs_ns_dat_simul_uncorr_ME_missing', char('Y_obs', 'P_obs'));
shock_mat=chol([1 0.5; 0.5 1])*0.05*randn(2,size(oo_.endo_simul,2));
oo_.endo_simul(strmatch('Y_obs',M_.endo_names,'exact'),:)=oo_.endo_simul(strmatch('Y_obs',M_.endo_names,'exact'),:)+shock_mat(1,:);
oo_.endo_simul(strmatch('P_obs',M_.endo_names,'exact'),:)=oo_.endo_simul(strmatch('P_obs',M_.endo_names,'exact'),:)+shock_mat(2,:);
datatomfile('fs_ns_dat_simul_corr_ME', char('Y_obs', 'P_obs'));
oo_.endo_simul(strmatch('Y_obs',M_.endo_names,'exact'),[7,199])=NaN;
oo_.endo_simul(strmatch('P_obs',M_.endo_names,'exact'),[151,199])=NaN;
datatomfile('fs_ns_dat_simul_corr_ME_missing', char('Y_obs', 'P_obs'));
estimated_params;
alp, 0.33;
gam, 0.0;
del, 0.02;
stderr e_a, 0.014;
stderr e_m, 0.005;
corr e_m, e_a, 0;
stderr P_obs, 0.05;
stderr Y_obs, 0.05;
//corr gp_obs, gy_obs,0;
end;
@#define mode_file_name="fs2000ns_uncorr_ME_mode"
@#define data_file_name="fs_ns_dat_simul_uncorr_ME"
@#include "fs2000ns_estimation_check.inc"

View File

@ -0,0 +1,18 @@
@#include "fs2000ns_model.inc"
estimated_params;
alp, 0.33;
gam, 0.0;
del, 0.02;
stderr e_a, 0.014;
stderr e_m, 0.005;
corr e_m, e_a, 0;
stderr P_obs, 0.05;
stderr Y_obs, 0.05;
//corr gp_obs, gy_obs,0;
end;
@#define mode_file_name="fs2000ns_uncorr_ME_missing_mode"
@#define data_file_name="fs_ns_dat_simul_uncorr_ME_missing"
@#include "fs2000ns_estimation_check.inc"