clear all; clc;
tic;
data = load('data.txt'); %Note: The first column in the data is spot log-price series and the second one is futures price series
data=data(:,1:2);

inSample =500;% In-sample window length
outOfSample = length(data) - inSample;

h = zeros(outOfSample,1);
for t = 1:outOfSample
    x = data(t:inSample+t,:);
    p = size(x, 2); % Number of variables.
    db0 = [ 1 ];        % One parameter implies restriction d = b.
    % Select lag length (after performing a series of tests omitted here).
    k = 1;
    % Select cointegrating rank.
    r = 1;
    % Obtain default options settings for unrestricted estimation.
    FCVARoptions = DefaultEstnOptions(db0, k, r, p);
    % Perform estimation.
    [ estimates ] = P_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);
    covSF = cov(epsilon);
    varF = var(epsilon(:,2));
    h(t) = covSF(1,2)/varF;
end
ratio=h;
toc;