load Yield_data;

z=Yield_data(1:159);

r=zeros(1,20);

N=20

% Compute autocorrelation, lag =1 to 20

for k=1:20
    
   r(k)=auto_corr(z,k);
   
end

[1:20; r]

% figure  the autocorrelation graph

stem(r)

x=0:0.1:20;
y(1:size(x))=2/sqrt(159);
hold on
plot(x,y);

title({'SACF for the yield series'})

xlabel('Lag k');

ylabel('r_k');


% Compute the partial autocorrelation using recusive formula

phi=zeros(20,20);
phi(1,1)=r(1);

for k=2:20
    
    r1=0;
    r2=0;
    for j=1:k-1
        r1=r1+phi(k-1,j)*r(k-j);
        r2=r2+phi(k-1,j)*r(j);
    end
    
    phi(k,k)=(r(k)-r1)/(1-r2);
    
    for j=1:k-1
        phi(k,j)=phi(k-1,j)-phi(k,k)*phi(k-1,k-j);
    end
   
end

PHI=diag(phi);

[1:20; PHI']
