Search

Search papers and lessons

Random Variable

Level 1 · 9 min read

A random variable is a rule that turns each outcome of a random experiment into a number, so we can ask how likely each value is.

On this page

  • Colour = topic mix
  • Probability
  • Sign in to see your progress

TL;DR

A random variable is a rule that turns each outcome of a random experiment into a number. Once outcomes are numbers, we can ask how likely each value is, what a typical value looks like, and how two quantities relate. Most of statistics is about random variables.

Where you'll see it: in the number of heads in ten coin flips, in the time a web request takes, in the noise added to a sensor reading, and in the hidden state of a model.

Intuition

Naming an outcome with a number

Flip two coins. The experiment has four outcomes, HH, HT, TH and TT, and none of them is a number. If you only care how many heads came up, you can attach a number to each outcome: 2, 1, 1, 0. That attachment is the random variable. It is a measuring device applied to a random outcome, so the value you see is random because the outcome is.

Three pieces are involved. The sample space Ω\Omega is the set of all outcomes. The outcome itself, written ω\omega, is what actually happens on one run. The random variable XX is a fixed rule that takes ω\omega and returns a number X(ω)X(\omega). The randomness lives in ω\omega. The rule XX is not random.

flowchart LR
  W[Outcome omega, random] --> X[Random variable X, a fixed rule]
  X --> V[Number such as 0, 1 or 2]

Why bother? Numbers can be added, averaged, compared and plotted. Outcomes like "heads then tails" cannot. Turning outcomes into numbers is what lets us use arithmetic and the tools built on top of it, such as the expected value and the variance.

Discrete and continuous

A random variable is discrete if it takes values from a list you could count, like 0, 1, 2 heads. It is continuous if it can take any value in an interval, like the time a request takes. The two cases use different tools, and the difference matters most when we come to probabilities of single values.

The math

Definition

Definition: Random variable

A random variable XX is a function X:Ω→RX : \Omega \to \mathbb{R} that assigns a real number X(ω)X(\omega) to each outcome ω\omega in the sample space Ω\Omega. It is defined on a probability space (Ω,F,P)(\Omega, \mathcal{F}, P), so that events about XX have probabilities.

An event about XX is a set of outcomes. For example, the event "XX equals xx" is the set {ω:X(ω)=x}\{\omega : X(\omega) = x\}, the outcomes the rule maps to xx. Its probability is written P(X=x)P(X = x). Writing P(X=x)P(X = x) is shorthand for "the probability of the outcomes that XX sends to xx".

Distributions for discrete variables

For a discrete XX, the probability mass function (PMF) is

p(x)=P(X=x).p(x) = P(X = x).

Each value is non-negative, and the values add up to 1: ∑xp(x)=1\sum_x p(x) = 1, where the sum runs over every value xx that XX can take. The PMF is the full description of a discrete random variable. It tells you which values are possible and how likely each one is.

The cumulative distribution function

The cumulative distribution function (CDF) works for discrete and continuous variables alike:

F(x)=P(X≤x).F(x) = P(X \le x).

It never decreases as xx grows, it approaches 0 on the far left and 1 on the far right, and the probability of an interval follows from it: P(a<X≤b)=F(b)−F(a)P(a \lt X \le b) = F(b) - F(a).

Continuous variables

For a continuous XX, a single point has zero probability: P(X=x)=0P(X = x) = 0 for every xx. A point has no width, so it cannot hold any of the probability. Probabilities come from intervals instead, through a density ff, which is the subject of the probability density function:

P(a≤X≤b)=∫abf(x) dx.P(a \le X \le b) = \int_a^b f(x)\, dx.

The CDF is then F(x)=∫−∞xf(t) dtF(x) = \int_{-\infty}^{x} f(t)\, dt. Here f(x)f(x) itself is not a probability and can be larger than 1. Only areas under ff are probabilities.

Functions of a random variable

Apply a rule to XX and you get a new random variable Y=g(X)Y = g(X), built on the same outcomes. If XX is the number of heads in two coin flips and Y=2X+1Y = 2X + 1, then YY takes the values 1, 3 and 5 with probabilities 14\tfrac14, 12\tfrac12 and 14\tfrac14. The probability of Y=3Y = 3 is the probability of X=1X = 1, because 2⋅1+1=32 \cdot 1 + 1 = 3.

When two random variables are considered together, their joint behaviour is described by joint probabilities such as P(X=x and Y=y)P(X = x \text{ and } Y = y). Whether they are related at all is the question of statistical independence.

Worked examples

Example
Heads in two coin flips

Flip a fair coin twice. The sample space is Ω={HH,HT,TH,TT}\Omega = \{HH, HT, TH, TT\}, and each outcome has probability 14\tfrac14. Let XX be the number of heads.

  1. Map each outcome to a number: X(HH)=2X(HH) = 2, X(HT)=1X(HT) = 1, X(TH)=1X(TH) = 1, X(TT)=0X(TT) = 0.
  2. Count the outcomes that give each value. X=0X = 0 has one outcome, X=1X = 1 has two, X=2X = 2 has one.
  3. So P(X=0)=14P(X = 0) = \tfrac14, P(X=1)=24=12P(X = 1) = \tfrac24 = \tfrac12, and P(X=2)=14P(X = 2) = \tfrac14.
  4. Check: 14+12+14=1\tfrac14 + \tfrac12 + \tfrac14 = 1, as it must.
  5. The probability of at least one head is P(X≥1)=12+14=34P(X \ge 1) = \tfrac12 + \tfrac14 = \tfrac34.

