Unit 2, lesson 16

State space models and the Kalman filter

How a state-space model links a latent state to noisy observations through a state equation and an observation equation, and how the Kalman filter uses the innovation covariance, the Kalman gain and the posterior covariance.

  • Video 16
  • Time series, volatility and filtering

The fair value of a thinly traded bond is not observed directly. Each trade reports a price equal to that value plus noise from the bid-ask spread and the size of the order, while the value itself drifts from day to day. How far should the estimate move when a new trade price arrives, and how uncertain is it afterward? This lesson writes the problem as a state-space model, derives the Kalman gain from a minimum-error update and from Gaussian conditioning, and runs two filtering steps with exact fractions.

Key terms

State-space model

Definition A linear state-space model describes a latent state xtx_t in Rn\mathbb{R}^n and an observation yty_t in Rm\mathbb{R}^m by two equations. The state equation and the observation equation are

xt=Axt−1+wt,yt=Hxt+vt,\begin{gathered} x_t = A x_{t-1} + w_t, \\[0.4em] y_t = H x_t + v_t, \end{gathered}

for t=1,2,…t = 1, 2, \dots, where the transition matrix AA is n×nn \times n, the observation matrix HH is m×nm \times n, and the process noise wtw_t and the observation noise vtv_t have mean zero and covariance matrices

Cov⁡(wt)=Q,Cov⁡(vt)=R,\begin{gathered} \operatorname{Cov}(w_t) = Q, \\[0.4em] \operatorname{Cov}(v_t) = R, \end{gathered}

which are positive semidefinite of sizes n×nn \times n and m×mm \times m. On this page the matrices are known, the initial state is x0∼N(m0,P0)x_0 \sim \mathcal{N}(m_0, P_0), the noises are Gaussian with wt∼N(0,Q)w_t \sim \mathcal{N}(0, Q) and vt∼N(0,R)v_t \sim \mathcal{N}(0, R), and x0x_0, all wtw_t and all vtv_t are mutually independent. Known matrices that change with tt work the same way date by date. A known intercept, such as a drift cc in xt=c+Axt−1+wtx_t = c + A x_{t-1} + w_t, can be carried as a separate term or by adding a constant component to the state. Correlated process and observation noise and nonlinear equations are outside this lesson.

In plain words The state equation says how the hidden quantity moves from one date to the next, and the observation equation says how a measurement relates to it. The matrix QQ measures how much the state moves unpredictably in one step, and RR measures how noisy each measurement is.

Example A constructed model for the fair value xtx_t of a bond and its trade price yty_t, both in the same price units, is the local level model with A=H=1A = H = 1,

xt=xt−1+wt,yt=xt+vt,\begin{gathered} x_t = x_{t-1} + w_t, \\[0.4em] y_t = x_t + v_t, \end{gathered}

with Q=0.25Q = 0.25 and R=1R = 1 in squared price units and x0∼N(0,1)x_0 \sim \mathcal{N}(0, 1). The fair value is a random walk, and each trade price is the fair value plus noise. A model whose state holds a level and a drift observed through one price would instead have a 2×22 \times 2 matrix AA, a 1×21 \times 2 matrix HH, a 2×22 \times 2 matrix QQ and a 1×11 \times 1 matrix RR.

Latent state

Definition The state xtx_t is latent because it is never observed. Only the observations are seen, and Ys\mathcal{Y}_s denotes the information generated by y1,…,ysy_1, \dots, y_s, with Y0\mathcal{Y}_0 containing no observations. Under the model’s probability P\mathbb{P}, estimating xtx_t from Ys\mathcal{Y}_s means computing E[xt∣Ys]\mathbb{E}[x_t \mid \mathcal{Y}_s] and the conditional covariance of its error. The problem is prediction when s<ts < t, filtering when s=ts = t and smoothing when s>ts > t. Write

