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 plotsSection 2.2 ended with a gap. A likelihood tells the robot how well each possibility explains a reading, but not how probable each possibility is. To get there, the robot must also use what it believed before the reading arrived. This section shows how.
Which tower am I at?¶
A delivery robot enters a large gated community with four residential towers. Each tower has a coloured signboard at its entrance:
| Tower | Blue | Green | Yellow | Red |
|---|
The robot must deliver to Green Tower. Its GPS is unreliable here: the tall buildings block and reflect satellite signals, so its position estimate can wander by tens of metres. It drives in, stops at an entrance, and its camera reports:
Signboard detected: GREEN
Should the robot now be certain it is at Green Tower? Not quite. The camera is good but not perfect: afternoon glare, a faded board, or a green-painted gate nearby can all fool it. So the robot asks: given everything I knew before, and this new evidence, how strongly should I believe I am at Green Tower?
Step 1 · The prior: what the robot believed before looking¶
Even before the camera fires, the robot is not completely ignorant. From the complex’s map and its own route, it knows roughly which entrances it could have reached. Green Tower has the widest entrance and sits closest to the main gate, and Red Tower is at the far end. From the map and past deliveries, the robot’s belief before looking is:
| Tower | Blue | Green | Yellow | Red |
|---|---|---|---|---|
| Prior | 0.25 | 0.35 | 0.25 | 0.15 |
This is called the prior probability: the belief prior to the new evidence. It is a PMF over the four towers, exactly like the vessel PMF in Section 2.1.
Step 2 · The evidence: how reliable is “green”?¶
The robot’s engineers tested the camera at every tower in all kinds of light. They recorded how often it reports “green” at each tower:
| If the robot is actually at… | Blue | Green | Yellow | Red |
|---|---|---|---|---|
| 0.05 | 0.90 | 0.04 | 0.03 |
The Green entry, 0.90, is how often the camera correctly recognises the green board. The others are false detections: for example, at Blue Tower the camera wrongly says “green” 5% of the time, perhaps because the blue board looks greenish under a sodium street light. This row is the likelihood of the observation “green” for each possible tower, the idea from Section 2.2.
Step 3 · Combining them: Bayes’ rule¶
The robot now reasons as follows. For each tower, how likely was it to be there, and how likely is it that the camera would say “green” from there? Multiplying these two numbers gives how strongly each tower is supported by both sources:
| Tower | Prior | × Likelihood | = Prior × Likelihood |
|---|---|---|---|
| Blue | 0.25 | 0.05 | 0.0125 |
| Green | 0.35 | 0.90 | 0.3150 |
| Yellow | 0.25 | 0.04 | 0.0100 |
| Red | 0.15 | 0.03 | 0.0045 |
| Total | 0.3420 |
These products are not yet probabilities, since they add up to 0.342 rather than 1. Dividing each by the total fixes that. The total, 0.342, is the overall probability that the camera would say “green” at all, whatever tower the robot is at.
Written as a formula, this reasoning is Bayes’ rule:
Here is the tower and is the observation (“green”). The result , the belief after the evidence, is called the posterior. In words: posterior = likelihood × prior, then normalize so it adds up to 1.
It comes directly from the rule for conditional probability, . Writing the joint probability both ways, as and as , and solving for gives (1).
towers = ["Blue", "Green", "Yellow", "Red"]
tower_colors = ["#2f6db3", "#2e8b57", "#d4a017", "#c0392b"]
prior = np.array([0.25, 0.35, 0.25, 0.15])
lik_green = np.array([0.05, 0.90, 0.04, 0.03]) # P(camera says "green" | tower)
joint = prior * lik_green
evidence = joint.sum()
posterior = joint / evidence
print(f"P(camera says green) = {evidence:.3f}\n")
print(f"{'tower':>7} | prior | likelihood | posterior")
for t, p0, l, p1 in zip(towers, prior, lik_green, posterior):
print(f"{t:>7} | {p0:.2f} | {l:.2f} | {p1:.3f}")P(camera says green) = 0.342
tower | prior | likelihood | posterior
Blue | 0.25 | 0.05 | 0.037
Green | 0.35 | 0.90 | 0.921
Yellow | 0.25 | 0.04 | 0.029
Red | 0.15 | 0.03 | 0.013
fig, ax = plt.subplots(1, 3, figsize=(11, 3))
for a, vals, title in zip(ax, [prior, lik_green, posterior], ["prior P(x)", 'likelihood P("green" | x)', 'posterior P(x | "green")']):
a.bar(towers, vals, color=tower_colors); a.set_ylim(0, 1); a.set_title(title, fontsize=10)
for i, v in enumerate(vals):
a.text(i, v + 0.02, f"{v:.2f}", ha="center", fontsize=8)
plt.tight_layout()
Figure 1:Prior belief, likelihood of the observation “green”, and posterior belief over the four towers. One detection moves the belief in Green Tower from 0.35 to about 0.92.
A single “green” detection raises the robot’s belief in Green Tower from 0.35 to 0.92. Note what drives this. The camera is reliable (0.90 correct), and false “green” reports at the other towers are rare (0.03–0.05). The prior also leaned towards Green. If the prior had strongly favoured Blue, the same detection would have moved the belief less.
The posterior is not 1. There is still about an 8% chance the robot is somewhere else, mostly at Blue Tower, because Blue is both a likely place to be and the tower most prone to a false “green”. A careful robot keeps this doubt in mind.
Step 4 · One answer: MAP estimation¶
The robot’s navigation software needs a single answer now: should it turn towards the Green Tower lift lobby or not? A natural rule is to choose the tower with the highest posterior probability. This is the maximum a posteriori (MAP) estimate:
The notation means “the value of that makes the following expression largest”, and the hat in marks an estimate. Here, Green.
MAP answers “which is most probable?”, which is not always the same question as “what should I do?”. If a wrong turn were very costly, the robot might first take another look even with 92% confidence. That trade-off between probability and consequences is the subject of Section 2.4.
x_map = towers[int(np.argmax(posterior))]
print("MAP estimate:", x_map, f"(posterior {posterior.max():.3f})")MAP estimate: Green (posterior 0.921)
Step 5 · Fusing several sources of information¶
The camera is not the robot’s only source. It also has a GPS estimate, which is weak here but not useless, and the map, which it has already used as its prior. How should it combine them?
“GPS suggests Green Tower with 60% confidence” sounds like a probability, but to use it in Bayes’ rule we must turn it into a likelihood: how often does the GPS point to “Green” when the robot is actually at each tower? From test drives, suppose:
| If the robot is actually at… | Blue | Green | Yellow | Red |
|---|---|---|---|---|
| 0.25 | 0.60 | 0.10 | 0.05 |
GPS confuses Green with Blue fairly often (25%) because the two towers stand side by side. It rarely confuses Green with the far-away Red Tower.
If the camera’s errors and the GPS errors are independent (knowing that the camera erred tells us nothing about whether the GPS erred), then the two likelihoods simply multiply:
The symbol means “is proportional to”: compute the right-hand side for every tower, then normalize so the results sum to 1.
lik_gps = np.array([0.25, 0.60, 0.10, 0.05]) # P(GPS says "green" | tower)
def normalize(v):
return v / v.sum()
beliefs = {
"map only (prior)": prior,
"map + GPS": normalize(prior * lik_gps),
"map + camera": normalize(prior * lik_green),
"map + camera + GPS": normalize(prior * lik_green * lik_gps),
}
for name, b in beliefs.items():
print(f"{name:>20}: " + " ".join(f"{t} {p:.3f}" for t, p in zip(towers, b))) map only (prior): Blue 0.250 Green 0.350 Yellow 0.250 Red 0.150
map + GPS: Blue 0.205 Green 0.689 Yellow 0.082 Red 0.025
map + camera: Blue 0.037 Green 0.921 Yellow 0.029 Red 0.013
map + camera + GPS: Blue 0.016 Green 0.978 Yellow 0.005 Red 0.001
fig, ax = plt.subplots(figsize=(8, 3.2))
names = list(beliefs)
bottom = np.zeros(len(names))
vals = np.array([beliefs[n] for n in names])
for i, t in enumerate(towers):
ax.barh(names, vals[:, i], left=bottom, color=tower_colors[i], label=t)
bottom += vals[:, i]
ax.set_xlim(0, 1); ax.set_xlabel("probability"); ax.legend(ncol=4, fontsize=8, loc="lower center", bbox_to_anchor=(0.5, 1.0))
ax.invert_yaxis(); ax.grid(False); plt.tight_layout()
Figure 2:Belief in each tower as sources are added. GPS alone is weak; the camera is strong; the two together push the belief in Green Tower to about 0.98.
Each source adds evidence. The GPS alone raises Green only from 0.35 to about 0.69. The camera alone takes it to 0.92, and the two together reach about 0.98. Neither sensor is trusted blindly; each contributes according to how reliable it has proven to be.
Fusing continuous estimates¶
The same logic works for continuous quantities, such as the distance to the gate from Section 2.2. Suppose the ultrasonic sensor gives 2.40 m with standard deviation 0.09 m, and the camera, judging the gate’s size in the image, estimates 2.70 m with standard deviation 0.30 m. When both are Gaussian and independent, the fused estimate is also Gaussian, with
The fused mean is a weighted average that trusts the less noisy source more, and the fused variance is smaller than either input. This single formula is the heart of the Kalman filter that the delivery e-cart uses in Chapter 7.
m1, s1 = 2.40, 0.02 + 0.03 * 2.40 # ultrasonic
m2, s2 = 2.70, 0.30 # camera size-based estimate
m = (s2**2 * m1 + s1**2 * m2) / (s1**2 + s2**2)
s = np.sqrt(s1**2 * s2**2 / (s1**2 + s2**2))
xs = np.linspace(1.8, 3.4, 500)
g = lambda x, mu, sd: np.exp(-0.5 * ((x - mu) / sd) ** 2) / (sd * np.sqrt(2 * np.pi))
plt.plot(xs, g(xs, m2, s2), c="#9fb7cf", lw=2, label=f"camera {m2:.2f} ± {s2:.2f} m")
plt.plot(xs, g(xs, m1, s1), c="#e07a3a", lw=2, label=f"ultrasonic {m1:.2f} ± {s1:.2f} m")
plt.plot(xs, g(xs, m, s), c="#1f3a5f", lw=2.5, ls="--", label=f"fused {m:.2f} ± {s:.3f} m")
plt.xlabel("distance to gate [m]"); plt.ylabel("density"); plt.legend(fontsize=8); plt.title("Fusing two distance estimates");
The fused estimate, about 2.43 m, stays close to the ultrasonic reading because that sensor is more than three times as precise, but it moves slightly towards the camera’s estimate.