data=load('ag.txt');
windowlength=500;
for i=1:size(data,1)-windowlength
    htx(i,:)=figarchvar(data(i:i+windowlength-1,1), 1, 1,'NORMAL');
    hty(i,:)=figarchvar(data(i:i+windowlength-1,2), 1, 1, '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
options = optimset('Display','iter','TolCon',10^-12,'TolFun',10^-4,'TolX',10^-6);
lower = 0.0001;
theta0 = 1;
for i=1:k
kappa1(i,:) = corrcoef12(norminv(u(:,i)),norminv(v(:,i)));
kappa2(i,:) = fmincon('claytonCL',1,[],[],[],[],0.0001,[],[],options,[u(:,i),v(:,i)]);
kappa3(i,:) = fmincon('claytonCL',theta0,[],[],[],[],lower,[],[],options,1-[u(:,i),v(:,i)]);
kappa4(i,:) = fmincon('plackettCL',theta0,[],[],[],[],lower,[],[],options,[u(:,i),v(:,i)]);
kappa5(i,:) = fmincon('frankCL',theta0,[],[],[],[],lower,[],[],options,[u(:,i),v(:,i)]);
kappa6(i,:) = fmincon('gumbelCL',2,[],[],[],[],1.1,[],[],options,[u(:,i),v(:,i)]);
kappa7(i,:) = fmincon('gumbelCL',2,[],[],[],[],1.1,[],[],options,1-[u(:,i),v(:,i)]);
end
for i=1:k
    lower1 = [-0.99 , 2.1 ];
    upper1 = [ 0.999, 100 ];
    theta01(:,i) = [kappa1(i);10];
end
for i=1:k
    kappa8(i,:) = fmincon('tcopulaCL',theta01(:,i),[],[],[],[],lower1,upper1,[],options,[u(:,i),v(:,i)]);
end
for i=1:k
lower2 = [0 , 0 ];
upper2 = [ 1 , 1];
theta02 = [0.25;0.25];
kappa9(i,:) = fmincon('sym_jc_CL',theta02,[],[],[],[],lower2,upper2,[],options,[u(:,i),v(:,i)]);
end



