data=load('ag.txt')/100;
windowlength=500;
for i=1:size(data,1)-windowlength
    htx(i,:)=conditionalvar(data(i:i+windowlength-1,1), 1, 1,'NORMAL');
    hty(i,:)=conditionalvar(data(i:i+windowlength-1,2), 1, 1, 'NORMAL');
    forecast1(i,:)=GARCHforecast(data(i:i+windowlength-1,1),'NORMAL');
    forecast2(i,:)=GARCHforecast(data(i:i+windowlength-1,2),'NORMAL');
end
htx=htx';
hty=hty';
for i=1:size(data,1)-windowlength
    stderrorx(:,i)=data(i:i+windowlength-1,1)./sqrt(htx(:,i));
    stderrory(:,i)=data(i:i+windowlength-1,2)./sqrt(hty(:,i));
end


[T,k] = size(stderrorx);
for i=1:k
u(:,i) = empiricalCDF(stderrorx(:,i));
v(:,i) = empiricalCDF(stderrory(:,i));
end

for i=1:k
kappa1(i,:) = corrcoef12(norminv(u(:,i)),norminv(v(:,i)));
end
%Volatility forecast
htab=kappa1.*sqrt(forecast1.*forecast2);
ratio=htab./forecast2;
hedgereturn=data(501:938,1)-ratio.*data(501:938,2);
naivereturn=data(501:938,1)-data(501:938,2);
hedgereturn=hedgereturn*100;
naivereturn=naivereturn*100;
varmodel=var(hedgereturn);
varnaive=var(naivereturn);
modelvol=hedgereturn.^2;
naivevol=naivereturn.^2;


