The Unscented Kalman Filter

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.

Push a cloud through a curve

4.00

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

When the Tangent Line Lies

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.

Bend the map, watch the centre move

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.

0.40
1.00

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 cSpread σEKF gap
0 (a straight line)any0, exact
0.40.50.10
0.41.00.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.

The road

  1. The tangent: what the EKF does, and its two costs.
  2. The flip: approximate the cloud, not the curve.
  3. Sigma points: three points that carry a bell curve.
  4. The transform: push, then average (the hero's teal line).
  5. The loop: predict and update with sigma points.
  6. Showcase: bend three curves and watch the means split.
  7. When to use it: measure the bias before you switch.
  8. Tuning: what α, β and κ do.
  9. Debugging: four bugs and how each one looks.
  10. The field guide: numbers, terms, code and sources.
Why can more readings not fix the EKF's error on a sharp curve with a wide belief?

Chapter 1

The EKF Draws a Tangent

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.

Say it aloud

h of x is about h of mu, plus H times x minus mu.

h(x)
h of x the true reading for a robot at x, in metres.
≈
is about the line matches the curve only near μ.
h(μ)
h of mu the reading at the mean of the belief.
H
H the slope of h at μ: metres of range per metre of position.
(x − μ)
x minus mu how far x sits from the mean.
In plain words

Start from the reading at the mean, h(μ). Measure how far x is from μ. Add H times that distance.

With numbers

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.

Slide the tangent along the range curve

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.

2.00
2.00

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.

CostCauseEffect
The Jacobiana line needs a slopederive and code H for every model; a sign error still tracks, but off the truth
The biasa line drops the curvaturethe 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.

Both EKF costs (deriving Jacobians, a biased mean) come from one decision. Which?

Chapter 2

Approximate the Cloud, Not the Curve

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.

Random samples against three chosen points

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.

100

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.

MethodApproximatesFunction callsMean correct to
EKFthe curve, by a tangent1, plus a Jacobianfirst order: the slope only
Monte Carlothe cloud, by random samplesthousandsany order, with random error
Unscented transformthe cloud, by chosen points2n + 1second 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.

What is the flip that defines the unscented approach?

Chapter 3

Sigma Points

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.

Say it aloud

chi-zero equals mu; chi-plus and chi-minus equal mu plus or minus the square root of n plus lambda, times P.

χ0
chi-zero the centre sigma point.
μ
mu the mean of the belief: 2 here.
χ±
chi-plus, chi-minus the outer pair of sigma points.
±
plus or minus one point on each side of the mean.
n
n the number of values in the state: 1 here.
λ
lambda the spread knob: 2 here, so n + λ = 3.
P
P the variance of the belief: 4 here, a standard deviation of 2.
In plain words

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 numbers

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.

Say it aloud

W-zero equals lambda over n plus lambda; each W-i equals one over two times n plus lambda.

W0
W-zero the weight of the centre point.
n + λ
n plus lambda the same scale as in the step: 3 here.
Wi
W-i the weight of each outer point, for i from 1 to 2n.
2
two the outer points come in pairs, so the 2n outer points share the rest.
In plain words

Give the centre point the share λ / (n + λ). Split the rest evenly over the 2n outer points. The weights add to 1.

With numbers

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.

Place the sigma points

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.

2.00
4.00

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.

How many sigma points does a 3-dimensional state use, and where do they sit?

Chapter 4

The Unscented Transform

Push each sigma point through the true curve, then take the weighted mean and spread

The unscented transform has three steps:

  1. Choose the 2n + 1 sigma points and weights from the mean and the variance.
  2. Push each point through the true function f.
  3. Recover the output mean and variance from the pushed points.

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).

Say it aloud

mu-y equals the sum over i of W-i times f of chi-i.

μy
mu-y the mean of the output cloud.
Σi
the sum over i add one term for each sigma point, i from 0 to 2n.
Wi
W-i the weight of point i: 0.667 for the centre, 0.167 for each outer point.
f(χi)
f of chi-i sigma point i pushed through the true function; here, squared.
In plain words

Push each sigma point χi through f. Multiply each result by its weight Wi. Add them. The total is the output mean μy.

With numbers

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.

Push the points one at a time

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.

4.0

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).

Say it aloud

P-y equals the sum over i of W-i times y-i minus mu-y, squared.

Py
P-y the variance of the output cloud.
Σi
the sum over i one term per sigma point.
Wi
W-i the same weights as for the mean.
yi
y-i sigma point i after the push: 4, 29.86 or 2.14.
μy
mu-y the output mean from the step before: 8.
²
squared so a miss above and a miss below both count as spread.
In plain words

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.

With numbers

