function [var_share] = vardcmp_fc(LL,ss,sxi,D)
global T;global F;
nt=48;qnt=48*4;

QF = zeros(qnt,qnt);QF(1:4,1:4)=LL;
Qs1 = zeros(qnt,qnt);Qs1(1,1)=ss(1,1);
Qs2 = zeros(qnt,qnt);Qs2(2,2)=ss(2,2);
Qs3 = zeros(qnt,qnt);Qs3(3,3)=ss(3,3);
Qs4 = zeros(qnt,qnt);Qs4(4,4)=ss(4,4);

tau = zeros(nt,nt);

 for i=1:nt
 for j=2:nt
     k=i-j+1;
     if k>0
     tau(i,j-1)=k;
     else 
     tau(i,j-1)=0;
     end
 end
 end

for nn=1:nt
    tmp = 4*(nn-1);
    ssxi(tmp+1,nn:nn+T-1) = sxi(1,:);
    ssxi(tmp+2,nn:nn+T-1) = sxi(2,:);
    ssxi(tmp+3,nn:nn+T-1) = sxi(3,:);
    ssxi(tmp+4,nn:nn+T-1) = sxi(4,:);
end

 dssxi = ssxi(:,nt:T);
 dsxi = sxi(:,nt:T);
 TT = T-nt+1;
 nn = 49;
 Xi=dssxi;

vVY_total=zeros(nn,TT);
vVY_1=zeros(nn,TT);
vVY_2=zeros(nn,TT);
vVY_3=zeros(nn,TT);
vVY_2_qf=zeros(nn,TT);
vVY_2_q1=zeros(nn,TT);
vVY_2_q2=zeros(nn,TT);
vVY_2_q3=zeros(nn,TT);
vVY_2_q4=zeros(nn,TT);

vVY_2_l1=zeros(nn,TT);
vVY_2_l2=zeros(nn,TT);
vVY_2_l3=zeros(nn,TT);
vVY_2_l4=zeros(nn,TT);

Xqf=zeros(qnt,qnt,nt);
Xqs1=zeros(qnt,qnt,nt);
Xqs2=zeros(qnt,qnt,nt);
Xqs3=zeros(qnt,qnt,nt);
Xqs4=zeros(qnt,qnt,nt);

SXqf=zeros(qnt,qnt);
SXqs1=zeros(qnt,qnt);
SXqs2=zeros(qnt,qnt);
SXqs3=zeros(qnt,qnt);
SXqs4=zeros(qnt,qnt);

for kk=1:nt
Xqf(:,:,kk)=(F^(kk-1))*QF*(F^(kk-1))';
Xqs1(:,:,kk)=(F^(kk-1))*Qs1*(F^(kk-1))';
Xqs2(:,:,kk)=(F^(kk-1))*Qs2*(F^(kk-1))';
Xqs3(:,:,kk)=(F^(kk-1))*Qs3*(F^(kk-1))';
Xqs4(:,:,kk)=(F^(kk-1))*Qs4*(F^(kk-1))';
end

for n=1:48
W1 = zeros(nt,TT);
W2 = zeros(nt,TT);
X1 = zeros(nt,TT);
X2 = zeros(nt,TT);
MW1 = ones(nt,nt,TT);
MW2 = ones(nt,nt,TT);
D_MW1 = zeros(nt,nt,TT);
D_MW2 = zeros(nt,nt,TT);
MP1 = zeros(nt,nt,TT);
MP2 = zeros(nt,nt,TT);
D_MP1 = zeros(nt,nt,TT);
D_MP2 = zeros(nt,nt,TT);
MC1 = zeros(nt,nt,TT);
MC2 = zeros(nt,nt,TT);
M_JH1 = zeros(nt,qnt,TT);
UL=zeros(nt,TT);
UH=zeros(nt,TT);
TU=zeros(nt,TT);
yhat = zeros(5,TT);
fJH1 = zeros(5,qnt,TT);
fJH = zeros(qnt,5,TT);


fVY_total=zeros(5,5,TT);
fVY_1=zeros(5,5,TT);
fVY_2=zeros(5,5,TT);
fVY_2_q1=zeros(5,5,TT);
fVY_2_q2=zeros(5,5,TT);
fVY_2_q3=zeros(5,5,TT);
fVY_2_q4=zeros(5,5,TT);
fVY_3=zeros(5,5,TT);
fxi0(:,n+1) = (F^n)*Xi(:,1);
for t=n+1:TT

 for i=1:nt
 W1(i,t) = fxi0(1+4*(i-1),t);
 W2(i,t) = fxi0(2+4*(i-1),t);
 X1(i,t) = fxi0(3+4*(i-1),t);
 X2(i,t) = fxi0(4+4*(i-1),t);
 end

 for i=2:nt
 for j=1:nt            
     if tau(i,j)>0
 MW1(i,j,t) = exp(-exp(X1(j,t)+D(tau(i,j),1)));
 MW2(i,j,t) = exp(-exp(X2(j,t)+D(tau(i,j),1)));
     else
 MW1(i,j,t) = 1;
 MW2(i,j,t) = 1;
 end
 end
 end
 
 D_MW1(1,1,t)= 1;
 D_MW2(1,1,t)= 1;
 