mt=E[xt∣Yt],Pt=Cov⁡(xt∣Yt),mt−=E[xt∣Yt−1],Pt−=Cov⁡(xt∣Yt−1),\begin{aligned} m_t &= \mathbb{E}[x_t \mid \mathcal{Y}_t], \\[0.4em] P_t &= \operatorname{Cov}(x_t \mid \mathcal{Y}_t), \\[0.4em] m_t^- &= \mathbb{E}[x_t \mid \mathcal{Y}_{t-1}], \\[0.4em] P_t^- &= \operatorname{Cov}(x_t \mid \mathcal{Y}_{t-1}), \end{aligned}

where the superscript minus marks the prediction made before yty_t arrives, not a negative value, and m0m_0 and P0P_0 are the initial mean and covariance. Because wtw_t has mean zero and is independent of xt−1x_{t-1} and y1,…,yt−1y_1, \dots, y_{t-1} together, conditioning the state equation on Yt−1\mathcal{Y}_{t-1} gives the one-step prediction

mt−=Amt−1,Pt−=APt−1A⊤+Q.\begin{gathered} m_t^- = A m_{t-1}, \\[0.4em] P_t^- = A P_{t-1} A^{\top} + Q . \end{gathered}

The prediction error xt−mt−=A(xt−1−mt−1)+wtx_t - m_t^- = A(x_{t-1} - m_{t-1}) + w_t is the sum of two pieces that are independent given Yt−1\mathcal{Y}_{t-1}. The first, with covariance APt−1A⊤A P_{t-1} A^{\top}, carries the uncertainty already present through the dynamics, and the second adds the new process noise QQ.

In plain words A filtered estimate uses the data up to its own date, a prediction uses only earlier data, and a smoothed estimate also uses later data. A smoothed value is useful for studying the past, but treating it as known at its own date brings future information into a backtest. Between observations, the dynamics move the mean and carry the uncertainty forward, and the process noise adds more.

Example In the bond model, m0=0m_0 = 0 and P0=1P_0 = 1, so the prediction for date 11 is m1−=0m_1^- = 0 with P1−=1+0.25=1.25P_1^- = 1 + 0.25 = 1.25. With the constructed prices y1=2y_1 = 2 and y2=0y_2 = 0, worked through in the last card, the filtered estimate of the date-1 state is E[x1∣Y1]=10/9\mathbb{E}[x_1 \mid \mathcal{Y}_1] = 10/9 with variance 5/95/9, and the prediction of the date-2 state from the same information is E[x2∣Y1]=10/9\mathbb{E}[x_2 \mid \mathcal{Y}_1] = 10/9 with the larger variance 29/3629/36. Conditioning the same Gaussian model on both prices gives the smoothed estimate E[x1∣Y2]=10/13≈0.769\mathbb{E}[x_1 \mid \mathcal{Y}_2] = 10/13 \approx 0.769 with variance 5/13≈0.3855/13 \approx 0.385. Its error variance is smaller than that of the filtered estimate 10/910/9, but it was not available at date 11.

Innovation covariance

Definition The innovation at date tt is the part of the observation that the prediction did not expect,

νt=yt−Hmt−=Het+vt,\nu_t = y_t - H m_t^- = H e_t + v_t,

where et=xt−mt−e_t = x_t - m_t^- is the state prediction error. Since vtv_t is independent of ete_t, the innovation covariance and the cross-covariance of the state error with the innovation are

St=HPt−H⊤+R,Cov⁡(et,νt)=Pt−H⊤.\begin{gathered} S_t = H P_t^- H^{\top} + R, \\[0.4em] \operatorname{Cov}(e_t, \nu_t) = P_t^- H^{\top} . \end{gathered}