(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.

The belief N(2, 4) goes through y = x². The true mean is 8. Why does the EKF say 4 while the unscented transform says 8?

Chapter 5

The UKF Loop

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:

  1. Make sigma points from the mean μ and the variance P.
  2. Push each point through f: where would that state move?
  3. Take the weighted mean and variance of the moved points.
  4. Add the process noise Q to the variance.

Update, with the sensor model h:

  1. Make fresh sigma points from the predicted belief.
  2. Push each through h: what would the sensor read from there?
  3. Take the weighted mean of those readings, and their variance plus the sensor noise R.
  4. Measure how state and reading move together.
  5. Correct the mean with the gain, and shrink the variance.

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.

Say it aloud

C equals the sum over i of W-i, times chi-i minus mu, times Z-i minus z-p.

C
C the cross-covariance, in metres of state times metres of range.
Σi
the sum over i one term per sigma point.
Wi
W-i the weight of point i.
χi − μ
chi-i minus mu how far sigma point i sits from the mean.
Zi − zp
Z-i minus z-p how far its reading sits from the predicted reading.
In plain words

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.

With numbers

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).

Say it aloud

mu-plus equals mu, plus C over S, times z minus z-p.

μ+
mu-plus the mean after the reading.
μ
mu the predicted mean before the reading: 2 m.
C / S
C over S the Kalman gain K: how far to move per metre of surprise.
z − zp
z minus z-p the innovation: the actual reading minus the predicted one.
In plain words

Subtract the predicted reading zp from the actual reading z. Multiply that surprise by the gain C / S. Add the result to the mean μ.

With numbers

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²

Run one update with a range beacon

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.

4.00
0.50

Illustrative numbers: predicted belief μ = 2 m, P = 1 m², n + λ = 3. These are the numbers of the worked example.

StepEKFUKF
Predicted meanf at the mean onlyweighted mean of f at every sigma point
State and reading linkthe Jacobian H, derived by handthe cross-covariance C, measured from the points
Gain and correctionthe Kalman updatethe same Kalman update
Calculus neededJacobians of f and hnone: 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.

In the UKF update, what does the job of the EKF's Jacobian H?

Chapter 6

Bend It and Watch the Means Split

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(μ).

Bend the curve, widen the cloud

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.

0.50
0.80

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:

  1. Curvature to 0. The curve is a line, and all three marks agree.
  2. Curvature up. The yellow mark stays behind; the teal mark follows the grey truth.
  3. Spread up too. The gap grows, because a wider cloud reaches further into the bend.
  4. Spread down to 0.1 at high curvature. The gap nearly closes. The EKF survives when the belief is narrow.

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.

Say it aloud

The expected value of f of x is about f of mu, plus one half f-double-prime of mu, times P.

E[f(x)]
the expected value of f of x the true mean of the output cloud.
f(μ)
f of mu the EKF's answer: the curve at the mean.
½
one half from the second-order term of the Taylor series.
f″(μ)
f-double-prime of mu the curvature at the mean; 0 for a straight line.
P
P the variance of the input, σ squared.
In plain words

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.

With numbers

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.

On a sharp curve, the EKF's gap nearly closes when you shrink the spread. Why?

Chapter 7

When to Reach for the UKF

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.

Run the bias test

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.

1.00
1.00

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 meanEKFUKFReading
0.1 m2.237 m2.236 m2.237 mall agree: the EKF is fine
1 m2.306 m2.236 m2.307 mthe EKF is 0.07 m low
2 m2.642 m2.236 m2.712 mthe 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:

SystemCurved mapWhy the UKF
Spacecraft attitudeorientation to star and sun directions (trigonometry)hard Jacobians, tight pointing
GPS and inertial navigationposition to satellite rangeswide belief at a cold start or in an outage
Bearing-only trackingposition to an angle (atan2)sharp curve, wide belief before triangulation
Battery state of chargecharge to terminal voltagesteep 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.

  1. Is the model linear? Use the plain Kalman filter.
  2. Is the curve sharp or the belief wide, by the bias test? Use the UKF.
  3. Is the Jacobian painful or error-prone? Use the UKF.
  4. Otherwise the EKF is simpler and as good.

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.

A spacecraft fuses gyros with a star tracker. The maps are trigonometric, the Jacobians are error-prone, and after a tumble the belief is wide. Why is this a classic UKF case?

Chapter 8

Tuning Alpha, Beta and Kappa

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.

Say it aloud

lambda equals alpha squared times n plus kappa, minus n.

λ
lambda the spread knob used in the step and the weights.
α²
alpha squared the reach factor, squared; α runs from about 0.001 to 1.
n
n the number of values in the state.
κ
kappa a second scale, usually 0 or 3 − n.
In plain words

