Introduction

Chance is described with numbers: the number of heads in ten tosses, the number of probes in a hash table, the waiting time until a server responds. A random variable attaches a number to each outcome of an experiment, so that we can talk about its distribution, its average value (expectation) and its spread (variance). These three ideas are used throughout algorithm analysis (expected running time, expected number of collisions in hashing), performance modelling and statistics.

Prerequisites

Core concepts

Random variables

A random variable (RV) XX is a function from the sample space SS to R\mathbb{R}. Example: toss three coins and let XX be the number of heads; the outcome HTHHTH gives X=2X = 2. The event {X=x}\{X = x\} is the set of outcomes mapped to xx.

  • XX is discrete if it takes finitely or countably many values.
  • XX is continuous if its probabilities are given by a density function, defined in the next table; then P(X=x)=0P(X = x) = 0 for every single value xx.

PMF, PDF and CDF

Discrete RVContinuous RV
Described byprobability mass function (pmf) p(x)=P(X=x)p(x) = P(X = x)probability density function (pdf) f(x)f(x)
Conditionsp(x)≥0p(x) \ge 0, ∑xp(x)=1\sum_x p(x) = 1f(x)≥0f(x) \ge 0, ∫−∞∞f(x) dx=1\int_{-\infty}^{\infty} f(x)\,dx = 1
P(a≤X≤b)P(a \le X \le b)∑a≤x≤bp(x)\sum_{a \le x \le b} p(x)∫abf(x) dx\int_a^b f(x)\,dx
P(X=a)P(X = a)p(a)p(a)0
Value at a pointa probability, at most 1a density; can exceed 1

The cumulative distribution function (cdf) is defined for every RV as

F(x)=P(X≤x),x∈R.F(x) = P(X \le x), \qquad x \in \mathbb{R}.

Properties: FF is non-decreasing, right-continuous, F(x)→0F(x) \to 0 as x→−∞x \to -\infty and F(x)→1F(x) \to 1 as x→∞x \to \infty; P(a<X≤b)=F(b)−F(a)P(a < X \le b) = F(b) - F(a). For a discrete RV, FF is a step function jumping by p(x)p(x) at each value xx. For a continuous RV, FF is continuous and F′(x)=f(x)F'(x)=f(x) almost everywhere (and at every point where the chosen density ff is continuous). For a continuous RV, << and ≤\le give the same probabilities.

PMF versus PDF versus CDF
Figure 1. Top row: the pmf of the number of heads in 3 fair tosses and its step-function cdf. Bottom row: the pdf f(x)=2xf(x) = 2x on [0,1][0, 1] and its cdf F(x)=x2F(x) = x^2.

Expectation

The expected value (mean) E[X]=μE[X] = \mu is the probability-weighted average:

E[X]=∑xx p(x)(discrete),E[X]=∫−∞∞x f(x) dx(continuous),E[X] = \sum_x x\, p(x) \quad \text{(discrete)}, \qquad E[X] = \int_{-\infty}^{\infty} x\, f(x)\,dx \quad \text{(continuous)},

provided the sum or integral converges absolutely (otherwise E[X]E[X] does not exist).

Law of the unconscious statistician (LOTUS): for a function gg, E[g(X)]=∑xg(x)p(x)E[g(X)] = \sum_x g(x) p(x) or ∫g(x)f(x) dx\int g(x) f(x)\,dx. There is no need to find the distribution of g(X)g(X) first.

Linearity of expectation: for any RVs X,YX, Y and constants a,ba, b,

E[aX+bY]=a E[X]+b E[Y],E[aX + bY] = a\,E[X] + b\,E[Y],

whether or not XX and YY are independent. This is a key fact in probabilistic algorithm analysis.

Products: E[XY]=E[X] E[Y]E[XY] = E[X]\,E[Y] if XX and YY are independent. The converse is false.

Indicator variables. For an event AA, the indicator IAI_A is 1 if AA occurs and 0 otherwise, so E[IA]=P(A)E[I_A] = P(A). Writing a count as a sum of indicators and using linearity avoids computing its distribution (Example 3).

Tail-sum formula. For an RV taking values in {0,1,2,…}\{0, 1, 2, \ldots\}, E[X]=∑k≥1P(X≥k)E[X] = \sum_{k \ge 1} P(X \ge k).

Variance and standard deviation

Var⁡(X)=σ2=E[(X−μ)2]=E[X2]−(E[X])2,σ=Var⁡(X).\operatorname{Var}(X) = \sigma^2 = E\big[(X - \mu)^2\big] = E[X^2] - (E[X])^2, \qquad \sigma = \sqrt{\operatorname{Var}(X)} .

The second form (the shortcut) follows by expanding the square and using linearity. Properties, for constants a,ba, b:

  • Var⁡(X)≥0\operatorname{Var}(X) \ge 0, with equality if and only if XX is constant with probability 1.
  • Var⁡(aX+b)=a2Var⁡(X)\operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X): shifting does not change spread; scaling by aa scales σ\sigma by ∣a∣|a|.
  • Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)+2Cov⁡(X,Y)\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) + 2\operatorname{Cov}(X, Y), where Cov⁡(X,Y)=E[XY]−E[X]E[Y]\operatorname{Cov}(X, Y) = E[XY] - E[X]E[Y].
  • If X,YX, Y are independent (or merely uncorrelated), Var⁡(X±Y)=Var⁡(X)+Var⁡(Y)\operatorname{Var}(X \pm Y) = \operatorname{Var}(X) + \operatorname{Var}(Y). Note the plus sign for the difference.

Variance convention. The variance of a random variable (a population quantity) is defined as the expectation E[(X−μ)2]E[(X - \mu)^2], with no nn or n−1n - 1 divisor. The sample variance of observed data uses a divisor n−1n - 1 and is covered in Descriptive Statistics.

Joint distributions (brief)

For discrete X,YX, Y, the joint pmf p(x,y)=P(X=x,Y=y)p(x, y) = P(X = x, Y = y) gives the marginals pX(x)=∑yp(x,y)p_X(x) = \sum_y p(x, y). XX and YY are independent if p(x,y)=pX(x) pY(y)p(x, y) = p_X(x)\,p_Y(y) for all x,yx, y. When both variances are finite and positive, the correlation coefficient ρ=Cov⁡(X,Y)/(σXσY)\rho=\operatorname{Cov}(X,Y)/(\sigma_X\sigma_Y) lies in [−1,1][-1,1]. If either variable is constant, ρ\rho is undefined, not automatically zero. With finite positive variances, independence implies ρ=0\rho=0; uncorrelated does not imply independent: if XX is uniform on {−1,0,1}\{-1, 0, 1\} and Y=X2Y = X^2, then Cov⁡(X,Y)=0\operatorname{Cov}(X, Y) = 0 but YY is determined by XX.

Covariance and correlation rules

Every rule involving correlation below assumes finite, strictly positive variances for both variables.

For RVs X,Y,ZX, Y, Z and constants a,b,c,da, b, c, d:

  • Cov⁡(X,X)=Var⁡(X)\operatorname{Cov}(X, X) = \operatorname{Var}(X), Cov⁡(X,Y)=Cov⁡(Y,X)\operatorname{Cov}(X, Y) = \operatorname{Cov}(Y, X), and Cov⁡(X,c)=0\operatorname{Cov}(X, c) = 0.
  • Covariance is bilinear: Cov⁡(aX+b,cY+d)=ac Cov⁡(X,Y)\operatorname{Cov}(aX + b, cY + d) = ac\,\operatorname{Cov}(X, Y) and Cov⁡(X+Y,Z)=Cov⁡(X,Z)+Cov⁡(Y,Z)\operatorname{Cov}(X + Y, Z) = \operatorname{Cov}(X, Z) + \operatorname{Cov}(Y, Z). Shifts drop out.
  • For a,c≠0a, c \ne 0, ρ(aX+b,cY+d)=sign⁡(ac) ρ(X,Y)\rho(aX + b, cY + d) = \operatorname{sign}(ac)\,\rho(X, Y): correlation has no units and ignores shifts and positive rescaling.
  • ∣ρ∣=1|\rho| = 1 if and only if Y=aX+bY = aX + b with probability 1 for some a≠0a \ne 0, and then ρ\rho has the sign of aa.
  • Var⁡(∑iXi)=∑iVar⁡(Xi)+2∑i<jCov⁡(Xi,Xj)\operatorname{Var}\big(\sum_i X_i\big) = \sum_i \operatorname{Var}(X_i) + 2\sum_{i < j} \operatorname{Cov}(X_i, X_j).

Conditional expectation. For discrete RVs, E[X∣Y=y]=∑xx P(X=x∣Y=y)E[X \mid Y = y] = \sum_x x\,P(X = x \mid Y = y). The law of total expectation is the expectation form of total probability:

E[X]=∑yE[X∣Y=y] P(Y=y).E[X] = \sum_y E[X \mid Y = y]\,P(Y = y).

