|
|
clear,clc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addpath('files');
|
|
|
load('model')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n_e=1;
|
|
|
[n_nodes,nodes,weights] = Monomials_2(n_e,eye(n_e));
|
|
|
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 |