clc
clg
echo on
%
%   This demo shows how to create the examples in chapter 1 of Hansen
%   and Sargent's RECURSIVE MODELS OF DYNAMIC LINEAR ECONOMIES.
%   We display the code used to generate each of the figures.
%
%   We start with the code used to create figure 1.
rand('normal');
a=[0 0 0 1];
A=compn(a);
C=zeros(4,1);
G=eye(4);
D=zeros(4,1);
w=zeros(20,1);
x0=[1 0 0 0]';
y=dlsim(A,C,G,D,w,x0);
y1=y(:,1);
v=[0 20 -.2 1.2];
pause    %  Press a key to plot a deterministic seasonal.
clc
axis(v)
plot(y1,'*'),hold
plot(y1),title('deterministic seasonal'),pause
axis
axis
hold
clg
clc
%
%
%   Now we create figure 2, an indeterministic seasonal with unit root.
%
C(1,1)=1;
w=rand(150,1);
y=dlsim(A,C,G,D,w,x0);
y2=y(:,1);
pause     %  Press a key to plot y2, the indeterministic seasonal.
plot(y2),title('indeterministic seasonal with unit root'),pause
clg
clc
%
%
%  Now we create figure 3, an indeterministic seasonal with no unit root.
%
a=[0 0 0 .7]
A=compn(a)
y=dlsim(A,C,G,D,w,x0);
y3=y(:,1);
pause   %   Press a key to plot y2, the no-unit root indeterministic seasonal

plot(y3),title('indeterministic seasonal, no unit root'),pause
clg
clc
%
%
%  We now create figure 4, a realization of a first order autoregression.
x0=[0 0 0 0]';
a=[.9 0 0 0 ];
A=compn(a);
y=dlsim(A,C,G,D,w,x0);
y4=y(:,1);
pause   % press a key to plot a realization of a first order autoregression.
plot(y4),title('first order autoregression'),pause
clg
clc
%
%
%  Now create figure 5, a realization of a second order autoregression.
a=[1.2 -.3 0 0];
A=compn(a);
y=dlsim(A,C,G,D,w,x0);
y5=y(:,1);
pause    % press a key to plot the second order autoregression.
plot(y5),title('second order autoregression'),pause
clg
clc
%
%
%  Now create figure 6, a realization of growth with homoskedastic noise.
x0=[10  0 0 0]';
a=[1.025 0 0 0];
A=compn(a);
y=dlsim(A,C,G,D,w,x0);
y6=y(:,1);
pause  % press a key to plot growth with homoskedastic noise.
 plot(y6),title('growth with homoskedastic noise'),pause
clg
clc
%
%
%  Now create figure 7, growth with heteroskedastic noise.
n=150      ;
t=[1:n]';
t=t./2;     ;
g=(1.025).^t;
wg=w.*g;
y=dlsim(A,C,G,D,wg,x0);
y7=y(:,1);
pause  %  press a key to plot growth with heteroskedastic noise.
 plot(y7),title('growth with heteroskedastic noise'),pause
clg
clc
%
%
%  Now create figure 8, a realization of a second order bivariate a.r.

w=rand(150,2);
A=[.9 .05 .05 .01;1 0 0 0;-.04 -.06 .75 -.1; 0 0 1 0];
C=zeros(4,2);
C(1,1)=1;C(3,2)=1;
G=zeros(2,4);
G(1,1)=1;
G(2,3)=1;
D=zeros(2,2);
x0=zeros(4,1);
y=dlsim(A,C,G,D,w,x0);
y8=y;
pause   % press any key to plot a second order bivariate a.r.
plot(y8),title('second order bivariate autoregression'),pause
clg
clc
%
%
%  Now create figure 9, the impulse response functions of our a.r.
%  First we create the impulse response functions.
i1=dimpulse(A,C,G,D,1,20);
i2=dimpulse(A,C,G,D,2,20);
pause  % press a key to plot the impulse response to the first innovation.
jj=[0:18];
plot(jj,i1(2:20,:)),title('response to first innovation'),pause
clg
clc
pause % hit any key to plot impulse response function to second innovn.
plot(jj,i2(2:20,:)),title('response to second innovation'),pause
clg
clc
%
%
%  Now create the table depicting the decompoistion of forecasting variance
%  corresponding to the above impulse response functions.
[tab1,tab2]=evardec(A,C,G,-18,-1,eye(2));
tab2,pause
clg
clc
%
%
%  Create figure 11, the realization of the rational expectations model.
a=[1.2 -.22];A=compn(a);
C=[1 0]';
G1=[1 0];
D=zeros(2,1);
G2=.5*G1/(eye(2)-.9*A);
G=[G1;G2];
x0=[0 0]';
w=rand(150,1);
y=dlsim(A,C,G,D,w,x0);
pause  % press a key to see a realization of m and p
plot(y),title('simulation of m and p'),pause
clg
clc
%
%
%   Create figure 12, the impulse response function of the rational
%   expectations model.
%

y=dimpulse(A,C,G,D,1,20);
pause  %  press a key to plot the impulse response function
plot(jj,y(2:20,:)),title('impulse response of m and p'),pause
clg
clc
%
%
%   Now we'll create the arma representation for the (m,p) process.
%   In particular, we find the representation
%
%      den(L) [m(t) p(t)]' =  num(L) w(t)
%
%   where den(L) is a scalar polynomial in the lag operator L
%   and num(L) is a (2 times 1) vector polynomial in L.
%   We create n(L) and d(L) as follows:
%
[num,den]=ss2tf(A,C,G,D,1)
