## What Is the Kalman Filter?

### Standard Kalman Filter

In the state-space model framework, the Kalman filter estimates the values of a latent, linear, stochastic, dynamic process based on possibly mismeasured observations. Given distribution assumptions on the uncertainty, the Kalman filter also estimates model parameters via maximum likelihood.

Starting with initial values for states (x0|0), the initial state variance-covariance matrix (P0|0), and initial values for all unknown parameters (θ0), the simple Kalman filter:

1. Estimates, for t = 1,...,T:

1. The 1-step-ahead vector of state forecasts vector for period t (${\stackrel{^}{x}}_{t|t-1}$) and its variance-covariance matrix (${P}_{t|t-1}$)

2. The 1-step-ahead vector of observation forecasts for period t (${\stackrel{^}{y}}_{t|t-1}$) and its estimated variance-covariance matrix (${V}_{t|t-1}$)

3. The filtered states for period t (${\stackrel{^}{x}}_{t|t}$) and its estimated variance-covariance matrix (${P}_{t|t}$)

2. Feeds the forecasted and filtered estimates into the data likelihood function

`$\mathrm{ln}p\left({y}_{T},...,{y}_{1}\right)=\sum _{t=1}^{T}\mathrm{ln}\varphi \left({y}_{t};{\stackrel{^}{y}}_{t|t-1},{V}_{t|t-1}\right),$`

where $\varphi \left({y}_{t};{\stackrel{^}{y}}_{t|t-1},{V}_{t|t-1}\right)$ is the multivariate normal probability density function with mean ${\stackrel{^}{y}}_{t|t-1}$ and variance ${V}_{t|t-1}$.

3. Feeds this procedure into an optimizer to maximize the likelihood with respect to the model parameters.

### State Forecasts

s-step-ahead, state forecasts are estimates of the states at period t using all information (for example, the observed responses) up to period ts.

The mt-by-1 vector of 1-step-ahead, state forecasts at period t is ${x}_{t|t-1}=E\left({x}_{t}|{y}_{t-1},...,{y}_{1}\right)$. The estimated vector of state forecasts is

`${\stackrel{^}{x}}_{t|t-1}={A}_{t}{\stackrel{^}{x}}_{t-1|t-1},$`

where ${\stackrel{^}{x}}_{t-1|t-1}$ is the mt – 1-by-1 filtered state vector at period t – 1.

At period t, the 1-step-ahead, state forecasts have the variance-covariance matrix

`${P}_{t|t-1}={A}_{t}{P}_{t-1|t-1}{A}_{t}^{\prime }+{B}_{t}{B}_{t}^{\prime },$`

where${P}_{t-1|t-1}$ is the estimated variance-covariance matrix of the filtered states at period t – 1, given all information up to period t – 1.

The corresponding 1-step-ahead forecasted observation is ${\stackrel{^}{y}}_{t|t-1}={C}_{t}{\stackrel{^}{x}}_{t|t-1},$, and its variance-covariance matrix is ${V}_{t|t-1}=Var\left({y}_{t}|{y}_{t-1},...,{y}_{1}\right)={C}_{t}{P}_{t|t-1}{C}_{t}^{\prime }+{D}_{t}{D}_{t}^{\prime }.$

In general, the s-step-ahead, forecasted state vector is ${x}_{t|t-s}=E\left({x}_{t}|{y}_{t-s},...,{y}_{1}\right)$. The s-step-ahead, vector of state forecasts is

`${\stackrel{^}{x}}_{t+s|t}=\left(\prod _{j=t+1}^{t+s}{A}_{j}\right){x}_{t|t}$`

and the s-step-ahead, forecasted observation vector is

`${\stackrel{^}{y}}_{t+s|t}={C}_{t+s}{\stackrel{^}{x}}_{t+s|t}.$`

### Filtered States

State forecasts at period t, updated using all information (for example the observed responses) up to period t.

