TExcelTAD
Home › Randomized Algorithms & Probabilistic Analysis › Expectation Tricks: A Recap

Expectation Tricks: A Recap

Almost every analysis in this course starts by computing an expectation. Four tools do most of the work: linearity, indicator variables, waiting times (the geometric distribution) and conditioning (the law of total expectation, used as first-step analysis). They were introduced in Part I and in the probability course; this lesson collects them in one place, adds the one-line probabilistic-method argument that turns an expectation into an existence proof, and previews where each one reappears.

Tool Statement First met in
Linearity E[X+Y]=E[X]+E[Y]\mathbb{E}[X + Y] = \mathbb{E}[X] + \mathbb{E}[Y], with no independence needed Linearity and Indicator Variables
Indicators E[IA]=Pr⁡[A]\mathbb{E}[I_A] = \Pr[A], so a count is a sum of probabilities Indicator Random Variables
Waiting times trials until the first success of probability pp: mean 1/p1/p Geometric and Negative Binomial
Conditioning E[X]=∑yPr⁡[Y=y] E[X∣Y=y]\mathbb{E}[X] = \sum_y \Pr[Y = y]\,\mathbb{E}[X \mid Y = y] Conditional Expectation

Linearity and indicators

The pattern: write the quantity of interest as a sum X=∑iIiX = \sum_i I_i of indicators, compute each Pr⁡[Ii=1]\Pr[I_i = 1], add. The indicators may be heavily dependent; linearity does not care. Randomized Quicksort and Its Expected Cost is the model example: the ii-th and jj-th smallest keys are compared with probability 2j−i+1\frac{2}{j - i + 1}, and summing over pairs gives 2nln⁡n+O(n)2n \ln n + O(n).

The same pattern gives one of the shortest existence proofs in combinatorics. Put each vertex of a graph with mm edges on a random side of a cut, independently with probability 1/21/2. An edge is cut when its endpoints land on different sides, which has probability 1/21/2, so

E[edges cut]=∑edges ePr⁡[e is cut]=m2.\mathbb{E}[\text{edges cut}] = \sum_{\text{edges } e} \Pr[e \text{ is cut}] = \frac{m}{2}.

A random variable cannot always be below its mean, so some cut has at least m/2m/2 edges. This is the probabilistic method in its simplest form. Turning such an argument into a deterministic algorithm is the subject of The Method of Conditional Expectations.

python · try it yourself
import random
from itertools import product

rng = random.Random(4)
n = 12
edges = [(u, v) for u in range(n) for v in range(u + 1, n) if rng.random() < 0.4]
m = len(edges)

def cut_size(side):
    return sum(side[u] != side[v] for u, v in edges)

samples = [cut_size([rng.randrange(2) for _ in range(n)]) for _ in range(5000)]
print(f"graph with n = {n}, m = {m} edges; m/2 = {m / 2}")
print(f"random cuts: mean {sum(samples) / len(samples):.2f}, largest seen {max(samples)}")
print(f"fraction of random cuts with at least m/2 edges: {sum(s >= m / 2 for s in samples) / len(samples):.3f}")
best = max(cut_size(side) for side in product([0, 1], repeat=n))
print("maximum cut by brute force over all 4096 cuts:", best)
graph with n = 12, m = 28 edges; m/2 = 14.0
random cuts: mean 13.98, largest seen 22
fraction of random cuts with at least m/2 edges: 0.621
maximum cut by brute force over all 4096 cuts: 22

Waiting times

If each attempt succeeds with probability pp, independently, the number of attempts up to and including the first success is geometric with mean 1/p1/p. When the success probability changes from phase to phase, linearity adds up the phases: the coupon collector needs ∑i=0n−1nn−i=nHn\sum_{i=0}^{n-1} \frac{n}{n - i} = n H_n draws to see all nn coupons. This shows up whenever a randomized algorithm "keeps trying until it works": Las Vegas loops (Las Vegas and Monte Carlo Algorithms), repeated Karger trials, and the coupling argument for the hypercube walk (Coupling), which is a coupon collector in disguise.

Conditioning and first-step analysis

