Source
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams.update({
"figure.figsize": (7, 3.6), "figure.dpi": 110,
"axes.spines.top": False, "axes.spines.right": False,
"axes.grid": True, "grid.alpha": 0.25, "font.size": 10,
})
rng = np.random.default_rng(7) # fixed seed so the text matches the plotsIn Section 2.1 the robot asked what might happen. Now something has happened: a sensor has produced a reading. The question becomes what did my sensor tell me, and how much should I trust it?
A delivery robot at the gate¶
A delivery robot is carrying a parcel into an apartment complex. It approaches the main gate, which it must stop in front of so the security guard can scan the parcel. Its ultrasonic sensor sends out a sound pulse and times the echo. The display says:
Distance to gate: 2.4 m
Is the gate exactly 2.4 m away? Almost certainly not. The echo time depends on air temperature and humidity. The gate’s bars reflect sound unevenly, and a slight tilt of the robot changes which bar returns the echo first. So the true distance is probably close to 2.4 m, but it could reasonably be 2.3 m or 2.5 m, and occasionally 2.2 m or 2.6 m.
The key idea of this section is:
measurement ≠ truth. A measurement is the truth plus some error.
The flow of information looks like this:
The robot sees only the reading. Its job is to work back from the reading to a belief about the actual distance.
Measurement = true value + noise¶
We can now write the idea in symbols. Let be the true distance (unknown) and the reading (known). Then
where is the noise: the error in this particular reading. We never know for a single reading; if we did, we would simply subtract it. What we can know from testing is how noise behaves on average. In Section 1.2 we modeled this sensor’s noise as Gaussian with a standard deviation that grows with distance:
At about 2.4 m, m, so most readings fall within about ±0.18 m (two standard deviations) of the truth.
def sigma_us(d):
return 0.02 + 0.03 * d
d_true = 2.40
readings = d_true + rng.normal(0, sigma_us(d_true), 500)
plt.hist(readings, bins=40, color="#9fb7cf", edgecolor="white", density=True)
plt.axvline(d_true, c="#1f3a5f", lw=2, label="true distance 2.40 m")
plt.axvspan(d_true - 2 * sigma_us(d_true), d_true + 2 * sigma_us(d_true), color="#e07a3a", alpha=0.12, label="±2σ")
plt.xlabel("ultrasonic reading z [m]"); plt.ylabel("density"); plt.legend(fontsize=8)
plt.title("500 readings of a gate that is truly 2.40 m away");
Noise is not failure¶
A working sensor still produces noisy readings. That is noise: small, random, and centred on the truth. It is different from failure, where the reading is wrong in a way that has nothing to do with the true distance. On an Indian residential road, an ultrasonic sensor can fail in several recognisable ways:
| Situation | What the sensor reports | Kind of error |
|---|---|---|
| Normal operation | 2.33, 2.46, 2.41, … | small noise around the truth |
| A parked scooter between robot and gate | 1.1 m | wrong object: it measured the scooter |
| Sound bounces off a glass guard cabin and never returns | 4.0 m (maximum range) | no echo: the pulse was lost |
| Heavy rain on the sensor face | random values | interference |
Noise is handled by averaging and by probability. Failures are handled differently: by checking whether a reading is plausible, comparing it with other sensors, and ignoring readings that make no physical sense. A good sensor model includes both.
# A realistic stream: mostly noise, with occasional failures
n = 300
kind = rng.choice(["ok", "scooter", "lost", "rain"], size=n, p=[0.88, 0.05, 0.04, 0.03])
z = np.where(kind == "ok", d_true + rng.normal(0, sigma_us(d_true), n),
np.where(kind == "scooter", rng.normal(1.1, 0.05, n),
np.where(kind == "lost", 4.0, rng.uniform(0.2, 4.0, n))))
colors = {"ok": "#1f3a5f", "scooter": "#e07a3a", "lost": "#9a4a16", "rain": "#9fb7cf"}
for k, c in colors.items():
m = kind == k
plt.scatter(np.where(m)[0], z[m], s=8, c=c, label=k)
plt.axhline(d_true, c="k", ls=":", lw=1)
plt.xlabel("reading number"); plt.ylabel("reading [m]"); plt.legend(fontsize=8, ncol=4)
plt.title("Noise (dark cluster) versus failures (the rest)");
The cooking thermometer, again¶
Back in the kitchen, the oil is at about 170 °C and the thermometer gives five readings:
| Reading | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Temperature (°C) | 167 | 171 | 169 | 173 | 170 |
Which reading should the robot trust? None of them alone. Each is the true temperature plus noise, exactly like (1) with in place of . The readings are all plausible, since they spread by only a few degrees, so none looks like a failure. The best use of them is the one from Section 2.1: average them. The mean is 170 °C, and with a standard deviation of about 2.2 °C across these readings, the mean of five has a standard error of about °C.
The same thinking applies to both robots. A single reading is weak evidence; a sensor model tells us how weak.
Sensor models¶
A sensor model answers the question: if the true state were , what readings would I expect? We write it as a conditional probability density
read as “the probability of reading given that the state is ”. The vertical bar means “given” or “assuming”. For our ultrasonic sensor, (1) and (2) give
which says: if the gate is at distance , readings are Gaussian, centred on , with spread .
Likelihood: turning the question around¶
The sensor model looks forward, from a known state to possible readings. But the robot faces the reverse problem. It has the reading m and wants to know which true distances are believable.
So we fix the observed reading and ask, for each candidate true distance: if the gate were really there, how likely is the reading I actually got?
This quantity is the likelihood of the candidate:
It is the same formula as the sensor model, but now is fixed at what we observed and varies. Let’s compute it for the five candidates:
def p_reading(z, d):
s = sigma_us(d)
return np.exp(-0.5 * ((z - d) / s) ** 2) / (s * np.sqrt(2 * np.pi))
z_obs = 2.4
candidates = np.array([2.2, 2.3, 2.4, 2.5, 2.6])
L = p_reading(z_obs, candidates)
print(f"{'true distance d':>16} | {'σ(d)':>6} | likelihood L(d) | relative to best")
for d, l in zip(candidates, L):
print(f"{d:>14.1f} m | {sigma_us(d):.3f} | {l:15.3f} | {l / L.max():.2f}") true distance d | σ(d) | likelihood L(d) | relative to best
2.2 m | 0.086 | 0.310 | 0.07
2.3 m | 0.089 | 2.384 | 0.55
2.4 m | 0.092 | 4.336 | 1.00
2.5 m | 0.095 | 2.413 | 0.56
2.6 m | 0.098 | 0.507 | 0.12
ds = np.linspace(2.0, 2.8, 400)
plt.plot(ds, p_reading(z_obs, ds), c="#1f3a5f", lw=2)
plt.scatter(candidates, L, c="#e07a3a", zorder=3)
for d, l in zip(candidates, L):
plt.annotate(f"{l:.2f}", (d, l), textcoords="offset points", xytext=(0, 7), ha="center", fontsize=8)
plt.xlabel("candidate true distance d [m]"); plt.ylabel("likelihood L(d)")
plt.title("Which true distances explain the reading 2.4 m?");
Figure 1:The likelihood of every possible true distance, given the single reading z = 2.4 m. Distances near 2.4 m explain the reading well; 2.2 m and 2.6 m explain it poorly. The dots are the five candidates from the table.
A distance of 2.4 m explains the reading best, 2.3 m and 2.5 m explain it about half as well, and 2.2 m or 2.6 m explain it poorly, roughly 7–12% as well as 2.4 m does.
Two points are important:
A likelihood is not a probability of the state. The likelihoods above do not add up to 1, and they do not say “the gate is at 2.4 m with probability 0.43”. They only compare how well each candidate explains the evidence.
To get probabilities, we need one more ingredient: what the robot believed before the reading. Combining the two is the job of Bayesian inference, in Section 2.3.