Bug in kalman_filter.m

Hi,

there is obviously a bug in the file kalman_filter.m used in Dynare. The respective part is

while notsteady & t<smpl t = t+1; v = Y(:,t)-a(mf); F = P(mf,mf) + H; if rcond(F) < kalman_tol if ~all(abs(F(:))<kalman_tol) return else a = T*a; P = T*P*transpose(T)+QQ; end else F_singular = 0; dF = det(F); iF = inv(F); lik(t) = log(dF)+transpose(v)*iF*v; K = P(:,mf)*iF; a = T*(a+K*v); P = T*(P-K*P(mf,:))*transpose(T)+QQ; end notsteady = max(max(abs(K-oldK))) > riccati_tol; oldK = K; end

If

and

Dynare only sets the variables a and P. However, after the if-clause, K is used in

max(max(abs(K-oldK))) But K was never defined, hence Dynare stops with an error.

Dear Johannes, Thanks for reporting this bug. I have never met this problem. When the condition rcond(F)<kalman_tol is true we cannot update the Kalman gain because the covariance matrix F is not invertible. In this case don’t need to check for the steady state of the Kalman filter. We just have to move :

    notsteady = max(max(abs(K-oldK))) > riccati_tol;
    oldK = K;

before the end keyword. I will commit this correction in couple of minutes. The next release 4.1.2 will contain the fix.

Best, Stéphane.