for j=1:nt
 D_MW1(j,j,t)= prod(MW1(j,:,t),2);
 D_MW2(j,j,t)= prod(MW2(j,:,t),2);
end
 
 MP1(1,:,t)=zeros(1,nt);
 MP2(1,:,t)=zeros(1,nt);

 for i=2:nt
 MP1(i,1:i-1,t)=W1(i,t)*D_MW1(i,i,t);
 MP2(i,1:i-1,t)=W2(i,t)*D_MW2(i,i,t);
 end

 MC1(1,:,t)=zeros(1,nt);
 MC2(1,:,t)=zeros(1,nt);

 for i=2:nt
 for j=1:nt 
     if tau(i,j)>0
 MC1(i,j,t) = -exp(X1(j,t)+D(tau(i,j),1));
 MC2(i,j,t) = -exp(X2(j,t)+D(tau(i,j),1));
     else
 MC1(i,j,t) = 0;
 MC2(i,j,t) = 0;
 end
 end
 end
 
for i=1:nt
 for j=1:nt 
 D_MP1(i,j,t)= MP1(i,j,t).*MC1(i,j,t);
 D_MP2(i,j,t)= MP2(i,j,t).*MC2(i,j,t);
 end
end

for i=1:nt
M_JH1(:,1+4*(i-1),t)=D_MW1(:,i,t);
M_JH1(:,2+4*(i-1),t)=D_MW2(:,i,t);
M_JH1(:,3+4*(i-1),t)=D_MP1(:,i,t);
M_JH1(:,4+4*(i-1),t)=D_MP2(:,i,t);
end

fJH1(1,:,t)= M_JH1(1,:,t);
fJH1(2,:,t)= M_JH1(2,:,t)+M_JH1(3,:,t);
fJH1(3,:,t)= M_JH1(4,:,t)+M_JH1(5,:,t)+M_JH1(6,:,t);
fJH1(4,:,t)= sum(M_JH1(7:12,:,t),1);
fJH1(5,:,t)= sum(M_JH1(13:nt,:,t),1);
fJH(:,:,t)=fJH1(:,:,t)';

%(3)Variance of Y: E[(Y(t+m)-Y(t+m|t))(Y(t+m)-Y(t+m|t))'] 

SXqf = sum(Xqf(:,:,1:n),3);
SXqs1 = sum(Xqs1(:,:,1:n),3);
SXqs2 = sum(Xqs2(:,:,1:n),3);
SXqs3 = sum(Xqs3(:,:,1:n),3);
SXqs4 = sum(Xqs4(:,:,1:n),3);
SXq = SXqf + SXqs1 + SXqs2 + SXqs3 + SXqs4;

vVY_2(n,t)=ones(1,5)*[(fJH(:,:,t)')*(SXq)*(fJH(:,:,t))]*ones(5,1);

vVY_2_qf(n,t)=ones(1,5)*[(fJH(:,:,t)')*(SXqf)*(fJH(:,:,t))]*ones(5,1);
vVY_2_q1(n,t)=ones(1,5)*[(fJH(:,:,t)')*(SXqs1)*(fJH(:,:,t))]*ones(5,1);
vVY_2_q2(n,t)=ones(1,5)*[(fJH(:,:,t)')*(SXqs2)*(fJH(:,:,t))]*ones(5,1);
vVY_2_q3(n,t)=ones(1,5)*[(fJH(:,:,t)')*(SXqs3)*(fJH(:,:,t))]*ones(5,1);
vVY_2_q4(n,t)=ones(1,5)*[(fJH(:,:,t)')*(SXqs4)*(fJH(:,:,t))]*ones(5,1);
fxi0(:,t+1) = (F^n)*Xi(:,t+1-n);
end
sum_vVY_q(n,1) = sum(vVY_2(n,n+1:TT),2)/(TT-n);
sum_vVY_q(n,2) = sum(vVY_2_qf(n,n+1:TT),2)/(TT-n);
sum_vVY_q(n,3) = sum(vVY_2_q1(n,n+1:TT),2)/(TT-n);
sum_vVY_q(n,4) = sum(vVY_2_q2(n,n+1:TT),2)/(TT-n);
sum_vVY_q(n,5) = sum(vVY_2_q3(n,n+1:TT),2)/(TT-n);
sum_vVY_q(n,6) = sum(vVY_2_q4(n,n+1:TT),2)/(TT-n);
end
for s=1:5
var_share(:,s) = sum_vVY_q(:,s+1)./sum_vVY_q(:,1);
end
end

