# Kalman filter

The Kalman filter keeps track of the estimated state of the system and the variance or uncertainty of the estimate. The estimate is updated using a state transition model and measurements. ${\displaystyle {\hat {x}}_{k\mid k-1}}$ denotes the estimate of the system's state at time step k before the k-th measurement yk has been taken into account; ${\displaystyle P_{k\mid k-1}}$ is the corresponding uncertainty.

Kalman filtering, also known as linear quadratic estimation (LQE), is an algorithm that uses a series of measurements observed over time, containing noise (random variations) and other inaccuracies, and produces estimates of unknown variables that tend to be more precise than those based on a single measurement alone. More formally, the Kalman filter operates recursively on streams of noisy input data to produce a statistically optimal estimate of the underlying system state. The filter is named after Rudolf (Rudy) E. Kálmán, one of the primary developers of its theory.

The Kalman filter has numerous applications in technology. A common application is for guidance, navigation and control of vehicles, particularly aircraft and spacecraft. Furthermore, the Kalman filter is a widely applied concept in time series analysis used in fields such as signal processing and econometrics. Kalman filters also are one of the main topics in the field of Robotic motion planning and control, and sometimes included in Trajectory optimization.

The algorithm works in a two-step process. In the prediction step, the Kalman filter produces estimates of the current state variables, along with their uncertainties. Once the outcome of the next measurement (necessarily corrupted with some amount of error, including random noise) is observed, these estimates are updated using a weighted average, with more weight being given to estimates with higher certainty. Because of the algorithm's recursive nature, it can run in real time using only the present input measurements and the previously calculated state and its uncertainty matrix; no additional past information is required.

It is a common misconception that the Kalman filter assumes that all error terms and measurements are Gaussian distributed. Kalman's original paper derived the filter using orthogonal projection theory to show that the covariance is minimized, and this result does not require any assumption, e.g., that the errors are Gaussian.[1] He then showed that the filter yields the exact conditional probability estimate in the special case that all errors are Gaussian-distributed.

Extensions and generalizations to the method have also been developed, such as the extended Kalman filter and the unscented Kalman filter which work on nonlinear systems. The underlying model is a Bayesian model similar to a hidden Markov model but where the state space of the latent variables is continuous and where all latent and observed variables have Gaussian distributions.

## Naming and historical development

Rudolf Emil Kalman, co-inventor and developer of the Kalman filter.

The filter is named after Hungarian émigré Rudolf E. Kálmán, although Thorvald Nicolai Thiele[2][3] and Peter Swerling developed a similar algorithm earlier. Richard S. Bucy of the University of Southern California contributed to the theory, leading to it often being called the Kalman–Bucy filter. Stanley F. Schmidt is generally credited with developing the first implementation of a Kalman filter. It was during a visit by Kalman to the NASA Ames Research Center that he saw the applicability of his ideas to the problem of trajectory estimation for the Apollo program, leading to its incorporation in the Apollo navigation computer. This Kalman filter was first described and partially developed in technical papers by Swerling (1958), Kalman (1960) and Kalman and Bucy (1961).

Kalman filters have been vital in the implementation of the navigation systems of U.S. Navy nuclear ballistic missile submarines, and in the guidance and navigation systems of cruise missiles such as the U.S. Navy's Tomahawk missile and the U.S. Air Force's Air Launched Cruise Missile. It is also used in the guidance and navigation systems of the NASA Space Shuttle and the attitude control and navigation systems of the International Space Station.

This digital filter is sometimes called the Stratonovich–Kalman–Bucy filter because it is a special case of a more general, non-linear filter developed somewhat earlier by the Soviet mathematician Ruslan Stratonovich.[4][5][6][7] In fact, some of the special case linear filter's equations appeared in these papers by Stratonovich that were published before summer 1960, when Kalman met with Stratonovich during a conference in Moscow.

## Overview of the calculation

The Kalman filter uses a system's dynamics model (e.g., physical laws of motion), known control inputs to that system, and multiple sequential measurements (such as from sensors) to form an estimate of the system's varying quantities (its state) that is better than the estimate obtained by using any one measurement alone. As such, it is a common sensor fusion and data fusion algorithm.

All measurements and calculations based on models are estimates to some degree. Noisy sensor data, approximations in the equations that describe how a system changes, and external factors that are not accounted for introduce some uncertainty about the inferred values for a system's state. The Kalman filter averages a prediction of a system's state with a new measurement using a weighted average. The purpose of the weights is that values with better (i.e., smaller) estimated uncertainty are "trusted" more. The weights are calculated from the covariance, a measure of the estimated uncertainty of the prediction of the system's state. The result of the weighted average is a new state estimate that lies between the predicted and measured state, and has a better estimated uncertainty than either alone. This process is repeated every time step, with the new estimate and its covariance informing the prediction used in the following iteration. This means that the Kalman filter works recursively and requires only the last "best guess", rather than the entire history, of a system's state to calculate a new state.

Because the certainty of the measurements is often difficult to measure precisely, it is common to discuss the filter's behavior in terms of gain. The Kalman gain is a function of the relative certainty of the measurements and current state estimate, and can be "tuned" to achieve particular performance. With a high gain, the filter places more weight on the measurements, and thus follows them more closely. With a low gain, the filter follows the model predictions more closely, smoothing out noise but decreasing the responsiveness. At the extremes, a gain of one causes the filter to ignore the state estimate entirely, while a gain of zero causes the measurements to be ignored.

When performing the actual calculations for the filter (as discussed below), the state estimate and covariances are coded into matrices to handle the multiple dimensions involved in a single set of calculations. This allows for representation of linear relationships between different state variables (such as position, velocity, and acceleration) in any of the transition models or covariances.

## Example application

As an example application, consider the problem of determining the precise location of a truck. The truck can be equipped with a GPS unit that provides an estimate of the position within a few meters. The GPS estimate is likely to be noisy; readings 'jump around' rapidly, though always remaining within a few meters of the real position. In addition, since the truck is expected to follow the laws of physics, its position can also be estimated by integrating its velocity over time, determined by keeping track of wheel revolutions and the angle of the steering wheel. This is a technique known as dead reckoning. Typically, dead reckoning will provide a very smooth estimate of the truck's position, but it will drift over time as small errors accumulate.

