% MATLAB PROGRAM: comovement.m

% DESCRIPTION:
% Given an estimated VAR stored in the matrix F, and a variance-covariance matrix
% for the residuals (SIGMAV), this program calculates the correlation of the VAR forecast
% errors at the forecast horizons given by the variable forecastrange.

% VARIABLES ASSUMED TO BE IN THE WORKSPACE BEFORE EXECUTION OF comovement.m
% (these will be variables associated with the estimated system)
% ntrend -- number of trends included in the VAR.
%   ntrend==0 --> VAR has only a constant
%   ntrend==1 --> VAR has a constant and linear trend
%   ntrend==2 --> VAR has a constant, linear and quadratic trend
% numlags-- the number of lags included in the estimated VAR
% X-- regressor matrix
% F-- estimated coefficient matrix (does not include the contstant
%       or trend coefficients
% RESID-- residuals from VAR estimation
% SIGMAV-- variance-covariance matrix of the residuals
% theta-- the estimated coefficients in a 1-column vector format

% GLOBAL VARIABLES ASSUMED TO BE DEFINED IN WORKSPACE:
% datevec- vector that gives series and sample start and end indices
% numvars-- number of variables (should be set=2)
% seriessdate-- index of the series start date (should = 1)
% seriesedate-- index of the series end date (should be the index of the last element
%               in the time-series)
% smplsdate-- index of the first element in the sample
% smpledate-- index of the last element in the sample
% identity-- a numvars by numvars indentity matrix
% lvdat-- number of observations in the sample range (smpledate-smplsdate+1)
% infocrit-- a string value equal to either 'aic' or 'bic'; gives the type of 
%            model selection criterion to use in selecting best VAR model
% unitroot-- value of 0 or 1, indicates whether a unitroot should be imposed in estimation
% maxforchor-- maximum periods ahead to estimate a correlation (maximum of forecastrange)
% numreplic-- number of replicated economies to use in confidence interval calculation
%             (used in coninterval.m program)
% onlyaic-- value of 0 or 1, indicates whether only selecting best model or actually 
%           estimating coefficients (allows program to skip steps if only selecting
%           best model

% OUTPUT:
% corroutprice-- the correlations of the VAR forecast errors at various forecast 
%                horizons specified by forecastrange

% 		These programs are available at:
%		ftp://weber.ucsd.edu/pub/wdenhaan/comov/
%
% September 25, 2000	
% Written by Steve Sumner, University of California, San Diego
% Support provided by Wouter den Haan's NSF grant #9708587 is gratefully acknowledged.
%
% Documentation of the program is given at the end of the program
% and on the website http://weber.ucsd.edu/~wdenhaan/soft.html
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

global datevec numvars seriessdate seriesedate smplsdate smpledate identity...
   lvdat infocrit unitroot maxforchor numreplic onlyaic

if unitroot==0
   F0=eye(numvars*numlags);
elseif unitroot==1
   F0=eye(numvars*(numlags+1));
else
   display('Unit root must equal 0 or 1');
end

% Create the Forecast Error Covariance Matrix
SIGH0=zeros(numvars,numvars);
for h=1:maxforchor
   eval(['F' num2str(h) '=F*F' num2str(h-1) ';']);
   eval(['SIGH' num2str(h) '=SIGH' num2str(h-1) '+F' num2str(h-1)...
         '(1:numvars,1:numvars)*SIGMAV*transpose(F' num2str(h-1) '(1:numvars,1:numvars));']);
end

% Create a times series of the Variances, and Covariances
varprice=zeros(maxforchor,1);
varoutput=zeros(maxforchor,1);
covpriceout=zeros(maxforchor,1);

% Create a vector of variances and covariances for each forecast period
for i=1:maxforchor
   eval([ 'varoutput(i)=SIGH' num2str(i) '(1,1);']);
   eval([ 'varprice(i)=SIGH' num2str(i) '(2,2);']);
   eval([ 'covpriceout(i)=SIGH' num2str(i) '(1,2);']);
end

% Calculate the standard deviations
sdprice=varprice.^.5;
sdout=varoutput.^.5;
corroutprice=covpriceout./(sdprice.*sdout);


