function [Rho] = acf(X,J)
%==========================================================================
% This function computes autocorrelations.  
% 
% It is used for "The Dynamic Effects of Personal and Corporate Income Tax 
% Changes in the United States: Comment" by Carsten Jentsch and Kurt G. 
% Lunsford.
% 
%   The inputs are:
%       X, the matrix of data
%       J, the number of autocorrelations to produce
%   The outputs are:
%       Rho, matrix of autocorrelations
%==========================================================================

    %dimensions of the data
    [T,N,order] = size(X);
    
    %average of the data;
    Xavg = zeros(T,N,order);
    for ii = 1:order
        preXavg = mean(X(:,:,ii));
        Xavg(:,:,ii) = ones(T,1)*preXavg;
    end

    %remove the sample average
    Xdemean = X - Xavg;
    
    %covariances
    covariance = zeros(J,N,order);
    Rho = zeros(J,N,order);
    for ii = 1:order
        for jj = 1:J
            covariance(jj,:,ii) =...
                diag(Xdemean(jj:T,:,ii)'*Xdemean(1:T-jj+1,:,ii))'/T;
        end
        Rho(:,:,ii) = covariance(:,:,ii)./(ones(J,1)*covariance(1,:,ii));
    end
end

