% the command running the dynare file and carrying out the related simulation of business cycle

 
addpath d:\dynare\4.4.2\matlab
cd C:\Users\Jacky\Desktop
addpath C:\Users\Jacky\Desktop
dynare RBC

m=2;% the number of the state variables k & a
n=3;% the number of the control variables

%    define s(t)=[k(t),epsilon(t)]
%    s(t)=A*s(t-1)+B*eta(t)      % A is a m*m(2*2) matrix,
%    eta(t) is a matrix w*1, and here w=1, B is 2*1 B=[0,1]'
%    define x(t)=[y(t),c(t),i(t)]'
%    x(t)=phi*s(t) phi is the policy function, and is 3*2, x(t) is 3*1 matrix 
% combining these two equations, x(t)=phi*A*s(t-1)+phi*B*eta(t)
% the full equation system can be expressed as: 
%         s(t)=A*s(t-1)+B*eta(t)   s(t)2*1
%         x(t)=C*s(t-1)+D*eta(t)  where C=phi*A 3*2 D=phi*B  3*1   
%combining these into one expression by denoting Y(t)=[s(t),x(t)], ¦·=[A C]¡¯, and ¦¸=[B D]¡¯:      
%                               Y(t)=¦·*s(t-1)+¦¸*eta(t)

psi=oo_.dr.ghx;% the coefficients of the ¦· matrix are stored in ¡°oo .dr.ghx¡±
omega=oo_.dr.ghu;%the coefficients of the ¦¸ matrix are stored in ¡°oo .dr.ghu¡±
ss=oo_.dr.ys;% the command of oo_.dr.ys is used to save the steady value of the endogenous variables. %The vector rows correspond to all endogenous in the declaration order

T=200;

es=sigma*randn(T,1); % randn(T,1) produces the random matrix 200*1£¬ sigmae is the variance of e in the dynare code
Xsim=zeros(n+m,T);    %

Xsim(:,1)=omega*es(1,1);  % replace the components in the first column of es ( the first shock to the five variables)

for j=2:T
   Xsim(:,j)=psi*Xsim(1:2,j-1)+omega*es(j,1);  % typo here? why the rows chosen are 3 and 4 
end

for j=1:T
Xsim(:,j)=Xsim(:,j)+ss;
end

% draw the simulation of business cycle
time=1:200;
output=Xsim(1,:);
investment=Xsim(2,:);
capital=Xsim(3,:);
technology=Xsim(4,:);
consumption=Xsim(5,:);
figure(2)
subplot(1,3,1);
plot(time,output);
subplot(1,3,2);
plot(time,investment);
subplot(1,3,3);
plot(time,consumption);