Inequalities

  • Markov: if X≥0X \ge 0 and a>0a > 0, then P(X≥a)≤E[X]/aP(X \ge a) \le E[X]/a.
  • Chebyshev: for finite variance and t>0t>0, P(∣X−μ∣≥t)≤Var⁡(X)/t2P(|X-\mu|\ge t)\le\operatorname{Var}(X)/t^2. If 0<σ2<∞0<\sigma^2<\infty, taking t=kσt=k\sigma, k>0k>0, gives P(∣X−μ∣≥kσ)≤1/k2P(|X-\mu|\ge k\sigma)\le1/k^2. Thus at least 1−1/k21-1/k^2 lies strictly within kk standard deviations.

Worked examples

Example 1: a fair die

XX uniform on {1,…,6}\{1, \ldots, 6\}. E[X]=21/6=3.5E[X] = 21/6 = 3.5. E[X2]=91/6E[X^2] = 91/6, so Var⁡(X)=91/6−49/4=35/12≈2.917\operatorname{Var}(X) = 91/6 - 49/4 = 35/12 \approx 2.917. For the sum TT of two independent dice: E[T]=7E[T] = 7, Var⁡(T)=35/6\operatorname{Var}(T) = 35/6. For Y=2X+3Y = 2X + 3: E[Y]=10E[Y] = 10, Var⁡(Y)=4⋅35/12=35/3\operatorname{Var}(Y) = 4 \cdot 35/12 = 35/3.

Example 2: a continuous RV

f(x)=2xf(x) = 2x for 0≤x≤10 \le x \le 1, and 0 otherwise. Check: ∫012x dx=1\int_0^1 2x\,dx = 1. CDF: F(x)=x2F(x) = x^2 on [0,1][0, 1]. E[X]=∫012x2 dx=2/3E[X] = \int_0^1 2x^2\,dx = 2/3; E[X2]=∫012x3 dx=1/2E[X^2] = \int_0^1 2x^3\,dx = 1/2; Var⁡(X)=1/2−4/9=1/18\operatorname{Var}(X) = 1/2 - 4/9 = 1/18. P(X>1/2)=1−F(1/2)=3/4P(X > 1/2) = 1 - F(1/2) = 3/4. The median mm solves F(m)=1/2F(m) = 1/2: m=1/2≈0.707m = 1/\sqrt{2} \approx 0.707.

Example 3: indicators (expected fixed points)

A random permutation of nn items. Let IiI_i indicate that item ii is in place; P(Ii=1)=1/nP(I_i = 1) = 1/n. The number of fixed points X=∑IiX = \sum I_i has E[X]=n⋅(1/n)=1E[X] = n \cdot (1/n) = 1, for every nn, although the IiI_i are dependent.

Example 4: hashing

kk keys hashed independently and uniformly into mm slots. Slot jj is empty with probability (1−1/m)k(1 - 1/m)^k, so the expected number of empty slots is m(1−1/m)km(1 - 1/m)^k. With m=k=100m = k = 100 this is about 36.636.6, close to m/em/e. The expected number of colliding pairs is (k2)/m\binom{k}{2}/m.

Example 5: finding kk in a pmf

P(X=x)=k xP(X = x) = k\,x for x=1,2,3,4x = 1, 2, 3, 4. Then k(1+2+3+4)=1k(1 + 2 + 3 + 4) = 1, so k=1/10k = 1/10; E[X]=(1+4+9+16)/10=3E[X] = (1 + 4 + 9 + 16)/10 = 3; E[X2]=(1+8+27+64)/10=10E[X^2] = (1 + 8 + 27 + 64)/10 = 10; Var⁡(X)=1\operatorname{Var}(X) = 1.

Example 6: a joint pmf table

Y=0Y = 0Y=1Y = 1pX(x)p_X(x)
X=0X = 00.20.10.3
X=1X = 10.20.50.7
pY(y)p_Y(y)0.40.61

E[X]=0.7E[X] = 0.7, E[Y]=0.6E[Y] = 0.6, and E[XY]=P(X=1,Y=1)=0.5E[XY] = P(X = 1, Y = 1) = 0.5, so Cov⁡(X,Y)=0.5−0.42=0.08\operatorname{Cov}(X, Y) = 0.5 - 0.42 = 0.08. As Bernoulli variables, Var⁡(X)=0.7×0.3=0.21\operatorname{Var}(X) = 0.7 \times 0.3 = 0.21 and Var⁡(Y)=0.6×0.4=0.24\operatorname{Var}(Y) = 0.6 \times 0.4 = 0.24, so ρ=0.08/0.21×0.24≈0.356\rho = 0.08/\sqrt{0.21 \times 0.24} \approx 0.356. XX and YY are dependent: p(0,0)=0.2≠0.3×0.4=0.12p(0, 0) = 0.2 \ne 0.3 \times 0.4 = 0.12. Finally Var⁡(X+Y)=0.21+0.24+2(0.08)=0.61\operatorname{Var}(X + Y) = 0.21 + 0.24 + 2(0.08) = 0.61, which matches the direct computation from the pmf of X+YX + Y (values 0, 1, 2 with probabilities 0.2, 0.3, 0.5).