In this example, the Kalman filter can be thought of as operating in two distinct phases: predict and update. In the prediction phase, the truck's old position will be modified according to the physical laws of motion (the dynamic or "state transition" model) plus any changes produced by the accelerator pedal and steering wheel. Not only will a new position estimate be calculated, but a new covariance will be calculated as well. Perhaps the covariance is proportional to the speed of the truck because we are more uncertain about the accuracy of the dead reckoning position estimate at high speeds but very certain about the position estimate when moving slowly. Next, in the update phase, a measurement of the truck's position is taken from the GPS unit. Along with this measurement comes some amount of uncertainty, and its covariance relative to that of the prediction from the previous phase determines how much the new measurement will affect the updated prediction. Ideally, if the dead reckoning estimates tend to drift away from the real position, the GPS measurement should pull the position estimate back towards the real position but not disturb it to the point of becoming rapidly changing and noisy.

## Technical description and context

The Kalman filter is an efficient recursive filter that estimates the internal state of a linear dynamic system from a series of noisy measurements. It is used in a wide range of engineering and econometric applications from radar and computer vision to estimation of structural macroeconomic models,[8][9] and is an important topic in control theory and control systems engineering. Together with the linear-quadratic regulator (LQR), the Kalman filter solves the linear-quadratic-Gaussian control problem (LQG). The Kalman filter, the linear-quadratic regulator and the linear-quadratic-Gaussian controller are solutions to what arguably are the most fundamental problems in control theory.

In most applications, the internal state is much larger (more degrees of freedom) than the few "observable" parameters which are measured. However, by combining a series of measurements, the Kalman filter can estimate the entire internal state.

In Dempster–Shafer theory, each state equation or observation is considered a special case of a linear belief function and the Kalman filter is a special case of combining linear belief functions on a join-tree or Markov tree. Additional approaches include belief filters which use Bayes or evidential updates to the state equations.

A wide variety of Kalman filters have now been developed, from Kalman's original formulation, now called the "simple" Kalman filter, the Kalman–Bucy filter, Schmidt's "extended" filter, the information filter, and a variety of "square-root" filters that were developed by Bierman, Thornton and many others. Perhaps the most commonly used type of very simple Kalman filter is the phase-locked loop, which is now ubiquitous in radios, especially frequency modulation (FM) radios, television sets, satellite communications receivers, outer space communications systems, and nearly any other electronic communications equipment.

## Underlying dynamic system model

The Kalman filters are based on linear dynamic systems discretized in the time domain. They are modelled on a Markov chain built on linear operators perturbed by errors that may include Gaussian noise. The state of the system is represented as a vector of real numbers. At each discrete time increment, a linear operator is applied to the state to generate the new state, with some noise mixed in, and optionally some information from the controls on the system if they are known. Then, another linear operator mixed with more noise generates the observed outputs from the true ("hidden") state. The Kalman filter may be regarded as analogous to the hidden Markov model, with the key difference that the hidden state variables take values in a continuous space (as opposed to a discrete state space as in the hidden Markov model). There is a strong duality between the equations of the Kalman Filter and those of the hidden Markov model. A review of this and other models is given in Roweis and Ghahramani (1999)[10] and Hamilton (1994), Chapter 13.[11]

In order to use the Kalman filter to estimate the internal state of a process given only a sequence of noisy observations, one must model the process in accordance with the framework of the Kalman filter. This means specifying the following matrices: Fk, the state-transition model; Hk, the observation model; Qk, the covariance of the process noise; Rk, the covariance of the observation noise; and sometimes Bk, the control-input model, for each time-step, k, as described below.

Model underlying the Kalman filter. Squares represent matrices. Ellipses represent multivariate normal distributions (with the mean and covariance matrix enclosed). Unenclosed values are vectors. In the simple case, the various matrices are constant with time, and thus the subscripts are dropped, but the Kalman filter allows any of them to change each time step.

The Kalman filter model assumes the true state at time k is evolved from the state at (k − 1) according to

${\displaystyle \mathbf {x} _{k}=\mathbf {F} _{k}\mathbf {x} _{k-1}+\mathbf {B} _{k}\mathbf {u} _{k}+\mathbf {w} _{k}}$

where

• Fk is the state transition model which is applied to the previous state xk−1;
• Bk is the control-input model which is applied to the control vector uk;
• wk is the process noise which is assumed to be drawn from a zero mean multivariate normal distribution with covariance Qk.
${\displaystyle \mathbf {w} _{k}\sim N(0,\mathbf {Q} _{k})}$

At time k an observation (or measurement) zk of the true state xk is made according to

${\displaystyle \mathbf {z} _{k}=\mathbf {H} _{k}\mathbf {x} _{k}+\mathbf {v} _{k}}$

where Hk is the observation model which maps the true state space into the observed space and vk is the observation noise which is assumed to be zero mean Gaussian white noise with covariance Rk.

${\displaystyle \mathbf {v} _{k}\sim N(0,\mathbf {R} _{k})}$

The initial state, and the noise vectors at each step {x0, w1, ..., wk, v1 ... vk} are all assumed to be mutually independent.

Many real dynamical systems do not exactly fit this model. In fact, unmodelled dynamics can seriously degrade the filter performance, even when it was supposed to work with unknown stochastic signals as inputs. The reason for this is that the effect of unmodelled dynamics depends on the input, and, therefore, can bring the estimation algorithm to instability (it diverges). On the other hand, independent white noise signals will not make the algorithm diverge. The problem of separating between measurement noise and unmodelled dynamics is a difficult one and is treated in control theory under the framework of robust control.[12][13]

## Details

The Kalman filter is a recursive estimator. This means that only the estimated state from the previous time step and the current measurement are needed to compute the estimate for the current state. In contrast to batch estimation techniques, no history of observations and/or estimates is required. In what follows, the notation ${\displaystyle {\hat {\mathbf {x} }}_{n\mid m}}$ represents the estimate of ${\displaystyle \mathbf {x} }$ at time n given observations up to, and including at time m ≤ n.

The state of the filter is represented by two variables:

The Kalman filter can be written as a single equation, however it is most often conceptualized as two distinct phases: "Predict" and "Update". The predict phase uses the state estimate from the previous timestep to produce an estimate of the state at the current timestep. This predicted state estimate is also known as the a priori state estimate because, although it is an estimate of the state at the current timestep, it does not include observation information from the current timestep. In the update phase, the current a priori prediction is combined with current observation information to refine the state estimate. This improved estimate is termed the a posteriori state estimate.