To compute the expected time a process needs to reach a goal, condition on its first step. A typical example, and the heart of the randomized 2-SAT algorithm (Randomized 2-SAT): a walker on {0,1,…,n}\{0, 1, \ldots, n\} moves one step left or right with probability 1/21/2 each, except at nn, where it always moves left. Let hjh_j be the expected number of steps to reach 0 from jj. Conditioning on the first step,

h0=0,hj=1+12hj−1+12hj+1 (0<j<n),hn=1+hn−1.h_0 = 0, \qquad h_j = 1 + \tfrac{1}{2} h_{j-1} + \tfrac{1}{2} h_{j+1} \ (0 < j < n), \qquad h_n = 1 + h_{n-1}.

The differences dj=hj−hj−1d_j = h_j - h_{j-1} satisfy dn=1d_n = 1 and dj=dj+1+2d_j = d_{j+1} + 2, so dj=2(n−j)+1d_j = 2(n - j) + 1, and summing gives

hj=∑i=1j(2(n−i)+1)=2nj−j2,hn=n2.h_j = \sum_{i=1}^{j} \bigl(2(n - i) + 1\bigr) = 2nj - j^2, \qquad h_n = n^2 .
python · try it yourself
import random

def hitting_times(n):
    # solve the first-step equations exactly: h_j = h_{j-1} + d_j with d_n = 1, d_j = d_{j+1} + 2
    d = [0] * (n + 1)
    d[n] = 1
    for j in range(n - 1, 0, -1):
        d[j] = d[j + 1] + 2
    h = [0] * (n + 1)
    for j in range(1, n + 1):
        h[j] = h[j - 1] + d[j]
    return h

n = 10
h = hitting_times(n)
print("exact h_j:", h)
print("formula 2nj - j^2:", [2 * n * j - j * j for j in range(n + 1)])

rng = random.Random(8)
def walk(j):
    steps = 0
    while j > 0:
        j = j - 1 if j == n or rng.random() < 0.5 else j + 1
        steps += 1
    return steps
runs = 3000
print(f"simulated from j = n: mean {sum(walk(n) for _ in range(runs)) / runs:.1f} steps (exact {h[n]})")
exact h_j: [0, 19, 36, 51, 64, 75, 84, 91, 96, 99, 100]
formula 2nj - j^2: [0, 19, 36, 51, 64, 75, 84, 91, 96, 99, 100]
simulated from j = n: mean 98.6 steps (exact 100)

Reflection at nn makes the walk slower to escape than a free walk would be, yet the hitting time is only quadratic. The 2-SAT walk is at least as favourable as this one, which is where its O(n2)O(n^2) bound comes from.

From expectation to probability

An expectation alone says little about a single run. The bridge from "expected cost μ\mu" to "cost at most cμc\mu with high probability" is a tail bound. Markov's inequality needs only X≥0X \ge 0 and gives Pr⁡[X≥cμ]≤1/c\Pr[X \ge c\mu] \le 1/c. The next lessons build stronger bridges: Chebyshev with the variance, and the Chernoff and Hoeffding bounds for sums of independent variables, which give exponentially small tails.

One more distinction to keep in mind throughout: in expectation and with high probability (w.h.p., meaning with probability at least 1−1/nc1 - 1/n^c for a constant cc, often c=1c = 1) are different guarantees. The maximum load of balls in bins, for example, is a with-high-probability statement, and its expectation has the same order but needs a separate argument.

Test yourself

The indicators I1,…,InI_1, \ldots, I_n are strongly dependent, and Pr⁡[Ii=1]=1/n\Pr[I_i = 1] = 1/n for each ii. What is E[I1+⋯+In]\mathbb{E}[I_1 + \cdots + I_n]?

  1. It cannot be computed without the joint distribution
  2. 1
  3. 1/n1/n
  4. nn

A random cut of a graph with mm edges has m/2m/2 edges in expectation. What can you conclude?

  1. Every cut has m/2m/2 edges
  2. Some cut has at least m/2m/2 edges
  3. The maximum cut has exactly m/2m/2 edges
  4. Half of all cuts have more than m/2m/2 edges

A walker on {0,…,n}\{0, \ldots, n\}, reflected at nn, steps left or right with probability 1/21/2. What is the expected time to reach 0 from nn?

  1. nn
  2. 2n2n
  3. n2n^2
  4. 2n2^n