The mt-by-1 vector of filtered states at period t is ${x}_{t|t}=E\left({x}_{t}|{y}_{t},...,{y}_{1}\right)$. The estimated vector of filtered states is

`${\stackrel{^}{x}}_{t|t}={\stackrel{^}{x}}_{t|t-1}+{K}_{t}{\stackrel{^}{\epsilon }}_{t},$`

where:

• ${\stackrel{^}{x}}_{t|t-1}$ is the vector of state forecasts at period t using the observed responses from periods 1 through t – 1.

• Kt is the mt-by-ht raw Kalman gain matrix for period t.

• ${\stackrel{^}{\epsilon }}_{t}={y}_{t}-{C}_{t}{\stackrel{^}{x}}_{t|t-1}$ is the ht-by-1 vector of estimated observation innovations at period t.

In other words, the filtered states at period t are the forecasted states at period t plus an adjustment based on the trustworthiness of the observation. Trustworthy observations have very little corresponding observation innovation variance (for example, the maximum eigenvalue of DtDt is relatively small). Consequently, for a given estimated observation innovation, the term ${K}_{t}{\stackrel{^}{\epsilon }}_{t}$ has a higher impact on the values of the filtered states than untrustworthy observations.

At period t, the filtered states have variance-covariance matrix

`${P}_{t|t}={P}_{t|t-1}-{K}_{t}{C}_{t}{P}_{t|t-1},$`

where ${P}_{t|t-1}$ is the estimated variance-covariance matrix of the state forecasts at period t, given all information up to period t – 1.

### Smoothed States

Smoothed states are estimated states at period t, which are updated using all available information (for example, all of the observed responses).

The mt-by-1 vector of smoothed states at period t is ${x}_{t|T}=E\left({x}_{t}|{y}_{T},...,{y}_{1}\right)$. The estimated vector of smoothed states is

`${\stackrel{^}{x}}_{t|T}={\stackrel{^}{x}}_{t|t-1}+{P}_{t|t-1}{r}_{t},$`

where:

• ${\stackrel{^}{x}}_{t|t-1}$ are the state forecasts at period t using the observed responses from periods 1 to t – 1.

• ${P}_{t|t-1}$ is the estimated variance-covariance matrix of the state forecasts, given all information up to period t – 1.

• ${r}_{t}=\sum _{s=t}^{T}\left\{\left[\prod _{j=t}^{s-1}\left({A}_{t}-{K}_{t}{C}_{t}\right)\right]{C}_{s}^{\prime }{V}_{s|s-1}^{-1}{\nu }_{s}\right\},$ where,

• Kt is the mt-by-ht raw Kalman gain matrix for period t.

• ${V}_{t|t-1}={C}_{t}{P}_{t|t-1}{C}_{t}^{\prime }+{D}_{t}{D}_{t}^{\prime }$, which is the estimated variance-covariance matrix of the forecasted observations.

• ${\nu }_{t}={y}_{t}-{\stackrel{^}{y}}_{t|t-1}$, which is the difference between the observation and its forecast at period t.

### Smoothed State Disturbances

Smoothed state disturbances are estimated, state disturbances at period t, which are updated using all available information (for example, all of the observed responses).

The kt-by-1 vector of smoothed, state disturbances at period t is ${u}_{t|T}=E\left({u}_{t}|{y}_{T},...,{y}_{1}\right)$. The estimated vector of smoothed, state disturbances is

`${\stackrel{^}{u}}_{t|T}={B}_{t}^{\prime }{r}_{t},$`

where rt is the variable in the formula to estimate the smoothed states.

At period t, the smoothed state disturbances have variance-covariance matrix

`${U}_{t|T}=I-{B}_{t}^{\prime }{N}_{t}{B}_{t},$`

where Nt is the variable in the formula to estimate the variance-covariance matrix of the smoothed states.

The software computes smoothed estimates using backward recursion of the Kalman filter.

