AI Wiki中文/EN

Random variables

随机变量

Map uncertain outcomes into the quantity you care about, and distinguish a distribution, a realized value, a probability mass, and a density.

On this pageIntuitionMathematics & methodImplementationLimitationsExercisesResearch connections

One 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 TT as processing time, taking values 0, 2, and 8 milliseconds. Define RR 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. TT measures duration; RR 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.

OutcomeModel probabilityTT, millisecondsRR
Cache hit0.500
Ordinary calculation0.420
Retry0.181

Transfer probability through a mapping

A real-valued random variable XX maps outcomes ω\omega in a sample space Ω\Omega to real numbers. It must be measurable: questions such as “is X≤tX\le t?” 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 XX assigns probabilities to sets of its values. In the finite case:

P(X=x)=∑ω:X(ω)=xP({ω}),FX(t)=P(X≤t).P(X=x)=\sum_{\omega:X(\omega)=x}P(\{\omega\}),\qquad F_X(t)=P(X\le t).

Here FXF_X is the cumulative distribution function (CDF). Several outcomes can map to the same value, so their probabilities must be added. The table gives P(R=0)=0.9P(R=0)=0.9, not 0.5 or 0.4 alone. Uppercase TT denotes the random variable; lowercase t=2t=2 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 fXf_X instead assigns interval probabilities by area:

P(a<X≤b)=∫abfX(t) dt,fX(t)≥0,∫−∞∞fX(t) dt=1.P(a<X\le b)=\int_a^b f_X(t)\,dt,\qquad f_X(t)\ge0,\qquad \int_{-\infty}^{\infty} f_X(t)\,dt=1.

For a uniform waiting time on [0,0.2][0,0.2] seconds, density is 5 inside the interval. This is valid: area, not height, is probability. The interval (0.05,0.10](0.05,0.10] has probability 5×0.05=0.255\times0.05=0.25. 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 nn input outcomes and kk distinct mapped values, aggregation takes expected O(n)O(n) dictionary work and O(k)O(k) 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

  1. What is P(T≤2)P(T\le2) in the table? Answer: 0.9, including cache hits and ordinary calculations.
  2. If YY maps all three outcomes to 7, what is its distribution? Answer: P(Y=7)=1P(Y=7)=1. A constant is also a random variable.
  3. Does a density value of 5 violate the probability bound? Answer: no. Integrated probability must lie in [0,1][0,1]; density height is not point probability.
  4. 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?

Choose one answer

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

  1. Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
    Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020
  2. 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:

Related topics

No matches. Try a synonym or acronym.

0 results shown

Wrap the whole query in double quotes for a phrase match.