% Simulate data and calculate business cycle statistics for the standard
% RBC model. TvR, 04/2009, rev 05/2012
% With thanks to Greg Ganics for many corrections


clear;
close all;


%----------------------------------------------------------------
% Solve the model using dynare
%----------------------------------------------------------------

dynare rbcmodel.mod;


%----------------------------------------------------------------
% Calculate business cycle statistics from simulated data
%----------------------------------------------------------------

% Define variable names:
varnames = {'c';'h';'K';'i';'y';'A';'util'};
varnames_dyn = M_.endo_names;
if ~isequal(varnames_dyn,varnames)
    error('you changed the number or order of variables');
end
index_y = strmatch('y',varnames(:,1),'exact');

% Load simulated data from dynare and drop first 1000 observations:
data = [c h K i y A util];
data = data(1001:end-3,:);

% Calculate steady state and mean:
levelstst = exp(oo_.steady_state);
mean_simul = mean(exp(data))';

% HP filter the data
for j = 1:size(data,2)
    data(:,j) = data(:,j) - sample_hp_filter(data(:,j),1600);
end

% Calculate sd, relative sd, autocorr and correlation with output:
sd_simul = sqrt(var(data))';
relsd_simul = sd_simul/sd_simul(index_y);

for j = 1:size(data,2)
    autocorr_simul(j,1) = corr(data(2:end,j),data(1:end-1,j));
    corry_simul(j,1) = corr(data(:,j),data(:,index_y));
end

dyntable(options_,'BUSINESS CYCLE MOMENTS',{'VARIABLE','ST.STATE','MEAN','STD.DEV.','REL.S.D.','AUTOCORR','CORR.Y'},varnames,[levelstst,mean_simul,sd_simul,relsd_simul,autocorr_simul,corry_simul],10,8,4);
