Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

Kalman Filter: Equations, Worked Example and Python, R, Excel Recipes

Kalman filter for researchers: linear state-space assumptions, predict and update equations, one hand-computed cycle, and NumPy, R and Excel recipes.

Written and maintained by CASRAI Editorial Board

Last updated

The Kalman filter is a recursive method for estimating the hidden state of a system from noisy measurements. This guide is for researchers who need to implement it, check it by hand or report it in a paper. By the end you can write the model, run one predict-and-update cycle on paper, and reproduce the numbers in NumPy, R or Excel.

Definition: what is the Kalman filter?

The Kalman filter is a set of equations that estimates the state of a process recursively, in a way that minimizes the mean of the squared error (Welch and Bishop, 2004). It was invented in 1960 by Rudolf E. Kálmán and is used in spacecraft navigation, robot motion planning, signal processing and wireless sensor networks (Pei et al., 2019). Each cycle combines a model-based prediction with a new measurement, weighted by how uncertain each one is. It belongs to both control theory and signal processing.

Kalman filter equations: the algorithm with every symbol explained

The state-space model

Both sources start from a linear model for the true state x and the measurement z:

x(k) = F x(k−1) + B u(k−1) + w(k−1)      (process model)
z(k) = H x(k) + v(k)                      (measurement model)
Symbol Meaning Size (n states, m measurements)
x State vector, for example position and velocity n × 1
z Measurement vector m × 1
F Transition matrix: how the previous state maps to the current one without control input or noise n × n
B, u Optional control matrix and control input n × l, l × 1
H Observation matrix: which part of the state the sensor sees m × n
w, v Process noise and measurement noise, zero-mean, with covariances Q and R n × 1, m × 1
P Error covariance of the state estimate n × n
K Kalman gain n × m

Predict and update (the Kalman filter algorithm)

The filter alternates a time update (predict) and a measurement update (correct). A superscript minus marks the prior, before the measurement is used.

Predict:  x̂⁻ = F x̂ + B u
          P⁻ = F P Fᵀ + Q

Update:   K  = P⁻ Hᵀ (H P⁻ Hᵀ + R)⁻¹
          x̂  = x̂⁻ + K (z − H x̂⁻)
          P  = (I − K H) P⁻

On the right of the predict step, x̂ and P are the updated values from the previous cycle. The quantity z − H x̂⁻ is called the innovation (Pei et al., 2019) or residual (Welch and Bishop, 2004).

Notation crosswalk between the two sources

The two most-cited free introductions use different symbols for the same objects, which is a common reason copied equations do not match.

This page Pei et al. (2019) Welch and Bishop (2004)
F Ft A
P (error covariance) Σ P
Prior estimate x̂⁻ x̂t|t−1 x̂⁻k
Updated estimate x̂ x̂t|t x̂k
Q, R, H, K Qt, Rt, Ht, Kt Q, R, H, Kk

Extended Kalman filter

The extended Kalman filter (EKF) handles nonlinear models, x(k) = f(x(k−1), u) + w and z(k) = h(x(k)) + v. It computes the prior state with the model itself, x̂⁻ = f(x̂, u), linearizes f and h with Jacobians evaluated at the current estimate, and then applies the same covariance and update equations with those Jacobians in the roles of F and H. Pei et al. (2019) call the EKF and the unscented Kalman filter heuristic approaches: the EKF performs well in some applications such as navigation and GPS, and the unscented filter tends to be more robust and accurate on highly nonlinear models at a higher computational cost.

Worked example: a Kalman filter example with the arithmetic shown

These are illustrative numbers chosen for checking, not measured data. An object moves along a line. The state is position (m) and velocity (m/s), the time step is 1 s, and only position is measured.

  • F = [[1, 1], [0, 1]], H = [1, 0], Q = 0.1 × I, R = 4 (a variance, so a standard deviation of 2 m).
  • Starting estimate x̂ = (0, 10), P = diag(4, 1). First measurement z = 11.

Predict. x̂⁻ = F x̂ = (0 + 1×10, 10) = (10, 10). F P = [[4, 1], [0, 1]]; (F P) Fᵀ = [[5, 1], [1, 1]]; adding Q gives P⁻ = [[5.1, 1], [1, 1.1]].

