Unknownpgr

Bayesian Filters and Kalman Filters

2024-04-27 14:41:59 | English, Korean

This post was translated from Korean into English by AI.

A Kalman filter is a probabilistic method for estimating the state of a system from measurements. There are several ways to explain the Kalman filter. For example, it can be interpreted as a state observer or viewed as a combination of a low-pass filter and a high-pass filter. In this post, however, I will explain the Kalman filter as an approximation of the Bayesian filter, which I consider the most fundamental perspective.

Multivariate Normal Distribution

The Kalman filter assumes that both the vector-valued state and its observations follow multivariate normal distributions. Therefore, understanding the Kalman filter first requires an understanding of these distributions.

Normal Distribution

First, the probability density function of a one-dimensional normal distribution is defined as follows.

xN(μ,σ2)p(x)=12πσexp(12(xμσ)2)x \sim N(\mu, \sigma^2) \Longleftrightarrow p(x) = \frac{1}{\sqrt{2\pi}\sigma} \exp\left(-\frac{1}{2}\left(\frac{x - \mu}{\sigma}\right)^2\right)

Here, μ\mu is the mean and σ2\sigma^2 is the variance.

Properties of Normal Distribution

The normal distribution has many useful properties. One of the most important is that normal distributions are closed under linear combinations. In other words, if x1N(μ1,σ12)x_1 \sim N(\mu_1, \sigma_1^2) and x2N(μ2,σ22)x_2 \sim N(\mu_2, \sigma_2^2), then ax1+bx2a x_1 + b x_2 also follows a normal distribution for any constants aa and bb.

This can be proved easily by first showing that normal distributions are closed under scalar multiplication and then showing that they are closed under addition. Closure under scalar multiplication is straightforward, so I will omit it and prove closure under addition. There are several ways to do this, but the most intuitive is to calculate it directly as follows.

First, the probability density function of the sum x=x1+x2x = x_1 + x_2 of two independent random variables x1p1(x)x_1 \sim p_1(x) and x2p2(x)x_2 \sim p_2(x) is given by the convolution of their probability density functions:

p(x)=p1(t)p2(xt)dtp(x) = \int p_1(t) p_2(x - t) dt

Substituting the probability density functions of the normal distributions gives the following.

p(x)=12πσ1exp(12(tμ1σ1)2)12πσ2exp(12(xtμ2σ2)2)dtp(x) = \int \frac{1}{\sqrt{2\pi}\sigma_1} \exp\left(-\frac{1}{2}\left(\frac{t - \mu_1}{\sigma_1}\right)^2\right) \frac{1}{\sqrt{2\pi}\sigma_2} \exp\left(-\frac{1}{2}\left(\frac{x - t - \mu_2}{\sigma_2}\right)^2\right) dt

To evaluate this integral, first combine the exp\exp functions, expand the exponent, complete the square in tt, and then use the following formula.

exp(ax2)dx=πa\int \exp(-ax^2) dx = \sqrt{\frac{\pi}{a}}

Evaluating the integral in this way gives the following.

12πσ12+σ222exp(μ12σ22+σ12(μ22+2μ2xx2)+(μ1σ22μ2σ12+σ12x)2σ12+σ222σ12σ22)\frac{1}{2 \sqrt{\pi} \sqrt{\sigma_1^2 + \sigma_2^2}}\sqrt{2} \exp\left(\frac{- \mu_1^2 \sigma_2^2 + \sigma_1^2 \left(- \mu_2^2 + 2 \mu_2 x - x^2\right) + \frac{\left(\mu_1 \sigma_2^2 - \mu_2 \sigma_1^2 + \sigma_1^2 x\right)^2}{\sigma_1^2 + \sigma_2^2}}{2 \sigma_1^2 \sigma_2^2}\right)

Factoring the exponent carefully with respect to xx simplifies it as follows.

