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 plotsThis chapter follows one story in four steps. Every robot in this book has to answer these four questions, in this order:
We begin with the first question, in a kitchen.
The next vessel at the washing station¶
A vessel-washing robot stands at a sink in a busy home kitchen. After every meal, vessels arrive one at a time: steel plates, tumblers, bowls, the cooking vessels (the kadai, the tawa, the cooker), and the occasional odd item such as a sieve or a ladle.
Before the next vessel arrives, the robot has to get ready. It might preheat water for a greasy kadai or open the rack for a stack of plates. So it asks: what will the next vessel be?
A tempting answer is “a plate, because plates are the most common”. That answer is often wrong. Plates are common, but not certain; if the robot commits to “plate” and a greasy kadai arrives, it has prepared the wrong cycle. What the robot needs is not a guess but a statement of how likely each possibility is.
From a few weeks of watching this kitchen, the robot has learned how often each type appears:
| Vessel type | Plate | Tumbler | Bowl | Cooking vessel | Other |
|---|---|---|---|---|---|
| 0.40 | 0.25 | 0.20 | 0.10 | 0.05 |
Random variables and PMFs¶
We call the type of the next vessel a random variable and write it as a capital letter, . A random variable is a quantity whose value we do not yet know, but whose possible values and their likelihoods we can describe. A particular value is written in lower case, , so reads “the next vessel is a plate”.
Because takes one of a list of values, it is a discrete random variable, and the table above is its probability mass function (PMF). Any PMF must satisfy two rules:
The first rule says a probability can never be negative. The second says the next vessel must be something on the list, so the probabilities add up to exactly 1. The symbol means “add up over every possible value ”.
A probability here expresses uncertainty about an unknown future event. “” does not mean the next vessel is 40% of a plate. It means that in the long run, about 40 out of every 100 vessels are plates, and that the robot should weigh “plate” accordingly when it prepares.
vessels = ["plate", "tumbler", "bowl", "cooking vessel", "other"]
pmf = np.array([0.40, 0.25, 0.20, 0.10, 0.05])
print("probabilities add up to", pmf.sum())
# Simulate one evening: 30 vessels arrive
evening = rng.choice(vessels, size=30, p=pmf)
print("first ten arrivals:", ", ".join(evening[:10]))probabilities add up to 1.0
first ten arrivals: tumbler, cooking vessel, bowl, plate, plate, cooking vessel, plate, bowl, bowl, tumbler
The sequence looks irregular, just as a real sink does. The PMF cannot tell us what this evening will look like, but it tells us what many evenings look like on average. We can check this by simulating many arrivals and comparing the observed frequencies with the PMF:
short = rng.choice(len(vessels), size=30, p=pmf)
long_ = rng.choice(len(vessels), size=1000, p=pmf)
f_short = np.bincount(short, minlength=5) / len(short)
f_long = np.bincount(long_, minlength=5) / len(long_)
xpos = np.arange(5)
plt.bar(xpos - 0.27, pmf, 0.27, color="#1f3a5f", label="PMF")
plt.bar(xpos, f_short, 0.27, color="#e07a3a", label="30 vessels")
plt.bar(xpos + 0.27, f_long, 0.27, color="#9fb7cf", label="1000 vessels")
plt.xticks(xpos, vessels); plt.ylabel("probability / fraction"); plt.legend(); plt.title("What might the next vessel be?");
Figure 1:The vessel PMF (dark bars) compared with the observed fraction of each vessel type in simulated streams of 30 and 1000 vessels. The short stream differs noticeably from the PMF; the long one matches it closely.
Measurements that never repeat exactly¶
Now move to the stove. The robot heats oil for a tadka and wants it near 170 °C. It holds the thermometer probe in the oil and reads it once per second. Nothing changes the heater setting, yet the readings are:
| Reading | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Temperature (°C) | 168 | 171 | 169 | 173 | 170 | 172 |
Why aren’t they identical? Several small effects add up. The oil is not perfectly mixed, so the probe tip sits in slightly hotter or cooler pockets. The electronics add a little electrical noise, and the display rounds to whole degrees. None of these is a fault. They are the normal behavior of a real sensor in a real kadai.
So the next reading is also a random variable, . Unlike the vessel type, it is continuous: it can take any value in a range, not just one of a list. We cannot predict the next reading exactly, but we can summarize where readings tend to fall.
Mean and variance¶
The mean is the centre of the readings, their ordinary average. For readings :
The bar in (“t-bar”) marks a value computed from data. For our six readings, °C.
The variance measures how spread out the readings are: it is the average squared distance from the mean. Computed from a sample, it is
Squaring makes every distance positive, so readings above and below the mean both count. We divide by rather than because the mean itself was estimated from the same readings, which makes the readings look slightly less spread than they really are; dividing by corrects for this. The standard deviation is in the same units as the readings (°C), which makes it easier to interpret.
t = np.array([168, 171, 169, 173, 170, 172], dtype=float)
t_bar = t.mean()
deviations = t - t_bar
s2 = np.sum(deviations**2) / (len(t) - 1)
print("readings :", t)
print("deviations :", deviations)
print(f"mean : {t_bar:.1f} °C")
print(f"variance s² : {s2:.2f} °C²")
print(f"std. dev. s : {np.sqrt(s2):.2f} °C")readings : [168. 171. 169. 173. 170. 172.]
deviations : [-2.5 0.5 -1.5 2.5 -0.5 1.5]
mean : 170.5 °C
variance s² : 3.50 °C²
std. dev. s : 1.87 °C
A model for repeated readings: the Gaussian¶
If we collect many more readings with the heater steady, a pattern appears: most readings cluster near the centre, and readings far from the centre become steadily rarer, in a symmetric bell shape. This shape appears whenever many small, independent effects add together, as they do in the kadai. It is called the Gaussian (or normal) distribution, and we write
Read this as “ is Gaussian with mean and variance ”. Greek letters (“mu”) and (“sigma”) denote the true mean and standard deviation of the process; and are our estimates of them from data. The Gaussian’s probability density is
For a continuous quantity we use a density (lower-case ) rather than a PMF. The chance that a reading falls in a range is the area under the curve over that range. Two rules of thumb are worth remembering: about 68% of readings fall within , and about 95% fall within .
mu_true, sigma_true = 170.0, 1.5 # the "real" oil and thermometer, hidden from the robot
readings = np.round(rng.normal(mu_true, sigma_true, 100)) # display shows whole degrees
m, s = readings.mean(), readings.std(ddof=1)
def gaussian(x, mu, sigma):
return np.exp(-0.5 * ((x - mu) / sigma) ** 2) / np.sqrt(2 * np.pi * sigma**2)
xs = np.linspace(163, 177, 300)
plt.hist(readings, bins=np.arange(163.5, 177.5, 1), density=True, color="#9fb7cf", edgecolor="white", label="100 readings")
plt.plot(xs, gaussian(xs, m, s), c="#1f3a5f", lw=2, label=f"model N({m:.1f}, {s:.2f}²)")
plt.axvspan(m - 2 * s, m + 2 * s, color="#e07a3a", alpha=0.12, label="mean ± 2s")
plt.xlabel("thermometer reading [°C]"); plt.ylabel("density"); plt.legend(fontsize=8)
print(f"mean {m:.2f} °C, std. dev. {s:.2f} °C, fraction within ±2s: {np.mean(abs(readings - m) < 2 * s):.2f}")mean 169.89 °C, std. dev. 1.60 °C, fraction within ±2s: 0.97