Example 7: law of total expectation

Roll a fair die, then toss a fair coin as many times as the die shows. Let NN be the die result, hence the number of coin tosses. Given N=nN=n, the expected number of heads is n/2n/2, so E[H]=∑n=16n2⋅16=E[N]2=1.75E[H] = \sum_{n=1}^{6} \frac{n}{2} \cdot \frac16 = \frac{E[N]}{2} = 1.75.

Example 8: expected inversions

In a random permutation of nn distinct keys, each of the (n2)\binom{n}{2} pairs is out of order with probability 1/21/2. By indicators and linearity, the expected number of inversions is (n2)/2=n(n−1)/4\binom{n}{2}/2 = n(n - 1)/4: 3 for n=4n = 4, 4950/2 = 2475 for n=100n = 100. This is the average number of adjacent swaps in bubble sort and adjacent-swap insertion sort; in the usual shift-based insertion sort it is the average number of rightward shifts.

Example 9: a function of a continuous RV

XX uniform on [0,1][0, 1] and Y=X2Y = X^2. Use the cdf: for 0≤y≤10 \le y \le 1, FY(y)=P(X≤y)=yF_Y(y) = P(X \le \sqrt{y}) = \sqrt{y}, so fY(y)=1/(2y)f_Y(y) = 1/(2\sqrt{y}), a density that is unbounded near 0 yet integrates to 1. By LOTUS, E[Y]=∫01x2 dx=1/3E[Y] = \int_0^1 x^2\,dx = 1/3, which differs from (E[X])2=1/4(E[X])^2 = 1/4 by Var⁡(X)=1/12\operatorname{Var}(X) = 1/12. The median of YY is (1/2)2=1/4(1/2)^2 = 1/4: medians pass through increasing functions, means do not.

Important properties and observations

  • E[X2]≥(E[X])2E[X^2] \ge (E[X])^2, since the difference is the variance.
  • E[1/X]≠1/E[X]E[1/X] \ne 1/E[X] and E[X2]≠(E[X])2E[X^2] \ne (E[X])^2 in general; only linear functions pass through EE.
  • The mean minimises E[(X−c)2]E[(X - c)^2] over constants cc; the minimum value is Var⁡(X)\operatorname{Var}(X).
  • A pdf value f(x)f(x) is not a probability; only areas under ff are.
  • For a non-negative continuous RV, E[X]=∫0∞P(X>x) dxE[X] = \int_0^\infty P(X > x)\,dx.

GATE examination perspective

  • Normalisation. Find the unknown constant in a pmf or pdf from total probability 1, then compute probabilities, mean or variance.
  • Mean and variance computations. Use E[X2]−μ2E[X^2] - \mu^2; use Var⁡(aX+b)=a2Var⁡(X)\operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X).
  • Linearity with indicators. Expected number of fixed points, empty bins, inversions, records; no independence needed.
  • CDF questions. Probabilities from F(b)−F(a)F(b) - F(a); pdf as the derivative of the cdf; for a continuous CDF, a median solves F(m)=1/2F(m)=1/2. Generally a median mm satisfies P(X≤m)≥1/2P(X\le m)\ge1/2 and P(X≥m)≥1/2P(X\ge m)\ge1/2.
  • Trap: adding variances of dependent RVs, or subtracting variances for X−YX - Y.
  • Trap: treating a density value larger than 1 as an error.
  • Joint tables. Marginals, E[XY]E[XY], covariance, correlation, and an independence check cell by cell (Example 6).
  • Trap: concluding independence from ρ=0\rho = 0; one cell with p(x,y)≠pX(x) pY(y)p(x, y) \ne p_X(x)\,p_Y(y) proves dependence.

Summary

Sources

  1. S. Ross, A First Course in Probability, 8th ed., Pearson Prentice Hall, 2010, Chapters 4 (Random Variables) and 5 (Continuous Random Variables), Sections 7.2 (Expectation of Sums of Random Variables), 7.4 (Covariance, Variance of Sums, and Correlations) and 8.2 (Chebyshev's Inequality and the Weak Law of Large Numbers).

Next steps