μ12σ22+σ12(μ22+2μ2xx2)+(μ1σ22μ2σ12+σ12x)2σ12+σ222σ12σ22=1σ12+σ22(μ12(σ12+σ22)σ12+(μ22+2μ2xx2)(σ12+σ22)σ22+(μ1σ22μ2σ12+σ12x)2σ12σ22)=1σ12+σ22(μ12μ12σ22σ12μ22σ12σ22μ22+2μ2xσ12σ22+2μ2xx2σ12σ22x2+μ12σ22σ12+μ22σ12σ22+x2σ12σ222μ1μ2+2μ1x2μ2xσ12σ22)=1σ12+σ22(μ12μ22+2μ2xx22μ1μ2+2μ1x)=1σ12+σ22((μ1+μ2)22(μ1+μ2)x+x2)=(x(μ1+μ2))2σ12+σ22\begin{align*} & \frac{- \mu_1^2 \sigma_2^2 + \sigma_1^2 \left(- \mu_2^2 + 2 \mu_2 x - x^2\right) + \frac{\left(\mu_1 \sigma_2^2 - \mu_2 \sigma_1^2 + \sigma_1^2 x\right)^2}{\sigma_1^2 + \sigma_2^2}}{2 \sigma_1^2 \sigma_2^2}\\ &=\frac{1}{\sigma_1^2 + \sigma_2^2}\left( - \frac{\mu_1^2(\sigma_1^2 + \sigma_2^2)}{\sigma_1^2} + \frac{\left(- \mu_2^2 + 2 \mu_2 x - x^2\right)(\sigma_1^2 + \sigma_2^2)}{\sigma_2^2} + \frac{\left(\mu_1 \sigma_2^2 - \mu_2 \sigma_1^2 + \sigma_1^2 x\right)^2}{\sigma_1^2\sigma_2^2} \right)\\ &=\frac{1}{\sigma_1^2 + \sigma_2^2}\left( - \mu_1^2 - \frac{\mu_1^2\sigma_2^2}{\sigma_1^2} - \frac{\mu_2^2\sigma_1^2}{\sigma_2^2} - \mu_2^2 + \frac{2 \mu_2 x \sigma_1^2}{\sigma_2^2} + 2 \mu_2 x - \frac{x^2\sigma_1^2}{\sigma_2^2} - x^2 + \frac{\mu_1^2\sigma_2^2}{\sigma_1^2} + \frac{\mu_2^2\sigma_1^2}{\sigma_2^2} + \frac{x^2\sigma_1^2}{\sigma_2^2} - 2 \mu_1 \mu_2 + 2 \mu_1 x - \frac{2 \mu_2 x \sigma_1^2}{\sigma_2^2} \right)\\ &=\frac{1}{\sigma_1^2 + \sigma_2^2}\left( - \mu_1^2 - \mu_2^2 + 2 \mu_2 x - x^2 - 2 \mu_1 \mu_2 + 2 \mu_1 x \right)\\ &=-\frac{1}{\sigma_1^2 + \sigma_2^2}\left( (\mu_1 + \mu_2)^2 - 2 (\mu_1+\mu_2) x + x^2 \right)\\ &=-\frac{(x- (\mu_1 + \mu_2))^2}{\sigma_1^2 + \sigma_2^2}\\ \end{align*}

Substituting this back into the original expression gives the following.

12πσ12+σ22exp((x(μ1+μ2))22(σ12+σ22))\frac{1}{\sqrt{2\pi} \sqrt{\sigma_1^2 + \sigma_2^2}} \exp\left(- \frac{\left(x-(\mu_1 + \mu_2) \right)^2}{2 (\sigma_1^2 + \sigma_2^2)}\right)

By the definition of the normal distribution, this is a normal distribution with mean μ1+μ2\mu_1 + \mu_2 and variance σ12+σ22\sigma_1^2 + \sigma_2^2.

Definition of Multivariate Normal Distribution

There are several definitions of the multivariate normal distribution. The most common defines it as a probability distribution in which every linear combination of the components is normally distributed. In other words, a random variable xRnx \in \mathbb{R}^n follows a multivariate normal distribution if aTxa^T x follows a normal distribution for every vector aRna \in \mathbb{R}^n.

An equivalent definition regards a multivariate normal distribution as a linear combination of nn mutually independent one-dimensional standard normal distributions. That is, for a vector zRkz \in \mathbb{R}^k consisting of kk independent one-dimensional standard normal random variables x1,x2,,xkx_1, x_2, \ldots, x_k, there exist a matrix AA and a vector μ\mu such that x=Az+μx = A z + \mu. Here, AA is an n×kn \times k matrix, and μ\mu is an nn-dimensional vector.

This can be expressed mathematically as follows.

xN(μ,Σ)ARn×k,μRn s.t. x=Az+μ,zN(0,I)\begin{align*} x &\sim N(\mu, \Sigma) \Longleftrightarrow \exists A \in \mathbb{R}^{n \times k}, \mu \in \mathbb{R}^n \text{ s.t. } x = A z + \mu, z \sim N(0, I) \\ \end{align*}

Properties of Multivariate Normal Distribution

The second definition makes several proofs more convenient, so I will use it here. When using the second definition, the relationship between the covariance matrix Σ\Sigma and AA is important. First, if the mean of the random variable xx is μ\mu, the covariance is defined as follows.

Σ=E[(xμ)(xμ)T]\Sigma = E[(x - \mu)(x - \mu)^T]

This means that the entry in row ii, column jj of the covariance matrix is the covariance between the ii-th and jj-th components.

Now, substituting x=Az+μx = A z + \mu into this expression gives the following covariance matrix.

