Probability for Robotics · Theory · M2
M2-T1 — Discrete Random Variables and Probability Mass Functions
A random variable turns “what happened” into a number, and its mass function says how the unit of probability is shared among those numbers.
Events answer yes-or-no questions. Robots mostly ask “how many?” and “which one?”: how many of the last five camera frames saw the marker, which grid cell is the robot in, how many encoder ticks arrived. This lesson introduces the object that answers such questions, the discrete random variable, and the three distributions built from it that appear throughout robotics software.
Learning objectives
After this lesson, you should be able to:
- Define a random variable as a function of the outcome, and distinguish the variable from its value.
- Write and check a probability mass function, and read a cumulative distribution from it.
- Use the Bernoulli, binomial and categorical distributions and say what each assumes.
- Derive the binomial distribution and use it to design a k-of-n voting rule.
The intuition
Rover's camera looks for the marker above the charging dock. One frame gives one of two outcomes: detected or not. Over five frames there are possible sequences, such as hit, miss, hit, hit, hit. For deciding whether the dock is really there, the sequence is more detail than we need. What matters is one number: how many frames detected the marker.
That number is a random variable. It is not random in itself and it does not vary in the way a program variable does. It is a rule that reads a number off whatever outcome occurred: the sequence above gives 4. Before the frames are taken we do not know which number we will get, but we can say how probable each one is.
The list of those probabilities is the probability mass function. Picture the one unit of probability from the last module as one kilogram of clay. The mass function says how much clay sits on each possible value: a lump on 0, a lump on 1, and so on up to 5.
The two charts already show why counting helps. A single frame is wrong 20% of the time when the marker is present and 10% of the time when it is absent. The count over five frames separates the two situations far better: the mass sits on 4 and 5 in one case and on 0 and 1 in the other.
Write a capital letter for the random variable (the rule) and a small letter for a value it can take. “P(X = 3)” reads: the probability that the count X comes out as 3.
Definitions
- Random variable
- A function that assigns a number to every outcome: . It is discrete when its possible values can be listed.
- Value
- One number that can produce. is an event: the set of outcomes that the rule maps to .
- Probability mass function
- The probability of each value: . Often written when the variable is clear.
- Cumulative distribution
- The probability of a value at or below : . It rises from 0 to 1 in steps.
- Support
- The set of values with non-zero probability.
Because the events for different are disjoint and together cover the sample space, the axioms of the last module turn into two conditions on a mass function, and a rule for any event about :
Three distributions to know
| Distribution | Describes | Mass function | Parameters |
|---|---|---|---|
| Bernoulli | One yes-or-no trial: a detection, an occupied cell, a dropped packet. | : probability of “yes” | |
| Binomial | The number of “yes” results in independent trials with the same . | trials, each | |
| Categorical | One trial with several possible results: which cell, which object class, which door. | that sum to 1 |
The symbol reads “n choose k”: the number of ways to pick which of the trials are the successes. A Bernoulli variable is a binomial with , and a categorical variable with two categories is a Bernoulli.
Derivation: the binomial distribution
Let be the number of detections in frames. Two assumptions are made, and both will matter later: every frame detects with the same probability , and the frames are independent, meaning that the result of one tells you nothing about another. M3-T3: Independence and Conditional Independence defines independence precisely; the property used here is that the probability of a whole sequence is the product of the probabilities of its parts.
Independence: multiply the probability of each frame’s result. The example has and hits.
The product does not depend on the order of the factors, so every sequence with the same number of hits has the same probability.
A sequence is fixed by choosing which of the positions are hits.
The event is the union of those sequences. They are disjoint (two different sequences cannot both occur), so axiom 3 adds their equal probabilities.
Check of normalization, by the binomial theorem. The masses use up exactly the unit of probability.
From the mass function to a voting rule
A k-of-n rule declares the marker present when at least of frames detect it. Its probability of firing is the mass at and above :
Counting over several independent trials turns a mediocre yes-or-no sensor into a reliable one. The binomial distribution says exactly how reliable, and the word “independent” says when the promise holds.
A worked example
The marker detector fires on a frame with probability 0.8 when the marker is in view, and with probability 0.1 when it is not (a false alarm). Rover uses a 3-of-5 rule. How often does the rule fire in each case?
- Marker present, mass function. . For example . The six masses are 0.0003, 0.0064, 0.0512, 0.2048, 0.4096 and 0.3277.
- Marker present, rule fires. . The rule misses a marker that is there about 6 times in 100, against 20 in 100 for a single frame.
- Marker absent. : . False alarms fall from 10 in 100 to fewer than 1 in 100.
- The trade-off. A stricter rule, 4-of-5, cuts false alarms to 0.0005 but detects a real marker only 73.7% of the time. A looser rule, 2-of-5, detects 99.3% but raises false alarms to 8.1%. The threshold is a design choice between two kinds of error, made with these numbers.
- Check by simulation. 20 000 simulated groups of five frames give 0.9405 for the present case, within sampling error of 0.9421 (the standard error is 0.0017).
from robotprob import Rng, binomial_pmf
n, p_det, p_false = 5, 0.8, 0.1
for k in range(n + 1):
print(k, round(binomial_pmf(k, n, p_det), 5), round(binomial_pmf(k, n, p_false), 5))
def fires(k_min, p): # P(X >= k_min)
return sum(binomial_pmf(k, n, p) for k in range(k_min, n + 1))
for k_min in (2, 3, 4):
print(k_min, 'of 5: detection =', round(fires(k_min, p_det), 4), ' false alarm =', round(fires(k_min, p_false), 4))
rng = Rng(5) # check the 3-of-5 detection rate by simulation
trials = 20000
hits = sum(1 for _ in range(trials) if sum(1 for _ in range(n) if rng.bernoulli(p_det)) >= 3)
print('simulated 3 of 5 detection =', round(hits / trials, 4))0 0.00032 0.59049 1 0.0064 0.32805 2 0.0512 0.0729 3 0.2048 0.0081 4 0.4096 0.00045 5 0.32768 1e-05 2 of 5: detection = 0.9933 false alarm = 0.0815 3 of 5: detection = 0.9421 false alarm = 0.0086 4 of 5: detection = 0.7373 false alarm = 0.0005 simulated 3 of 5 detection = 0.9405
In robotics
Debouncing and voting
Bumpers, cliff sensors, fiducial detectors, “person present” classifiers and loop-closure candidates are all confirmed by some k-of-n rule before the robot acts on them. The binomial gives the detection and false-alarm rates of the rule, with one caution that the derivation made explicit: it assumes independent trials. Consecutive camera frames taken 30 ms apart share the same glare, the same blur and the same occlusion. Their errors come in runs, so the real rule is worse than the formula says. Either space the trials out or measure the rule's rates directly.
A belief over cells is a categorical distribution
A grid localizer stores one number per cell, non-negative and summing to 1. That is the parameter vector of a categorical distribution, and “the robot's cell” is the random variable. The histogram filter of M6-T2: Histogram Filters and Grid Localization is nothing more than two operations on this vector.
An occupancy cell is a Bernoulli variable
Each cell of an occupancy grid map is occupied or free, with a single parameter . Mapping is the estimation of that parameter for every cell from range beams (M10-T1: Occupancy Grids and the Binary Bayes Filter; the SLAM course has the practical view in Occupancy Grid Mapping).
Counts in estimation algorithms
The number of inliers that RANSAC finds for a wrong model, the number of packets lost in a burst of 100, and the number of beams of a scan that return nothing are binomial-like counts. Knowing their distribution tells you what count is ordinary and what count is a symptom (see Robust Estimation and RANSAC).
Quantized sensors
A wheel encoder reports a whole number of ticks and an analogue-to-digital converter a whole number of counts. Their outputs are discrete random variables even though the quantity behind them is continuous. The next lesson deals with the continuous side.
Common misconceptions
“A random variable is a variable whose value is random.”
It is a fixed function from outcomes to numbers; the randomness is in which outcome occurs. Keeping the function X and its value x apart is what makes statements like P(X = x) meaningful: X is the rule “count the hits”, x is 3.
“The binomial applies to any count of successes.”
It needs a fixed number of trials, the same success probability in each, and independence. Camera frames in a burst, lidar beams hitting the same glass pane and retries over the same bad radio link violate independence, and the binomial then gives overconfident numbers.
“A value with small mass will not happen.”
With five values the mass on any one can be small while one of them must occur. And small masses add up over time: a false alarm with probability 0.0086 per decision, evaluated ten times a second, occurs on average once every twelve seconds.
“More votes are always better.”
More trials improve both error rates only if the threshold is chosen well, and they cost time. Requiring 5 of 5 makes false alarms negligible but detects a real marker only a third of the time. Latency matters too: 5 frames at 10 Hz delay every decision by half a second.
Key takeaways
- A random variable is a rule that assigns a number to each outcome. A discrete one has a list of possible values.
- Its probability mass function gives the probability of each value: non-negative masses that sum to 1. The probability of any set of values is the sum of their masses.
- Bernoulli: one yes-or-no trial. Binomial: the number of successes in n independent trials with equal probability. Categorical: one trial with several outcomes.
- The binomial mass is (n choose k) p^k (1 − p)^(n − k): the probability of one sequence times the number of sequences.
- A k-of-n vote trades missed detections against false alarms. The binomial quantifies the trade, and it is optimistic when the trials are not independent.
References and further reading
Each source is marked free or paid. Free to read does not mean free to reuse: the course resources page lists every source with its licence.
- J. Tsitsiklis and P. Jaillet, MIT RES.6-012 Introduction to Probability , lectures 5 to 7 (discrete random variables). Free videos and notes, licence CC BY-NC-SA 4.0.
- J. Blitzstein and J. Hwang, Introduction to Probability, 2nd edition, CRC Press, 2019 , chapter 3 (random variables and their distributions). Free to read online; the book is copyrighted.
- C. Piech, Probability for Computer Scientists (Stanford CS109 course reader) , part 2: random variables, binomial distribution. Free to read online.
- S. Thrun, W. Burgard and D. Fox, Probabilistic Robotics, MIT Press, 2005, section 2.2 (discrete and continuous random variables). Paid book.
Knowledge check
Three questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
A proximity sensor reports “object” on a single reading with probability 0.9 when an object is there and 0.2 when it is not. The robot uses a 2-of-3 rule on three readings taken a second apart. Compute the detection and false-alarm probabilities of the rule.
Explanation
With n = 3 the rule fires for X = 2 or X = 3. Object present: 3 × 0.9² × 0.1 + 0.9³ = 0.243 + 0.729 = 0.972. Object absent: 3 × 0.2² × 0.8 + 0.2³ = 0.096 + 0.008 = 0.104. The vote improves detection from 0.90 to 0.972 and halves false alarms from 0.20 to 0.104. Readings a second apart are plausibly independent, so the binomial is a fair model here; with such a poor single-reading false-alarm rate a stricter rule or more readings would still be needed.
Question 2
In the expression P(X = 4), say what X, 4 and “X = 4” each are for the five-frame detection example, using the words outcome, function, value and event.
Explanation
An outcome is one complete sequence of five results, for example hit, miss, hit, hit, hit. X is a function (the random variable): it maps each outcome to the number of hits in it. 4 is a value that the function can take. “X = 4” is an event: the set of the five outcomes that contain exactly four hits. Its probability is the sum of the probabilities of those five sequences, 5 × 0.8⁴ × 0.2 = 0.4096.
Question 3
Your 3-of-5 fiducial rule should give false alarms 0.86% of the time according to the binomial, but in the field it gives about 4%. The per-frame false-alarm rate was measured correctly at 10%. What is the most likely explanation, and how would you test it?
Explanation
The frames are not independent. False detections are caused by something that persists across frames, such as a reflection or a poster that resembles the marker, so when one frame is wrong its neighbours tend to be wrong too. Runs of errors make “three or more of five” far more likely than the product of independent probabilities. Test: from logged data, compute the fraction of false frames that are followed by another false frame. Under independence it would be about 10%; a much higher value confirms the correlation. Remedies: vote over frames spaced further apart in time or viewpoint, or measure the rule’s rate directly instead of predicting it.
What’s next
Counts and cell indices are discrete. Distances, angles and times are not: they can take any value in a range. The next lesson shows what replaces the mass function when no single value can carry any mass.
Related content
Nothing below is required to finish this lesson.
Other courses on this site treat neighbouring topics from their own angle:
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.