Typically, the two phases alternate, with the prediction advancing the state until the next scheduled observation, and the update incorporating the observation. However, this is not necessary; if an observation is unavailable for some reason, the update may be skipped and multiple prediction steps performed. Likewise, if multiple independent observations are available at the same time, multiple update steps may be performed (typically with different observation matrices Hk).[14][15]

### Update

 Innovation or measurement residual ${\displaystyle {\tilde {\mathbf {y} }}_{k}=\mathbf {z} _{k}-\mathbf {H} _{k}{\hat {\mathbf {x} }}_{k\mid k-1}}$ Innovation (or residual) covariance ${\displaystyle \mathbf {S} _{k}=\mathbf {H} _{k}\mathbf {P} _{k\mid k-1}\mathbf {H} _{k}^{T}+\mathbf {R} _{k}}$ Optimal Kalman gain ${\displaystyle \mathbf {K} _{k}=\mathbf {P} _{k\mid k-1}\mathbf {H} _{k}^{T}\mathbf {S} _{k}^{-1}}$ Updated (a posteriori) state estimate ${\displaystyle {\hat {\mathbf {x} }}_{k\mid k}={\hat {\mathbf {x} }}_{k\mid k-1}+\mathbf {K} _{k}{\tilde {\mathbf {y} }}_{k}}$ Updated (a posteriori) estimate covariance ${\displaystyle \mathbf {P} _{k|k}=(I-\mathbf {K} _{k}\mathbf {H} _{k})\mathbf {P} _{k|k-1}}$

The formula for the updated estimate and covariance above is only valid for the optimal Kalman gain. Usage of other gain values require a more complex formula found in the derivations section.

### Invariants

If the model is accurate, and the values for ${\displaystyle {\hat {\mathbf {x} }}_{0\mid 0}}$ and ${\displaystyle \mathbf {P} _{0\mid 0}}$ accurately reflect the distribution of the initial state values, then the following invariants are preserved: (all estimates have a mean error of zero)

where ${\displaystyle {\textrm {E}}[\xi ]}$ is the expected value of ${\displaystyle \xi }$, and covariance matrices accurately reflect the covariance of estimates

### Estimation of the noise covariances Qk and Rk

Practical implementation of the Kalman Filter is often difficult due to the difficulty of getting a good estimate of the noise covariance matrices Qk and Rk. Extensive research has been done in this field to estimate these covariances from data. One of the more promising and practical approaches to do this is the Autocovariance Least-Squares (ALS) technique that uses the time-lagged autocovariances of routine operating data to estimate the covariances.[16][17] The GNU Octave and Matlab code used to calculate the noise covariance matrices using the ALS technique is available online under the GNU General Public License license.[18]

### Optimality and performance

It follows from theory that the Kalman filter is optimal cases where a) the model perfectly matches the real system, b) the entering noise is white and c) the covariances of the noise are exactly known. Several methods for the noise covariance estimation have been proposed during past decades, including ALS, mentioned in the previous paragraph. After the covariances are estimated, it is useful to evaluate the performance of the filter, i.e. whether it is possible to improve the state estimation quality. If the Kalman filter works optimally, the innovation sequence (the output prediction error) is a white noise, therefore the whiteness property of the innovations measures filter performance. Several different methods can be used for this purpose.[19]

## Example application, technical

Consider a truck on frictionless, straight rails. Initially the truck is stationary at position 0, but it is buffeted this way and that by random uncontrolled forces. We measure the position of the truck every Δt seconds, but these measurements are imprecise; we want to maintain a model of where the truck is and what its velocity is. We show here how we derive the model from which we create our Kalman filter.

Since ${\displaystyle \mathbf {F} ,\mathbf {H} ,\mathbf {R} ,\mathbf {Q} }$ are constant, their time indices are dropped.

The position and velocity of the truck are described by the linear state space

${\displaystyle \mathbf {x} _{k}={\begin{bmatrix}x\\{\dot {x}}\end{bmatrix}}}$

where ${\displaystyle {\dot {x}}}$ is the velocity, that is, the derivative of position with respect to time.

We assume that between the (k − 1) and k timestep uncontrolled forces cause a constant acceleration of ak that is normally distributed, with mean 0 and standard deviation σa. From Newton's laws of motion we conclude that

${\displaystyle \mathbf {x} _{k}=\mathbf {F} \mathbf {x} _{k-1}+\mathbf {G} a_{k}}$

(note that there is no ${\displaystyle \mathbf {B} u}$ term since we have no known control inputs. Instead, we assume that ak is the effect of an unknown input and ${\displaystyle \mathbf {G} }$ applies that effect to the state vector) where

${\displaystyle \mathbf {F} ={\begin{bmatrix}1&\Delta t\\0&1\end{bmatrix}}}$

and

${\displaystyle \mathbf {G} ={\begin{bmatrix}{\frac {\Delta t^{2}}{2}}\\[6pt]\Delta t\end{bmatrix}}}$

so that

${\displaystyle \mathbf {x} _{k}=\mathbf {F} \mathbf {x} _{k-1}+\mathbf {w} _{k}}$
${\displaystyle \mathbf {Q} =\mathbf {G} \mathbf {G} ^{\text{T}}\sigma _{a}^{2}={\begin{bmatrix}{\frac {\Delta t^{4}}{4}}&{\frac {\Delta t^{3}}{2}}\\[6pt]{\frac {\Delta t^{3}}{2}}&\Delta t^{2}\end{bmatrix}}\sigma _{a}^{2}.}$

At each time step, a noisy measurement of the true position of the truck is made. Let us suppose the measurement noise vk is also normally distributed, with mean 0 and standard deviation σz.

${\displaystyle \mathbf {z} _{k}=\mathbf {Hx} _{k}+\mathbf {v} _{k}}$

where

${\displaystyle \mathbf {H} ={\begin{bmatrix}1&0\end{bmatrix}}}$

and

${\displaystyle \mathbf {R} ={\textrm {E}}[\mathbf {v} _{k}\mathbf {v} _{k}^{\text{T}}]={\begin{bmatrix}\sigma _{z}^{2}\end{bmatrix}}}$

We know the initial starting state of the truck with perfect precision, so we initialize

