function logl = likel(param,y,ns);
%KF log likelihood for CDS model
[p,bigt]=size(y);
mu=param(1:3); phi=reshape(param(4:12),3,3);
Q=diag(param(13:15).^2); H=eye(p)*(param(16)^2); lambda=param(17);
p0g0=reshape(inv(eye(9)-kron(phi,phi))*reshape(Q,9,1),3,3);
a0g0=inv(eye(3)-phi)*mu;
for i=1:p;
    Zmat(i,1)=1;
    Zmat(i,2)=(1-exp(-lambda*ns(i)/4))/(lambda*ns(i)/4);
    Zmat(i,3)=Zmat(i,2)-exp(-lambda*ns(i)/4);
end;

atgt=a0g0; ptgt=p0g0;
for t=1:bigt;
    atgtm1=mu+(phi*atgt); ptgtm1=(phi*ptgt*phi')+Q; 
    v=y(:,t)-(Zmat*atgtm1);
    F=(Zmat*ptgtm1*Zmat')+H;
    if max(max(isinf(F)))+max(max(isnan(F)))>0; l(t)=nan; else;
        atgt=atgtm1+(ptgtm1*Zmat'*pinv(F)*v);   
        ptgt=ptgtm1-(ptgtm1*Zmat'*pinv(F)*Zmat*ptgtm1);
        l(t)=-(0.5*log(2*pi))-(0.5*log(det(F)))-(0.5*v'*pinv(F)*v);
    end;
end;
logl=-sum(l);
if isnan(logl)==1 | isreal(logl)==0; logl=inf; end;