Σ=E[(Az+μμ)(Az+μμ)T]=E[AzzTAT]=AE[zzT]AT=AIATzN(0,I)=AAT\begin{align*} \Sigma &= E[(A z + \mu - \mu)(A z + \mu - \mu)^T] \\ &= E[A z z^T A^T] \\ &= A E[z z^T] A^T \\ &= A I A^T \because z \sim N(0, I) \\ &= A A^T \end{align*}

This covariance matrix is positive definite. Positive definiteness means that xTΣx>0x^T \Sigma x \gt 0 holds for every nonzero vector xRnx \in \mathbb{R}^n. This is easy to see as follows.

xTΣx=xTAATx=(ATx)T(ATx)=ATx2>0x^T \Sigma x = x^T A A^T x = (A^T x)^T (A^T x) = \|A^T x\|^2 \gt 0

One exception is when the variance of some component is zero. This case is called degenerate, and the covariance matrix then has no inverse. In such cases, that component must be removed through marginalization or a similar technique before performing the calculation. This is a special case, however, and will not be covered here.

From this, we can show that the covariance matrix must be invertible.

Probability Density Function of Multivariate Normal Distribution

Next, we can derive the probability density function of the multivariate normal distribution. Consider an nn-dimensional random variable zz whose components are mutually independent and each have the standard normal probability density function. Since the probability density function of zz is simply the product of the probability density functions of its components, it is given by the following.

12πexp(12z12)12πexp(12z22)12πexp(12zn2)\frac{1}{\sqrt{2\pi}} \exp\left(-\frac{1}{2} z_1^2\right) \frac{1}{\sqrt{2\pi}} \exp\left(-\frac{1}{2} z_2^2\right) \ldots \frac{1}{\sqrt{2\pi}} \exp\left(-\frac{1}{2} z_n^2\right)

Simplifying this and writing it in vector form gives the following.

=1(2π)n/2exp(12(z12+z22++zn2))p(z)=1(2π)n/2exp(12zTz)\begin{align*} &= \frac{1}{(2\pi)^{n/2}} \exp\left(-\frac{1}{2} (z_1^2 + z_2^2 + \ldots + z_n^2)\right)\\ p(z) &= \frac{1}{(2\pi)^{n/2}} \exp\left(-\frac{1}{2} z^T z\right) \\ \end{align*}

Next, for any multivariate probability distribution xx and any invertible transformation ff, the following holds.

y=f(x)p(y)=p(x)xy=p(f1(y))1det(J)\begin{align*} y = f(x) \Rightarrow p(y) &= p(x) \left|\frac{\partial x}{\partial y}\right|\\ &= p(f^{-1}(y)) \frac{1}{|\det(J)|} \end{align*}

Here, JJ is the Jacobian of ff. If ff is a linear transformation, the Jacobian is the transformation matrix AA, in which case the expression is given as follows.

xp(x)Ax1det(A)p(x)y=Axp(y)=1det(A)p(A1y)\begin{align*} x\sim p(x) &\Rightarrow A x \sim \frac{1}{|\det(A)|} p(x)\\ \therefore y = A x &\Rightarrow p(y) = \frac{1}{|\det(A)|} p(A^{-1} y) \end{align*}

Now let us apply this to the multivariate normal distribution derived above.

z=Ax+μp(z)=1det(A)p(A1(zμ))=1det(A)1(2π)n/2exp(12(A1(zμ))TA1(zμ))=1det(A)1(2π)n/2exp(12(zμ)T(AAT)1(zμ))\begin{align*} z = A x + \mu \Rightarrow p(z) &= \frac{1}{|\det(A)|} p(A^{-1} (z - \mu))\\ &= \frac{1}{|\det(A)|} \frac{1}{(2\pi)^{n/2}} \exp\left(-\frac{1}{2} (A^{-1} (z - \mu))^T A^{-1} (z - \mu)\right)\\ &= \frac{1}{|\det(A)|} \frac{1}{(2\pi)^{n/2}} \exp\left(-\frac{1}{2} (z - \mu)^T (A A^T)^{-1} (z - \mu)\right)\\ \end{align*}

As shown above, AAT=ΣA A^T = \Sigma. Substituting this gives the following probability density function of the multivariate normal distribution.

p(z)=1(2π)n/2Σ1/2exp(12(zμ)TΣ1(zμ))p(z) = \frac{1}{(2\pi)^{n/2}|\Sigma|^{1/2}} \exp\left(-\frac{1}{2} (z - \mu)^T \Sigma^{-1} (z - \mu)\right)

The identity det(A)=det(Σ)1/2|\det(A)|=|\det(\Sigma)|^{1/2} can be shown as follows.

