Probabilistic Robotics
  • Introduction
  • Basics
    • Recursive State Estimation
      • Basic Concepts in Probability
      • Robot Environment Interaction
      • Bayes Filters
    • Gaussian Filters
      • Kalman Filter
      • Extended Kalman Filter
    • Nonparametric Filters
      • Histogram Filter
      • Particle Filter
    • Robot Perception
      • Beam Models of Range Finders
      • Likelihood Fields for Range Finders
  • Localization
    • Taxonomy of Localization Problems
    • Grid and Monte Carlo
      • Monte Carlo Localization
    • Markov and Gaussian
  • Projects
    • Mislocalization Heatmap
Powered by GitBook
On this page
  • Linear Gaussian Systems
  • 1. Linear Transition
  • 2. Linear Measurement
  • 3. Normal Belief
  • Kalman Filter Algorithm
  • Code Example

Was this helpful?

  1. Basics
  2. Gaussian Filters

Kalman Filter

PreviousGaussian FiltersNextExtended Kalman Filter

Last updated 5 years ago

Was this helpful?

Linear Gaussian Systems

The Kalman filter implements belief computational for continuous state. It is not applicable to discrete or hybrid state spaces. The filter represents belief by moments parameterization. At time ttt, the belief is represented by the mean μt\mu_{t}μt​ and the covariance Σt\Sigma_{t}Σt​. Posteriors are Gaussian if the following three properties hold in addition to Markov assumption.

1. Linear Transition

The state transition probability p(xt∣ut,xt−1)p(x_{t} \mid u_{t}, x_{t-1})p(xt​∣ut​,xt−1​) must be a linear function in its arguments with added Gaussian noises.

xt=Atxt−1+Btut+ϵt(1)\tag{1} x_{t} = A_{t}x_{t-1} + B_{t}u_{t} + \epsilon_{t}xt​=At​xt−1​+Bt​ut​+ϵt​(1)

The state and control are vectors. In our notation, they are vertical vectors.

xt=∣xt[0]xt[1]xt[2]...xt[n−1]∣ut=∣ut[0]ut[1]ut[2]...ut[m−1]∣x_{t} = \begin{vmatrix} x_{t}[0] \\ x_{t}[1] \\ x_{t}[2] \\ ... \\ x_{t}[n-1] \end{vmatrix} \quad u_{t} = \begin{vmatrix} u_{t}[0] \\ u_{t}[1] \\ u_{t}[2] \\ ... \\ u_{t}[m-1] \end{vmatrix}xt​=​xt​[0]xt​[1]xt​[2]...xt​[n−1]​​ut​=​ut​[0]ut​[1]ut​[2]...ut​[m−1]​​

Here AtA_{t}At​ and BtB_{t}Bt​ are matrices. AtA_{t}At​ is of size n by n and BtB_{t}Bt​ is of size n by m. The dimension of state vector is n and the dimension of control vector is m. The random variable ϵt\epsilon_{t}ϵt​ has the same dimension as the state vector. Its mean is zero and its covariance will be denoted as RtR_{t}Rt​. It is there to modifies the uncertainty introduced by the state transition. The mean of the posterior state is given by equation 1 and covariance by RtR_{t}Rt​.

p(xt∣ut,xt−1)=det(2πRt)−0.5  exp⁡[−12(xt−Atxt−1−Btut)TRt−1(xt−Atxt−1−Btut)]p(x_{t} \mid u_{t}, x_{t-1}) = det(2\pi R_{t})^{-0.5} \; \exp\left[ \frac{-1}{2} (x_{t} - A_{t}x_{t-1} - B_{t}u_{t})^{T} R_{t}^{-1} (x_{t} - A_{t}x_{t-1} - B_{t}u_{t}) \right]p(xt​∣ut​,xt−1​)=det(2πRt​)−0.5exp[2−1​(xt​−At​xt−1​−Bt​ut​)TRt−1​(xt​−At​xt−1​−Bt​ut​)]