Add κ to n, multiply by α squared, and subtract n. Then n + λ = α²(n + κ), so the outer points sit α√(n + κ) standard deviations from the mean.

With numbers

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.

Turn the reach knob

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.

1.000

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)103 − n0.667
Scaled default (Wan and van der Merwe 2000)0.00120about −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 α.

What does lowering α from 1 to 0.001 do?

Chapter 9

Debugging the UKF

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.

Plant a bug

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.

BugSymptomMeasureFix
R left out of SP collapses to almost 0; the error looks fine but the band misses the truthcount the steps with the truth inside the bandadd R to S
Q = 0the filter trusts its model forever and lags when the target speeds upinnovations keep one sign for many stepsan honest Q
Flipped sign in Ceach correction pushes the wrong way; the estimate runs offC must have the sign of h's slopecheck C against a finite difference of h
P not positive definitethe Cholesky call fails, then NaN everywherethe smallest eigenvalue of P, every stepsquare-root UKF; keep P symmetric
Too many statesmisses its real-time deadline at full size2n + 1 times the cost of one model calla 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.

A UKF runs for thousands of steps, then the matrix square root fails and NaN appears. What is the likely cause and the standard fix?

Chapter 10

The Field Guide

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.

The numbers

WhatNumberWhy it matters
Sigma points for N(2, 4)2, 5.46, −1.46; weights 0.667, 0.167, 0.167they return the mean 2 and variance 4
Through y = x²true 8; UKF 8.00; EKF 4the EKF is off by exactly P
Output variancetrue 96; UKF 96.0n + λ = 3 matches a Gaussian
Range beacon, 3 m, μ = 2, σ = 2EKF 3.61 m; true 3.99 mthe tangent sits below a convex curve
One updatezp 3.70, C 0.513, S 0.782, K 0.656, μ+ 2.19, P+ 0.66the whole loop on one line
ex at μ = 0, P = 1true 1.65; UKF 1.64; rule 1.50; EKF 1.00second order and beyond
α = 0.001, κ = 0centre weight ≈ −999,999; float32 gives 7.875use float64 with tiny α
Cost2n + 1 calls: 7 for n = 3, 31 for n = 15linear in the state size

The terms

TermMeaning
BiasA shift of the mean in one direction that averaging does not remove.
JacobianThe slope of a model at the mean; the EKF's straight-line stand-in.
Sigma points2n + 1 chosen points that carry the mean and covariance of the belief.
WeightsCoefficients of the averages; the centre one can be negative.
Unscented transformPush the sigma points through the true function, then take weighted means.
Cross-covarianceHow 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 UKFA UKF that carries a square root of P to keep it positive definite.

The checklist

  1. Run the bias test at your real spread. If the EKF passes, keep the EKF.
  2. Code f and h as plain functions. No Jacobians.
  3. Start with α = 1, κ = 3 − n, β = 0 for a small state, or the scaled default in float64.
  4. Print the weights once: the mean weights add to 1.
  5. Every step: Cholesky of P succeeds, and the truth (in simulation) stays inside μ ± 2√P about 95% of the time.

The core as code

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)

Keep going

The takeaway. Do not approximate the curve. Approximate the belief with a few chosen points, and let the true curve move them.
A colleague says the UKF is an EKF that uses a few extra points. What is the correction?

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.

Sources

  1. S. J. Julier and J. K. Uhlmann, "A New Extension of the Kalman Filter to Nonlinear Systems," Proc. SPIE 3068, 1997. The unscented transform and sigma points. doi
  2. S. J. Julier and J. K. Uhlmann, "Unscented Filtering and Nonlinear Estimation," Proceedings of the IEEE 92(3), 401–422, 2004. The accuracy orders and the tutorial form. doi
  3. S. J. Julier, "The Scaled Unscented Transformation," Proc. American Control Conference, 2002. The α, β, κ form.
  4. E. A. Wan and R. van der Merwe, "The Unscented Kalman Filter for Nonlinear Estimation," Proc. IEEE AS-SPCC Symposium, 2000. The name and the filter equations used today. doi
  5. R. van der Merwe and E. A. Wan, "The Square-Root Unscented Kalman Filter for State and Parameter Estimation," Proc. IEEE ICASSP, 2001. doi
  6. J. L. Crassidis and F. L. Markley, "Unscented Filtering for Spacecraft Attitude Estimation," Journal of Guidance, Control, and Dynamics 26(4), 2003.
  7. G. L. Plett, "Sigma-point Kalman filtering for battery management systems of LiPB-based HEV battery packs," Journal of Power Sources 161, 2006.
Sensor Fusion: Classical to Modern · Lesson 8: The Unscented Kalman Filter
Next: The Information Filter · Back to Gleams