det(Σ)=det(AAT)=det(A)det(AT)det(AB)=det(A)det(B)=det(A)det(A)=det(A)2det(A)=det(AT)det(A)2=det(Σ)det(A)=det(Σ)1/2\begin{align*} |\det(\Sigma)| &= |\det(A A^T)| = |\det(A)\det(A^T)| \\ &\because \det(AB) = \det(A)\det(B)\\ &=|\det(A)\det(A)| = |\det(A)|^2\\ &\because \det(A)=\det(A^T)\\ \therefore |\det(A)|^2&=|\det(\Sigma)| \\ \therefore |\det(A)| &= |\det(\Sigma)|^{1/2} \end{align*}

Kalman Filter

First, the Bayesian filter equations are given as follows.

p(xtz1:t)=p(ztxt)p(xtz1:t1)p(ztxt)p(xtz1:t1)dxtp(xtz1:t1)=p(xtxt1)p(xt1z1:t1)dxt1\begin{align*} p(x_t | z_{1:t})&= \frac{p(z_t | x_t) p(x_t | z_{1:t-1})}{\int p(z_t | x_t) p(x_t | z_{1:t-1}) dx_t} \\ p(x_t | z_{1:t-1}) &= \int p(x_t | x_{t-1}) p(x_{t-1} | z_{1:t-1}) dx_{t-1} \end{align*}

Given a system model and an observation model, a Bayesian filter infers the state from observations and provides mathematically optimal inference for a system represented as a hidden Markov chain. However, a Bayesian filter contains two integrals, and there is no easy way to evaluate them for arbitrary probability distributions. Consequently, these equations cannot be solved and used directly; they must be approximated in another form. As discussed in the previous post, the particle filter is a nonparametric approximation of the Bayesian filter and therefore makes no assumptions about the probability distributions or models. The Kalman filter discussed in this post is a parametric approximation of the Bayesian filter that assumes the system model and measurement model are linear Gaussian models of the following form.

xt=Ftxt1+Btut+ϵtzt=Htxt+δt\begin{align*} x_t &= F_t x_{t-1} + B_t u_t + \epsilon_t \\ z_t &= H_t x_t + \delta_t \end{align*}

The variables have the following meanings.

The state noise and measurement noise also follow Gaussian distributions as follows.

ϵtN(0,Qt)δtN(0,Rt)\begin{align*} \epsilon_t &\sim N(0, Q_t) \\ \delta_t &\sim N(0, R_t) \end{align*}

We can now use these assumptions to derive the Kalman filter from the Bayesian filter. Because the Kalman filter assumes that the probability distributions are multivariate normal, each distribution is completely determined by its mean and covariance matrix. The Kalman filter therefore estimates the state mean μ\mu and covariance Σ\Sigma as follows.

Prediction Step

First, the state prediction step of the Bayesian filter is given as follows.

p(xtz1:t1)=p(xtxt1)p(xt1z1:t1)dxt1p(x_t | z_{1:t-1}) = \int p(x_t | x_{t-1}) p(x_{t-1} | z_{1:t-1}) dx_{t-1}

Because p(xtxt1)p(x_t | x_{t-1}) and p(xt1z1:t1)p(x_{t-1} | z_{1:t-1}) follow Gaussian distributions, however, this integral can be calculated simply as follows.

xtt1N(μtt1,Σtt1)μtt1=Fμt1t1+ButΣtt1=FΣt1t1FT+Q\begin{align*} x_{t|t-1} &\sim N(\mu_{t|t-1}, \Sigma_{t|t-1}) \\ \mu_{t|t-1} &= F \mu_{t-1|t-1} + B u_{t} \\ \Sigma_{t|t-1} &= F \Sigma_{t-1|t-1} F^T + Q \end{align*}

The proof is as follows. The mean can be obtained directly from the linearity of the expectation operator E[x]E[x].

μtt1=E[xtt1]=E[Ftxt1t1+Btut+ϵt]=FtE[xt1t1]+Btut+E[ϵt]=Ftμt1t1+Btut\begin{align*} \mu_{t|t-1} = E[x_{t|t-1}] &= E[F_t x_{t-1|t-1} + B_t u_t + \epsilon_t] \\ &= F_t E[x_{t-1|t-1}] + B_t u_t + E[\epsilon_t] \\ &= F_t \mu_{t-1|t-1} + B_t u_t \end{align*}

The covariance is also readily derived from its definition. Because the calculation is complicated, I will temporarily omit the time subscripts in the proof and use the following notation.

Σtt1=Σ^Σt1t1=Σμtt1=μ^μt1t1=μxtt1=x^xt1t1=xϵt=ϵ\begin{align*} \Sigma_{t|t-1} &= \hat\Sigma & \Sigma_{t-1|t-1} &= \Sigma\\ \mu_{t|t-1} &= \hat\mu & \mu_{t-1|t-1} &= \mu\\ x_{t|t-1} &= \hat x & x_{t-1|t-1} &= x\\ \epsilon_t &= \epsilon \end{align*}

