% MATLAB FUNCTION: lags.m
% DESCRIPTION:
% Creates a matrix containing lagged values of the input matrix for the 
%  given sample range

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUTS:
%		xmatrix--original data matrix of time-series to be used in creating 
%					the lagged series
%		numberlag--the length of the lag of the original matrix (xmatrix)
%		seriessdate--index of the first observation in the xmatrix
%		seriesedate--index of the last observation in the xmatrix
%		smplsdate--index of the first observation in the xmatrix for which the
%						the lag series should be created
% 		smpledate--index of the last observation in the xmatrix for which the
%						the lag matrix should be created
%
% OUTPUTS:
%		lagmatrix--matrix of lagged values running from smplsdate to smpledate
%
% 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function lagmatrix = lags(xmatrix,numberlag,seriessdate, seriesedate,...
   smplsdate, smpledate)

% Check to make sure the sample date range ends before or at the same 
%  time as the series date range
if smpledate>seriesedate
	display('Your sample is outside the series date range');
	break;
end

% Check to see if you have enough data in the series to create a lag of
%  the requested length
if seriessdate>smplsdate-numberlag
   display(['You do not have enough data to create a lagged series of length ' numberlag]);
   break;
else
   lagmatrix=xmatrix(smplsdate-numberlag:smpledate-numberlag,:);
end
