 function [tab]=vardeci(A,C,K,V,N,j)
%function[tab]=vardeci(A,C,K,V,N,j)
%  Calculates the variance decomposition of the k-step
%  ahead prediction errors for z(t) in the orthogonalized innovations system
%
%      x(t+1) = A * x(t) + B * v(t)
%
%        z(t) = C * x(t) + D*v(t)
%
%       E v(t)*v(t)' = I  .
%
%   Here r'*r=V (Cholesky decomposition of V)
%   B=K*r';   D=r' where V = E u(t)u(t)', where u(t) are the
%   innovations in z and the v(t) are the orthogonalized innovations.
%   The program prepares a table (tab)
%   of diagonal elements of the covariance matrix of the
%   k-step ahead prediction errors in z, k=1,2,...,N.
%   The output is stored in the matrix tab which has N
%   rows and m=max(size(V)) columns.  The (k,h) element of this matrix
%   is the variance in predicting the hth component of z k-steps ahead
%   that is attributable to the jth orthogonalized innovation.
%
%   SEE ALSO: VARDEC
r=chol(V);
D=r';
ktemp=K*r';
B=ktemp;
m=max(size(V));
										
  m=max(size(V));
tab=zeros(N,m);
ij=zeros(m,m);
ij(j,j)=1;
Sj=ij*ij';
V=Sj;
v1=D*V*D';
tab(1,:)=diag(v1)';
v2=v1+C*B*V*B'*C';
tab(2,:)=diag(v2)';
v0=v2;
for i=3:N
AI=A^(i-1);
vv=v0+C*AI*B*V*B'*AI'*C';
tab(i,:)=diag(vv)';
v0=vv;
end
tab;