2. Linear Measurement

3. Normal Belief

Kalman Filter Algorithm

Before we perform the measurement update, we need to compute Kalman gain from equation 3b, which specifies the degree to which the measurement is incorporated into the new state estimate. Then we use the gain to get the new state.

Code Example

For simplicity sake, I will omit the time dependence for transformation matrices. Let's define three matrices A, B, and C using numpy . Note that A is the state transition model or function, B is the control input model, and C is the observation model.

A = np.array([[1.0, 1.0], [0.0, 1.0]])
B = np.array([[1.0, 0.0], [0.0, 1.0]])
C = np.array([[1.0, 0.0]])

If we were to express them in matrix form, they would look like the following.

x = np.array([[0.0], [0.0]])
x_cov = np.array([[1000.0, 0.0], [0.0, 1000.0]])
u = np.array([[0.0], [0.0]])
u_cov = np.array([[0.0, 0.0], [0.0, 0.0]])
Z = [np.array([[1]]), np.array([[2]]), np.array([[3]])
z_cov = np.array([[1]])

Now we can put everything together and construct a Kalman filter algorithm.

def predict_new_belief(x, x_cov, u, u_cov):
    x = dot(A, x) + dot(B, u)
    x_cov = dot(dot(A, x_cov), A.T) + u_cov
    return x, x_cov


def incorporate_measurement(x, x_cov, z, z_cov):
    S = dot(dot(C, x_cov), C.T) + z_cov
    K = dot(dot(x_cov, C.T), linalg.inv(S))

    x = x + dot(K, z - C.dot(x))
    x_cov = dot(identity(2) - dot(K, C), x_cov)
    return x, x_cov


for i in range(len(Z)):
    x, x_cov = predict_new_belief(x, x_cov, u, u_cov)
    x, x_cov = incorporate_measurement(x, x_cov, Z[i], z_cov)

The measurement probability p(zt∣xt)p(z_{t} \mid x_{t})p(zt​∣xt​) must also be linear with added Gaussian noise.

zt=Ctxt+δt(2)\tag{2} z_{t} = C_{t}x_{t} + \delta_{t}zt​=Ct​xt​+δt​(2)

Here CtC_{t}Ct​is a matrix of size k by n where k is the dimension of the measurement vector. The δ\deltaδ describes the measurement noise. The distribution of noise is a multivariate Gaussian with zero mean and covariance of QtQ_{t}Qt​.

p(zt∣xt)=det(2πQt)−0.5exp⁡[−12(zt−Ctxt)TQt−1(zt−Ctxt)]p(z_{t}\mid x_{t}) = det(2\pi Q_{t})^{-0.5} \exp \left[ \frac{-1}{2}(z_{t} - C_{t}x_{t})^{T}Q_{t}^{-1} (z_{t} - C_{t}x_{t}) \right]p(zt​∣xt​)=det(2πQt​)−0.5exp[2−1​(zt​−Ct​xt​)TQt−1​(zt​−Ct​xt​)]

The initial belief bel(x0)bel(x_{0})bel(x0​) must be normally distributed with mean μ0\mu_{0}μ0​ and covariance Σ0\Sigma_{0}Σ0​.

bel(x0)=p(x0)=det(2πΣ0)−0.5exp⁡[−12(x0−μ0)TΣ0T(x0−μ0)]bel(x_{0})= p(x_{0}) = det(2\pi\Sigma_{0})^{-0.5} \exp\left[ \frac{-1}{2}(x_{0} - \mu_{0})^{T} \Sigma_{0}^{T} (x_{0} - \mu_{0}) \right]bel(x0​)=p(x0​)=det(2πΣ0​)−0.5exp[2−1​(x0​−μ0​)TΣ0T​(x0​−μ0​)]

Given arguments μt−1\mu_{t-1}μt−1​, Σt−1\Sigma_{t-1}Σt−1​, utu_{t}ut​, and ztz_{t}zt​, we have the following update rules.

μt‾=Atμt−1+Btut  Σt‾=AtΣt−1At−1T+Rt(3a)\tag{3a} \overline{\mu_{t}} = A_{t}\mu_{t-1} + B_{t}u_{t} \\\;\\ \overline{\Sigma_{t}} = A_{t}\Sigma_{t-1}A_{t-1}^{T} + R_{t}μt​​=At​μt−1​+Bt​ut​Σt​​=At​Σt−1​At−1T​+Rt​(3a)

The predicted belief μt‾\overline{\mu_{t}}μt​​ and Σt‾\overline{\Sigma_{t}}Σt​​ are calculated to represent the belief bel‾(xt)\overline{bel}(x_{t})bel(xt​), one time step later, but before incorporating the measurement ztz_{t}zt​.

Kt=Σ‾tCtT(CtΣ‾CtT+QT)−1(3b)\tag{3b} K_{t} = \overline{\Sigma}_{t} C_{t}^{T} (C_{t}\overline{\Sigma}C_{t}^{T} + Q_{T})^{-1}Kt​=Σt​CtT​(Ct​ΣCtT​+QT​)−1(3b)
μt=μt‾+Kt(zt−Ctμt‾)  Σt=(I−KtCt)Σt‾(3c)\tag{3c} \mu_{t} = \overline{\mu_{t}} + K_{t}(z_{t} - C_{t}\overline{\mu_{t}}) \\\;\\ \Sigma_{t} = (I - K_{t}C_{t})\overline{\Sigma_{t}}μt​=μt​​+Kt​(zt​−Ct​μt​​)Σt​=(I−Kt​Ct​)Σt​​(3c)

The key concept here is innovation, which is the difference between the actual measurement and expected measurement, denoted by zt−Ctμt‾z_{t} - C_{t}\overline{\mu_{t}}zt​−Ct​μt​​ .

A=∣1.01.00.01.0∣  B=∣1.00.00.01.0∣  C=∣1.00.0∣A = \begin{vmatrix} 1.0 & 1.0 \\ 0.0 & 1.0 \end{vmatrix} \\\;\\ B = \begin{vmatrix} 1.0 & 0.0 \\ 0.0 & 1.0 \end{vmatrix} \\\;\\ C = \begin{vmatrix} 1.0 & 0.0 \end{vmatrix}A=​1.00.0​1.01.0​​B=​1.00.0​0.01.0​​C=​1.0​0.0​​

Suppose our robot is at coordinate (0.0,0.0)(0.0, 0.0)(0.0,0.0) initially and we don't apply any external control to it. Let's denote x to be μ\muμ, x_cov to be Σ\SigmaΣ, u to be uuu, and u_cov to be RRR.

μ=∣00∣  Σ=∣1000.00.00.01000.0∣  u=∣0.00.0∣  R=∣0.00.00.00.0∣\mu = \begin{vmatrix} 0 \\ 0 \end{vmatrix} \; \Sigma = \begin{vmatrix} 1000.0 & 0.0 \\ 0.0 & 1000.0 \end{vmatrix} \\\;\\ u = \begin{vmatrix} 0.0 \\ 0.0 \end{vmatrix}\; R = \begin{vmatrix} 0.0 & 0.0 \\ 0.0 & 0.0 \end{vmatrix}μ=​00​​Σ=​1000.00.0​0.01000.0​​u=​0.00.0​​R=​0.00.0​0.00.0​​

Let Z to be (z0,z1,z2,...)(z_{0}, z_{1}, z_{2}, ...)(z0​,z1​,z2​,...)and z_cov to be QQQ.

z0=∣1∣  z1=∣2∣etc...  Q=∣0∣z_{0} = \begin{vmatrix} 1 \end{vmatrix} \; z_{1} = \begin{vmatrix} 2 \end{vmatrix} etc...\\\;\\ Q = \begin{vmatrix} 0 \end{vmatrix}z0​=​1​​z1​=​2​​etc...Q=​0​​