The covariance can then be calculated as follows.

Σ^=E[(x^μ^)(x^μ^)T]=E[(Fx+Bu+ϵFμBu)T(Fx+Bu+ϵFμBu)]=E[(F(xμ)+ϵ)(F(xμ)+ϵ)T]=E[F(xμ)(xμ)TFT+F(xμ)ϵT+ϵ(xμ)TFT+ϵϵT]=FE[(xμ)(xμ)T]FT+E[ϵϵT]=FΣFT+Q\begin{align*} \hat\Sigma &= E[(\hat x - \hat\mu)(\hat x - \hat\mu)^T] \\ &= E[(F x + B u + \epsilon - F \mu - B u)^T (F x + B u + \epsilon - F \mu - B u)] \\ &= E[(F (x - \mu) + \epsilon)(F (x - \mu) + \epsilon)^T] \\ &= E[F (x - \mu)(x - \mu)^T F^T + F (x - \mu) \epsilon^T + \epsilon (x - \mu)^T F^T + \epsilon \epsilon^T] \\ &= F E[(x - \mu)(x - \mu)^T] F^T + E[\epsilon \epsilon^T] \\ &= F \Sigma F^T + Q \end{align*}

Update Step

Now let us examine the measurement update step. The measurement update step is given as follows.

p(xtz1:t)=p(ztxt)p(xtz1:t1)p(ztxt)p(xtz1:t1)dxtp(x_t | z_{1:t}) = \frac{p(z_t | x_t) p(x_t | z_{1:t-1})}{\int p(z_t | x_t) p(x_t | z_{1:t-1}) dx_t}

This expression is very difficult to evaluate. In fact, however, we can show that it is a Gaussian distribution as follows.

  1. The denominator of this expression is constant with respect to xtx_t.
  2. When expanded, the numerator takes the form exp(xtTAxt)\exp(-x_tTAx_t) with respect to xtx_t. Therefore, as long as its integral is 1, this distribution is Gaussian.
  3. The denominator of this expression is the numerator integrated with respect to xtx_t. Therefore, integrating the entire expression with respect to xtx_t must give 1.

Accordingly, there is no need to calculate the entire expression; we only need to examine its mean and variance.

First, the probability density functions of the prior distribution and the measurement model are as follows.

p(xtz1:t1)=1(2π)n/2Σtt11/2exp(12(xtμtt1)TΣtt11(xtμtt1))p(ztxt)=1(2π)m/2Rt1/2exp(12(ztHtxt)TRt1(ztHtxt))\begin{align*} p(x_t | z_{1:t-1}) &= \frac{1}{(2\pi)^{n/2}|\Sigma_{t|t-1}|^{1/2}} \exp\left(-\frac{1}{2} (x_t - \mu_{t|t-1})^T \Sigma_{t|t-1}^{-1} (x_t - \mu_{t|t-1})\right) \\ p(z_t | x_t) &= \frac{1}{(2\pi)^{m/2}|R_t|^{1/2}} \exp\left(-\frac{1}{2} (z_t - H_t x_t)^T R_t^{-1} (z_t - H_t x_t)\right) \end{align*}

Their product is therefore as follows.

p(ztxt)p(xtz1:t1)=1(2π)n/2Σtt11/2(2π)m/2Rt1/2exp(12((xtμtt1)TΣtt11(xtμtt1)+(ztHtxt)TRt1(ztHtxt)))=1(2π)n/2Σtt11/2(2π)m/2Rt1/2exp(12(xtTΣtt11xt2xtTΣtt11μtt1+μtt1TΣtt11μtt1+ztTRt1zt2ztTRt1Htxt+xtTHtTRt1Htxt))=1(2π)n/2Σtt11/2(2π)m/2Rt1/2exp(12(xtT(Σtt11+HtTRt1Ht)xt2xtT(Σtt11μtt1+HtTRt1zt)+ztTRt1zt+μtt1TΣtt11μtt1))\begin{align*} p(z_t | x_t) p(x_t | z_{1:t-1}) &= \frac{1}{(2\pi)^{n/2}|\Sigma_{t|t-1}|^{1/2} (2\pi)^{m/2}|R_t|^{1/2}} \exp\left(-\frac{1}{2} \left((x_t - \mu_{t|t-1})^T \Sigma_{t|t-1}^{-1} (x_t - \mu_{t|t-1}) + (z_t - H_t x_t)^T R_t^{-1} (z_t - H_t x_t)\right)\right) \\ % Expansion &= \frac{1}{(2\pi)^{n/2}|\Sigma_{t|t-1}|^{1/2} (2\pi)^{m/2}|R_t|^{1/2}} \exp\left(-\frac{1}{2} \left(x_t^T \Sigma_{t|t-1}^{-1} x_t - 2 x_t^T \Sigma_{t|t-1}^{-1} \mu_{t|t-1} + \mu_{t|t-1}^T \Sigma_{t|t-1}^{-1} \mu_{t|t-1} + z_t^T R_t^{-1} z_t - 2 z_t^T R_t^{-1} H_t x_t + x_t^T H_t^T R_t^{-1} H_t x_t\right)\right)\\ % Collect and simplify the terms in x &= \frac{1}{(2\pi)^{n/2}|\Sigma_{t|t-1}|^{1/2} (2\pi)^{m/2}|R_t|^{1/2}} \exp\left(-\frac{1}{2} \left(x_t^T (\Sigma_{t|t-1}^{-1} + H_t^T R_t^{-1} H_t) x_t - 2 x_t^T (\Sigma_{t|t-1}^{-1} \mu_{t|t-1} + H_t^T R_t^{-1} z_t) + z_t^T R_t^{-1} z_t + \mu_{t|t-1}^T \Sigma_{t|t-1}^{-1} \mu_{t|t-1}\right)\right)\\ \end{align*}

