A good thermometer and a bad one disagree. Their plain average is twice as noisy as the good one alone. Weight each by one over its variance, and the result beats both.
Pick an estimator, then drag the noise of sensor B. Watch the four error bars under the chart. Then we build the rule piece by piece: precision, the error bowl, the shrinking variance, and the Kalman update hidden inside it.
You need basic arithmetic and the idea of a bell curve. We build the rest from zero.
Lesson 3 of 22 in Sensor Fusion: Classical to Modern. It follows Fusion Architectures and leads to The Bayes Filter.
Toy simulation, illustrative: the true temperature is 21 °C and each sensor adds Gaussian noise of the size you set. The bars are the root mean square error over the last 300 readings.
Chapter 0
See two honest sensors disagree on every reading, and name what you must know about each
You hold two thermometers in one pot of water. One reads 20.0 °C. The other reads 22.0 °C. The water has one temperature, and you must write one number on the sheet. Which number?
Every Kalman filter, GPS receiver and poll average answers this question many times a second: two noisy readings of one quantity, one best estimate. The first guess is to split the difference, 21.0 °C. That is right only if both thermometers are equally good. Usually you know more: one is a cheap probe and one is a lab instrument.
Each sensor gives a reading and a variance: the average of its squared errors. A small variance means the readings sit close to the truth. With made-up numbers: errors of +0.4, −0.6 and +0.2 °C give a variance of (0.16 + 0.36 + 0.04)/3 = 0.19 °C².
Two assumptions carry the whole lesson. Chapter 9 breaks each one.
Each press adds 20 readings from each sensor. Blue is the lab thermometer, orange the cheap probe. Drag the noise and watch the width of each pile.
Illustrative toy: true value 21 °C, Gaussian noise, seeded so every visit shows the same readings.
Both piles centre on the dashed truth, so neither sensor leans. The orange pile is four times wider. If you treat the two as equals, the estimate jumps around with the probe. Measure each sensor's spread on a pot of known temperature, then trust it in proportion. The rest of this lesson computes that proportion.
Chapter 1
Replace the fixed half and half with a dial, and watch where the fused estimate lands
Take an extreme case. Sensor A is accurate to about 1 m and reads 100 m. Sensor B is accurate to about 10 m and reads 130 m. The plain average says 115 m. It gives B's wild reading half the say.
The fix is a dial. Call the number you report the fused estimate, x-f. Call the weight on sensor A w, a number from 0 to 1. Sensor B gets the rest, 1 − w.
x-f equals w times x-A, plus one minus w, times x-B.
Take a share w of reading x-A and the rest of reading x-B, then add them. If both sensors read the same value, x-f is that value for any w.
A reads 100 m, B reads 130 m. With w = 0.5, x-f = 50 + 65 = 115 m, the plain average. With w = 0.8, x-f = 80 + 26 = 106 m. With w = 1, x-f = 100 m and B drops out.
So the plain average is the dial stuck at 0.5. Which setting is best? One pair of readings cannot tell you, because noise moves them. Repeat the experiment many times and look at the spread of x-f.
Each teal dot is one fused estimate from a fresh pair of readings. Sensor A has a noise of ±1 m, sensor B ±3 m. Drag w and find the narrowest teal cloud.
Illustrative toy: true position 100 m, 60 seeded pairs of Gaussian readings. The spread is the expected standard deviation of x-f at this w.
The narrowest cloud sits near w = 0.9, not 0.5. The best dial depends on which sensor is tighter. A dial set by hand also goes stale: rain or age changes a sensor, and the estimate drifts toward the worse one. Chapter 3 computes w from the variances, so it follows the sensors.
Chapter 2
Turn the vague word trust into a number that grows as a sensor gets better
A lab thermometer has a typical error of 0.5 °C. A cheap probe has 2 °C. The typical error is the standard deviation σ (sigma). Its square σ² is the variance: 0.25 °C² for the lab thermometer and 4 °C² for the probe.
Squaring does two jobs. Every error counts as positive, so +2 and −2 do not cancel. Big errors count more: an error of 4 adds 16, an error of 2 adds only 4.
Variance is small for a good sensor. We want a number that is big for a good sensor, so we flip it. Call it the precision P.
P equals one over sigma squared.
Square the sensor's typical error sigma to get its variance. Divide 1 by the variance. A small error gives a big precision P.
Lab thermometer: sigma = 0.5 °C, sigma squared = 0.25, P = 1/0.25 = 4. Cheap probe: sigma = 2 °C, sigma squared = 4, P = 0.25. The lab thermometer is 16 times more precise.
The bells show how each sensor's readings spread. The purple bars show each sensor's share of the total precision. Drag a sigma.
Illustrative values. The bars show P-A/(P-A + P-B) and P-B/(P-A + P-B), the vote shares Chapter 3 derives.
Why flip the variance and not use sigma? When you combine independent sensors, precision is the quantity that adds. Chapter 5 proves it.
Precision is only as good as the sigma you feed it. Suppose a datasheet says 0.5 °C but the probe wanders by 1.5 °C. You compute P = 4 when the truth is 0.44, so you overstate its precision nine times. Chapter 8 measures sigma properly.
Chapter 3
Write the fused variance as a function of the dial, then find the dial that makes it smallest
Which w is best? Choose the w that makes the fused estimate's error smallest on average. Measure that error by its variance, sigma-f squared. Two facts about variance do the work. Scale a reading by c and its variance scales by c squared. Add two independent readings and their variances add.
sigma-f squared of w equals w squared times sigma-A squared, plus one minus w, squared, times sigma-B squared.
Multiply A's variance by w squared and B's variance by one minus w, squared. Add the two. The sum is how noisy x-f is at that dial setting.
Thermometers: sigma-A squared = 0.25, sigma-B squared = 4. At w = 0.5: 0.25 × 0.25 + 0.25 × 4 = 1.06. At w = 1: 0.25. At w = 0.94: 0.235, the lowest point.
The teal curve is sigma-f squared for every w. The faint blue and orange curves are the two leak terms. Drag w to the bottom, then change a variance and watch the bottom move.
Exact curve for independent sensors; the starting values are the two thermometers.
The bottom of the bowl is where the slope is zero. Take the slope with respect to w, set it to zero and solve:
2w · σA² − 2(1 − w) · σB² = 0 → w* = σB² / (σA² + σB²)
Call the best weight w-star. Divide its top and bottom by sigma-A squared times sigma-B squared. Each variance turns into a precision:
w-star equals P-A over, P-A plus P-B.
Add the two precisions. The weight w-star on A is A's share of that total. B gets the rest.
P-A = 4 and P-B = 0.25, so the total is 4.25. w-star = 4/4.25 = 0.941, and B gets 0.059.
One failure hides here. If you set a variance to zero, its precision is infinite and w-star pins to 1. The weights read 1.00 and 0.00, and the second sensor does nothing. Give every variance a floor above zero.
Chapter 4
Put the best weight into the dial and get the one rule this whole series builds on
Put w-star into the trust dial. Each sensor's weight becomes its share of the total precision. Each reading votes, as loud as its precision.
x-f equals P-A times x-A, plus P-B times x-B, all over P-A plus P-B.
Multiply each reading by its precision. Add the two products. Divide by the sum of the precisions.
Thermometers: (4 × 22.0 + 0.25 × 20.0)/4.25 = (88 + 5)/4.25 = 21.88 °C. The plain average said 21.00. The rule leans 94% toward the lab thermometer.
This is the inverse-variance weighted average. Statisticians call it the best linear unbiased estimator (BLUE): no other weighted average of unbiased readings has a smaller variance. A second case: a survey GPS reads 100.0 m with variance 0.25 m², a phone chip reads 109.0 m with variance 9 m². The precisions are 4 and 0.111, so x-f = 100.24 m. The plain average, 104.5 m, lands 4.3 m toward the chip.
| Edge case | Weights A / B | x-f becomes |
|---|---|---|
| Equal variances | 0.5 / 0.5 | the plain average |
| B's variance huge (a dead sensor) | near 1 / near 0 | A's reading, with no special code |
| A's variance tiny | near 1 / near 0 | A's reading |
The purple bar shows how the vote splits. Below it, each sensor's reading has a bar of one sigma, and the teal dot is x-f. Raise B's variance and watch x-f slide toward A.
Exact arithmetic on the numbers you set; the bars are one sigma wide on each side.
pythondef fuse(x_a, var_a, x_b, var_b): p_a, p_b = 1.0 / var_a, 1.0 / var_b # precisions: the vote strengths x_f = (p_a * x_a + p_b * x_b) / (p_a + p_b) # precision-weighted average var_f = 1.0 / (p_a + p_b) # precisions add (Chapter 5) return x_f, var_f fuse(22.0, 0.25, 20.0, 4.0) # (21.88, 0.235) leans to A, tighter than A fuse(22.0, 1.0, 20.0, 1.0) # (21.0, 0.5) equal trust: the plain average fuse(22.0, 0.25, 20.0, 1e9) # (22.0, 0.25) a dead sensor drops out
A dead sensor needs no special code: give it a huge variance. A frozen sensor is worse. It repeats its last value and still reports its normal variance. If a sensor's readings stop changing, set its variance huge until they move again.
Chapter 5
Find how sure the fused estimate is, and see it beat the better sensor
How sure is x-f? Put w-star back into the bowl formula. The algebra collapses to the product of the two variances over their sum:
σf² = σA² · σB² / (σA² + σB²)
Take one over both sides. Call one over sigma-f squared the fused precision, P-f:
P-f equals P-A plus P-B.
Add the two precisions to get P-f. One over P-f is the fused variance.
P-f = 4 + 0.25 = 4.25, so sigma-f squared = 1/4.25 = 0.235 °C² and sigma-f = 0.485 °C. The lab thermometer alone had 0.5 °C. A worse sensor made the best one better.
Both precisions are positive, so P-f is larger than either one. The fused variance is always below the smaller input variance. For honest, independent sensors, fusion never hurts.
Fuse n readings that each have sigma. The precision adds n times, so the fused sigma falls with the square root of n:
σf = σ / √n
With sigma = 2 °C: 4 readings give 1 °C, 16 give 0.5 °C, 100 give 0.2 °C. Each halving of the error costs four times the readings.
Treat each reading as a bell of belief, centred at the reading, as wide as its sigma. To combine independent evidence, multiply the two bells point by point. The product of two bells is another bell. Its centre is x-f and its precision is P-A + P-B. Two different derivations reach one answer.
Blue and orange are the two sensors' bells. Teal is their product, scaled to area 1. Drag the centres and widths: the teal bell is always the narrowest.
Exact Gaussian product, on an illustrative axis.
The square root of n law needs independent readings. Ten readings one millisecond apart share the same slow drift. The symptom is that averaging stops helping after a few readings. Plot the error against n to see it, then space the readings out or use an Allan variance plot (Chapter 8).
Chapter 6
Rename sensor A as the prediction and sensor B as the new measurement, and derive the Kalman gain
A Kalman filter tracks a quantity over time. At each tick it holds a prior: its prediction, with a variance. Then a measurement arrives with its own variance. The filter fuses the two with this lesson's rule.
Kalman books use their own names. The prior is x-minus with variance P-minus. The measurement is z with variance R. Add and subtract the prior inside x-f: x-f = x-minus + (1 − w)(z − x-minus). The fraction 1 − w is the Kalman gain K.
K equals P-minus over, P-minus plus R.
Add the two variances. K is the prior's share of that total. An unsure prior gives a big K.
Prior variance P-minus = 4, measurement variance R = 1. K = 4/5 = 0.8.
x equals x-minus plus K times, z minus x-minus.
Find the surprise, z minus x-minus. Start at the prediction x-minus and move a fraction K of the surprise.
Prior x-minus = 60, measurement z = 40, K = 0.8. Innovation = −20. x = 60 + 0.8 × (−20) = 44. The precision-weighted average gives (0.25 × 60 + 1 × 40)/1.25 = 44 as well.
The variance update matches Chapter 5. Call the new variance P: P = (1 − K) · P-minus = 0.2 × 4 = 0.8, and 1/(0.25 + 1) = 0.8.
| Situation | K | The filter |
|---|---|---|
| Measurement noisy (R large) | near 0 | stays near its prediction |
| Prediction unsure (P-minus large) | near 1 | jumps to the measurement |
| Equal variances | 0.5 | splits the difference |
Top: the prior and the measurement vote by precision. Bottom: start at the prior and move a fraction K toward the measurement. Change the variances; both teal dots stay on one vertical line.
Exact arithmetic with prior x-minus = 60 and measurement z = 40.
If you set R too small, K stays near 1 and the filter chases every noisy measurement. The symptom is a jittery track. The innovations come out larger than the square root of P-minus plus R predicts. The fix is to raise R to the measured noise.
Chapter 7
Check every claim of this lesson on a thousand trials, then break the unbiased assumption and watch the rule stay sure
Every claim so far is about averages over many trials. So run many trials. Each trial draws a fresh reading from each sensor and fuses them. The teal histogram is the thousand fused estimates. The blue outline is sensor A alone.
The teal line over the histogram is the spread the formula claims. The dashed line is the truth. Set the variances, then switch on the bias.
Illustrative toy: truth 50, Gaussian noise, 1,000 seeded trials. The bias adds a fixed offset to every reading of B while B still reports its variance.
Try these in order:
Work step 4 by hand. A is honest at 50 with variance 4; B reads 58 with variance 1. The precisions are 0.25 and 1, so x-f = (12.5 + 58)/1.25 = 56.4. The formula claims sigma-f = 0.89. The real error is 6.4, about 7 claimed sigmas. The rule did its arithmetic right on a wrong input.
Chapter 8
Get an honest variance for each sensor, and reject the readings no variance can explain
The arithmetic is easy. The variances are the hard part, because wrong variances give wrong weights, even with clean maths. There are four sources, from quick to careful.
| Source | How | Watch out for |
|---|---|---|
| Datasheet | Square the 1-sigma accuracy: ±0.5 °C gives 0.25 °C². | Specs given at 2 sigma or as a maximum; lab conditions on a new unit. |
| Known truth | Read a known value many times. Average the squared errors. | A reference that is itself noisy. |
| Allan variance | Plot variance against averaging time for drifting sensors, such as gyros and clocks. | One number will not describe a drifting sensor. |
| Innovations, online | Keep a running average of squared innovations and raise the variance when they grow. | Faults that inflate the estimate; gate them out first. |
A worked case for the second row. A rangefinder aims at a wall placed at exactly 5.00 m. Ten readings give errors in cm of +2, −1, +3, 0, −2, +1, +2, −3, +1, −3. The squares sum to 42. The variance is 42/10 = 4.2 cm², so sigma is about 2.05 cm.
Datasheet variances come from a lab, on a fresh unit, at room temperature. A hot, dusty, ageing sensor is noisier. Trust the datasheet as is and the fusion becomes overconfident. Start from the datasheet, then inflate from the measured innovations.
A fault is not noise: reject it, do not fuse it. The test uses the innovation ν (nu), the measurement minus the prediction. Its expected variance S is the prior variance plus the measurement variance, P-minus + R. Pick a gate width g in sigmas, and reject a reading when:
|ν| > g · √S
With P-minus = 4 and R = 1, S = 5 and its square root is 2.24. A 3-sigma gate rejects any innovation beyond 6.7.
Orange dots are 195 honest innovations. Red dots are 5 faults. The band is the gate. Narrow it and you reject honest readings; widen it and faults slip through.
Illustrative toy: S = 5, so one sigma is 2.24. The faults sit at 3.4, 4.0, 5.4, 3.7 and 6.0 sigma.
Chapter 9
Break each assumption in turn, and learn the test that catches it
The rule is optimal only if its inputs are true: unbiased sensors, independent errors, correct variances. Break one and it still returns a confident number. The danger is not a crash. It is a narrow error bar around a wrong answer.
| Trap | Symptom | Test | Fix |
|---|---|---|---|
| Optimistic variances | Real errors larger than the claimed sigma | The average of ν²/S, the normalized innovation squared, sits well above 1 | Measure the variances; inflate them |
| Correlated sensors | A second sensor adds little real accuracy | List shared error sources; check whether the two residuals move together | Model the correlation, or use sensors of different kinds |
| Bias | A steady offset in one direction | The mean of a sensor's residuals over time is far from 0 | Calibrate out the offset, or reject the sensor |
Two GPS receivers on one car share the same atmospheric delay. Each has variance sigma squared = 4 m². Call the correlation of their errors ρ (rho): 0 when independent, 1 when the errors are identical. Average the two readings. The true variance of the average, sigma-true squared, is:
sigma-true squared equals sigma squared times, one plus rho, over two.
With rho at 0, averaging halves the variance. As rho grows, the second receiver repeats the first one's error. At rho = 1 averaging keeps the whole variance.
sigma squared = 4 m². Rho = 0 gives 2 m², rho = 0.5 gives 3 m², rho = 1 gives 4 m². The independent formula reports 2 m² every time.
Each dot is one pair of errors: A across, B up. Raise rho and the cloud stretches along the diagonal. The bars compare what the formula claims with the truth.
Illustrative toy: 140 seeded error pairs, each receiver with sigma = 2 m. The bars use the exact formula.
Sensor B's residuals, its reading minus the fused estimate, over six ticks: +1.8, +2.1, +1.9, +2.0, +2.2, +1.7. All are positive and the mean is +1.95. An honest sensor's residuals straddle zero. Subtract 2 from every reading of B before you fuse.
pythondef monitor(innovations, S, residuals): # innovations: measurement - prediction; S = P_minus + R, their expected variance nis = sum(v * v / S for v in innovations) / len(innovations) # near 1 if variances are honest mean_r = sum(residuals) / len(residuals) # near 0 if the sensor is unbiased return {"overconfident": nis > 2.0, "mean_residual": mean_r} monitor([2.1, -1.5, 3.0, -2.4], 5.0, [1.8, 2.1, 1.9, 2.0, 2.2, 1.7]) # {'overconfident': False, 'mean_residual': 1.95} -> the variances look fine, B is biased
Chapter 10
Keep the numbers, the checklist and the code in one place, and see where the rule goes next
Two unbiased, independent readings of one quantity combine best when you weight each one by its precision, one over its variance. The precisions add, so the result is tighter than either sensor. Rename the readings as prediction and measurement and you have the Kalman update. The rule trusts its inputs, so check them.
| Quantity | Formula | Thermometer numbers |
|---|---|---|
| Precision | P = 1/σ² | 4 and 0.25 |
| Weight on A | w* = PA/(PA + PB) | 0.941 |
| Fused estimate | xf = (PAxA + PBxB)/(PA + PB) | 21.88 °C |
| Fused precision | Pf = PA + PB | 4.25, so sigma-f = 0.485 °C |
| n equal readings | σf = σ/√n | 2 °C over 16 readings: 0.5 °C |
| Kalman gain | K = P−/(P− + R) | 4/(4 + 1) = 0.8 |
| Gate | reject if |ν| > g√S, S = P− + R | 3 sigma with S = 5: 6.7 |
| Correlated pair | σtrue² = σ²(1 + ρ)/2 | 4 m² at rho 0.5: 3 m² |
pythondef kalman_update(x_prior, P_prior, z, R, gate=3.0): S = P_prior + R # expected variance of the innovation nu = z - x_prior # innovation: the surprise if nu * nu > gate * gate * S: # outside the gate: a fault, not noise return x_prior, P_prior # reject; keep the prediction K = P_prior / S # gain = the weight on the measurement return x_prior + K * nu, (1 - K) * P_prior # same as the precision-weighted average kalman_update(60, 4, 40, 1) # innovation -20 is beyond 6.7: rejected -> (60, 4) kalman_update(60, 4, 55, 1) # innovation -5 passes -> (56.0, 0.8)
The first call shows the gate at work: with S = 5, a surprise of 20 is almost 9 sigmas, so the update keeps the prediction. Chapter 6 fused the same numbers without a gate and got 44.
| System | The two estimates | What the rule gives |
|---|---|---|
| Any Kalman filter | the prediction and the new measurement | the update, every tick |
| A GNSS receiver | ranges to many satellites, each with its own quality | one position, from weighted least squares: this rule in matrix form |
| Poll of polls, meta-analysis | many studies of one effect, each with an error bar | the fixed-effect estimate: big studies weigh more |
| Redundant gauges | two or three readings of one pressure | a sharper reading when the variances are known; median voting is the common fault-tolerant choice |
A poll worked by hand: poll A of 2,400 people gives 52% with a margin of about ±2 points, so sigma is about 1. Poll B of 600 gives 47% with ±4, so sigma is about 2. The precisions are 1 and 0.25, the weights 0.8 and 0.2, and the fused value is 51.0% with sigma 0.89.
Now press Present or Teach and derive it from memory: precision, the error bowl, the fused estimate, why precisions add, and the Kalman gain. Then go back to the hero and say why the plain average loses to sensor A alone.
Lesson 3 of 22 in Sensor Fusion: Classical to Modern on Engineermaxxing. The derivations follow the standard treatments in the references above. Every toy number on this page carries the word illustrative.