The matrix StS_t is m×mm \times m and positive semidefinite, and it is invertible whenever RR is positive definite. In the Gaussian model, νt\nu_t given Yt−1\mathcal{Y}_{t-1} has the distribution N(0,St)\mathcal{N}(0, S_t), and the innovations at different dates are independent. The joint density of y1,…,yTy_1, \dots, y_T is therefore the product of the N(0,St)\mathcal{N}(0, S_t) densities evaluated at ν1,…,νT\nu_1, \dots, \nu_T, which is how the likelihood of a state-space model is computed when its parameters are estimated.

In plain words The innovation is the surprise in the new measurement. Its covariance has two sources, the uncertainty about the state as seen through HH and the noise of the measurement itself. A surprise is large or small only relative to StS_t.

Example In the bond model at date 11, the predicted price is Hm1−=0H m_1^- = 0 and the observed price is y1=2y_1 = 2, so ν1=2\nu_1 = 2. The innovation variance and the cross-covariance are

S1=1.25+1=2.25,Cov⁡(e1,ν1)=1.25.\begin{gathered} S_1 = 1.25 + 1 = 2.25, \\[0.4em] \operatorname{Cov}(e_1, \nu_1) = 1.25 . \end{gathered}

Here S1S_1 is simply the variance of y1=x0+w1+v1y_1 = x_0 + w_1 + v_1, which is 1+0.25+11 + 0.25 + 1, and the standardized surprise is ν1/S1=2/1.5=4/3\nu_1/\sqrt{S_1} = 2/1.5 = 4/3. At date 22 the innovation is ν2=−10/9\nu_2 = -10/9 with S2=65/36S_2 = 65/36, a standardized surprise of about −0.827-0.827.

Kalman gain

Definition Assume StS_t is invertible. Among updates of the form mt−+Kνtm_t^- + K \nu_t with a fixed n×mn \times m matrix KK, the error et−Kνte_t - K \nu_t has covariance

C(K)=Pt−+KStK⊤−KHPt−−Pt−H⊤K⊤.\begin{aligned} C(K) &= P_t^- + K S_t K^{\top} \\[0.4em] &\quad - K H P_t^- - P_t^- H^{\top} K^{\top} . \end{aligned}

The Kalman gain is

Kt=Pt−H⊤St−1,K_t = P_t^- H^{\top} S_t^{-1},

and completing the square around it gives

C(K)=Pt−−KtStKt⊤+(K−Kt) St (K−Kt)⊤.\begin{aligned} C(K) &= P_t^- - K_t S_t K_t^{\top} \\[0.4em] &\quad + (K - K_t)\, S_t\, (K - K_t)^{\top} . \end{aligned}

The last term is positive semidefinite for every KK, so KtK_t gives the smallest error covariance in the matrix order, and in particular the smallest mean squared error tr⁡C(K)\operatorname{tr} C(K). The updated mean is

mt=mt−+Kt(yt−Hmt−).m_t = m_t^- + K_t (y_t - H m_t^-) .

In the Gaussian model, xtx_t and yty_t are jointly normal given Yt−1\mathcal{Y}_{t-1}. The conditional mean of a jointly normal vector is affine in the conditioning variable and has the smallest mean squared error among all estimates, so it coincides with this update, and mtm_t is exactly E[xt∣Yt]\mathbb{E}[x_t \mid \mathcal{Y}_t]. For a scalar state observed directly, with H=1H = 1, Pt−>0P_t^- > 0 and R>0R > 0, multiplying the prior density N(mt−,Pt−)\mathcal{N}(m_t^-, P_t^-) by the likelihood of yty_t and completing the square in the exponent gives a normal posterior with precision 1/Pt−+1/R1/P_t^- + 1/R and mean

mt=mt−/Pt−+yt/R1/Pt−+1/R=mt−+Pt−Pt−+R (yt−mt−),\begin{aligned} m_t &= \frac{m_t^-/P_t^- + y_t/R}{1/P_t^- + 1/R} \\[0.4em] &= m_t^- + \frac{P_t^-}{P_t^- + R}\,(y_t - m_t^-), \end{aligned}