Here, some terms involving xtx_t were rewritten as terms involving xtTx_t^T. This is because those terms are scalars and therefore remain unchanged when transposed.

We can now obtain the mean and standard deviation from the probability density above by comparing it with the expanded probability density function of a Gaussian distribution. Expanding the probability density function of a Gaussian distribution with mean mm and covariance matrix PP gives the following.

p(x)=1(2π)n/2P1/2exp(12(xm)TP1(xm))=1(2π)n/2P1/2exp(12(xTP1x2xTP1m+mTP1m))\begin{align*} p(x) &= \frac{1}{(2\pi)^{n/2}|P|^{1/2}} \exp\left(-\frac{1}{2} (x - m)^T P^{-1} (x - m)\right) \\ &= \frac{1}{(2\pi)^{n/2}|P|^{1/2}} \exp\left(-\frac{1}{2} \left(x^T P^{-1} x - 2 x^T P^{-1} m + m^T P^{-1} m\right)\right) \end{align*}

From this, we can see that the inverse of the coefficient in xT()xx^T(\bullet)x is the covariance, and that multiplying the coefficient in 2xT()-2x^T(\bullet) by the covariance from the left gives the mean. Thus, the covariance of the original expression is

Σtt=(Σtt11+HtTRt1Ht)1\Sigma_{t|t} = (\Sigma_{t|t-1}^{-1} + H_t^T R_t^{-1} H_t)^{-1}

and its mean is

μtt=Σtt(Σtt11μtt1+HtTRt1zt)\mu_{t|t} = \Sigma_{t|t} (\Sigma_{t|t-1}^{-1} \mu_{t|t-1} + H_t^T R_t^{-1} z_t)

In other words, the posterior distribution is given as follows.

xttN(μtt,Σtt)μtt=Σtt(Σtt11μtt1+HtTRt1zt)Σtt=(Σtt11+HtTRt1Ht)1\begin{align*} x_{t|t} &\sim N(\mu_{t|t}, \Sigma_{t|t}) \\ \mu_{t|t} &= \Sigma_{t|t} (\Sigma_{t|t-1}^{-1} \mu_{t|t-1} + H_t^T R_t^{-1} z_t) \\ \Sigma_{t|t} &= (\Sigma_{t|t-1}^{-1} + H_t^T R_t^{-1} H_t)^{-1}\\ \end{align*}

Summary

Combining the update and prediction steps, the Kalman filter can now be summarized as follows.

Prediction Stepμtt1=Ftμt1t1+BtutΣtt1=FtΣt1t1FtT+QtUpdate Stepμtt=Σtt(Σtt11μtt1+HtTRt1zt)Σtt=(Σtt11+HtTRt1Ht)1\begin{align*} \text{Prediction Step} \\ \mu_{t|t-1} &= F_t \mu_{t-1|t-1} + B_t u_t \\ \Sigma_{t|t-1} &= F_t \Sigma_{t-1|t-1} F_t^T + Q_t \\ \text{Update Step} \\ \mu_{t|t} &= \Sigma_{t|t} (\Sigma_{t|t-1}^{-1} \mu_{t|t-1} + H_t^T R_t^{-1} z_t) \\ \Sigma_{t|t} &= (\Sigma_{t|t-1}^{-1} + H_t^T R_t^{-1} H_t)^{-1}\\ \end{align*}

Comparison with General Form

However, the equations derived in this post differ from the form in which the Kalman filter is commonly presented. The Kalman Gain does not even appear in these equations. In fact, the Wikipedia article on the Kalman filter presents the prediction and update steps as follows. Although the notation differs slightly, the prediction step is identical to the one derived in this post, while the update step not only contains more stages but also has a completely different form.