Update.

  1. Innovation variance: H P⁻ Hᵀ + R = 5.1 + 4 = 9.1.
  2. Gain: K = P⁻ Hᵀ / 9.1 = (5.1, 1) / 9.1 = (0.5604, 0.1099).
  3. Innovation: z − H x̂⁻ = 11 − 10 = 1.
  4. Updated state: x̂ = (10 + 0.5604×1, 10 + 0.1099×1) = (10.5604, 10.1099).
  5. Updated covariance: P11 = 5.1 − 5.1²/9.1 = 2.2418; P12 = 1 − 5.1×1/9.1 = 0.4396; P22 = 1.1 − 1²/9.1 = 0.9901.

A second cycle with z = 22 gives a prior of (20.6703, 10.1099), a gain of (0.5128, 0.1741) and an updated state of (21.3522, 10.3414).

Interpretation: how to read the gain, innovation and covariance

The position variance fell from 5.1 (prediction) to 2.2418, lower than both the prediction variance and the measurement variance of 4. For the observed component the update is an inverse-variance weighted average, the same arithmetic used in inverse-variance weighting in meta-analysis: (10/5.1 + 11/4) / (1/5.1 + 1/4) = 10.5604. Velocity moved even though it was never measured, because the prediction errors in position and velocity are correlated (P12 = 1 before the update).

Quantity How to read it
Gain K near 0 The updated estimate stays close to the prediction. This happens when the prediction is far more precise than the measurement; Welch and Bishop (2004) note that K tends to zero as P⁻ tends to zero.
Gain K near 1 (scalar case) The updated estimate follows the measurement; K weights the residual more heavily as R tends to zero (Welch and Bishop, 2004).
Innovation z − H x̂⁻ The part of the measurement the prediction did not explain. It is multiplied by K and added to the prior.
Diagonal of P Estimated error variance of each state component, in squared units (P is defined as the expected value of e eᵀ for estimate error e).
Off-diagonal of P Covariance between errors of different components. It lets a measurement of one component adjust another, as velocity moved above (Pei et al., 2019, Section 6.3).

Assumptions and common mistakes

  • Linear model with additive noise. Both sources derive the filter for the linear equations above; nonlinear models need the EKF or unscented filter, which Pei et al. call heuristic.
  • Zero-mean noise, uncorrelated across steps and with the initial state (Pei et al., 2019). Welch and Bishop (2004) assume white noise with normal distributions, N(0, Q) and N(0, R).
  • Believing the noise must be Gaussian. Pei et al. call this a common misconception: the filter is the best linear unbiased estimator without it. With Gaussian noise it is as good as any unbiased nonlinear estimator; with non-Gaussian noise a nonlinear estimator may achieve lower mean squared error.
  • Choosing Q and R without a basis. R is usually measured before operation from off-line sample measurements. Q is generally harder to determine, and a simple process model can give acceptable results if enough uncertainty is injected through Q (Welch and Bishop, 2004).
  • Mixing up variance and standard deviation. Q and R are covariances (squared units). In the example above R = 4 means a 2 m standard deviation.
  • Expecting P to react to the data. In the linear filter, P and K depend only on F, H, Q, R and the starting P, not on the measured values, as the equations show.

For the matrices behind P, see the covariance formula, and for the noise distributions see normal and standard normal distribution.

How to report a Kalman filter (APA example)

Report the model matrices, noise settings, starting values and software so the run can be reproduced. A template (the numbers are the worked example above; replace them with yours):

State estimates were computed with a linear Kalman filter (Kalman, 1960) implemented in [software and version]. The model used a constant-velocity transition matrix (Δt = 1 s), a position-only observation matrix H = [1 0], measurement noise variance R = 4 m², process noise covariance Q = 0.1 I, and starting values x̂ = (0, 10) with P = diag(4, 1), following the notation of Welch and Bishop (2004). R was set from [source of the value].

Reference entries in APA style:

  • Kalman, R. E. (1960). A new approach to linear filtering and prediction problems. Journal of Basic Engineering, 82(1), 35–45. https://doi.org/10.1115/1.3662552
  • Pei, Y., Biswas, S., Fussell, D. S., & Pingali, K. (2019). An elementary introduction to Kalman filtering. Communications of the ACM, 62(11), 122–133. https://doi.org/10.1145/3363294
  • Welch, G., & Bishop, G. (2004). An introduction to the Kalman filter (TR 95-041). University of North Carolina at Chapel Hill, Department of Computer Science.