Interpretation: the random variable turns four equally likely sequences into three numbers with unequal weights. The middle value is the most likely because two of the four sequences produce it.

Example
The sum of two dice

Roll two fair dice and let SS be the sum of the faces. The sample space has 36 equally likely ordered pairs.

  1. S=2S = 2 only for (1,1)(1, 1), so P(S=2)=136P(S = 2) = \tfrac{1}{36}.
  2. S=7S = 7 for (1,6),(2,5),(3,4),(4,3),(5,2),(6,1)(1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1), six pairs, so P(S=7)=636=16P(S = 7) = \tfrac{6}{36} = \tfrac16.
  3. S≤4S \le 4 covers the sums 2, 3 and 4, which have 1+2+3=61 + 2 + 3 = 6 pairs between them. So P(S≤4)=636=16P(S \le 4) = \tfrac{6}{36} = \tfrac16.
  4. The CDF at 4 is F(4)=16F(4) = \tfrac16, and P(5≤S≤7)=F(7)−F(4)P(5 \le S \le 7) = F(7) - F(4) requires the sums 5, 6 and 7, which contribute 4+5+6=154 + 5 + 6 = 15 pairs, a probability of 1536\tfrac{15}{36}.

Interpretation: SS is discrete, so probabilities attach to single values, and a table of 11 numbers describes it completely.

Example
A wait with a uniform distribution

A request waits TT seconds, where TT is uniform between 0 and 10. Its density is f(t)=110f(t) = \tfrac{1}{10} on [0,10][0, 10] and 0 elsewhere, so its CDF is F(t)=t10F(t) = \tfrac{t}{10} for 0≤t≤100 \le t \le 10.

  1. P(T≤2.5)=F(2.5)=2.510=0.25P(T \le 2.5) = F(2.5) = \tfrac{2.5}{10} = 0.25.
  2. P(4≤T≤7)=F(7)−F(4)=0.7−0.4=0.3P(4 \le T \le 7) = F(7) - F(4) = 0.7 - 0.4 = 0.3.
  3. P(T=5)=0P(T = 5) = 0, because a single value has no width. The chance of landing exactly on 5.000000 seconds is zero, yet TT still lands somewhere every time.

Interpretation: questions about continuous variables are always about intervals. A request finishes within 2.5 seconds one time in four.

−202468101200.20.40.60.81t (seconds)P(T ≤ t)
  • CDF of T, uniform on [0, 10]
The CDF rises linearly from 0 to 1 across the interval. The marked points are the values used in the worked example.

Tradeoffs and failure modes

Treating a random variable as a single fixed number is the most common mistake. The rule is fixed, but its output changes from run to run, so the right question is always about the distribution, not one value.

SituationWhat goes wrongBetter choice
Using a density value as if it were a probabilityFor continuous XX, f(x)f(x) can exceed 1 and is not a chanceCompute the area under ff over an interval
Asking for P(X=x)P(X = x) on a continuous variableThe answer is always 0, which hides the real questionAsk about an interval, or use the CDF
Treating a count as continuousA count jumps in whole steps, so a smooth density misstates itUse a PMF for counts
Calling a fixed but unknown quantity randomMixes up uncertainty about a fixed value with randomness in a processSay explicitly what is random: the data, a parameter, or a hidden state

Random variables also hide dependence. Two variables with the same distribution can still be tied together. The distribution of each alone does not reveal that, so check the joint behaviour before combining them.

History

Early probability was about games of chance. In 1654, Blaise Pascal and Pierre de Fermat exchanged letters about how to divide the stakes of an interrupted game. They reasoned about outcomes and their weights, but they had no general notion of a quantity that takes values.

Through the 1700s, Jacob Bernoulli, Abraham de Moivre and Pierre-Simon Laplace worked out the distributions of counts and sums, such as the number of successes in many trials. These were the first distributions of quantities that we would now call random variables, though the term had not been fixed yet.

The modern definition came with Andrey Kolmogorov's 1933 axiomatisation of probability, which described a random variable as a function on a probability space. That formulation let one set of tools, sums, averages and integrals, handle dice, measurements and noise in a single framework. It is why the idea caught on and why nearly every later statistics text starts here.

Check yourself

QuestionIn your own words, what is a random variable? Is it random?Show answer

A random variable is a rule that assigns a number to each outcome of a random experiment. The rule itself is fixed and not random. What is random is the outcome, and so the value the rule produces.

QuestionYou roll two fair dice and let MM be the larger of the two faces. What is P(M≤3)P(M \le 3)?Show answer

M≤3M \le 3 means both dice show 1, 2 or 3. That is 3×3=93 \times 3 = 9 of the 36 equally likely pairs, so P(M≤3)=936=14P(M \le 3) = \tfrac{9}{36} = \tfrac14.

QuestionWhy is P(T=5)=0P(T = 5) = 0 for a continuous wait time TT, even though TT always takes some value?Show answer

A single point has no width, so it covers none of the area under the density. Probability for a continuous variable comes from intervals, where the area is positive. The value 5 is not impossible, but the chance of landing exactly on it is zero.

QuestionA store's waiting time WW, in minutes, is uniform between 0 and 20. What is P(W>15)P(W \gt 15), and what does it tell a manager?Show answer

P(W>15)=1−F(15)=1−1520=14P(W \gt 15) = 1 - F(15) = 1 - \tfrac{15}{20} = \tfrac14. About one customer in four waits more than 15 minutes, so a manager should plan staffing for that tail, not just for the average wait of 10 minutes.

Sources