### Forecasted Observations

s-step-ahead, forecasted observations are estimates of the observations at period t using all information (for example, the observed responses) up to period ts.

The nt-by-1 vector of 1-step-ahead, forecasted observations at period t is ${y}_{t|t-1}=E\left({y}_{t}|{y}_{t-1},...,{y}_{1}\right)$. The estimated vector of forecasted observations is

`${\stackrel{^}{y}}_{t|t-1}={C}_{t}{\stackrel{^}{x}}_{t|t-1},$`

where ${\stackrel{^}{x}}_{t|t-1}$ is the mt-by-1 estimated vector of state forecasts at period t.

At period t, the 1-step-ahead, forecasted observations have variance-covariance matrix

`${V}_{t|t-1}=Var\left({y}_{t}|{y}_{t-1},...,{y}_{1}\right)={C}_{t}{P}_{t|t-1}{C}_{t}^{\prime }+{D}_{t}{D}_{t}^{\prime }.$`

where ${P}_{t|t-1}$ is the estimated variance-covariance matrix of the state forecasts at period t, given all information up to period t – 1.

In general, the s-step-ahead, vector of state forecasts is ${x}_{t|t-s}=E\left({x}_{t}|{y}_{t-s},...,{y}_{1}\right)$. The s-step-ahead, forecasted observation vector is

`${\stackrel{^}{y}}_{t+s|t}={C}_{t+s}{\stackrel{^}{x}}_{t+s|t}.$`

### Smoothed Observation Innovations

Smoothed observation innovations are estimated, observation innovations at period t, which are updated using all available information (for example, all of the observed responses).

The ht-by-1 vector of smoothed, observation innovations at period t is ${\epsilon }_{t|T}=E\left({\epsilon }_{t}|{y}_{T},...,{y}_{1}\right)$. The estimated vector of smoothed, observation innovations is

`${\stackrel{^}{\epsilon }}_{t}={D}_{t}^{\prime }{V}_{t|t-1}^{-1}{\nu }_{t}-{D}_{t}^{\prime }{K}_{t}^{\prime }{r}_{t+1},$`

where:

• rt and νt are the variables in the formula to estimate the smoothed states.

• Kt is the mt-by-ht raw Kalman gain matrix for period t.

• ${V}_{t|t-1}={C}_{t}{P}_{t|t-1}{C}_{t}^{\prime }+{D}_{t}{D}_{t}^{\prime }$, which is the estimated variance-covariance matrix of the forecasted observations.

At period t, the smoothed observation innovations have variance-covariance matrix

`${E}_{t|T}=I-{D}_{t}^{\prime }\left({V}_{t|t-1}^{-1}-{K}_{t}^{\prime }{N}_{t+1}{K}_{t}\right){D}_{t}.$`

The software computes smoothed estimates using backward recursion of the Kalman filter.

### Kalman Gain

• The raw Kalman gain is a matrix that indicates how much to weigh the observations during recursions of the Kalman filter.

The raw Kalman gain is an mt -by-ht matrix computed using

`${K}_{t}={P}_{t|t-1}{C}_{t}^{\prime }{\left({C}_{t}{P}_{t|t-1}{C}_{t}^{\prime }+{D}_{t}{D}_{t}^{\prime }\right)}^{-1},$`

where ${P}_{t|t-1}$ is the estimated variance-covariance matrix of the state forecasts, given all information up to period t – 1.

The value of the raw Kalman gain determines how much weight to put on the observations. For a given estimated observation innovation, if the maximum eigenvalue of DtDt is relatively small, then the raw Kalman gain imparts a relatively large weight on the observations. If the maximum eigenvalue of DtDt is relatively large, then the raw Kalman gain imparts a relatively small weight on the observations. Consequently, the filtered states at period t are close to the corresponding state forecasts.