Figure 2:One hundred thermometer readings of oil held near 170 °C (histogram) and the Gaussian model fitted to them (curve). The shaded band marks the mean ± 2 standard deviations, where about 95% of readings fall.
Do we need all 100 readings?¶
The robot now has 100 readings. Each takes a second, and a tadka does not wait. Does it need all of them to know the average temperature?
Two words help here. The population is everything we could in principle measure: all possible readings of this oil at this setting. A sample is the readings we actually take. Here we treat the 100 readings as the population and ask how well smaller samples estimate their mean.
We draw many random samples of 5, 20, and 50 readings, compute each sample’s mean, and see how much those means vary:
pop_mean = readings.mean()
sizes = [5, 20, 50]
fig, axes = plt.subplots(1, 3, figsize=(11, 3), sharex=True, sharey=True)
for ax, n in zip(axes, sizes):
means = np.array([rng.choice(readings, n, replace=False).mean() for _ in range(2000)])
ax.hist(means, bins=np.arange(167, 173.01, 0.2), color="#1f3a5f", alpha=0.8)
ax.axvline(pop_mean, c="#e07a3a", ls="--", lw=2)
ax.set_title(f"samples of {n}: spread ±{2 * means.std():.2f} °C", fontsize=9)
ax.set_xlabel("sample mean [°C]")
axes[0].set_ylabel("count"); plt.tight_layout()
print(f"mean of all 100 readings: {pop_mean:.2f} °C")mean of all 100 readings: 169.89 °C

Figure 3:Means of 2000 random samples of each size, drawn from the 100 readings. Small samples give means that scatter widely; larger samples cluster tightly around the mean of all 100 readings (dashed line).
The pattern is clear. With 5 readings, the sample mean can easily be off by more than a degree. With 50, it is almost always within a few tenths of a degree of the full-population mean. This variation from sample to sample is called sampling uncertainty.
There is a simple rule behind it. If single readings have standard deviation , the mean of independent readings has standard deviation
This is called the standard error of the mean. Quadrupling the number of readings only halves the uncertainty, so each extra reading helps less than the one before. For the robot, 10–20 readings (10–20 seconds) usually give a good enough average; waiting for 100 buys little extra precision while the mustard seeds burn.