so the gain Kt=Pt−/(Pt−+R)K_t = P_t^-/(P_t^- + R) is the share of the total variance that comes from the prediction. With Pt−P_t^- fixed, the gain tends to 00 as R→∞R \to \infty and to 11 as R→0R \to 0.

In plain words The gain decides how far the estimate moves toward the new measurement. It is large when the prediction is uncertain compared with the measurement, and small when the measurement is noisy compared with the prediction. It is a ratio of variances inside the model, not a judgment about how far the data deserve to be trusted outside it.

Example At date 11 of the bond model, K1=1.25/2.25=5/9K_1 = 1.25/2.25 = 5/9, so the estimate moves five ninths of the way from the prediction 00 toward the price 22, and m1=59⋅2=10/9m_1 = \tfrac59 \cdot 2 = 10/9. The precision form gives the same mean, since 1/1.25+1/1=1.81/1.25 + 1/1 = 1.8 and (0/1.25+2/1)/1.8=10/9(0/1.25 + 2/1)/1.8 = 10/9. With the same prediction, a much noisier price with R=100R = 100 would give K=1.25/101.25≈0.0123K = 1.25/101.25 \approx 0.0123, and a nearly exact price with R=0.01R = 0.01 would give K=1.25/1.26≈0.992K = 1.25/1.26 \approx 0.992.

Posterior covariance

Definition The posterior covariance is the error covariance after the update, which in the Gaussian model is Pt=Cov⁡(xt∣Yt)P_t = \operatorname{Cov}(x_t \mid \mathcal{Y}_t). With the Kalman gain,

Pt=(I−KtH) Pt−=Pt−−KtStKt⊤.\begin{aligned} P_t &= (I - K_t H)\, P_t^- \\[0.4em] &= P_t^- - K_t S_t K_t^{\top} . \end{aligned}

For an arbitrary gain KK, the error (I−KH)et−Kvt(I - KH) e_t - K v_t has the Joseph form covariance

C(K)=(I−KH) Pt−(I−KH)⊤+KRK⊤,\begin{aligned} C(K) &= (I - KH)\, P_t^- (I - KH)^{\top} \\[0.4em] &\quad + K R K^{\top}, \end{aligned}

a sum of two positive semidefinite terms, one for the prediction error that the update leaves and one for the measurement noise that it lets in. The Joseph form holds for every KK. The shorter expression (I−KH)Pt−(I - KH) P_t^- comes from substituting K=KtK = K_t, and for other gains it generally gives the wrong covariance. The Joseph form also helps keep a computed covariance symmetric and positive semidefinite under rounding. In the Gaussian model, PtP_t does not depend on the observed values, so the whole sequence P1−,P1,P2−,P2,…P_1^-, P_1, P_2^-, P_2, \dots can be computed before any data arrive. When Pt−P_t^- and RR are invertible, the precisions add,

Pt−1=(Pt−)−1+H⊤R−1H.P_t^{-1} = (P_t^-)^{-1} + H^{\top} R^{-1} H .

In plain words After the update, the uncertainty is the predicted uncertainty minus the part the measurement explained. The Joseph form tells the same story as a sum, the prediction error the gain did not remove plus the measurement noise the gain let in. When the predicted covariance is positive definite, even a noiseless measurement removes the uncertainty only in the combinations of the state that it measures.

Example At date 11 of the bond model, P1=(1−59)⋅1.25=5/9P_1 = (1 - \tfrac59) \cdot 1.25 = 5/9. The Joseph form gives the same value,

(49)2⋅1.25+(59)2⋅1=2081+2581=59,\begin{aligned} &\bigl(\tfrac49\bigr)^2 \cdot 1.25 + \bigl(\tfrac59\bigr)^2 \cdot 1 \\[0.4em] &= \tfrac{20}{81} + \tfrac{25}{81} = \tfrac59 , \end{aligned}

and so does the precision form, 1/(1/1.25+1)=5/91/(1/1.25 + 1) = 5/9. In this scalar model with H=1H = 1 the posterior variance equals KtRK_t R, which here is the gain itself because R=1R = 1. The value 5/95/9 would be the same whatever price had been observed at date 11.

Kalman filter

Definition The Kalman filter is the recursion that starts from m0m_0 and P0P_0 and, for t=1,2,…t = 1, 2, \dots, alternates a prediction and an update,

mt−=Amt−1,Pt−=APt−1A⊤+Q,St=HPt−H⊤+R,Kt=Pt−H⊤St−1,mt=mt−+Kt(yt−Hmt−),Pt=(I−KtH) Pt−.\begin{aligned} m_t^- &= A m_{t-1}, \\[0.4em] P_t^- &= A P_{t-1} A^{\top} + Q, \\[0.4em] S_t &= H P_t^- H^{\top} + R, \\[0.4em] K_t &= P_t^- H^{\top} S_t^{-1}, \\[0.4em] m_t &= m_t^- + K_t (y_t - H m_t^-), \\[0.4em] P_t &= (I - K_t H)\, P_t^- . \end{aligned}

Under the Gaussian assumptions of the state-space card, and with every StS_t invertible, induction on tt with the Gaussian update of the gain card shows that the conditional distribution of xtx_t given Yt\mathcal{Y}_t is exactly N(mt,Pt)\mathcal{N}(m_t, P_t). If x0x_0 and the noises are only mutually uncorrelated with the stated means and covariances, the same recursion gives the best estimate of xtx_t among those affine in y1,…,yty_1, \dots, y_t, with error covariance PtP_t, but the true conditional distribution need not be normal and its mean may differ from mtm_t. Correlated process and observation noise needs extra cross-covariance terms, and nonlinear models need a separate argument or an approximation.

In plain words Predict with the dynamics, compare the new measurement with what was expected, and move the estimate by the gain times the surprise. Each date uses its own gain, computed from the uncertainty just before its own observation.

Example In the bond model with the constructed prices y1=2y_1 = 2 and y2=0y_2 = 0, the two steps are

Dateyty_tmt−m_t^-Pt−P_t^-StS_tKtK_tmtm_tPtP_t
1122005/45/49/49/45/95/910/910/95/95/9
220010/910/929/3629/3665/3665/3629/6529/658/138/1329/6529/65

At date 22 the prediction keeps the mean 10/910/9, because A=1A = 1, and adds QQ to the variance, 5/9+1/4=29/365/9 + 1/4 = 29/36. The innovation is 0−10/9=−10/90 - 10/9 = -10/9, and

m2=109(1−2965)=813,P2=2936⋅3665=2965.\begin{aligned} m_2 &= \tfrac{10}{9}\bigl(1 - \tfrac{29}{65}\bigr) = \tfrac{8}{13}, \\[0.4em] P_2 &= \tfrac{29}{36} \cdot \tfrac{36}{65} = \tfrac{29}{65} . \end{aligned}

The second gain 29/65≈0.44629/65 \approx 0.446 is smaller than the first, 5/9≈0.5565/9 \approx 0.556, because the prediction before the second price is more precise than the one before the first. Conditioning the joint normal distribution of x2x_2, y1y_1 and y2y_2 directly gives the same mean 8/13≈0.6158/13 \approx 0.615 and variance 29/6529/65.

Check yourself

  1. A scalar state with A=1A = 1 and H=1H = 1 has filtered mean mt−1=1m_{t-1} = 1 and variance Pt−1=1.5P_{t-1} = 1.5. The process variance is Q=0.5Q = 0.5, the observation variance is R=2R = 2 and the next observation is yt=3y_t = 3. Find the predicted variance, the innovation variance, the gain, and the updated mean and variance.
Show the answer