${\displaystyle {\hat {\mathbf {x} }}_{0\mid 0}={\begin{bmatrix}0\\0\end{bmatrix}}}$

and to tell the filter that we know the exact position and velocity, we give it a zero covariance matrix:

${\displaystyle \mathbf {P} _{0\mid 0}={\begin{bmatrix}0&0\\0&0\end{bmatrix}}}$

If the initial position and velocity are not known perfectly the covariance matrix should be initialized with a suitably large number, say L, on its diagonal.

${\displaystyle \mathbf {P} _{0\mid 0}={\begin{bmatrix}L&0\\0&L\end{bmatrix}}}$

The filter will then prefer the information from the first measurements over the information already in the model.

## Derivations

### Deriving the a posteriori estimate covariance matrix

Starting with our invariant on the error covariance Pk | k as above

${\displaystyle \mathbf {P} _{k\mid k}=\mathrm {cov} (\mathbf {x} _{k}-{\hat {\mathbf {x} }}_{k\mid k})}$

substitute in the definition of ${\displaystyle {\hat {\mathbf {x} }}_{k\mid k}}$

${\displaystyle \mathbf {P} _{k\mid k}={\textrm {cov}}(\mathbf {x} _{k}-({\hat {\mathbf {x} }}_{k\mid k-1}+\mathbf {K} _{k}{\tilde {\mathbf {y} }}_{k}))}$
${\displaystyle \mathbf {P} _{k\mid k}={\textrm {cov}}(\mathbf {x} _{k}-({\hat {\mathbf {x} }}_{k\mid k-1}+\mathbf {K} _{k}(\mathbf {z} _{k}-\mathbf {H} _{k}{\hat {\mathbf {x} }}_{k\mid k-1})))}$
${\displaystyle \mathbf {P} _{k\mid k}={\textrm {cov}}(\mathbf {x} _{k}-({\hat {\mathbf {x} }}_{k\mid k-1}+\mathbf {K} _{k}(\mathbf {H} _{k}\mathbf {x} _{k}+\mathbf {v} _{k}-\mathbf {H} _{k}{\hat {\mathbf {x} }}_{k\mid k-1})))}$

and by collecting the error vectors we get

${\displaystyle \mathbf {P} _{k|k}={\textrm {cov}}((I-\mathbf {K} _{k}\mathbf {H} _{k})(\mathbf {x} _{k}-{\hat {\mathbf {x} }}_{k\mid k-1})-\mathbf {K} _{k}\mathbf {v} _{k})}$

Since the measurement error vk is uncorrelated with the other terms, this becomes

${\displaystyle \mathbf {P} _{k|k}={\textrm {cov}}((I-\mathbf {K} _{k}\mathbf {H} _{k})(\mathbf {x} _{k}-{\hat {\mathbf {x} }}_{k\mid k-1}))+{\textrm {cov}}(\mathbf {K} _{k}\mathbf {v} _{k})}$

by the properties of vector covariance this becomes

${\displaystyle \mathbf {P} _{k\mid k}=(I-\mathbf {K} _{k}\mathbf {H} _{k}){\textrm {cov}}(\mathbf {x} _{k}-{\hat {\mathbf {x} }}_{k\mid k-1})(I-\mathbf {K} _{k}\mathbf {H} _{k})^{\text{T}}+\mathbf {K} _{k}{\textrm {cov}}(\mathbf {v} _{k})\mathbf {K} _{k}^{\text{T}}}$

which, using our invariant on Pk | k−1 and the definition of Rk becomes

${\displaystyle \mathbf {P} _{k\mid k}=(I-\mathbf {K} _{k}\mathbf {H} _{k})\mathbf {P} _{k\mid k-1}(I-\mathbf {K} _{k}\mathbf {H} _{k})^{\text{T}}+\mathbf {K} _{k}\mathbf {R} _{k}\mathbf {K} _{k}^{\text{T}}}$

This formula (sometimes known as the "Joseph form" of the covariance update equation) is valid for any value of Kk. It turns out that if Kk is the optimal Kalman gain, this can be simplified further as shown below.

### Kalman gain derivation

The Kalman filter is a minimum mean-square error estimator. The error in the a posteriori state estimation is

${\displaystyle \mathbf {x} _{k}-{\hat {\mathbf {x} }}_{k\mid k}}$

We seek to minimize the expected value of the square of the magnitude of this vector, ${\displaystyle {\textrm {E}}[\|\mathbf {x} _{k}-{\hat {\mathbf {x} }}_{k|k}\|^{2}]}$. This is equivalent to minimizing the trace of the a posteriori estimate covariance matrix ${\displaystyle \mathbf {P} _{k|k}}$. By expanding out the terms in the equation above and collecting, we get:

{\displaystyle {\begin{aligned}\mathbf {P} _{k\mid k}&=\mathbf {P} _{k\mid k-1}-\mathbf {K} _{k}\mathbf {H} _{k}\mathbf {P} _{k\mid k-1}-\mathbf {P} _{k\mid k-1}\mathbf {H} _{k}^{\text{T}}\mathbf {K} _{k}^{\text{T}}+\mathbf {K} _{k}(\mathbf {H} _{k}\mathbf {P} _{k\mid k-1}\mathbf {H} _{k}^{\text{T}}+\mathbf {R} _{k})\mathbf {K} _{k}^{\text{T}}\\[6pt]&=\mathbf {P} _{k\mid k-1}-\mathbf {K} _{k}\mathbf {H} _{k}\mathbf {P} _{k\mid k-1}-\mathbf {P} _{k\mid k-1}\mathbf {H} _{k}^{\text{T}}\mathbf {K} _{k}^{\text{T}}+\mathbf {K} _{k}\mathbf {S} _{k}\mathbf {K} _{k}^{\text{T}}\end{aligned}}}

The trace is minimized when its matrix derivative with respect to the gain matrix is zero. Using the gradient matrix rules and the symmetry of the matrices involved we find that

${\displaystyle {\frac {\partial \;\mathrm {tr} (\mathbf {P} _{k\mid k})}{\partial \;\mathbf {K} _{k}}}=-2(\mathbf {H} _{k}\mathbf {P} _{k\mid k-1})^{\text{T}}+2\mathbf {K} _{k}\mathbf {S} _{k}=0.}$

Solving this for Kk yields the Kalman gain:

