Random variables
随机变量
Map uncertain outcomes into the quantity you care about, and distinguish a distribution, a realized value, a probability mass, and a density.
random-variablesOne experiment, several useful measurements
A small service has three possible outcomes for a request: a cache hit, an ordinary calculation, or a retry. In an invented model, their probabilities are 0.5, 0.4, and 0.1. Define as processing time, taking values 0, 2, and 8 milliseconds. Define as the retry indicator: 0, 0, and 1 for the same outcomes.
The request produces one outcome, but we can ask different questions about it. measures duration; records whether retrying occurred. Once the outcome is known, these mappings are fixed. The uncertainty comes from which outcome occurs, not from changing the definition of the mapping.
| Outcome | Model probability | , milliseconds | |
|---|---|---|---|
| Cache hit | 0.5 | 0 | 0 |
| Ordinary calculation | 0.4 | 2 | 0 |
| Retry | 0.1 | 8 | 1 |
Transfer probability through a mapping
A real-valued random variable maps outcomes in a sample space to real numbers. It must be measurable: questions such as “is ?” must correspond to events to which the model assigns probabilities. For a finite space with all subsets allowed as events, every such mapping meets this requirement. MML, Section 6.1
The distribution of assigns probabilities to sets of its values. In the finite case:
Here is the cumulative distribution function (CDF). Several outcomes can map to the same value, so their probabilities must be added. The table gives , not 0.5 or 0.4 alone. Uppercase denotes the random variable; lowercase may denote one observed value. Other texts use different typography, so read their notation explicitly.
A discrete distribution has a probability mass function over a finite or countable set. A distribution with a probability density instead assigns interval probabilities by area:
For a uniform waiting time on seconds, density is 5 inside the interval. This is valid: area, not height, is probability. The interval has probability . Density has reciprocal units; here it is measured per second. Deep Learning, Sections 3.2–3.3
Aggregate outcomes without sampling
This complete Python program computes a finite distribution by enumeration. Exact fractions make it possible to check the probability sums directly. A separate CDF calculates the uniform interval probability.
from fractions import Fraction as F
import math
def distribution(outcomes, mapping):
if not outcomes or any(p < 0 for _, p in outcomes):
raise ValueError("nonempty nonnegative probabilities required")
if sum(p for _, p in outcomes) != 1:
raise ValueError("probabilities must sum to one")
result = {}
for outcome, probability in outcomes:
value = mapping[outcome]
result[value] = result.get(value, F(0)) + probability
return result
worlds = [("cache", F(1,2)), ("compute", F(2,5)), ("retry", F(1,10))]
time = distribution(worlds, {"cache": 0, "compute": 2, "retry": 8})
retry = distribution(worlds, {"cache": 0, "compute": 0, "retry": 1})
assert time == {0: F(1,2), 2: F(2,5), 8: F(1,10)}
assert retry == {0: F(9,10), 1: F(1,10)}
assert sum(p for t, p in time.items() if t <= 2) == F(9,10)
def uniform_cdf(t, low=0.0, high=0.2):
if not (all(math.isfinite(v) for v in [t,low,high]) and low < high):
raise ValueError("finite value and ordered bounds required")
return min(1.0, max(0.0, (t-low)/(high-low)))
assert uniform_cdf(-1) == 0 and uniform_cdf(1) == 1
assert math.isclose(uniform_cdf(.10)-uniform_cdf(.05), .25)
assert 1/.2 == 5
With input outcomes and distinct mapped values, aggregation takes expected dictionary work and storage, apart from fraction arithmetic costs. Enumeration establishes the result for this finite model. A random simulation would estimate these probabilities with sampling error instead.
Not every distribution has an ordinary density
Suppose a waiting time is exactly zero with probability 0.3 and otherwise lies uniformly between 1 and 3 seconds. Its CDF jumps by 0.3 at zero, then rises continuously later. An ordinary density integrated against length cannot represent that point mass. A CDF still describes the whole real-valued distribution; the discrete-versus-density distinction does not exhaust every possibility. MML, Section 6.2.2
For a distribution with a density, every particular singleton has probability zero. That does not mean sampling fails to produce a value. Distinguish a prespecified exact value from an interval, and distinguish a mathematical measurement from its rounded display.
Coding categories as integers does not make arithmetic on those codes meaningful. Assigning request types codes 1, 2, and 3 does not make type 3 “three times” type 1. Choose a numerical quantity such as duration or an indicator when arithmetic is needed.
Identify the object before calculating
- What is in the table? Answer: 0.9, including cache hits and ordinary calculations.
- If maps all three outcomes to 7, what is its distribution? Answer: . A constant is also a random variable.
- Does a density value of 5 violate the probability bound? Answer: no. Integrated probability must lie in ; density height is not point probability.
- Can two different random variables have the same distribution? Answer: yes. Their individual distributions need not reveal how their values are coupled in a joint experiment.
Choose the quantity your model predicts
Regression may predict a duration, classification may model a category, and reliability analysis may model a failure indicator. These quantities have different supports, units, and useful summaries. Defining the random variable first clarifies whether a model output is a sample, a mean, a probability, or a density. The next articles derive summaries and examine relationships between variables.
Make a prediction, then check
A density is constant at 5 over an interval. What is the probability of a subinterval of width 0.05?
Learning objectives
- Map finite outcomes to values and aggregate their probabilities into a distribution.
- Distinguish probability mass, density, CDFs, and point masses in mixed distributions.
Prerequisites & connections
Explore related knowledge →References
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020 - Deep Learning — Chapter 3: Probability and Information Theory
Ian Goodfellow; Yoshua Bengio; Aaron Courville · 2016
Version1 · Editorial status: Recorded technical checks completed
View technical check record (AI)
Codex · 2026-09-05 · Codex technical check: cited-source comparison, re-derivation and recalculation, bilingual argument comparison, and executed Python assertions; no independent human peer review.
- Recomputed outcome-to-duration and retry-indicator probabilities, checking P(R=0)=0.9 and uniform interval probability 0.25.
- Defined CDFs, measurable mappings, density units, and point masses in mixed distributions; checked finite enumeration and finite CDF input constraints.
- Compared arguments, modeling assumptions, numbers, and exercise answers across languages; structural checks separately compare display formulas, code, references, and main sections. No independent human peer review.
Source locations checked:
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft) · §6.1–6.2; PDF pp. 181, 186–188 (2024-01-15 draft)
- Deep Learning — Chapter 3: Probability and Information Theory · §3.2–3.3: random variables, probability mass, and density
Related topics