Prediction StepX^kk1=FkX^k1k1+Bkuk1Pkk1=FkPk1k1Fk+Qk1Update Stepy~k=zkHkx^kk1Sk=HkPkk1Hk+RkKk=Pkk1HkSk1x^kk=x^kk1+Kky~kPkk=(IKkHk)Pkk1\begin{align*} \text{Prediction Step} \\ \hat{\mathbf{X}}_{k|k-1} &= \mathbf{F}_k \hat{\mathbf{X}}_{k-1|k-1} + \mathbf{B}_k \mathbf{u}_{k-1}\\ \mathbf{P}_{k|k-1} &= \mathbf{F}_k \mathbf{P}_{k-1|k-1} \mathbf{F}_k^\top + \mathbf{Q}_{k-1}\\ \text{Update Step} \\ \tilde{\mathbf{y}}_k &= \mathbf{z}_k - \mathbf{H}_k\hat{\mathbf{x}}_{k|k-1} \\ \mathbf{S}_k &= \mathbf{H}_k \mathbf{P}_{k|k-1} \mathbf{H}_k^\top + \mathbf{R}_k \\ \mathbf{K}_k &= \mathbf{P}_{k|k-1} \mathbf{H}_k^\top \mathbf{S}_k^{-1} \\ \hat{\mathbf{x}}_{k|k} &= \hat{\mathbf{x}}_{k|k-1} + \mathbf{K}_k\tilde{\mathbf{y}}_k \\ \mathbf{P}_{k|k} &= (\mathbf{I} - \mathbf{K}_k \mathbf{H}_k) \mathbf{P}_{k|k-1} \\ \end{align*}

However, we can show that the update step is also simply another form of the same equations.

First, to show that the covariances are equal, expand the covariance matrix in the update step of the general form, including the Kalman Gain, as follows.

Pkk=Pkk1Pkk1Hk(HkPkk1Hk+Rk)1HkPkk1\begin{align*} \mathbf{P}_{k|k} &= \mathbf{P}_{k|k-1} - \mathbf{P}_{k|k-1} \mathbf{H}_k^\top (\mathbf{H}_k \mathbf{P}_{k|k-1} \mathbf{H}_k^\top + \mathbf{R}_k)^{-1} \mathbf{H}_k \mathbf{P}_{k|k-1} \\ \end{align*}

Next, we apply the Woodbury matrix identity to the covariance matrix in the update step derived in this post. The Woodbury matrix identity is the following identity.

(A+UCV)1=A1A1U(C1+VA1U)1VA1(A + UCV)^{-1} = A^{-1} - A^{-1} U (C^{-1} + V A^{-1} U)^{-1} V A^{-1}

Letting

A=Σtt11U=HtTC=Rt1V=Ht\begin{align*} A &= \Sigma_{t|t-1}^{-1} \\ U &= H_t^T \\ C &= R_t^{-1} \\ V &= H_t \end{align*}

gives

(Σtt11+HtTRt1Ht)1=Σtt1Σtt1HtT(Rt+HtΣtt1HtT)1HtΣtt1(\Sigma_{t|t-1}^{-1} + H_t^T R_t^{-1} H_t)^{-1} = \Sigma_{t|t-1} - \Sigma_{t|t-1} H_t^T (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} H_t \Sigma_{t|t-1}

which is equal to the covariance matrix in the update step above.

The same method can be used to show that the means are also equal. As above, expanding the mean in the update step of the general form gives the following.

x^kk=x^kk1+Pkk1Hk(HkPkk1Hk+Rk)1y~k=x^kk1+Pkk1Hk(HkPkk1Hk+Rk)1(zkHkx^kk1)=x^kk1+Pkk1Hk(HkPkk1Hk+Rk)1zkPkk1Hk(HkPkk1Hk+Rk)1Hkx^kk1\begin{align*} \hat{\mathbf{x}}_{k|k} &= \hat{\mathbf{x}}_{k|k-1} + \mathbf{P}_{k|k-1} \mathbf{H}_k^\top (\mathbf{H}_k \mathbf{P}_{k|k-1} \mathbf{H}_k^\top + \mathbf{R}_k)^{-1} \tilde{\mathbf{y}}_k \\ &= \hat{\mathbf{x}}_{k|k-1} + \mathbf{P}_{k|k-1} \mathbf{H}_k^\top (\mathbf{H}_k \mathbf{P}_{k|k-1} \mathbf{H}_k^\top + \mathbf{R}_k)^{-1} (\mathbf{z}_k - \mathbf{H}_k\hat{\mathbf{x}}_{k|k-1}) \\ &= \hat{\mathbf{x}}_{k|k-1} + \mathbf{P}_{k|k-1} \mathbf{H}_k^\top (\mathbf{H}_k \mathbf{P}_{k|k-1} \mathbf{H}_k^\top + \mathbf{R}_k)^{-1} \mathbf{z}_k - \mathbf{P}_{k|k-1} \mathbf{H}_k^\top (\mathbf{H}_k \mathbf{P}_{k|k-1} \mathbf{H}_k^\top + \mathbf{R}_k)^{-1} \mathbf{H}_k\hat{\mathbf{x}}_{k|k-1} \\ \end{align*}