The prediction keeps the mean at mt−=1m_t^- = 1 and raises the variance to Pt−=1.5+0.5=2P_t^- = 1.5 + 0.5 = 2. The innovation variance is St=2+2=4S_t = 2 + 2 = 4, so the gain is Kt=2/4=0.5K_t = 2/4 = 0.5. The innovation is 3−1=23 - 1 = 2, which gives

mt=1+0.5⋅2=2,Pt=(1−0.5)⋅2=1.\begin{aligned} m_t &= 1 + 0.5 \cdot 2 = 2, \\[0.4em] P_t &= (1 - 0.5) \cdot 2 = 1 . \end{aligned}

As a check, the precisions add, 1/2+1/2=1=1/Pt1/2 + 1/2 = 1 = 1/P_t.

  1. In the two-step bond example, a classmate reuses the first gain 5/95/9 at date 22 instead of computing K2K_2, and reports the error variance (1−59)⋅2936(1 - \tfrac59) \cdot \tfrac{29}{36}. Find the classmate’s estimate and its true error variance, and explain both mistakes.
Show the answer

With the old gain the estimate is 109+59(0−109)=4081≈0.494\tfrac{10}{9} + \tfrac59 \bigl(0 - \tfrac{10}{9}\bigr) = \tfrac{40}{81} \approx 0.494 instead of 8/13≈0.6158/13 \approx 0.615. The gain must be recomputed because the uncertainty before the second price, P2−=29/36P_2^- = 29/36, differs from the uncertainty 5/45/4 before the first. The short formula (1−K)P2−(1 - K)P_2^- holds only for the optimal gain, so the reported 29/81≈0.35829/81 \approx 0.358 is not the error variance of this estimate. The Joseph form holds for any gain and gives

(49)2⋅2936+(59)2⋅1=341729≈0.468.\begin{aligned} &\bigl(\tfrac49\bigr)^2 \cdot \tfrac{29}{36} + \bigl(\tfrac59\bigr)^2 \cdot 1 \\[0.4em] &= \tfrac{341}{729} \approx 0.468 . \end{aligned}

This exceeds the optimal 29/65≈0.44629/65 \approx 0.446 by (K−K2)2S2≈0.022(K - K_2)^2 S_2 \approx 0.022, as the completed square says. The reused gain does worse than K2K_2, while the short formula reports a number even below the optimum.

  1. Two latent fair values form the state x=(a,b)x = (a, b), with predicted mean 00 and predicted covariance P−=IP^- = I. Only the sum a+ba + b is observed, without noise, so H=[11]H = \begin{bmatrix} 1 & 1 \end{bmatrix} and R=0R = 0, and the observed sum is 33. A classmate says that a measurement without noise leaves no uncertainty. Find the gain, the updated mean and the posterior covariance, and check the claim.
Show the answer

The innovation covariance is S=HP−H⊤+R=2S = H P^- H^{\top} + R = 2, which is invertible even though R=0R = 0, so

K=P−H⊤S−1=[1/21/2].K = P^- H^{\top} S^{-1} = \begin{bmatrix} 1/2 \\ 1/2 \end{bmatrix}.

The innovation is 3−0=33 - 0 = 3, so the updated mean is (1.5,1.5)(1.5, 1.5), and the posterior covariance is

P=I−KSK⊤=[1/2−1/2−1/21/2].\begin{aligned} P &= I - K S K^{\top} \\[0.4em] &= \begin{bmatrix} 1/2 & -1/2 \\ -1/2 & 1/2 \end{bmatrix}. \end{aligned}

The claim is false. With 1=(1,1)\mathbf{1} = (1, 1) and d=(1,−1)d = (1, -1), the sum has posterior variance 1⊤P1=0\mathbf{1}^{\top} P \mathbf{1} = 0, so it is known exactly, but the difference a−ba - b has variance d⊤Pd=2d^{\top} P d = 2, the same as before the measurement, and each fair value still has variance 1/21/2. A noiseless measurement removes the uncertainty only in the combination of the state that it observes, and here the posterior covariance has rank 11.

How this lesson connects