%  This routine calculates the autocorrelation and cross-correlation (sample) 
%of the simulated time series of the RBC model
%The numerical output (the time series) of the simulation comes out in the "sim_xyz.mat"
% file after a particular exampl.m" routine is run

%The order of the variables is as follows:

%Capital (K)
%Consumption (C)
%Output (Y)    
%Labou (L)
%Interest rate (i)
%Investment (I)
%Technology (A)

%IN order to avoid interference with the names of the variables generated when the "example" is run, 
%we define our variables by adding an "xx" to its original name. Therefore:
    
    Kxx=sim_xyz(1,:);
    Cxx=sim_xyz(2,:);
    Yxx=sim_xyz(3,:);
    Lxx=sim_xyz(4,:);
    ixx=sim_xyz(5,:);
    Ixx=sim_xyz(6,:);
    Axx=sim_xyz(7,:);
    
    % Producing the time series of the simulated variables
    
    t=1:1:150;
    figure    
    plot(t,Kxx,'k',t,Cxx,'r',t,Yxx,'b');title('Time series: K (black), C (red), Y (blue)')
    figure
    plot(t,Axx,'k',t,Ixx,'r',t,Yxx,'b');title('Time series: A (black), I (red), Y (blue)')
    figure
    plot(t,Lxx,'r',t,ixx,'k',t,Yxx,'b');title('Time series: L (red), i (black), Y (blue)')
    
    %Now let us calculate the sample autocorrelation function for each
    %variable
    %fi
    lag=0:20;
        figure
    subplot(221);plot(lag,autocorr(Kxx,20),'o-b'),grid on;title('Autocorrelation: Capital')
    subplot(222);plot(lag,autocorr(Cxx,20),'*-g'),grid on;title('Autocorrelation: Consumption')
    subplot(223);plot(lag,autocorr(Yxx,20),'+-r'),grid on;title('Autocorrelation: Output')
    subplot(224);plot(lag,autocorr(Lxx,20),'p-k'),grid on;title('Autocorrelation: Labor')
    
    figure
    subplot(221);plot(lag,autocorr(ixx,20),'o-b'),grid on; title('Autocorrelation: Interest')
    subplot(222);plot(lag,autocorr(Ixx,20),'*-g'),grid on;title('Autocorrelation: Investment')
    subplot(223);plot(lag,autocorr(Yxx,20),'+-r'),grid on;title('Autocorrelation: Output')
    subplot(224);plot(lag,autocorr(Axx,20),'p-k'),grid on;title('Autocorrelation: Technology')
    
    %Now let's go for the crosscorrelation between each variable and Output
    %(Y). Notice that Yxx against Yxx (subplot 223)gives autocorrelation of
    %Y
    
    lags=-20:20;
    
     figure
     
    subplot(221);plot(lags,crosscorr(Kxx,Yxx,20),'o-b'),grid on;title('Cross-Correlation: K vs Y')
    subplot(222);plot(lags,crosscorr(Cxx,Yxx,20),'*-g'),grid on;title('Cross-Correlation: C vs Y')
    subplot(223);plot(lags,crosscorr(Yxx,Yxx,20),'+-r'),grid on; title('Cross-Correlation: Y vs Y')
    subplot(224);plot(lags,crosscorr(Lxx,Yxx,20),'p-k'),grid on; title('Cross-Correlation: L vs Y')
  
    
    figure
    
    subplot(221);plot(lags,crosscorr(ixx,Yxx,20),'o-b'),grid on; title('Cross-Correlation: i vs Y')
    subplot(222);plot(lags,crosscorr(Ixx,Yxx,20),'*-g'),grid on; title('Cross-Correlation: I vs Y')
    subplot(223);plot(lags,crosscorr(Yxx,Yxx,20),'+-r'),grid on;title('Cross-Correlation: Y vs Y')
    subplot(224);plot(lags,crosscorr(Axx,Yxx,20),'p-k'),grid on;title('Cross-Correlation: A vs Y')
    
    %Now, let's finish with calculating the Standard DEviation of each
    %variable.
    sigmaK=std(Kxx)
    sigmaC=std(Cxx)
    sigmaY=std(Yxx)
    sigmaL=std(Lxx)
    sigmai=std(ixx)
    sigmaI=std(Ixx)
    sigmaA=std(Axx)
    
       
    figure
    y_y=[sigmaK sigmaC sigmaY sigmaL sigmai sigmaI sigmaA];
    names={'K';'C';'Y';'L';'i';'I';'A'}
    bar(y_y)
    set(gca,'xticklabel',names)
    title('Standard Deviation')
    grid on
   
    
