% Demonstrating the estimation of the fractionally cointegrated VAR model.
% Lee Morin & Morten Nielsen
% August 26, 2011
%
% FCVARmodel demonstrates the estimation of the fractional cointegration model.
% 
% Calls the following functions:
% DefaultEstnOptions(db0, k, r, p) to set estimation options.
% RankTests(x, k, db0, FCVARoptions) to perform tests to determine
%       cointegrating rank.
% FCVARestn(x, k, r, FCVARoptions) to estimate the model without restrictions.
% GetResiduals(x, k, r, dbHat, alphaHat, betaHat, rhoHat, GammaHat, EstnOptions)
%       to compute the residuals (which are then plotted) for diagnostics.
% RestrictEstnOptions(db0, k, r, p, defaultFCVARoptions) to reset estimation options
%       to impose various restrictions on the model.
% FCVARestn(x, k, r, db0, FCVARoptions) to repeat estimation with restrictions.
% GetResiduals(x, k, r, dbHat, alphaHat, betaHat, rhoHat, GammaHat, EstnOptions)
%       to compute the residuals from the restricted model for further diagnostics.
%______________________________________________________


clear
fprintf(1, '-----------------------------------------------------------------------------------------------------\n');
fprintf(1, '-----------------------------------------------------------------------------------------------------\n');
fprintf(1, '-----------------------------------------------------------------------------------------------------\n');


%-------------------------------------------------------------------------
%                   Fractionally Cointegrated VAR: Sample Program         
%-------------------------------------------------------------------------

% Load data.
x = load('TheDanishData.csv');

% Extract variables m2, y, i^b and i^d.
x = x(:, [ 3 4 6 7 ]);
p = size(x, 2); % Number of variables.



%------------------------------------------------------------------------
%                                Model Selection                         
%------------------------------------------------------------------------

% Perform model selection beginning with size of db0.
% Starting values.
db0 = [ 1 ];        % One parameter implies restriction d = b.
% db0 = [ 1 1 ];      % Two parameters define d and b separately.


if length(db0) == 1
    fprintf(1, 'Restriction d = b is imposed.\n');
elseif length(db0) == 2
    fprintf(1, 'Parameters d and b estimated separately.\n');
else
    fprintf(1, 'Warning: Starting values db0 improperly specified.\n');
end


% Select lag length (after performing a series of tests omitted here).
k = 1;




%-----------------------------------------------------------------------
%                       Cointegration Rank Tests
%-----------------------------------------------------------------------

% Obtain default options settings for unrestricted estimation.
rankTestOptions = DefaultEstnOptions(db0, k, p, p);

[ rankTestStats ] = RankTests(x, k, db0, rankTestOptions);

% Select cointegrating rank.
r = 1;



%------------------------------------------------------------------------
%             Fractionally Cointegrated VAR: Unrestricted Estimation     
%------------------------------------------------------------------------


% Obtain default options settings for unrestricted estimation.
FCVARoptions = DefaultEstnOptions(db0, k, r, p);

% Perform estimation.
[ estimates ] = FCVARestn(x, k, r, db0, FCVARoptions);

% Obtain values of estimated parameters.
dbHat = cell2mat(estimates(1));
alphaHat = cell2mat(estimates(2));
betaHat = cell2mat(estimates(3));
rhoHat = cell2mat(estimates(4));
PiHat = cell2mat(estimates(5));
OmegaHat = cell2mat(estimates(6));
GammaHat = cell2mat(estimates(7));


% Compute residuals from unrestricted estimation.

% Obtain EstnOptions from FCVARoptions.
EstnOptions = FCVARoptions{2};

% Now compute and plot residuals.
[ epsilon ] = GetResiduals(x, k, r, dbHat, alphaHat, betaHat, rhoHat, GammaHat, EstnOptions);
%plot(epsilon);
%title('Residuals from Estimation of Unrestricted Model')


%------------------------------------------------------------------------
%             Fractionally Cointegrated VAR: Restricted Estimation       
%------------------------------------------------------------------------


% Obtain restricted options settings.
restrictedFCVARoptions = RestrictEstnOptions(db0, k, r, p, FCVARoptions);

% Perform estimation.
[ restrictedEstimates ] = FCVARestn(x, k, r, db0, restrictedFCVARoptions);

% Obtain values of restricted parameter estimates.
dbHatR = cell2mat(restrictedEstimates(1));
alphaHatR = cell2mat(restrictedEstimates(2));
betaHatR = cell2mat(restrictedEstimates(3));
rhoHatR = cell2mat(restrictedEstimates(4));
PiHatR = cell2mat(restrictedEstimates(5));
OmegaHatR = cell2mat(restrictedEstimates(6));
GammaHatR = cell2mat(restrictedEstimates(7));


% Compute residuals from restricted estimation.

% Obtain EstnOptions from FCVARoptions.
restrictedEstnOptions = FCVARoptions{2};

% Now compute and plot residuals.
[ epsilonR ] = GetResiduals(x, k, r, dbHat, alphaHatR, betaHatR, rhoHatR, GammaHatR, restrictedEstnOptions);
%plot(epsilon);
%title('Residuals from Estimation of Restricted Model')


fprintf(1, '-----------------------------------------------------------------------------------------------------\n');
fprintf(1, '-----------------------------------------------------------------------------------------------------\n');
fprintf(1, '-----------------------------------------------------------------------------------------------------\n');
% end
