File size: 6,429 Bytes
ed7d493 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
clear,clc
%---------------------------------------------------------
% Add folder 'files' to the search path and load the model
%---------------------------------------------------------
addpath('files');
load('model')
%----------------------------------------------------------------------------
% Provide nodes and weights for the quadrature that approximates expectations
%----------------------------------------------------------------------------
n_e=1; % number of shocks.
[n_nodes,nodes,weights] = Monomials_2(n_e,eye(n_e)); % this quadrature function was written by Judd, Maliar, Maliar and Valero (2014).
nodes=nodes'; % transpose to n_e-by-n_nodes
%--------------------------------------------
% parameter values (for the fixed parameters)
%--------------------------------------------
GAMMA=2; ALPHA=.3; RHO=.8; SIGMA=.02;
params=eval(symparams);
%--------------------------------------------------------------------
% Start with a perturbation solution for the case of fixed parameters
%--------------------------------------------------------------------
BETA=.96; DELTA=.1;
% Steady state values
kss=((1/BETA-1+DELTA)/ALPHA)^(1/(ALPHA-1));
zss=0;
css=kss^ALPHA-DELTA*kss;
nxss=[kss;zss];
nyss=css;
pert_order=model.order(2);
M=get_moments(nodes,weights,pert_order);
% Get a perturbation solution for the case of fixed parameters. You need to
% provide the fixed value of the ms parameters (the variable chi_fixed)
chi_fixed=eval(chi);
[derivs,stoch_pert,nonstoch_pert,model]=get_pert(model,params,M,nxss,nyss,chi_fixed);
%---------------------------------------
% Proceed to Markov-switching parameters
%---------------------------------------
% The values of the parameters in each regime (there are 2 regimes)
msparams=[BETA-.02,BETA+.02;% BETA varies across regimes
DELTA-.05,DELTA+.05]; % DELTA varies across regimes
transition=[.9,.1;
.8,.2];
initial_guess=stoch_pert; % the initial guess has n_regimes columns (one for each regime).
x0=nxss; % evaluate the system at the steady state
c0=nxss; % the center of the initial guess is the steady state.
tolX=1e-6;
tolF=1e-6;
maxiter=10;
[coeffs,model]=tpsolve(initial_guess,x0,model,params,msparams,transition,c0,nodes,weights,tolX,tolF,maxiter);
% compute the model residuals
[R_funMS,g_funMS,Phi_funMS,aux_funMS]=residual(coeffs,x0,params,msparams,transition,c0,nodes,weights);
% R_funMS is a n_y-by-n_regimes matrix of the model residuals.
% g_funMS is a n_y-by-n_regimes matrix of the endogenous control variables for each current regime.
% Phi_funMS is a n_x-by-n_nodes-by-n_regimes-by-n_regimes array of next period state variables for each future node and current and future regimes.
% aux_funMS is a similar array of the auxiliary functions (for each future
% node and current/future regimes)
% To compute the model residuals only for a specific regime, add the
% required regime as the last argument
specific_regime=1;
[R_specific,g_specific,Phi_specific,auxvars_specific]=residual(coeffs,x0,params,msparams,transition,c0,nodes,weights,specific_regime);
% Compute the function g given the state x0 and the regime specific_regime
g_fun=evalg(x0,specific_regime,coeffs,c0);
% Compute the function Phi given the state x0, the control y0, the current
% and future regimes and the future shock epsp
y0=g_fun;
current_regime=1;
future_regime=2;
epsp=0;
Phi_fun=evalPhi(x0,y0,epsp,future_regime,current_regime,params,msparams);
%---------------------------------
% simulate the model for T periods
%---------------------------------
T=10000;
shocks=randn(1,T+1);
rshock=rand(1,T+1); % to determine the regime
x_simul=zeros(model.n_x,T+1);
regime_simul=zeros(1,T+1);
y_simul=zeros(model.n_y,T);
R_simul=zeros(model.n_y,T);
coeffs=reshape(coeffs,[],model.n_regimes);
x_simul(:,1)=x0;
regime_simul(1)=1;
% option=1; % compute only simulated variables
option=2; % compute model residuals
for t=1:T
xt=x_simul(:,t); % current state
regimet=regime_simul(t); % current regime
epsp=shocks(t+1); % future shock
% future regime
regimet_next=1+model.n_regimes-sum(double(logical((rshock(t+1)-cumsum(transition(regimet,:)))<0)));
% Option 1 - compute only the simulated variables
if option==1
yt=evalg(xt,regimet,coeffs,c0);
y_simul(:,t)=yt;
x_simul(:,t+1)=evalPhi(xt,yt,epsp,regimet_next,regimet,params,msparams);
regime_simul(t+1)=regimet_next;
else
% Option 2 - compute also model residuals
[Rt,yt]=residual(coeffs,xt,params,msparams,transition,c0,nodes,weights,regimet);
y_simul(:,t)=yt;
x_simul(:,t+1)=evalPhi(xt,yt,epsp,regimet_next,regimet,params,msparams);
regime_simul(t+1)=regimet_next;
R_simul(:,t)=Rt;
end
end
% Note that the simulated capital level is considerably different than the
% initial approximation point that we used. To improve accuracy, we can solve the
% model at the mean of the ergodic distribution.
meank=mean(x_simul(1,:));
ergodic_x0=[meank;0]; % solve at the mean of the ergodic distribution
[coeffs,model]=tpsolve(coeffs,ergodic_x0,model,params,msparams,transition,c0,nodes,weights,tolX,tolF,maxiter);
% simulate again and store residuals in R_simul2
x_simul(:,1)=ergodic_x0;
regime_simul(1)=1;
R_simul2=R_simul;
for t=1:T
xt=x_simul(:,t);
regimet=regime_simul(t);
epsp=shocks(t+1);
% future regime
regimet_next=1+model.n_regimes-sum(double(logical((rshock(t+1)-cumsum(transition(regimet,:)))<0)));
% Option 1 - compute only the simulated variables
if option==1
yt=evalg(xt,regimet,coeffs,c0);
y_simul(:,t)=yt;
x_simul(:,t+1)=evalPhi(xt,yt,epsp,regimet_next,regimet,params,msparams);
regime_simul(t+1)=regimet_next;
else
% Option 2 - compute also model residuals
[Rt,yt]=residual(coeffs,xt,params,msparams,transition,c0,nodes,weights,regimet);
y_simul(:,t)=yt;
x_simul(:,t+1)=evalPhi(xt,yt,epsp,regimet_next,regimet,params,msparams);
regime_simul(t+1)=regimet_next;
R_simul2(:,t)=Rt;
end
end
% compare mean errors for the two simulations
mean(abs(R_simul))
mean(abs(R_simul2))
% the second simulation is more accurate |