• Consider obtaining the 1-step-ahead state forecasts for period t + 1 using all information up to period t. The adjusted Kalman gain (${K}_{adj,t}$) is the amount of weight put on the estimated observation innovation for period t (${\stackrel{^}{\epsilon }}_{t}$) as compared to the 2-step-ahead state forecast (${\stackrel{^}{x}}_{t+1|t-1}$).

That is,

`${\stackrel{^}{x}}_{t+1|t}={A}_{t}{\stackrel{^}{x}}_{t|t}={A}_{t}{\stackrel{^}{x}}_{t|t-1}+{A}_{t}{K}_{t}{\stackrel{^}{\epsilon }}_{t}={\stackrel{^}{x}}_{t+1|t-1}+{K}_{adj,t}{\stackrel{^}{\epsilon }}_{t}.$`

### Backward Recursion of the Kalman Filter

Backward recursion of the Kalman filter estimates smoothed states, state disturbances, and observation innovations.

The software estimates the smoothed values by:

1. Setting rT + 1 = 0, and NT + 1 to an mT-by-mT matrix of 0s

2. For t = T,...,1, it recursively computes:

1. rt (see Smoothed States)

2. ${\stackrel{^}{x}}_{t|T}$, which is the matrix of smoothed states

3. Nt (see Smoothed States)

4. ${P}_{t|T}$, which is the estimated variance-covariance matrix of the smoothed states

5. ${\stackrel{^}{u}}_{t|T}$, which is the matrix of smoothed state disturbances

6. ${U}_{t|T}$, which is the estimated variance-covariance matrix of the smoothed state disturbances

7. ${\stackrel{^}{\epsilon }}_{t|T}$, which is the matrix of smoothed observation innovations

8. ${E}_{t|T}$, which is the estimated variance-covariance matrix of the smoothed observation innovations

### Diffuse Kalman Filter

Consider a state-space model written so that the m diffuse states (xd) are segregated from the n stationary states (xs). That is, the moments of the initial distributions are

`${\mu }_{0}=\left[\begin{array}{c}{\mu }_{d0}\\ {\mu }_{s0}\end{array}\right]\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{and}\text{\hspace{0.17em}}\text{\hspace{0.17em}}{\Sigma }_{0}=\left[\begin{array}{cc}{\Sigma }_{d0}& 0\\ 0& {\Sigma }_{s0}\end{array}\right].$`

• μd0 is an m-vector of zeros

• μs0 is an n-vector of real numbers

• Σd0 = κIm, where Im is the m-by-m identity matrix and κ is a positive real number.

• Σs0 is an n-by-n positive definite matrix.

• The diffuse states are uncorrelated with each other and the stationary states.

One way to analyze such a model is to set κ to a relatively large, positive real number, and then implement the standard Kalman filter (see `ssm`). This treatment is an approximation to an analysis that treats the diffuse states as if their initial state covariance approaches infinity.

The diffuse Kalman filter or exact-initial Kalman filter  treats the diffuse states by taking κ to ∞. The diffuse Kalman filter filters in two stages: the first stage initializes the model so that it can subsequently be filtered using the standard Kalman filter, which is the second stage. The initialization stage mirrors the standard Kalman filter. It sets all initial filtered states to zero, and then augments that vector of initial filtered states with the identity matrix, which composes an (m + n)-by-(m + n + 1) matrix. After a sufficient number of periods, the precision matrices become nonsingular. That is, the diffuse Kalman filter uses enough periods at the beginning of the series to initialize the model. You can consider this period as presample data.

The second stage commences when the precision matrices are nonsingular. Specifically, the initialization stage returns a vector of filtered states and their precision matrix. Then, the standard Kalman filter uses those estimates and the remaining data to filter, smooth, and estimate parameters. For more details, see `dssm` and , Sec. 5.2.

 Durbin J., and S. J. Koopman. Time Series Analysis by State Space Methods. 2nd ed. Oxford: Oxford University Press, 2012.