Skip to the lesson
FixTheRobot Explore Robotics / Probability for Robotics / M2 Random Variables and Probability Distributions / M2-T1

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.

  • Lesson 1 of 4
  • Beginner
  • Module 2: Random Variables and Probability Distributions
  • Theory
  • About 13 minutes to read

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 25=322^5 = 32 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.

Probability mass functions of the number of detections in five framesTwo bar charts over the values 0 to 5. Left, marker present, detection probability 0.8 per frame: the bars rise to 0.410 at four and 0.328 at five. Right, marker absent, false detection probability 0.1 per frame: the bars fall from 0.590 at zero to almost nothing at three. In both charts the bars for three, four and five are highlighted: their total is the probability that a three-of-five vote fires.00.20.40.6k = frames with a detection, out of 5Marker presentp = 0.8 per frame0<0.00110.00620.05130.20540.41050.32800.20.40.6k = frames with a detection, out of 5Marker absentp = 0.1 per frame00.59010.32820.07330.0084<0.0015<0.001
How the unit of probability is shared among the possible counts, when the marker is there and when it is not. A rule “declare the dock if at least 3 of 5 frames detect it” fires with the total height of the highlighted bars.

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 XX
A function that assigns a number to every outcome: X:Ω→RX : \Omega \to \mathbb{R}. It is discrete when its possible values can be listed.
Value xx
One number that XX can produce. {X=x}\{X = x\} is an event: the set of outcomes that the rule maps to xx.
Probability mass function pX(x)p_X(x)
The probability of each value: pX(x)=P(X=x)p_X(x) = P(X = x). Often written p(x)p(x) when the variable is clear.
Cumulative distribution FX(x)F_X(x)
The probability of a value at or below xx: FX(x)=P(X≤x)F_X(x) = P(X \le x). It rises from 0 to 1 in steps.
Support
The set of values with non-zero probability.

Because the events {X=x}\{X = x\} for different xx 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 XX:

Probability mass function
pX(x)≥0,∑xpX(x)=1,P(X∈A)=∑x∈ApX(x)p_X(x) \ge 0, \qquad \sum_{x} p_X(x) = 1, \qquad P(X \in A) = \sum_{x \in A} p_X(x)
The sums run over the possible values. The third formula is axiom 3: the probability of a set of values is the sum of their masses.

Three distributions to know

Three discrete distributions
DistributionDescribesMass functionParameters
BernoulliOne yes-or-no trial: a detection, an occupied cell, a dropped packet.p(1)=p,  p(0)=1−pp(1) = p,\; p(0) = 1 - ppp: probability of “yes”
BinomialThe number of “yes” results in nn independent trials with the same pp.p(k)=(nk)pk(1−p)n−kp(k) = \binom{n}{k} p^{k} (1-p)^{n-k}nn trials, pp each
CategoricalOne trial with several possible results: which cell, which object class, which door.p(i)=θip(i) = \theta_iθ1,…,θm≥0\theta_1, \dots, \theta_m \ge 0 that sum to 1

The symbol (nk)=n!k! (n−k)!\binom{n}{k} = \frac{n!}{k!\,(n-k)!} reads “n choose k”: the number of ways to pick which kk of the nn trials are the successes. A Bernoulli variable is a binomial with n=1n = 1, and a categorical variable with two categories is a Bernoulli.

Derivation: the binomial distribution

Let XX be the number of detections in nn frames. Two assumptions are made, and both will matter later: every frame detects with the same probability pp, 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.

  1. P(hit, hit, miss, hit, miss)=p⋅p⋅(1−p)⋅p⋅(1−p)=p3(1−p)2P(\text{hit, hit, miss, hit, miss}) = p \cdot p \cdot (1-p) \cdot p \cdot (1-p) = p^{3}(1-p)^{2}

    Independence: multiply the probability of each frame’s result. The example has n=5n = 5 and k=3k = 3 hits.

  2. P(any one sequence with k hits)=pk(1−p)n−kP(\text{any one sequence with } k \text{ hits}) = p^{k}(1-p)^{n-k}

    The product does not depend on the order of the factors, so every sequence with the same number of hits has the same probability.

  3. #{sequences with k hits}=(nk)\#\{\text{sequences with } k \text{ hits}\} = \binom{n}{k}

    A sequence is fixed by choosing which kk of the nn positions are hits.

  4. P(X=k)=(nk) pk(1−p)n−kP(X = k) = \binom{n}{k}\, p^{k}(1-p)^{n-k}

    The event {X=k}\{X = k\} is the union of those sequences. They are disjoint (two different sequences cannot both occur), so axiom 3 adds their equal probabilities.

  5. ∑k=0n(nk) pk(1−p)n−k=(p+(1−p))n=1\sum_{k=0}^{n} \binom{n}{k}\, p^{k}(1-p)^{n-k} = \bigl(p + (1-p)\bigr)^{n} = 1

    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 kk of nn frames detect it. Its probability of firing is the mass at and above kk:

Probability that a k-of-n rule fires
P(X≥k)=∑j=kn(nj) pj(1−p)n−j=1−FX(k−1)P(X \ge k) = \sum_{j=k}^{n} \binom{n}{j}\, p^{j}(1-p)^{n-j} = 1 - F_X(k-1)
Evaluate it twice: with pp = the per-frame detection probability (you want the result near 1) and with pp = the per-frame false-alarm probability (you want it near 0).

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?

  1. Marker present, mass function. X∼Binomial(5,0.8)X \sim \text{Binomial}(5, 0.8). For example P(X=3)=(53) 0.83 0.22=10×0.512×0.04=0.2048P(X = 3) = \binom{5}{3}\,0.8^{3}\,0.2^{2} = 10 \times 0.512 \times 0.04 = 0.2048. The six masses are 0.0003, 0.0064, 0.0512, 0.2048, 0.4096 and 0.3277.
  2. Marker present, rule fires. P(X≥3)=0.2048+0.4096+0.3277=0.9421P(X \ge 3) = 0.2048 + 0.4096 + 0.3277 = 0.9421. The rule misses a marker that is there about 6 times in 100, against 20 in 100 for a single frame.
  3. Marker absent. X∼Binomial(5,0.1)X \sim \text{Binomial}(5, 0.1): P(X≥3)=0.0081+0.00045+0.00001=0.0086P(X \ge 3) = 0.0081 + 0.00045 + 0.00001 = 0.0086. False alarms fall from 10 in 100 to fewer than 1 in 100.
  4. 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.
  5. 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
Columns of the first table: k, mass with the marker present, mass with the marker absent.

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 pp. 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

  1. A random variable is a rule that assigns a number to each outcome. A discrete one has a list of possible values.
  2. 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.
  3. Bernoulli: one yes-or-no trial. Binomial: the number of successes in n independent trials with equal probability. Categorical: one trial with several outcomes.
  4. The binomial mass is (n choose k) p^k (1 − p)^(n − k): the probability of one sequence times the number of sequences.
  5. 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.

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.

Next lesson

M2-T2 — Continuous Random Variables, Densities and the CDF

Explain why a density is not a probability, and compute probabilities of intervals from a density or a cumulative distribution.

Nothing below is required to finish this lesson.

Other courses on this site treat neighbouring topics from their own angle: