A robot fixes its position from eight ranges. One wrong match drags the weighted fit 45 cm off, while its 95% ellipse claims 5 cm. A robust cost brings it back to 2 cm, inside an ellipse that holds the truth in about 95% of draws.
Pick an estimator, then add wrong matches. Watch the magnifier: the verdict says whether the purple ellipse still holds the pink truth. Then we build, piece by piece, covariance, propagation, gating, weights, robust costs and the test that catches a lying filter.
You need matrix multiply, the idea of a Gaussian and a little Python. We build the rest from zero.
Lesson 3 of the Robotics Engineering track. Before: Lesson 2, Time and Sync. Next: Lesson 4, The Sensor Suite. Lesson 1 asked which frame a number is in, Lesson 2 which instant. This lesson asks how sure.
Illustrative 12 m by 8 m room, eight beacons. Six laser ranges with σ = 3 cm, two ultrasonic ranges with σ = 40 cm, one seeded noise draw; a wrong match adds 1.6 m to a laser range. Over 2,000 seeded draws, the weighted ellipse holds the truth 95.4% of the time with no wrong match. With one, Cauchy's holds it 96.6%, plain weighting's 0%.
Chapter 0
Compare the error a filter claims with the error it makes, and turn the gap into metres of clearance
A warehouse robot drives a 1.20 m aisle. Its localisation reports a one-sigma error of 2 cm. A survey of the same run shows the true error wandering out to 40 cm. Nothing crashed and nothing diverged. The filter is confident and wrong.
The covariance is not a log line. The planner reads it: it pads every shelf by three sigma and drives in the gap that is left. A lie in the covariance becomes a clearance that is too small, in metres.
The planner pads the shelves by three times the claimed sigma (purple). Each pink ring is one pass of the robot's centre, drawn with the true sigma; red dots touched a shelf. Drag the true sigma, then make the filter honest.
Illustrative aisle: 1.20 m wide, robot 0.70 m, so 0.25 m each side. The lateral error is Gaussian with the true sigma; contact means it passes 0.25 m. The 60 passes are one seeded draw; the percentage in the read-out is exact.
With the claimed σ of 2 cm the planner pads 6 cm and sees room to spare. The chance of contact it computes is 2 × Φ(−12.5), about 7 × 10−36. With the true σ of 40 cm, a pass touches a shelf whenever the error passes 25 cm: 53% of passes. The lie is 20 times in sigma and about 1035 times in risk, because the Gaussian tail falls off exponentially.
How do you catch it? Compare each error with the sigma the filter claimed. Call the true error e and the claimed standard deviation σ (sigma).
q equals e squared over sigma squared.
Divide the error e by the sigma the filter claimed, and square the result. If the filter is honest, e is a normal draw with spread sigma, so q averages 1 over many updates.
e = 0.40 m and σ = 0.02 m: q = 0.16 / 0.0004 = 400. An honest filter passes 3.84 only 5% of the time. The square root of 400, 20, is the overconfidence in sigma.
q needs the truth, so it lives in simulation and on a motion-capture rig. Chapter 7 builds the version that needs no truth and runs on the robot in the field.
Symptom: the reported covariance shrinks to a floor and stays there, while the error against ground truth keeps growing.
Measure: on a rig run, average q over a window. It should sit near the state's dimension: 1 for one number, 2 for a 2-D position.
Fix: find which noise the model leaves out (Chapter 7 names three causes) before you touch a gain.
Chapter 1
Build a covariance from five samples, read its ellipse, and find how much of the cloud a contour holds
Five fixes of the same landmark, in metres: (2, 3), (4, 7), (6, 7), (8, 11) and (10, 12). The x values spread, the y values spread, and they rise together. One number per axis cannot say that last part.
A covariance is the average product of the errors. Subtract the mean from each sample. Average the squared x parts, the squared y parts, and the x part times the y part. Divide by N − 1, not N, because the mean came from the same samples.
| Entry | Sum of products | ÷ 4 |
|---|---|---|
| Mean | x: 30 / 5, y: 40 / 5 | (6, 8) |
| Σxx | 16 + 4 + 0 + 4 + 16 = 40 | 10 |
| Σyy | 25 + 1 + 1 + 9 + 16 = 52 | 13 |
| Σxy | 20 + 2 + 0 + 6 + 16 = 44 | 11 |
So Σ (capital sigma) = [[10, 11], [11, 13]] m². Its eigenvectors are the axes of an ellipse. The square roots of its eigenvalues are the semi-axis lengths: 4.75 m along a line 48.9° above the x axis, and 0.63 m across it. The per-axis spreads, √10 = 3.16 m and √13 = 3.61 m, hide that the landmark is 7.5 times better known across that line than along it.
800 samples of a 2-D Gaussian (pink). The purple ellipse is the covariance scaled by k. Change k and the correlation; the dashed box is the per-axis spread at the same k.
Seeded standard-normal draws mapped through the covariance's Cholesky factor, with unit variance on each axis. We count the share inside; we do not compute it.
How much of the cloud does a contour hold? Scale the one-sigma ellipse by a factor k. In two dimensions the share of samples inside, p, has a closed form.
p equals one minus e to the minus k squared over two.
Square the scale k and halve it. Take e to the minus that. One minus the result is the share p inside the ellipse.
k = 1: p = 1 − e−0.5 = 39.3%, not the 68.3% of one dimension. For 95%, solve for k: k = √(−2 ln 0.05) = 2.448, not 1.96. At 1.96 the ellipse holds 85.4%.
The drop gets steeper with dimension. The one-sigma ellipsoid holds 19.9% in 3-D, and 1.4% for a 6-D pose.
Symptom: uncertainty plots look tight, and the truth lands outside the drawn ellipse far more than 5% of the time.
Measure: on a test set with truth, count how often the truth falls inside the 95% ellipse.
Fix: scale a 2-D ellipse by 2.448 for 95%; for other dimensions use the square root of the chi-square point (table in Chapter 3).
Chapter 2
Push a covariance through a change of coordinates with the Jacobian, and see where the straight-line guess breaks
A LiDAR measures range and bearing. The map wants x and y. The uncertainty must make the same trip as the number.
Near the mean, a smooth function looks like a straight line: a small input error comes out multiplied by the function's slopes. The Jacobian J holds those slopes: row i, column j says how far output i moves when input j moves by one. Call the input covariance Σx and the output covariance Σy.
Sigma-y equals J times Sigma-x times J-transpose.
Find the slopes J of the function at the mean. Multiply the input covariance Sigma-x by J on the left and by J-transpose on the right. The result is Sigma-y.
A return at r = 10 m, bearing θ = 30°, with σr = 0.10 m and σθ = 2° = 0.0349 rad. For x = r cos θ, y = r sin θ: J = [[cos θ, −r sin θ], [sin θ, r cos θ]] = [[0.866, −5.000], [0.500, 8.660]]. Then Σy = [[0.0380, −0.0484], [−0.0484, 0.0939]] m²: σx = 0.195 m, σy = 0.306 m.
Check it against the physics. Along the beam the spread is σr = 0.10 m. Across the beam it is r × σθ = 0.349 m. The axes of Σy are exactly 0.100 m and 0.349 m: a thin sliver across the beam. Bearing error grows with range.
The blue dot is the sensor. Pink: 900 range-bearing samples pushed through the true map. Purple: the linearised 95% ellipse and its centre. Raise the bearing sigma and watch the cloud curve and the pink mean slide toward the sensor.
Seeded draws, σr = 0.10 m, bearing 30°. We count the share inside on these 900 samples. The table's shares use 200,000; its mean shifts are exact.
The straight line is a guess. For a Gaussian bearing, the true mean of r cos θ is r cos θ̄ × e−σθ²/2. Averaging over a spread of bearings pulls the point toward the sensor. The Jacobian never sees this.
| Bearing σ | Linear 95% ellipse holds | True mean moves toward sensor |
|---|---|---|
| 2° | 94.9% | 0.6 cm |
| 10° | 73.4% | 15.1 cm |
| 25° | 37.3% | 90.8 cm |
At 25° the mean moves 0.91 m along the beam, where the ellipse claims 0.10 m: a 9-sigma bias that no amount of averaging removes. Here the line is safe below about 5°. Past that, re-linearise, use sigma points (the UKF) or carry particles.
Symptom: the gate accepts everything, or rejects everything, from the first frame.
Measure: the average NIS (Chapter 7) is off by a factor near 3,283, or near 1/3,283.
Fix: (180/π)² = 3,282.8. A bearing variance in square degrees went where the Jacobian expects square radians. Find it.
Chapter 3
Measure a residual in units of its own uncertainty, and turn the distance into accept or reject
The robot predicts where a landmark should appear. A detection lands 0.36 m away. Is it the same landmark? A distance threshold cannot say. Along the uncertain direction 0.36 m is ordinary; across the sure direction it is absurd.
Two doubts add. The prediction carries the state covariance P, pushed into measurement space by the measurement Jacobian H. The sensor adds its own noise R. Their sum is the innovation covariance, S = H P HT + R. The innovation y is the detection minus the prediction.
d squared equals y-transpose times S-inverse times y.
Factor S as L times L-transpose (its Cholesky factor). Solve L times u equals y: that whitens y into u, measured in plain sigmas. d squared is the sum of the squares of u. Compare it with the gate.
S = [[0.10, 0.11], [0.11, 0.13]] m². y = (0.3, 0.2) m gives d² = 2.78: accept. y = (0.3, −0.2) m is also 0.36 m long. It gives d² = 32.1: reject, 5.7 sigmas out.
Purple: the predicted landmark. Blue: a detection, joined by the yellow innovation. The yellow ellipse is the 95% gate from S; the grey circle is a distance threshold of the same area. Swing the detection around and find where they disagree.
Illustrative S from the worked example. The round S has the same determinant, so both gates cover the same area. Gate: d² ≤ 5.991, the 95% point of chi-square with 2 degrees of freedom.
Under an honest model, u is a vector of m independent standard normals, where m is the measurement's dimension. So d² follows a chi-square distribution with m degrees of freedom, and a distance becomes a decision with a known false-reject rate.
| Measurement dimension m | 90% gate | 95% gate | 99% gate |
|---|---|---|---|
| 1: a range, an altitude | 2.706 | 3.841 | 6.635 |
| 2: a pixel (u, v), a 2-D landmark | 4.605 | 5.991 | 9.210 |
| 3: a 3-D point, a GPS fix | 6.251 | 7.815 | 11.345 |
| 6: a full relative pose | 10.645 | 12.592 | 16.812 |
Choose the rate, not the distance. A 95% gate throws away one good match in 20. With 400 candidates a frame you can afford a tight gate. With 11 in a bare corridor, gate loose and let a robust cost (Chapter 6) handle what slips through.
Symptom: the gate passes almost every match, outliers included.
Measure: check which residual the gate uses. After the update, the residual is smaller than the innovation and has a smaller covariance than S.
Fix: gate the innovation, with S, before the update.
Chapter 4
Fuse readings in proportion to how well each sensor knows the answer, and add a prior as one more sensor
Three sensors range the same beacon. Two lasers read 10.20 m and 9.80 m, each with σ = 0.10 m. A cheap ultrasonic reads 12.00 m with σ = 1.00 m. The plain average is 10.67 m: the noisy sensor got an equal vote and pulled the answer 66 cm.
Give each reading a weight equal to its information, one over its variance. VNAV Lecture 9 derives why Gaussian noise leads to this weighted squared cost; here we use the result. Call reading i zi and its weight wi = 1/σi².
x-hat equals the sum over i of w-i times z-i, divided by the sum over i of w-i.
Square each sensor's sigma and invert it to get its weight w-i. Multiply each reading z-i by its weight and add. Divide by the total weight. The variance of x-hat is one over the total weight.
Weights 100, 100 and 1. The sum of w times z is 1020 + 980 + 12 = 2012, and the total weight is 201. So x̂ = 10.010 m. Its σ is √(1/201) = 0.0705 m, better than either laser alone.
A prior is one more reading. Say the last step's estimate was 9.50 m with σ = 0.20 m, a weight of 25. Add it to both sums: x̂ = (2012 + 237.5) / 226 = 9.954 m, with σ = 0.0665 m. This is MAP estimation (maximum a posteriori), and in this form it is also the Kalman update.
With many unknowns, the weights become matrices. Call the stacked readings z, the measurement matrix H, the noise covariance R, and the prior x0 with covariance P0.
H-transpose R-inverse H plus P-nought-inverse, times x-hat, equals H-transpose R-inverse z, plus P-nought-inverse x-nought.
Add the measurement and prior information: that is the left matrix. On the right, multiply each information by its own data and add. Solve for x-hat. The inverse of the left matrix is its covariance.
The beacon again: H is a column of three ones, R = diag(0.01, 0.01, 1) and P0 = 0.04. The left side is 201 + 25 = 226 and the right side is 2012 + 237.5, so x̂ = 9.954 m, as before.
Blue bells are the readings, the faint purple bell the prior, the solid one the answer. The bar stacks each source's information. Pick an estimator, then move the sliders.
Illustrative readings from the worked example; prior mean 9.50 m.
Try the extremes. An ultrasonic at σ = 0.10 m earns an equal vote, and the answer is 10.67 m again. A prior at σ = 0.05 m weighs 400, twice both lasers, and drags the answer to 9.67 m. An over-tight prior is why an estimate "never moves from the initial guess".
Symptom: the solver converges in one step and returns the initial guess.
Measure: whiten each residual and compare the final cost, half the sum of squares, with (M − N)/2 for M residuals and N unknowns. For 2,231 residuals and 750 unknowns that is 740, not 1.4 × 105.
Fix: check the units of R. A pixel sigma on a residual in normalised image coordinates is wrong by the focal length squared: 250,000 at 500 px.
Chapter 5
Watch one bad point flip a slope, and measure how hard each residual pulls
The truth is y = 1 + 2x. Three points sit on it: (0, 1), (1, 3) and (2, 5). A fourth comes from a wrong match: (3, 0) instead of (3, 7). Least squares returns intercept 2.40 and slope −0.10. One point in four turned the slope upside down.
Read the residuals at that fit: −1.4, +0.7, +2.8 and −2.1. The largest belongs to a good point. This is masking: the outlier dragged the fit so far that a good point looks worst. Delete the biggest residual and you delete the wrong point.
Why does it happen? Describe a cost by three functions of the whitened residual r. The cost ρ(r) (rho) is what the solver adds up. The influence ψ(r) (psi), its slope, is how hard that residual pulls on the answer. The weight w(r) = ψ(r)/r is what you implement. For least squares ρ = r²/2, ψ = r and w = 1: a residual ten times larger pulls ten times harder, without limit.
Huber (1964) keeps the square near zero and goes straight past a threshold δ (delta). Its influence is ψH:
psi-H of r equals r when the size of r is at most delta, and delta times the sign of r otherwise.
Inside delta, the residual r pulls in proportion, as in least squares. Outside, the pull stops growing at delta. The weight, psi over r, is 1 inside and delta over the size of r outside.
δ = 1. At r = 0.5 the pull is 0.5 and the weight is 1. At r = 10 the pull is 1.0, not 10, and the weight is 0.1.
Cauchy goes further: ψ = r / (1 + (r/c)²), which rises to a peak at r = c and then falls back toward zero. A cost whose pull falls back is redescending.
| Residual r | Least squares ψ | Huber ψ (w), δ = 1 | Cauchy ψ (w), c = 1 |
|---|---|---|---|
| 0.5 | 0.50 | 0.50 (1.00) | 0.40 (0.80) |
| 2 | 2.00 | 1.00 (0.50) | 0.40 (0.20) |
| 10 | 10.0 | 1.00 (0.10) | 0.099 (0.0099) |
Three panels: the cost ρ, the pull ψ and the weight w. Grey is least squares, warm is Huber, purple is Cauchy. Drag the residual out to 10 and read the middle panel.
Exact formulas; the residual is in sigmas.
The trade: Huber's cost is convex, so it has exactly one minimum and any start finds it. Cauchy's is not convex, so a bad start can settle in a wrong minimum. Chapter 6 shows both sides of that trade on the four points.
Symptom: the pose jumps when one feature match changes.
Measure: that match's share of the pull, |ψ(rk)| / Σ|ψ(ri)|. Under least squares a single bad match can hold half of it (Chapter 8).
Fix: wrap residuals that come from data association in a robust kernel.
Chapter 6
Turn any robust cost into repeated weighted least squares, and see why the starting point decides the answer
Minimising the sum of ρ(ri) looks harder than least squares. It is not. At the minimum, the slope of the total cost is zero: the sum of ψ(ri) times row i of the measurement matrix is zero. Write ψ(r) as w(r) times r, and that condition becomes the weighted normal equations of Chapter 4.
Take a line fit. Call its two parameters, intercept and slope, θ (theta). Call the design matrix H: one row (1, xi) per point. Call the measured y values z and the diagonal matrix of weights W.
H-transpose W H, times theta, equals H-transpose W z, where W is the diagonal matrix of the weights w of r-i.
Start from a guess for theta. Compute each residual r-i and its weight. Solve the weighted problem for a new theta. Repeat until theta stops moving. The loop is IRLS, iteratively reweighted least squares.
The four points, Huber with δ = 1, from the least-squares start. Residuals (−1.4, 0.7, 2.8, −2.1) give weights (0.71, 1, 0.36, 0.48), and the next fit is (2.25, −0.075). The loop ends at (2.05, −0.05): still wrong.
Blue dots are the data, sized by their weight; the red one is the wrong match. Pink dashes are the truth. Choose a start and a kernel, then step the loop.
Exact IRLS on the chapter's four points, residuals in data units. We checked every number with numpy.
The start decided it. Begin from a consensus estimate instead: a RANSAC pair through (0, 1) and (1, 3) gives θ = (1, 2). Now the outlier stands alone with a residual of −7. Cauchy weights it 0.02, and in three steps the fit is (1.10, 1.85).
From the least-squares start, Cauchy is worse than Huber. It ends at slope −0.36, because good points now look far and Cauchy ignores them. A robust cost polishes a roughly right answer; it does not find one.
Set the threshold in sigmas. Whiten each residual by its own sigma first. Then Huber with δ = 1.345 and Cauchy with c = 2.385 each keep 95% of least squares' efficiency on clean Gaussian data. If the sigma is unknown, estimate it robustly as 1.4826 times the median absolute deviation (MAD) of the residuals.
| Factor | Robust kernel? | Why |
|---|---|---|
| Feature match, reprojection | Yes | Matching is where wrong answers come from. |
| Loop closure | Yes, strongly | A false one welds two places together. |
| IMU preintegration | No | It is physics. A bad one means a bad model or calibration. |
| Marginalisation prior | Never | It is the window's memory; down-weighting deletes history. |
| Gauge prior | Never | It removes a null space; switching it off brings the singularity back. |
Symptom: turning on Huber moved the answer by less than a millimetre, and someone concludes there are no outliers.
Measure: the histogram of the final weights. If every weight is above 0.9, the kernel never engaged. Refit from a RANSAC start and compare.
Fix: start from consensus, and take sigma from the sensor, not from a contaminated fit. On the four points, MAD gives δ = 2.79 and every weight stays above 0.99.
Chapter 7
Test the filter's claimed spread against its own innovations, then test whether the innovations are white
Chapter 0's test needed the truth. On a customer's robot there is none. But every update already computes the innovation y and its covariance S. If the model is honest, y is a zero-mean Gaussian with covariance S.
So whiten each innovation, as in Chapter 3. The result, the normalised innovation squared (NIS), is chi-square with m degrees of freedom and averages m. On the state error, with the truth, the same test is the NEES, normalised estimation error squared (Bar-Shalom, Li and Kirubarajan, 2001). NEES runs in simulation; NIS runs in the field.
One NIS is noisy. Average a window of N updates. A sum of N independent chi-squares with m degrees each is a chi-square with N·m degrees, so the average has a tight band.
One over N times the sum over k of y-k-transpose S-k-inverse y-k lies between chi-square at 2.5 percent of N m, over N, and chi-square at 97.5 percent of N m, over N.
Compute the NIS at each update and average the last N. Look up the 2.5% and 97.5% points of a chi-square with N times m degrees, and divide both by N. An honest filter's average lands inside that band 95% of the time.
N = 50 and m = 1: a chi-square with 50 degrees has its 2.5% and 97.5% points at 32.36 and 71.42, so the band is [0.647, 1.428]. N = 500 tightens it to [0.880, 1.128].
Top: a 1-D filter's yellow error against its purple ±3σ claim. Bottom: the average NIS and the innovations' lag-1 autocorrelation against their yellow bands. Shrink Q, or correlate the noise.
Illustrative seeded random walk: process sigma 5 cm a step, measurement sigma 20 cm, 50 updates after a 50-update burn-in. Correlated noise is AR(1) with the same variance. The rates in the text come from 400 runs.
Three causes make a filter lie. First, process noise Q modelled too small: the claimed spread shrinks, the error wanders, and NIS climbs. At 0.06 of the true Q, the average NIS over 400 runs is 1.48. A 50-update window catches it 47% of the time; a 500-update window, every time.
Second, correlated measurement noise fed in as independent. It pushes NIS down, not up, and each innovation predicts the next. The lag-1 autocorrelation ρ1 divides the sum of ykyk−1 by the sum of yk². It should sit within ±1.96/√N of zero: ±0.277 at N = 50. At a correlation of 0.5, NIS stays inside its band 83% of the time, while ρ1 flags 76% of windows. Run both tests.
Third, linearisation error. It shows only where the function bends; test it with Chapter 2's Monte Carlo push.
Symptom: the filter passes on the bench and fails on the robot only at speed.
Measure: plot the windowed NIS against the turn rate over a recorded run. A rising trend is linearisation error.
Fix: re-linearise (an iterated EKF) or use sigma points, and put the NIS band in CI separately for each operating regime.
Chapter 8
Pull one point away from a line, and read how much of the vote it keeps under each cost
Fourteen points lie near y = 1 + 2x, with noise σ = 0.5. Three fits run at once: least squares, Huber with δ = 0.7 and Cauchy with c = 1.2, about 1.345σ and 2.385σ. Under each fit, a bar shows the point's share of pull: its |ψ| over the sum of every point's |ψ|.
Pick a point, then pull it with the slider (or drag it with a mouse). Grey is least squares, warm is Huber, purple is Cauchy; pink dashes are the truth. Then change δ and c.
Illustrative seeded data. Each robust fit is IRLS from the previous fit's answer (least squares, then Huber, then Cauchy), 200 steps. The plot shows y from −10 to 25; the slider reaches a pull of 20.
On clean data each point holds about 1/14 of the pull, 7%. Pull the middle point down and the bars split. Least squares climbs to exactly 50% and locks. At a least-squares fit the residuals sum to zero, so once the pulled point is the only negative one, its share is one half. Pulled 17 down it holds 50.00%; pulled 1,000 down, still 50.00%, while the intercept slides from 1.18 to −84.5.
Huber locks lower, at 11.9%, by a pull of 1, and its fit stops moving altogether. Past δ the point's pull is the constant δ, so its y value no longer enters the equations. The intercept sits 0.04 low forever: bounded influence, permanent bias.
Cauchy rises to a peak near r = c, then gives ground: 3.6% at a pull of 8, 1.8% at 17, 0.03% at 1,000. Only the redescending cost keeps forgetting the point.
Now pick the far-end point. With the same pull of 8, least squares tilts to slope 1.69, Huber to 1.96, Cauchy to 1.99. A point far out in x has leverage: it can bend the fit toward itself and keep its own residual small, and Huber's cap does not stop that.
Symptom: the robust fit jitters on clean data and its reported covariance grows.
Measure: the effective sample size, (Σw)² / Σw², over the number of points. Below about 0.5, the kernel is eating good data.
Fix: raise δ toward 1.345 sigmas, after whitening each residual by its true sigma.
Chapter 9
Keep the failure table, the code and the next steps in one place
A covariance is a shape, not an error bar. Carry it through every change of coordinates with the Jacobian, and check the line with samples when the function bends. Gate in sigmas, not metres. Weight every reading by its information. Wrap residuals that come from matching in a robust kernel, but start from consensus. Then prove the claimed spread is honest: NIS in its band, innovations white.
| Failure | Symptom | Signature that names it |
|---|---|---|
| Q too small | covariance shrinks, error grows | average NIS above its band |
| Correlated noise counted as independent | fine at low rate, bad at high rate | lag-1 autocorrelation past 1.96/√N |
| Linearisation error | bad only at long range or fast turns | NIS rises with turn rate; samples leave the ellipse |
| Degrees for radians | gate passes all or nothing from frame 1 | NIS off by about 3,283 |
| 1σ drawn as 68% | truth outside plots too often | 2-D 1σ holds 39.3% |
| Euclidean gate | good matches lost, junk accepted | verdicts disagree with d² |
| Gate on the posterior residual | outliers pass the gate | residual smaller than innovation |
| Units wrong in R | solver returns the initial guess | final cost far from (M − N)/2 |
| Masking | kernel on, answer unchanged | all weights above 0.9 |
| δ too small | jittery fit, growing covariance | effective sample size under half |
| Kernel on a prior | slow drift; restart helps | prior or IMU weight below 1 |
python import numpy as np from scipy.stats import chi2 def propagate(Sigma, J): """First-order covariance transport: Sigma_y = J Sigma J^T.""" return J @ Sigma @ J.T def mahalanobis2(y, S): """d^2 = y^T S^-1 y by whitening. Cholesky raises if S is not positive definite.""" L = np.linalg.cholesky(S) u = np.linalg.solve(L, y) # u = L^-1 y, in sigmas return float(u @ u) def map_estimate(H, z, R, x0=None, P0=None): """Weighted least squares; with (x0, P0) the prior is one more term. Returns x_hat, P_hat.""" Ri = np.linalg.inv(R) A, b = H.T @ Ri @ H, H.T @ Ri @ z if x0 is not None: P0i = np.linalg.inv(P0) A, b = A + P0i, b + P0i @ x0 return np.linalg.solve(A, b), np.linalg.inv(A) def huber_w(r, d=1.345): return np.minimum(1.0, d / np.maximum(np.abs(r), 1e-12)) def cauchy_w(r, c=2.385): return 1.0 / (1.0 + (r / c) ** 2) def irls(H, z, sigma, weight, theta0, iters=50): """Robust fit. theta0 should come from consensus (RANSAC), not from least squares.""" theta = np.asarray(theta0, float) for _ in range(iters): r = (z - H @ theta) / sigma # whiten first: the kernel works in sigmas w = weight(r) / sigma ** 2 theta = np.linalg.solve(H.T @ (w[:, None] * H), H.T @ (w * z)) return theta def nis_band(N, m, alpha=0.05): """Two-sided band on the MEAN NIS of N updates of dimension m.""" return chi2.ppf(alpha / 2, N * m) / N, chi2.ppf(1 - alpha / 2, N * m) / N def whiteness(y): """Lag-1 autocorrelation of a scalar innovation sequence, and its 95% band.""" y = np.asarray(y, float) rho1 = (y[1:] * y[:-1]).sum() / (y ** 2).sum() return rho1, 1.96 / np.sqrt(y.size)
We ran every function above in numpy on this lesson's examples. mahalanobis2 gives 2.78 and 32.1 for Chapter 3's detections, map_estimate gives 9.954 m for the beacon, and nis_band(50, 1) gives (0.647, 1.428).
| Number | Value | Where it comes from |
|---|---|---|
| 2-D share inside 1σ | 39.3% | 1 − e−1/2 |
| 2-D 95% ellipse scale | 2.448 | √5.991 |
| 95% gates, m = 1, 2, 3, 6 | 3.84, 5.99, 7.81, 12.59 | chi-square table |
| Huber, Cauchy thresholds | 1.345, 2.385 σ | 95% efficiency on Gaussian data |
| Robust sigma | 1.4826 × MAD | 1 / 0.6745, the normal's quartile |
| Mean NIS band, N = 50, m = 1 | [0.647, 1.428] | chi-square, 50 degrees, / 50 |
| Whiteness band, N = 50 | ±0.277 | 1.96 / √50 |
| Degrees-for-radians factor | 3,282.8 | (180/π)² |
Where it sits. A filter carries a dense covariance over its whole state; a factor graph carries a sparse information matrix and the same weighted, robust cost, one term per factor. The tests here apply to both. Graduated non-convexity (Yang and co-authors, 2020) starts a redescending kernel wide and narrows it, which removes much of the need for a good start.
Now teach it: press Present or Teach and explain why one wrong range drags a weighted fit 45 cm while its ellipse claims 5. Then go back to the beacons, set three wrong matches, and predict which estimator survives before you look.