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 | , with no independence needed | Linearity and Indicator Variables |
| Indicators | , so a count is a sum of probabilities | Indicator Random Variables |
| Waiting times | trials until the first success of probability : mean | Geometric and Negative Binomial |
| Conditioning | Conditional Expectation |
Linearity and indicators
The pattern: write the quantity of interest as a sum of indicators, compute each , add. The indicators may be heavily dependent; linearity does not care. Randomized Quicksort and Its Expected Cost is the model example: the -th and -th smallest keys are compared with probability , and summing over pairs gives .
The same pattern gives one of the shortest existence proofs in combinatorics. Put each vertex of a graph with edges on a random side of a cut, independently with probability . An edge is cut when its endpoints land on different sides, which has probability , so
A random variable cannot always be below its mean, so some cut has at least 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.
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 , independently, the number of attempts up to and including the first success is geometric with mean . When the success probability changes from phase to phase, linearity adds up the phases: the coupon collector needs draws to see all 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 moves one step left or right with probability each, except at , where it always moves left. Let be the expected number of steps to reach 0 from . Conditioning on the first step,
The differences satisfy and , so , and summing gives
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 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 bound comes from.
From expectation to probability
An expectation alone says little about a single run. The bridge from "expected cost " to "cost at most with high probability" is a tail bound. Markov's inequality needs only and gives . 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 for a constant , often ) 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 are strongly dependent, and for each . What is ?
- It cannot be computed without the joint distribution
- 1
A random cut of a graph with edges has edges in expectation. What can you conclude?
- Every cut has edges
- Some cut has at least edges
- The maximum cut has exactly edges
- Half of all cuts have more than edges
A walker on , reflected at , steps left or right with probability . What is the expected time to reach 0 from ?