${\displaystyle \mathbf {K} _{k}\mathbf {S} _{k}=(\mathbf {H} _{k}\mathbf {P} _{k\mid k-1})^{\text{T}}=\mathbf {P} _{k\mid k-1}\mathbf {H} _{k}^{\text{T}}}$
${\displaystyle \mathbf {K} _{k}=\mathbf {P} _{k\mid k-1}\mathbf {H} _{k}^{\text{T}}\mathbf {S} _{k}^{-1}}$

This gain, which is known as the optimal Kalman gain, is the one that yields MMSE estimates when used.

### Simplification of the a posteriori error covariance formula

The formula used to calculate the a posteriori error covariance can be simplified when the Kalman gain equals the optimal value derived above. Multiplying both sides of our Kalman gain formula on the right by SkKkT, it follows that

${\displaystyle \mathbf {K} _{k}\mathbf {S} _{k}\mathbf {K} _{k}^{\mathrm {T} }=\mathbf {P} _{k\mid k-1}\mathbf {H} _{k}^{\mathrm {T} }\mathbf {K} _{k}^{\mathrm {T} }}$

Referring back to our expanded formula for the a posteriori error covariance,

${\displaystyle \mathbf {P} _{k\mid k}=\mathbf {P} _{k\mid k-1}-\mathbf {K} _{k}\mathbf {H} _{k}\mathbf {P} _{k\mid k-1}-\mathbf {P} _{k\mid k-1}\mathbf {H} _{k}^{\mathrm {T} }\mathbf {K} _{k}^{\mathrm {T} }+\mathbf {K} _{k}\mathbf {S} _{k}\mathbf {K} _{k}^{\mathrm {T} }}$

we find the last two terms cancel out, giving

${\displaystyle \mathbf {P} _{k\mid k}=\mathbf {P} _{k\mid k-1}-\mathbf {K} _{k}\mathbf {H} _{k}\mathbf {P} _{k\mid k-1}=(I-\mathbf {K} _{k}\mathbf {H} _{k})\mathbf {P} _{k\mid k-1}.}$

This formula is computationally cheaper and thus nearly always used in practice, but is only correct for the optimal gain. If arithmetic precision is unusually low causing problems with numerical stability, or if a non-optimal Kalman gain is deliberately used, this simplification cannot be applied; the a posteriori error covariance formula as derived above must be used.

## Sensitivity analysis

The Kalman filtering equations provide an estimate of the state ${\displaystyle {\hat {\mathbf {x} }}_{k\mid k}}$ and its error covariance ${\displaystyle \mathbf {P} _{k\mid k}}$ recursively. The estimate and its quality depend on the system parameters and the noise statistics fed as inputs to the estimator. This section analyzes the effect of uncertainties in the statistical inputs to the filter.[20] In the absence of reliable statistics or the true values of noise covariance matrices ${\displaystyle \mathbf {Q} _{k}}$ and ${\displaystyle \mathbf {R} _{k}}$, the expression

${\displaystyle \mathbf {P} _{k\mid k}=(\mathbf {I} -\mathbf {K} _{k}\mathbf {H} _{k})\mathbf {P} _{k\mid k-1}(\mathbf {I} -\mathbf {K} _{k}\mathbf {H} _{k})^{\mathrm {T} }+\mathbf {K} _{k}\mathbf {R} _{k}\mathbf {K} _{k}^{\mathrm {T} }}$

