Push a belief with mean 2 and variance 4 through y = x². The true mean comes out at 8. The EKF's tangent line says 4. Three sigma points say 8.
Switch between Tangent line and Sigma points, then drag the spread. Watch which answer stays on the dashed truth. Then we build, piece by piece, the sigma points, the unscented transform and the full filter loop.
You need the EKF (Lesson 7) and what a mean and a variance are. We build the rest from zero.
Lesson 8 of 22 in Sensor Fusion: Classical to Modern. The method is from Julier and Uhlmann (1997) and Wan and van der Merwe (2000). Previous: The Extended Kalman Filter. Next: The Information Filter.
The input is a Gaussian with mean 2. The grey histogram is 2,000 evenly spaced quantiles of it, squared. The true mean of y = x² is μ² + P, exactly.
Chapter 0
See a curve move the centre of a cloud, and a straight line miss the move
A beacon on a wall reports how far away a robot is. Distance is the square root of the squared coordinates, so the map from position to reading is curved. Your belief about the robot is a cloud: a mean and a spread.
Push that cloud through a curve. Where does the centre of the result land? The EKF answers with one point. It pushes the mean through the curve and calls the result the new mean. On a straight line that is exact. On a curve it is not.
Each line carries one sample of the input cloud (left axis) to its output (right axis). Raise the curvature and watch the outputs bunch on one side.
Illustrative toy: f(x) = x + c·x² on an input with mean 0. The device draws forty evenly spaced quantiles of the input.
With this curve and an input mean of 0, the EKF says the output mean is f(0) = 0. The true mean is c times the variance. The gap grows with the curvature and with the spread:
| Curvature c | Spread σ | EKF gap |
|---|---|---|
| 0 (a straight line) | any | 0, exact |
| 0.4 | 0.5 | 0.10 |
| 0.4 | 1.0 | 0.40 |
This error is a bias: a shift of the mean in one direction. Noise averages out over many readings. A bias does not, so the filter grows sure of a wrong answer.
The failure: an EKF on a sharp curve with a wide belief. Symptom: the innovation (reading minus predicted reading) does not average to zero. Measure: average the innovations over 100 steps. Fix: the rest of this lesson.
The Unscented Kalman Filter (UKF) keeps the curve exact and approximates the cloud instead. It picks a few points, pushes each through the true curve, and reads the mean and spread off where they land.
Chapter 1
Replace the curve with a straight line at the mean, and find where the line fails
The plain Kalman filter (Lesson 5) needs straight-line models: the reading is a number times the state. A range sensor breaks this. Put a beacon 3 m to the side of a straight track. A robot at position x on the track reads the range h(x) = √(x² + 9).
The EKF replaces the curve with its tangent line at the mean: the straight line that touches the curve there with the same slope. Call the mean of the belief μ (mu). Call the slope of h at μ H, the Jacobian. For one state, H is the derivative of h.
h of x is about h of mu, plus H times x minus mu.
Start from the reading at the mean, h(μ). Measure how far x is from μ. Add H times that distance.
Beacon 3 m off the track, μ = 2 m. Then h(μ) = √13 = 3.61 m and H = μ / h(μ) = 0.555. At x = 0 the line gives 3.61 − 0.555 × 2 = 2.50 m. The true range is 3.00 m, so the line is 0.50 m low.
The grey curve is the true range. The yellow line is the EKF's tangent at μ. The blue band is μ ± σ. Drag the mean and the spread.
Illustrative geometry: a beacon 3 m to the side of a straight track. The true mean of the readings comes from 2,000 quantiles of the belief.
The range curve bends up on both sides of its lowest point, so the tangent sits below it everywhere. Every point of the cloud reads lower on the line than on the curve. For μ = 2 m and σ = 2 m, the EKF predicts a reading of 3.61 m; the true mean reading is 3.99 m.
| Cost | Cause | Effect |
|---|---|---|
| The Jacobian | a line needs a slope | derive and code H for every model; a sign error still tracks, but off the truth |
| The bias | a line drops the curvature | the mean is off by an amount that grows with curvature and spread |
The failure: a wrong Jacobian. Symptom: the filter converges, but beside the truth. Measure: compare H with a finite difference, (h(μ + 0.001) − h(μ − 0.001)) / 0.002. Fix: correct H, or stop needing it.
Chapter 2
Keep the curve exact and replace the cloud with a few points
A curve can bend in endless ways. A Gaussian belief is two things: a mean and a variance (in more dimensions, a covariance). Julier and Uhlmann put the idea this way: it is easier to approximate a distribution than a nonlinear function.
So flip the target. Keep the curve exact. Replace the cloud with a few points that have its mean and variance. Push each point through the true curve. Read the new mean and variance from where the points land.
One honest way is Monte Carlo: draw many random samples, push them all, and average. It works, but slowly. The error of the average shrinks only as one over the square root of the sample count.
Each dot is the Monte Carlo mean of N random samples of the belief (mean 2, variance 4) pushed through y = x². The device shows twenty runs. The teal mark is the answer from three sigma points.
Seeded random draws. The true mean is 8 and the true output variance is 96 (both derived in Chapter 4).
The output variance here is 96. With N = 100 samples, the average wanders by about √(96 / 100) ≈ 1.0 around the true 8. With N = 10,000 it wanders by about 0.1. Three chosen points give 8.00 every time. Think of three test shots fired at the low, middle and high muzzle angle, in place of one straight-line guess.
| Method | Approximates | Function calls | Mean correct to |
|---|---|---|---|
| EKF | the curve, by a tangent | 1, plus a Jacobian | first order: the slope only |
| Monte Carlo | the cloud, by random samples | thousands | any order, with random error |
| Unscented transform | the cloud, by chosen points | 2n + 1 | second order; third for a Gaussian input |
"Second order" means it gets the curvature term right: the term the tangent line drops. The orders in the table are from Julier and Uhlmann (2004).
The failure: too few random samples. Symptom: the mean jitters from step to step with a fixed input. Measure: run one step twice with new seeds. Fix: many more samples, or chosen points.
Chapter 3
Place 2n + 1 points that carry the mean and the variance exactly
Call the number of values in the state n. The unscented transform uses 2n + 1 sigma points: one at the mean, and a pair along each direction of the spread. One state gives 3 points. A 15-state navigation filter gives 31.
How far out does the pair sit? The step is the square root of a scaled variance. Call the variance P. A tuning number λ (lambda) sets the scale. For now, take n + λ = 3, the classic choice; Chapter 8 explains it.
chi-zero equals mu; chi-plus and chi-minus equal mu plus or minus the square root of n plus lambda, times P.
Put one point on the mean μ. Multiply the variance P by n + λ and take the square root. Step that far to each side of μ.
With μ = 2, P = 4 and n + λ = 3, the step is √12 = 3.46. The three points are 2, 5.46 and −1.46.
Each point also gets a weight: its share in the averages taken later.
W-zero equals lambda over n plus lambda; each W-i equals one over two times n plus lambda.
Give the centre point the share λ / (n + λ). Split the rest evenly over the 2n outer points. The weights add to 1.
With λ = 2 and n = 1: W0 = 2/3 = 0.667 and each outer weight is 1/6 = 0.167. The weighted mean of the points is 0.667 × 2 + 0.167 × 5.46 + 0.167 × (−1.46) = 2 = μ. The weighted spread is 2 × 0.167 × 3.46² = 4 = P.
The blue shape is the belief. Purple dots are the sigma points; the bars under them are their weights. Drag the mean and the variance, then switch to two dimensions.
Illustrative. In 2-D the covariance is P times [[1, 0.5], [0.5, 0.6]], and n + λ = 3 again (n = 2, λ = 1).
In n dimensions the step is one column of a matrix square root of (n + λ)P, usually from a Cholesky factorisation. Each column is one direction of the spread. The count grows linearly with n, not like the thousands of Monte Carlo samples.
The failure: a covariance that is not positive definite. Symptom: the Cholesky call fails or returns NaN. Measure: check the smallest eigenvalue of P each step. Fix: Chapter 9.
Chapter 4
Push each sigma point through the true curve, then take the weighted mean and spread
The unscented transform has three steps:
Test it on a case with a known answer. Push the belief with mean 2 and variance 4 through f(x) = x². For a Gaussian input, the true output mean is μ² + P = 8, and the true output variance is 4μ²P + 2P² = 96. The EKF squares the mean and says 4.
Call each pushed point yi = f(χi) (y-i). Call the output mean μy (mu-y).
mu-y equals the sum over i of W-i times f of chi-i.
Push each sigma point χi through f. Multiply each result by its weight Wi. Add them. The total is the output mean μy.
The points 2, 5.46 and −1.46 square to 4, 29.86 and 2.14. Weighted: 0.667 × 4 = 2.67, 0.167 × 29.86 = 4.98, 0.167 × 2.14 = 0.36. The sum is 8.00, the true mean. The far point at 5.46 carries the curvature that the EKF never sees.
Press Step. Each sigma point climbs the parabola, and its weighted share joins the bar on the right. The dashed line is the true mean; the yellow tick is the EKF's answer.
Input mean fixed at 2, n + λ = 3. The bar is the running sum of Wi × yi.
The variance comes back the same way. Compare each pushed point yi with the output mean. Call the output variance Py (P-y).
P-y equals the sum over i of W-i times y-i minus mu-y, squared.
Take each pushed point yi, subtract the output mean μy, and square it. Multiply by the weight Wi. Add them. The total is the output variance Py.
(4 − 8)² = 16, (29.86 − 8)² = 477.7 and (2.14 − 8)² = 34.3. Weighted: 10.67 + 79.61 + 5.72 = 96.0, the true variance.
For a parabola, any symmetric set with the right mean and variance gets the mean exactly. The variance also comes out exact here, because n + λ = 3 matches the fourth moment of a Gaussian too.
The failure: a plain average in place of the weighted one. Symptom: the mean comes out 12.0, not 8. Measure: check that the weights add to 1 and that the weighted input points return μ and P. Fix: use Wi.
Build the transform yourself. The Code Lab gives you the curve and the plot; you write the three sigma points, the mean and the variance.
Chapter 5
Run the transform twice per step: through the motion model, then through the sensor
A filter step has two halves, as in Lesson 5. The UKF puts the unscented transform into both.
Predict, with the motion model f:
Update, with the sensor model h:
The EKF used the slope H to say how a state error shows up in the reading. The UKF measures it from the points. Call the reading of sigma point i Zi = h(χi) (Z-i). Call their weighted mean zp (z-p), the predicted reading. Call C the cross-covariance: how the state and the reading vary together.
C equals the sum over i of W-i, times chi-i minus mu, times Z-i minus z-p.
For each sigma point, multiply its offset from μ by its reading's offset from zp. Weight each product by Wi and add. A positive C means a larger state gives a larger reading.
Beacon 3 m off the track, predicted belief μ = 2 m and P = 1 m². The points 2, 3.73 and 0.27 read 3.61, 4.79 and 3.01 m. So zp = 3.70 m (the EKF would say 3.61). Then C = 0.167 × 1.73 × 1.08 + 0.167 × (−1.73) × (−0.69) = 0.513.
Call S the variance of the reading: the weighted spread of the Zi, plus the sensor noise R. The ratio C / S is the Kalman gain K, the trust dial of Lesson 5. Call the actual reading z and the corrected mean μ+ (mu-plus).
mu-plus equals mu, plus C over S, times z minus z-p.
Subtract the predicted reading zp from the actual reading z. Multiply that surprise by the gain C / S. Add the result to the mean μ.
The spread of the Zi is 0.282 and R = 0.5, so S = 0.782 and K = 0.513 / 0.782 = 0.656. The beacon reads z = 4.00 m. Then μ+ = 2 + 0.656 × (4.00 − 3.70) = 2.19 m.
variance after: P+ = P − K · S · K = 1 − 0.656 × 0.782 × 0.656 = 0.66 m²
The robot moves on the bottom line; the beacon hangs 3 m above the 0 m mark. Press Step to spread the sigma points, push them through the range sensor, take the reading, and correct.
Illustrative numbers: predicted belief μ = 2 m, P = 1 m², n + λ = 3. These are the numbers of the worked example.
| Step | EKF | UKF |
|---|---|---|
| Predicted mean | f at the mean only | weighted mean of f at every sigma point |
| State and reading link | the Jacobian H, derived by hand | the cross-covariance C, measured from the points |
| Gain and correction | the Kalman update | the same Kalman update |
| Calculus needed | Jacobians of f and h | none: only calls to f and h |
The failure: R left out of S. Symptom: the gain is too large and P collapses toward 0. Measure: compare √P with the real error over a run. Fix: add R; Chapter 9 shows the collapse.
Wire the gain and the correction yourself. The Code Lab tracks a target through the same kind of range beacon for 60 steps.
Chapter 6
Drive the transform on three curves and measure how the EKF's gap grows
Pick a curve, bend it, and widen the cloud. The grey histogram is the true output. Three marks compete: the true mean, the UKF mean from 3 sigma points, and the EKF's f(μ).
Choose a curve. Drag the curvature and the spread. Purple rings are the sigma points; follow them up to the curve and across to the output axis.
Illustrative curves on an input with mean 0.6: x + c·x², (e2cx − 1) / 2c and sin(3cx) / 3c. Each becomes a straight line at c = 0. The truth comes from 2,000 quantiles of the input.
Run four experiments:
How big is the gap? Expand f around the mean to second order. Call the curvature of f at the mean f″(μ) (f-double-prime of mu): how fast the slope changes there.
The expected value of f of x is about f of mu, plus one half f-double-prime of mu, times P.
Start from the EKF's answer f(μ). Add half the curvature f″(μ) times the variance P. That added term is the EKF's bias, and the sigma points catch it.
For f(x) = x + 0.5x², the curvature is 1 everywhere, so with P = 1 the bias is 0.5, exactly. For f(x) = ex at μ = 0 with P = 1, the rule gives 1.50. The true mean is 1.65, the UKF gives 1.64 and the EKF gives 1.00.
The bias is curvature times variance. Both knobs of the device enter as one product, which is why experiments 2 and 4 disagree on the same curve.
The failure: trusting the rule on a wide cloud. Symptom: the rule's estimate misses the truth, as 1.50 against 1.65 above. Measure: compare with a dense sample. Fix: use the sigma points, which also catch part of the higher terms.
Turn the device into a curve of errors. The Code Lab sweeps the spread and plots the EKF's mean error against the UKF's.
Chapter 7
Measure the bias at your real spread before you switch filters
The UKF earns its cost in three cases: a sharp curve, a wide belief, or a Jacobian that is hard to derive. Do not guess which case you are in. Push your real belief through your real sensor model both ways and compare with a dense sample.
A target sits 2 m along a track; the beacon is b metres to the side. Set your belief's spread and the beacon offset, and read the three predicted ranges.
Illustrative: range h(x) = √(x² + b²), belief mean 2 m. The verdict line is a 0.05 m bias, a made-up tolerance; use your sensor's own noise.
With the beacon 1 m off the track, the test gives:
| Spread σ | True mean | EKF | UKF | Reading |
|---|---|---|---|---|
| 0.1 m | 2.237 m | 2.236 m | 2.237 m | all agree: the EKF is fine |
| 1 m | 2.306 m | 2.236 m | 2.307 m | the EKF is 0.07 m low |
| 2 m | 2.642 m | 2.236 m | 2.712 m | the EKF is 0.41 m low; the UKF is 0.07 m high |
The UKF is not exact at 2 m of spread, but its error is a sixth of the EKF's. The same three cases show up across fielded systems:
| System | Curved map | Why the UKF |
|---|---|---|
| Spacecraft attitude | orientation to star and sun directions (trigonometry) | hard Jacobians, tight pointing |
| GPS and inertial navigation | position to satellite ranges | wide belief at a cold start or in an outage |
| Bearing-only tracking | position to an angle (atan2) | sharp curve, wide belief before triangulation |
| Battery state of charge | charge to terminal voltage | steep curve, no direct measurement |
The price is function calls. The EKF calls each model once per step. The UKF calls it 2n + 1 times and takes a matrix square root. A 15-state navigation filter makes 31 calls per step; that is cheap unless each call is a heavy simulation.
The failure: a UKF where it buys nothing. Symptom: the same accuracy as the EKF, at a higher cost. Measure: the bias test at your real spread. Fix: drop back to the EKF.
Chapter 8
Set how far the sigma points reach, and see what the weights do
The spread knob λ comes from two numbers in the scaled unscented transform (Julier 2002). Call the reach factor α (alpha) and a second scale κ (kappa). The state size is n, as before.
lambda equals alpha squared times n plus kappa, minus n.
Add κ to n, multiply by α squared, and subtract n. Then n + λ = α²(n + κ), so the outer points sit α√(n + κ) standard deviations from the mean.
With α = 1, κ = 2 and n = 1: λ = 3 − 1 = 2, the value used so far. With α = 0.001 and κ = 0: λ = −0.999999, so n + λ = 0.000001 and the centre weight is λ / (n + λ) ≈ −999,999.
The third number, β (beta), adds 1 − α² + β to the centre weight in the variance sum only. With a small α on a Gaussian, β = 2 is the usual choice. With α = 1 and κ = 3 − n, use β = 0, so the variance weights equal the mean weights.
The belief (mean 2 m, σ = 2 m) goes through the range curve of a beacon 1 m off the track. Drag α and switch κ. Watch the points move in and the centre weight go negative.
Illustrative. The slider moves α on a log scale from 0.001 to 1. A weight below zero hangs below the axis.
No α is exact on this curve. A tiny α samples next to the mean and returns the second-order answer, 2.41 m. α = 1 with κ = 2 reaches out to ±3.46 m and returns 2.71 m. The truth is 2.64 m. α decides how much of the curve the points see.
| Starting set | α | β | κ | Centre weight, n = 1 |
|---|---|---|---|---|
| Julier and Uhlmann (1997) | 1 | 0 | 3 − n | 0.667 |
| Scaled default (Wan and van der Merwe 2000) | 0.001 | 2 | 0 | about −1,000,000 |
For n above 3, the first set has κ below 0, and its centre weight turns negative too.
The failure: huge weights in low precision. With α = 0.001, terms of about 4,000,000 cancel to give 8. Symptom: in float32 the x² example returns 7.875, not 8. Measure: rerun the step in float64. Fix: use float64, or a larger α.
Chapter 9
Plant four common bugs in a working tracker and learn how each one looks
A UKF fails in its own ways. Below, a 1-D UKF tracks a target through a range beacon for 60 steps. The target speeds up between steps 25 and 35. The band is μ ± 2√P: a healthy filter keeps the truth inside it about 95% of the time.
Choose a version of the filter. The grey dashed line is the truth, the teal line the estimate, the shaded band μ ± 2√P. Press Run to replay.
Illustrative toy with seeded noise: beacon 3 m off the track, R = 0.1 m², Q = 0.04 m², n + λ = 3. Every version sees the same readings.
| Bug | Symptom | Measure | Fix |
|---|---|---|---|
| R left out of S | P collapses to almost 0; the error looks fine but the band misses the truth | count the steps with the truth inside the band | add R to S |
| Q = 0 | the filter trusts its model forever and lags when the target speeds up | innovations keep one sign for many steps | an honest Q |
| Flipped sign in C | each correction pushes the wrong way; the estimate runs off | C must have the sign of h's slope | check C against a finite difference of h |
| P not positive definite | the Cholesky call fails, then NaN everywhere | the smallest eigenvalue of P, every step | square-root UKF; keep P symmetric |
| Too many states | misses its real-time deadline at full size | 2n + 1 times the cost of one model call | a smaller sigma set (the spherical simplex uses n + 2), or split the state |
From the device: the correct filter has an error of 0.33 m and keeps the truth in the band for 54 of 60 steps. Forgot R has an error of 0.42 m, about as good, but the band covers the truth for 0 of 60 steps. It will ignore its next surprise.
The square-root UKF (van der Merwe and Wan 2001) carries a square root of P from step to step, never P itself. So round-off cannot give P a negative variance.
Chapter 10
Keep the numbers, the terms, the checks and the core code of the lesson on one page
The EKF replaces a curve with a tangent line, which needs a Jacobian and biases the mean by about half the curvature times the variance. The UKF keeps the curve and replaces the belief with 2n + 1 sigma points. It pushes each through the true model and takes weighted means. The filter loop is the Kalman loop, with the cross-covariance in place of the Jacobian.
| What | Number | Why it matters |
|---|---|---|
| Sigma points for N(2, 4) | 2, 5.46, −1.46; weights 0.667, 0.167, 0.167 | they return the mean 2 and variance 4 |
| Through y = x² | true 8; UKF 8.00; EKF 4 | the EKF is off by exactly P |
| Output variance | true 96; UKF 96.0 | n + λ = 3 matches a Gaussian |
| Range beacon, 3 m, μ = 2, σ = 2 | EKF 3.61 m; true 3.99 m | the tangent sits below a convex curve |
| One update | zp 3.70, C 0.513, S 0.782, K 0.656, μ+ 2.19, P+ 0.66 | the whole loop on one line |
| ex at μ = 0, P = 1 | true 1.65; UKF 1.64; rule 1.50; EKF 1.00 | second order and beyond |
| α = 0.001, κ = 0 | centre weight ≈ −999,999; float32 gives 7.875 | use float64 with tiny α |
| Cost | 2n + 1 calls: 7 for n = 3, 31 for n = 15 | linear in the state size |
| Term | Meaning |
|---|---|
| Bias | A shift of the mean in one direction that averaging does not remove. |
| Jacobian | The slope of a model at the mean; the EKF's straight-line stand-in. |
| Sigma points | 2n + 1 chosen points that carry the mean and covariance of the belief. |
| Weights | Coefficients of the averages; the centre one can be negative. |
| Unscented transform | Push the sigma points through the true function, then take weighted means. |
| Cross-covariance | How the state and the predicted reading vary together; it replaces P·H. |
| α, β, κ | Reach, a variance correction for the centre point, and a second scale. |
| Square-root UKF | A UKF that carries a square root of P to keep it positive definite. |
pythonimport numpy as np def sigma_points(mu, P, lam=2.0): # 1-D state, n = 1 s = np.sqrt((1 + lam) * P) chi = np.array([mu, mu + s, mu - s]) W = np.array([lam, 0.5, 0.5]) / (1 + lam) # 0.667, 0.167, 0.167 return chi, W def ukf_update(mu, P, z, h, R): chi, W = sigma_points(mu, P) Z = h(chi) # call the TRUE sensor model zp = np.sum(W * Z) # predicted reading S = np.sum(W * (Z - zp)**2) + R # reading variance C = np.sum(W * (chi - mu) * (Z - zp)) # replaces the Jacobian K = C / S return mu + K * (z - zp), P - K * S * K chi, W = sigma_points(2.0, 4.0) print(np.sum(W * chi**2)) # 8.0, the true mean of x^2 h = lambda x: np.sqrt(x**2 + 9) # beacon 3 m off the track print(ukf_update(2.0, 1.0, 4.0, h, 0.5)) # (2.194, 0.664)
Now press Present or Teach. Explain from memory where the sigma points go, why the far one pulls the mean of x² up to 8, and what replaces the Jacobian. Then go back to the parabola, switch to the tangent line, and say why its error equals P.