# Bug in kalman\_filter.m

**URL:** <https://forum.dynare.org/t/bug-in-kalman-filter-m/1486>\
**Category:** ML/Bayesian estimation\
**Created:** [3 June 2010 07:40 UTC](https://forum.dynare.org/t/bug-in-kalman-filter-m/1486 "2010-06-03T07:40:14Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [3 June 2010 07:40 UTC](https://forum.dynare.org/t/bug-in-kalman-filter-m/1486/1 "2010-06-03T07:40:14Z")

</div>

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

```auto

```

and

```auto

```

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.

---

_[View the full topic](https://forum.dynare.org/t/bug-in-kalman-filter-m/1486)._