no longer provides the actual error covariance. In other words, ${\displaystyle \mathbf {P} _{k\mid k}\neq E[(\mathbf {x} _{k}-{\hat {\mathbf {x} }}_{k\mid k})(\mathbf {x} _{k}-{\hat {\mathbf {x} }}_{k\mid k})^{\mathrm {T} }]}$. In most real time applications the covariance matrices that are used in designing the Kalman filter are different from the actual noise covariances matrices.{{ safesubst:#invoke:Unsubst||date=__DATE__ |\$B= {{#invoke:Category handler|main}}{{#invoke:Category handler|main}}[citation needed] }} This sensitivity analysis describes the behavior of the estimation error covariance when the noise covariances as well as the system matrices ${\displaystyle \mathbf {F} _{k}}$ and ${\displaystyle \mathbf {H} _{k}}$ that are fed as inputs to the filter are incorrect. Thus, the sensitivity analysis describes the robustness (or sensitivity) of the estimator to misspecified statistical and parametric inputs to the estimator.

This discussion is limited to the error sensitivity analysis for the case of statistical uncertainties. Here the actual noise covariances are denoted by ${\displaystyle \mathbf {Q} _{k}^{a}}$ and ${\displaystyle \mathbf {R} _{k}^{a}}$ respectively, whereas the design values used in the estimator are ${\displaystyle \mathbf {Q} _{k}}$ and ${\displaystyle \mathbf {R} _{k}}$ respectively. The actual error covariance is denoted by ${\displaystyle \mathbf {P} _{k\mid k}^{a}}$ and ${\displaystyle \mathbf {P} _{k\mid k}}$ as computed by the Kalman filter is referred to as the Riccati variable. When ${\displaystyle \mathbf {Q} _{k}\equiv \mathbf {Q} _{k}^{a}}$ and ${\displaystyle \mathbf {R} _{k}\equiv \mathbf {R} _{k}^{a}}$, this means that ${\displaystyle \mathbf {P} _{k\mid k}=\mathbf {P} _{k\mid k}^{a}}$. While computing the actual error covariance using ${\displaystyle \mathbf {P} _{k\mid k}^{a}=E[(\mathbf {x} _{k}-{\hat {\mathbf {x} }}_{k\mid k})(\mathbf {x} _{k}-{\hat {\mathbf {x} }}_{k\mid k})^{\mathrm {T} }]}$, substituting for ${\displaystyle {\widehat {\mathbf {x} }}_{k\mid k}}$ and using the fact that ${\displaystyle E[\mathbf {w} _{k}\mathbf {w} _{k}^{\mathrm {T} }]=\mathbf {Q} _{k}^{a}}$ and ${\displaystyle E[\mathbf {v} _{k}\mathbf {v} _{k}^{\mathrm {T} }]=\mathbf {R} _{k}^{a}}$, results in the following recursive equations for ${\displaystyle \mathbf {P} _{k\mid k}^{a}}$ :

${\displaystyle \mathbf {P} _{k\mid k-1}^{a}=\mathbf {F} _{k}\mathbf {P} _{k-1\mid k-1}^{a}\mathbf {F} _{k}^{\mathrm {T} }+\mathbf {Q} _{k}^{a}}$

and

${\displaystyle \mathbf {P} _{k\mid k}^{a}=(\mathbf {I} -\mathbf {K} _{k}\mathbf {H} _{k})\mathbf {P} _{k\mid k-1}^{a}(\mathbf {I} -\mathbf {K} _{k}\mathbf {H} _{k})^{\mathrm {T} }+\mathbf {K} _{k}\mathbf {R} _{k}^{a}\mathbf {K} _{k}^{\mathrm {T} }}$

While computing ${\displaystyle \mathbf {P} _{k\mid k}}$, by design the filter implicitly assumes that ${\displaystyle E[\mathbf {w} _{k}\mathbf {w} _{k}^{\mathrm {T} }]=\mathbf {Q} _{k}}$ and ${\displaystyle E[\mathbf {v} _{k}\mathbf {v} _{k}^{\mathrm {T} }]=\mathbf {R} _{k}}$. Note that the recursive expressions for ${\displaystyle \mathbf {P} _{k\mid k}^{a}}$ and ${\displaystyle \mathbf {P} _{k\mid k}}$ are identical except for the presence of ${\displaystyle \mathbf {Q} _{k}^{a}}$ and ${\displaystyle \mathbf {R} _{k}^{a}}$ in place of the design values ${\displaystyle \mathbf {Q} _{k}}$ and ${\displaystyle \mathbf {R} _{k}}$ respectively.

## Square root form

One problem with the Kalman filter is its numerical stability. If the process noise covariance Qk is small, round-off error often causes a small positive eigenvalue to be computed as a negative number. This renders the numerical representation of the state covariance matrix P indefinite, while its true form is positive-definite.

Positive definite matrices have the property that they have a triangular matrix square root P = S·ST. This can be computed efficiently using the Cholesky factorization algorithm, but more importantly, if the covariance is kept in this form, it can never have a negative diagonal or become asymmetric. An equivalent form, which avoids many of the square root operations required by the matrix square root yet preserves the desirable numerical properties, is the U-D decomposition form, P = U·D·UT, where U is a unit triangular matrix (with unit diagonal), and D is a diagonal matrix.

Between the two, the U-D factorization uses the same amount of storage, and somewhat less computation, and is the most commonly used square root form. (Early literature on the relative efficiency is somewhat misleading, as it assumed that square roots were much more time-consuming than divisions,[21]:69 while on 21-st century computers they are only slightly more expensive.)

Efficient algorithms for the Kalman prediction and update steps in the square root form were developed by G. J. Bierman and C. L. Thornton.[21][22]

The L·D·LT decomposition of the innovation covariance matrix Sk is the basis for another type of numerically efficient and robust square root filter.[23] The algorithm starts with the LU decomposition as implemented in the Linear Algebra PACKage (LAPACK). These results are further factored into the L·D·LT structure with methods given by Golub and Van Loan (algorithm 4.1.2) for a symmetric nonsingular matrix.[24] Any singular covariance matrix is pivoted so that the first diagonal partition is nonsingular and well-conditioned. The pivoting algorithm must retain any portion of the innovation covariance matrix directly corresponding to observed state-variables Hk·xk|k-1 that are associated with auxiliary observations in yk. The L·D·LT square-root filter requires orthogonalization of the observation vector.[22][23] This may be done with the inverse square-root of the covariance matrix for the auxiliary variables using Method 2 in Higham (2002, p. 263).[25]

## Relationship to recursive Bayesian estimation

The Kalman filter can be presented as one of the most simple dynamic Bayesian networks. The Kalman filter calculates estimates of the true values of states recursively over time using incoming measurements and a mathematical process model. Similarly, recursive Bayesian estimation calculates estimates of an unknown probability density function (PDF) recursively over time using incoming measurements and a mathematical process model.[26]

In recursive Bayesian estimation, the true state is assumed to be an unobserved Markov process, and the measurements are the observed states of a hidden Markov model (HMM).

Because of the Markov assumption, the true state is conditionally independent of all earlier states given the immediately previous state.

${\displaystyle p({\textbf {x}}_{k}\mid {\textbf {x}}_{0},\dots ,{\textbf {x}}_{k-1})=p({\textbf {x}}_{k}\mid {\textbf {x}}_{k-1})}$

Similarly the measurement at the k-th timestep is dependent only upon the current state and is conditionally independent of all other states given the current state.

${\displaystyle p({\textbf {z}}_{k}\mid {\textbf {x}}_{0},\dots ,{\textbf {x}}_{k})=p({\textbf {z}}_{k}\mid {\textbf {x}}_{k})}$

Using these assumptions the probability distribution over all states of the hidden Markov model can be written simply as:

${\displaystyle p({\textbf {x}}_{0},\dots ,{\textbf {x}}_{k},{\textbf {z}}_{1},\dots ,{\textbf {z}}_{k})=p({\textbf {x}}_{0})\prod _{i=1}^{k}p({\textbf {z}}_{i}\mid {\textbf {x}}_{i})p({\textbf {x}}_{i}\mid {\textbf {x}}_{i-1})}$

However, when the Kalman filter is used to estimate the state x, the probability distribution of interest is that associated with the current states conditioned on the measurements up to the current timestep. This is achieved by marginalizing out the previous states and dividing by the probability of the measurement set.

This leads to the predict and update steps of the Kalman filter written probabilistically. The probability distribution associated with the predicted state is the sum (integral) of the products of the probability distribution associated with the transition from the (k − 1)-th timestep to the k-th and the probability distribution associated with the previous state, over all possible ${\displaystyle x_{k-1}}$.

${\displaystyle p({\textbf {x}}_{k}\mid {\textbf {Z}}_{k-1})=\int p({\textbf {x}}_{k}\mid {\textbf {x}}_{k-1})p({\textbf {x}}_{k-1}\mid {\textbf {Z}}_{k-1})\,d{\textbf {x}}_{k-1}}$

The measurement set up to time t is

${\displaystyle {\textbf {Z}}_{t}=\left\{{\textbf {z}}_{1},\dots ,{\textbf {z}}_{t}\right\}}$

The probability distribution of the update is proportional to the product of the measurement likelihood and the predicted state.

${\displaystyle p({\textbf {x}}_{k}\mid {\textbf {Z}}_{k})={\frac {p({\textbf {z}}_{k}\mid {\textbf {x}}_{k})p({\textbf {x}}_{k}\mid {\textbf {Z}}_{k-1})}{p({\textbf {z}}_{k}\mid {\textbf {Z}}_{k-1})}}}$

The denominator

${\displaystyle p({\textbf {z}}_{k}\mid {\textbf {Z}}_{k-1})=\int p({\textbf {z}}_{k}\mid {\textbf {x}}_{k})p({\textbf {x}}_{k}\mid {\textbf {Z}}_{k-1})d{\textbf {x}}_{k}}$

is a normalization term.

The remaining probability density functions are

${\displaystyle p({\textbf {x}}_{k}\mid {\textbf {x}}_{k-1})={\mathcal {N}}({\textbf {F}}_{k}{\textbf {x}}_{k-1},{\textbf {Q}}_{k})}$
${\displaystyle p({\textbf {z}}_{k}\mid {\textbf {x}}_{k})={\mathcal {N}}({\textbf {H}}_{k}{\textbf {x}}_{k},{\textbf {R}}_{k})}$
${\displaystyle p({\textbf {x}}_{k-1}\mid {\textbf {Z}}_{k-1})={\mathcal {N}}({\hat {\textbf {x}}}_{k-1},{\textbf {P}}_{k-1})}$

Note that the PDF at the previous timestep is inductively assumed to be the estimated state and covariance. This is justified because, as an optimal estimator, the Kalman filter makes best use of the measurements, therefore the PDF for ${\displaystyle \mathbf {x} _{k}}$ given the measurements ${\displaystyle \mathbf {Z} _{k}}$ is the Kalman filter estimate.

## Information filter

In the information filter, or inverse covariance filter, the estimated covariance and estimated state are replaced by the information matrix and information vector respectively. These are defined as:

${\displaystyle {\textbf {Y}}_{k\mid k}={\textbf {P}}_{k\mid k}^{-1}}$
${\displaystyle {\hat {\textbf {y}}}_{k\mid k}={\textbf {P}}_{k\mid k}^{-1}{\hat {\textbf {x}}}_{k\mid k}}$

Similarly the predicted covariance and state have equivalent information forms, defined as:

${\displaystyle {\textbf {Y}}_{k\mid k-1}={\textbf {P}}_{k\mid k-1}^{-1}}$
${\displaystyle {\hat {\textbf {y}}}_{k\mid k-1}={\textbf {P}}_{k\mid k-1}^{-1}{\hat {\textbf {x}}}_{k\mid k-1}}$

as have the measurement covariance and measurement vector, which are defined as:

${\displaystyle {\textbf {I}}_{k}={\textbf {H}}_{k}^{\text{T}}{\textbf {R}}_{k}^{-1}{\textbf {H}}_{k}}$
${\displaystyle {\textbf {i}}_{k}={\textbf {H}}_{k}^{\text{T}}{\textbf {R}}_{k}^{-1}{\textbf {z}}_{k}}$

The information update now becomes a trivial sum.

${\displaystyle {\textbf {Y}}_{k\mid k}={\textbf {Y}}_{k\mid k-1}+{\textbf {I}}_{k}}$
${\displaystyle {\hat {\textbf {y}}}_{k\mid k}={\hat {\textbf {y}}}_{k\mid k-1}+{\textbf {i}}_{k}}$

The main advantage of the information filter is that N measurements can be filtered at each timestep simply by summing their information matrices and vectors.

${\displaystyle {\textbf {Y}}_{k\mid k}={\textbf {Y}}_{k\mid k-1}+\sum _{j=1}^{N}{\textbf {I}}_{k,j}}$
${\displaystyle {\hat {\textbf {y}}}_{k\mid k}={\hat {\textbf {y}}}_{k\mid k-1}+\sum _{j=1}^{N}{\textbf {i}}_{k,j}}$

To predict the information filter the information matrix and vector can be converted back to their state space equivalents, or alternatively the information space prediction can be used.

${\displaystyle {\textbf {M}}_{k}=[{\textbf {F}}_{k}^{-1}]^{\text{T}}{\textbf {Y}}_{k-1\mid k-1}{\textbf {F}}_{k}^{-1}}$
${\displaystyle {\textbf {C}}_{k}={\textbf {M}}_{k}[{\textbf {M}}_{k}+{\textbf {Q}}_{k}^{-1}]^{-1}}$
${\displaystyle {\textbf {L}}_{k}=I-{\textbf {C}}_{k}}$
${\displaystyle {\textbf {Y}}_{k\mid k-1}={\textbf {L}}_{k}{\textbf {M}}_{k}{\textbf {L}}_{k}^{\text{T}}+{\textbf {C}}_{k}{\textbf {Q}}_{k}^{-1}{\textbf {C}}_{k}^{\text{T}}}$
${\displaystyle {\hat {\textbf {y}}}_{k\mid k-1}={\textbf {L}}_{k}[{\textbf {F}}_{k}^{-1}]^{\text{T}}{\hat {\textbf {y}}}_{k-1\mid k-1}}$

Note that if F and Q are time invariant these values can be cached. Note also that F and Q need to be invertible.

## Fixed-lag smoother

The optimal fixed-lag smoother provides the optimal estimate of ${\displaystyle {\hat {\textbf {x}}}_{k-N\mid k}}$ for a given fixed-lag ${\displaystyle N}$ using the measurements from ${\displaystyle {\textbf {z}}_{1}}$ to ${\displaystyle {\textbf {z}}_{k}}$. It can be derived using the previous theory via an augmented state, and the main equation of the filter is the following:

${\displaystyle {\begin{bmatrix}{\hat {\textbf {x}}}_{t\mid t}\\{\hat {\textbf {x}}}_{t-1\mid t}\\\vdots \\{\hat {\textbf {x}}}_{t-N+1\mid t}\\\end{bmatrix}}={\begin{bmatrix}{\textbf {I}}\\0\\\vdots \\0\\\end{bmatrix}}{\hat {\textbf {x}}}_{t\mid t-1}+{\begin{bmatrix}0&\ldots &0\\{\textbf {I}}&0&\vdots \\\vdots &\ddots &\vdots \\0&\ldots &I\\\end{bmatrix}}{\begin{bmatrix}{\hat {\textbf {x}}}_{t-1\mid t-1}\\{\hat {\textbf {x}}}_{t-2\mid t-1}\\\vdots \\{\hat {\textbf {x}}}_{t-N+1\mid t-1}\\\end{bmatrix}}+{\begin{bmatrix}{\textbf {K}}^{(0)}\\{\textbf {K}}^{(1)}\\\vdots \\{\textbf {K}}^{(N-1)}\\\end{bmatrix}}{\textbf {y}}_{t\mid t-1}}$

where:

${\displaystyle {\textbf {K}}^{(i)}={\textbf {P}}^{(i)}{\textbf {H}}^{T}\left[{\textbf {H}}{\textbf {P}}{\textbf {H}}^{\mathrm {T} }+{\textbf {R}}\right]^{-1}}$
and
${\displaystyle {\textbf {P}}^{(i)}={\textbf {P}}\left[\left[{\textbf {F}}-{\textbf {K}}{\textbf {H}}\right]^{T}\right]^{i}}$
where ${\displaystyle {\textbf {P}}}$ and ${\displaystyle {\textbf {K}}}$ are the prediction error covariance and the gains of the standard Kalman filter (i.e., ${\displaystyle {\textbf {P}}_{t\mid t-1}}$).

If the estimation error covariance is defined so that

${\displaystyle {\textbf {P}}_{i}:=E\left[\left({\textbf {x}}_{t-i}-{\hat {\textbf {x}}}_{t-i\mid t}\right)^{*}\left({\textbf {x}}_{t-i}-{\hat {\textbf {x}}}_{t-i\mid t}\right)\mid z_{1}\ldots z_{t}\right],}$

then we have that the improvement on the estimation of ${\displaystyle {\textbf {x}}_{t-i}}$ is given by:

${\displaystyle {\textbf {P}}-{\textbf {P}}_{i}=\sum _{j=0}^{i}\left[{\textbf {P}}^{(j)}{\textbf {H}}^{T}\left[{\textbf {H}}{\textbf {P}}{\textbf {H}}^{\mathrm {T} }+{\textbf {R}}\right]^{-1}{\textbf {H}}\left({\textbf {P}}^{(i)}\right)^{\mathrm {T} }\right]}$

## Fixed-interval smoothers

The optimal fixed-interval smoother provides the optimal estimate of ${\displaystyle {\hat {\textbf {x}}}_{k\mid n}}$ (${\displaystyle k) using the measurements from a fixed interval ${\displaystyle {\textbf {z}}_{1}}$ to ${\displaystyle {\textbf {z}}_{n}}$. This is also called "Kalman Smoothing". There are several smoothing algorithms in common use.

### Rauch–Tung–Striebel

The Rauch–Tung–Striebel (RTS) smoother is an efficient two-pass algorithm for fixed interval smoothing.[27]

The forward pass is the same as the regular Kalman filter algorithm. These filtered state estimates ${\displaystyle {\hat {\textbf {x}}}_{k\mid k}}$ and covariances ${\displaystyle {\textbf {P}}_{k\mid k}}$ are saved for use in the backwards pass.

In the backwards pass, we compute the smoothed state estimates ${\displaystyle {\hat {\textbf {x}}}_{k\mid n}}$ and covariances ${\displaystyle {\textbf {P}}_{k\mid n}}$. We start at the last time step and proceed backwards in time using the following recursive equations:

${\displaystyle {\hat {\textbf {x}}}_{k\mid n}={\hat {\textbf {x}}}_{k\mid k}+{\textbf {C}}_{k}({\hat {\textbf {x}}}_{k+1\mid n}-{\hat {\textbf {x}}}_{k+1\mid k})}$
${\displaystyle {\textbf {P}}_{k\mid n}={\textbf {P}}_{k\mid k}+{\textbf {C}}_{k}({\textbf {P}}_{k+1\mid n}-{\textbf {P}}_{k+1\mid k}){\textbf {C}}_{k}^{\mathrm {T} }}$

where

${\displaystyle {\textbf {C}}_{k}={\textbf {P}}_{k\mid k}{\textbf {F}}_{k}^{\mathrm {T} }{\textbf {P}}_{k+1\mid k}^{-1}}$

### Modified Bryson–Frazier smoother

An alternative to the RTS algorithm is the modified Bryson–Frazier (MBF) fixed interval smoother developed by Bierman.[22] This also uses a backward pass that processes data saved from the Kalman filter forward pass. The equations for the backward pass involve the recursive computation of data which are used at each observation time to compute the smoothed state and covariance.

The recursive equations are

${\displaystyle {\tilde {\Lambda }}_{k}={\textbf {H}}_{k}^{T}{\textbf {S}}_{k}^{-1}{\textbf {H}}_{k}+{\hat {\textbf {C}}}_{k}^{T}{\hat {\Lambda }}_{k}{\hat {\textbf {C}}}_{k}}$
${\displaystyle {\hat {\Lambda }}_{k-1}={\textbf {F}}_{k}^{T}{\tilde {\Lambda }}_{k}{\textbf {F}}_{k}}$
${\displaystyle {\hat {\Lambda }}_{n}=0}$
${\displaystyle {\tilde {\lambda }}_{k}=-{\textbf {H}}_{k}^{T}{\textbf {S}}_{k}^{-1}{\textbf {y}}_{k}+{\hat {\textbf {C}}}_{k}^{T}{\hat {\lambda }}_{k}}$
${\displaystyle {\hat {\lambda }}_{k-1}={\textbf {F}}_{k}^{T}{\tilde {\lambda }}_{k}}$
${\displaystyle {\hat {\lambda }}_{n}=0}$

where ${\displaystyle {\textbf {S}}_{k}}$ is the residual covariance and ${\displaystyle {\hat {\textbf {C}}}_{k}={\textbf {I}}-{\textbf {K}}_{k}{\textbf {H}}_{k}}$. The smoothed state and covariance can then be found by substitution in the equations

${\displaystyle {\textbf {P}}_{k\mid n}={\textbf {P}}_{k\mid k}-{\textbf {P}}_{k\mid k}{\hat {\Lambda }}_{k}{\textbf {P}}_{k\mid k}}$
${\displaystyle {\textbf {x}}_{k\mid n}={\textbf {x}}_{k\mid k}-{\textbf {P}}_{k\mid k}{\hat {\lambda }}_{k}}$

or