Next, substituting the covariance matrix result derived above into the mean from the update step derived in this post gives the following.

μtt=Σtt(Σtt11μtt1+HtTRt1zt)=(Σtt1Σtt1HtT(Rt+HtΣtt1HtT)1HtΣtt1)(Σtt11μtt1+HtTRt1zt)=μtt1+Σtt1HtTRt1ztΣtt1HtT(Rt+HtΣtt1HtT)1Htμtt1Σtt1HtT(Rt+HtΣtt1HtT)1HtΣtt1HtTRt1zt=μtt1+Σtt1HtT(Rt1(Rt+HtΣtt1HtT)1HtΣtt1HtTRt1)ztΣtt1HtT(Rt+HtΣtt1HtT)1Htμtt1\begin{align*} \mu_{t|t} &= \Sigma_{t|t} (\Sigma_{t|t-1}^{-1} \mu_{t|t-1} + H_t^T R_t^{-1} z_t) \\ &= (\Sigma_{t|t-1} - \Sigma_{t|t-1} H_t^T (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} H_t \Sigma_{t|t-1}) (\Sigma_{t|t-1}^{-1} \mu_{t|t-1} + H_t^T R_t^{-1} z_t) \\ &= \mu_{t|t-1} + \Sigma_{t|t-1} H_t^T R_t^{-1} z_t - \Sigma_{t|t-1} H_t^T (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} H_t \mu_{t|t-1} - \Sigma_{t|t-1} H_t^T (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} H_t \Sigma_{t|t-1}H_t^T R_t^{-1} z_t \\ &= \mu_{t|t-1} + \Sigma_{t|t-1} H_t^T(R_t^{-1} - (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} H_t \Sigma_{t|t-1}H_t^T R_t^{-1}) z_t - \Sigma_{t|t-1} H_t^T (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} H_t \mu_{t|t-1} \end{align*}

The expression in parentheses in the second term simplifies as follows.

Rt1(Rt+HtΣtt1HtT)1HtΣtt1HtTRt1=(I(Rt+HtΣtt1HtT)1HtΣtt1HtT)Rt1=(I(Rt+HtΣtt1HtT)1HtΣtt1HtT(Rt+HtΣtt1HtT)1Rt+(Rt+HtΣtt1HtT)1Rt)Rt1=(I(Rt+HtΣtt1HtT)1(Rt+HtΣtt1HtT)+(Rt+HtΣtt1HtT)1Rt)Rt1=(II+(Rt+HtΣtt1HtT)1Rt)Rt1=(Rt+HtΣtt1HtT)1RtRt1=(Rt+HtΣtt1HtT)1\begin{align*} & R_t^{-1} - (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} H_t \Sigma_{t|t-1}H_t^T R_t^{-1} \\ &= (I - (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} H_t \Sigma_{t|t-1}H_t^T) R_t^{-1} \\ &= (I - (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} H_t \Sigma_{t|t-1}H_t^T - (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} R_t + (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} R_t ) R_t^{-1} \\ &= (I - (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1}(R_t + H_t \Sigma_{t|t-1} H_t^T) + (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} R_t ) R_t^{-1} \\ &= (I - I + (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} R_t ) R_t^{-1} \\ &= (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} R_t R_t^{-1} \\ &= (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} \end{align*}

Substituting this back into the original expression gives the following.

μtt=μtt1+Σtt1HtT(Rt+HtΣtt1HtT)1ztΣtt1HtT(Rt+HtΣtt1HtT)1Htμtt1\begin{align*} \mu_{t|t} &= \mu_{t|t-1} + \Sigma_{t|t-1} H_t^T(R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} z_t - \Sigma_{t|t-1} H_t^T (R_t + H_t \Sigma_{t|t-1} H_t^T)^{-1} H_t \mu_{t|t-1} \\ \end{align*}

This is equal to the mean in the update step of the general form obtained earlier. Therefore, the method derived in this post and the general method—that is, the method using the Kalman Gain—are exactly equivalent.

Conclusion

In this post, we derived the Kalman filter directly from the Bayesian filter and showed that it is equivalent to the commonly used form of the Kalman filter. This allowed us to understand the Kalman filter as a parametric approximation of the Bayesian filter.

References


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -