function [aa,bb,cc,dd,V1]=innov(ao,c,sy,D,R)
%function [aa,bb,cc,dd,V1]=innov(ao,c,sy,D,R)
%   INNOV computes an innovations representation for a recursive linear
%   model whose observations are currupted by first-order serially
%   correlated measurement errors.
%   The model occurs in the state space form
%             x(t+1) = ao*x(t) + c*w(t+1)
%               y(t) = sy*x(t) + e(t+1)
%   where e(t) is a measurement error process
%              e(t+1) = D*e(t) + ee(t+1)
%   and where ee(t+1) is a vector white noise with covariance matrix
%   R.  We assume that ee(t+1) and w(t+1) are orthogonal at all
%   leads and lags.  This specification permits the outputs of
%   our model to be measured with error.
%   The matrices ao,c,sy,D,and R must be in memory.
%   The matrices D and R must each be m x m, where [m,n]=size(sy).
%   The program uses the Kalman filter to form the innovations representation
%              zh(t+1) = aa*zh(t) + bb*u(t)
%               y(t+1) = cc*xh(t) + dd*u(t)
%   where u(t)=y(t+1) - E[y(t+1)| y(t),y(t-1),...].  The program creates
%   the covariance matrix of u, which it stores in V1.
%   The program uses evardec.m to compute a decomposition of variance for the
%   innovations system (which is equivalent with the vector autoregression).
   Q=c*c';
   G=sy;
   [m,n]=size(sy);
   Vm=eye(m);
   Vmm=zeros(m);
   GG=G*ao-D*G;
   Q1=Q;
   W1=Q*G';
   R1=R+G*Q*G';
   [k1,s1]=kfilter(ao,GG,Q1,R1,W1);
   V1=R1+GG*s1*GG';
   [aa,bb,cc,dd]=series(ao,k1,GG,Vm,D,Vm,Vm,Vmm);
   [t1,t2]=evardec(aa,bb,cc,-20,-1,V1)
disp('Variance attributable to the ith innovation')
   for i=1:m
   'i =    ',i
   mm=m+1;
   tt=t2(:,(i-1)*mm+1:i*mm)
   end