For general reporting conventions see APA statistics reporting.

How to calculate it in Python, R or Excel

Kalman filter in Python (NumPy)

import numpy as np

F = np.array([[1.0, 1.0], [0.0, 1.0]])
H = np.array([[1.0, 0.0]])
Q = 0.1 * np.eye(2)
R = np.array([[4.0]])
x = np.array([[0.0], [10.0]])
P = np.diag([4.0, 1.0])
z = np.array([[11.0]])

x_prior = F @ x
P_prior = F @ P @ F.T + Q
S = H @ P_prior @ H.T + R
K = P_prior @ H.T @ np.linalg.inv(S)
x_post = x_prior + K @ (z - H @ x_prior)
P_post = (np.eye(2) - K @ H) @ P_prior
print(x_post.ravel().round(4), P_post.round(4))
# [10.5604 10.1099] [[2.2418 0.4396] [0.4396 0.9901]]

Kalman filter in R

F = matrix(c(1, 0, 1, 1), 2, 2)   # [[1,1],[0,1]]
H = matrix(c(1, 0), 1, 2)
Q = 0.1 * diag(2)
R = matrix(4, 1, 1)
x = matrix(c(0, 10), 2, 1)
P = diag(c(4, 1))
z = 11

x_prior = F %*% x
P_prior = F %*% P %*% t(F) + Q
S = H %*% P_prior %*% t(H) + R
K = P_prior %*% t(H) %*% solve(S)
x_post = x_prior + K %*% (z - H %*% x_prior)
P_post = (diag(2) - K %*% H) %*% P_prior
round(x_post, 4); round(P_post, 4)

Kalman filter in Excel (position component only)

With the prior position in B1 (10), prior variance in B2 (5.1), R in B3 (4) and the measurement in B4 (11), the scalar update is:

K (B5):          =B2/(B2+B3)          gives 0.5604
Updated x (B6):  =B1+B5*(B4-B1)       gives 10.5604
Updated P (B7):  =(1-B5)*B2           gives 2.2418

These match the position row of the matrix example. The full two-state update needs array formulas with MMULT and MINVERSE.

FAQ

What is the Kalman filter algorithm in short?

It repeats two steps: predict the state and covariance from the model, then correct them with the measurement through the Kalman gain.

Does the Kalman filter require Gaussian noise?

No. Pei et al. (2019) call that belief a common misconception; see the assumptions above.

What is the extended Kalman filter?

A heuristic extension of the filter to nonlinear models, described in the equations section above.

How do I set Q and R?

Measure R from off-line sample measurements of the sensor; Q is harder (see the assumptions above).

Where can I find a Kalman filter tutorial, PDF or book?

Both Pei et al. (2019) and Welch and Bishop (2004) are free PDFs. The reference list of Pei et al. also includes Chui and Chen, Kalman Filtering: With Real-Time Applications (5th ed., Springer, 2017); Grewal and Andrews, Kalman Filtering: Theory and Practice with MATLAB (4th ed., Wiley-IEEE Press, 2014); and Eubank, A Kalman Filter Primer (Chapman & Hall/CRC, 2005).

Sources

  • Pei, Y., Biswas, S., Fussell, D. S., & Pingali, K. (2019). An elementary introduction to Kalman filtering. Communications of the ACM, 62(11), 122–133. Open version (arXiv update of 27 June 2019): https://arxiv.org/abs/1710.04055 ; DOI: https://doi.org/10.1145/3363294
  • Welch, G., & Bishop, G. (2004, updated 5 April 2004). An introduction to the Kalman filter (TR 95-041). University of North Carolina at Chapel Hill: https://www.cs.unc.edu/techreports/95-041.pdf
  • Kalman, R. E. (1960). A new approach to linear filtering and prediction problems. Journal of Basic Engineering, 82(1), 35–45: https://doi.org/10.1115/1.3662552

Source versions as of November 2026. This page is general reference material for research use.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Kalman Filter: Equations, Worked Example and Python, R, Excel Recipes

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →