Expected value and linearity

Lesson 3 of 99 min read
In this lesson6 sections

Quant interviews like to end a probability question with "so, would you play?", and data science interviews ask for the expected number of something: idle servers, repeat users, retries. Both reward the same habit. You rarely need the full distribution of a random quantity. You need its average, and the fastest route to an average is usually to break the quantity into small pieces and add up their averages. By the end of this lesson you will be able to compute an expected count without its distribution, give the spread of a sum correctly, find an expected waiting time, and price a simple game well enough to decide whether to play.

Expected value is a probability-weighted average

A random variable is a number decided by chance, such as a payout or a count. Its expected value E[X], also called its mean μ, is each possible value times its probability, summed:

E[X] = sum over all values x of x × P(X = x).

It is the long-run average per trial, and it need not be a value X can actually take.

A scratch card costs $1. It pays $10 with probability 1/20, $2 with probability 1/5, and nothing otherwise:

E[payout] = 10 × 1/20 + 2 × 1/5 = 0.50 + 0.40 = $0.90.

Each card loses 10 cents on average, 10% of the price.

Linearity and indicator variables

Linearity of expectation says E[X + Y] = E[X] + E[Y], and E[cX] = c × E[X] for any constant c. It holds whether or not X and Y are independent, and that is what makes it so useful.

Pair it with indicator variables. The indicator of an event is 1 if the event happens and 0 if it doesn't, so its expected value is the event's probability. Any count is a sum of indicators, one for each thing that might be counted, so

E[count] = the sum of the probabilities that each thing gets counted.

Example 1. Twenty people put their names in a hat, and each draws one name at random. How many do you expect to draw their own name? Each person draws their own with probability 1/20. Twenty indicators, each with mean 1/20, add up to 1. The indicators are dependent (if 19 people hold their own names, the 20th must too), and linearity does not care. The answer is 1 for a group of any size.

Example 2. Ten jobs go to ten servers, each job to a server chosen uniformly at random, independently. How many servers do you expect to sit idle? A given server is idle when all ten jobs miss it, which has probability (9/10)^10 ≈ 0.349. With ten servers, that is 10 × 0.349 ≈ 3.49 idle servers, about a third of the fleet.

"I'll write the number of idle servers as a sum of ten indicators, one per server. Each one is 1 with probability (9/10)^10, about 0.35, so the expected count is about 3.5. The idle indicators aren't independent of each other, but linearity doesn't need them to be."

What the interviewer sees from a candidate who hasn't absorbed this: they start computing the probability of exactly 0 idle servers, then exactly 1, then exactly 2, building the whole distribution. That route takes most of the interview and invites mistakes. The indicator route takes one line.

Linearity is about sums. It does not give E[XY] = E[X] × E[Y]; that needs the covariance from the next section to be zero.

The variance of a sum has a covariance term

Variance measures spread: the expected squared distance from the mean, Var(X) = E[(X - μ)^2], which is easier to compute as E[X^2] - μ^2. The standard deviation σ is its square root, and it is in the same units as X.

For a sum there is one more term:

Var(X + Y) = Var(X) + Var(Y) + 2 Cov(X, Y).

The covariance, Cov(X, Y) = E[XY] - E[X] × E[Y], measures how X and Y move together. It equals ρ × σ(X) × σ(Y), where ρ is their correlation. The variances add whenever Cov(X, Y) = 0, and being uncorrelated is enough for that; independent variables are always uncorrelated. The reverse fails: zero covariance does not mean independence. If X is -1, 0 or 1 with equal chance and Y = X^2, then E[XY] = E[X^3] = 0 and E[X] = 0, so Cov(X, Y) = 0, yet Y is fixed by X. Correlation measures only straight-line association.

Daily signups from the web have a standard deviation of 30, and signups from the app have one of 40. The spread of total daily signups depends on how the two move together:

Web and app signupsVar(total)SD of total
Independent (ρ = 0)900 + 1,600 = 2,50050
Rise and fall together (ρ = 0.5)2,500 + 2 × 0.5 × 30 × 40 = 3,70060.8
Offset each other (ρ = -0.5)2,500 - 1,200 = 1,30036.1

The common slip is adding standard deviations, 30 + 40 = 70. Variances add, plus the covariance term; standard deviations do not. The other slip is assuming independence without saying so. In product metrics independence often fails: web and app traffic share weekends, holidays and marketing pushes, which pushes ρ above 0, while users switching from one to the other pushes it below.

With constants a and b, the rule becomes Var(aX + bY) = a^2 Var(X) + b^2 Var(Y) + 2ab Cov(X, Y), so Var(X - Y) = Var(X) + Var(Y) - 2 Cov(X, Y). The difference, web minus app signups, has an SD of 50 if the two are independent, the same as the total, but 36.1 when ρ = 0.5, because the swings they share cancel. Comparing two groups means looking at a difference, so this formula is behind the two-group standard error in hypothesis tests and p-values: for independent groups, the variance of the difference is the sum of the two variances.

The same rule covers a count built from indicators. An indicator with probability p has variance p(1 - p), and every pair of indicators adds twice its covariance. For the ten servers, the pairs are negatively related: if one server sits idle, all ten jobs went to the other nine, which makes a second idle server less likely. Without the covariance terms, the variance of the idle count would be 10 × 0.349 × 0.651 ≈ 2.27; with them it is about 0.99. Ask about the covariance whenever the pieces share a fixed total.

Price a game and decide whether to play

A bag holds 10 tickets numbered 1 to 10. You draw 3 without replacement and are paid the sum of their numbers in dollars. It costs $15 to play. Do you play?

Expected payout first, with indicators. Each ticket is equally likely to be among the 3 drawn, so each one is drawn with probability 3/10. The payout is the sum, over all ten tickets, of the ticket's number times its indicator:

E[payout] = (1 + 2 + ... + 10) × 3/10 = 55 × 0.3 = $16.50.

That is the fair price. At $15 the game pays you $1.50 per play on average, 10% of the price, so the expectation says play.

Then the risk. A single draw is equally likely to be any of the ten tickets, so E[X] = 5.5 and E[X^2] = 385/10 = 38.5, giving Var(X) = 38.5 - 5.5^2 = 8.25. Without replacement the draws are negatively correlated: a high first ticket leaves lower ones behind. A tidy way to get the covariance between two draws:

  1. If you drew all 10 tickets, the total would always be 55, so its variance would be 0.

  2. That variance has 10 variance terms and 90 covariance terms, one per ordered pair of draws. By symmetry every pair of draws has the same covariance, Cov, so 10 × 8.25 + 90 × Cov = 0, and Cov = -82.5/90 = -11/12, about -0.917.

  3. For your 3 tickets: Var = 3 × 8.25 + 6 × (-11/12) = 24.75 - 5.5 = 19.25, an SD of about $4.39.

Drawing with replacement would remove the covariance terms and leave 24.75, an SD of about $4.97.

An average profit of $1.50 against an SD of $4.39 means one game is mostly noise: a payout below $15 happens in exactly 40 of the 120 possible hands, so you lose money on 1 game in 3. Over 100 independent games, the expected profit grows to $150 while the SD of the total grows only to √100 × 4.39 ≈ $43.9. Ending 100 games behind would take a result more than three SDs below the mean, and the normal approximation, which the next lesson covers, puts its probability at about 3 in 10,000.

"Fair value is $16.50, so at $15 I have a 10% edge. I lose a third of single games, but the edge grows with the number of games and the noise grows only with its square root. So I play, as many times as they'll let me, with a stake I can afford to lose."

That is the answer the interviewer is listening for: the expectation tells you whether a game is good, and the variance tells you how much to stake on it. This simulation checks the numbers:

import numpy as np

rng = np.random.default_rng(3)
hands = rng.permuted(np.tile(np.arange(1, 11), (200_000, 1)), axis=1)[:, :3]
payout = hands.sum(axis=1)
print(payout.mean(), payout.std())  # close to 16.5 and 4.39
print((payout < 15).mean())         # close to 1/3

Waiting for the first success

Repeat a trial that succeeds with probability p each time, independently, until the first success. The number of trials N, counting the successful one, has a geometric distribution, and E[N] = 1/p.

The derivation fits on one line. The first trial costs 1. With probability p you are done; with probability 1 - p you are back where you started, facing the same expected wait. So E[N] = 1 + (1 - p) × E[N], which solves to E[N] = 1/p. Writing one equation for each situation you can be in is the method behind states and recursion.

A bug shows up on 1 run in 8, independently from run to run. You expect 8 runs to see it once. To capture it 3 times, linearity adds three separate waits: 3 × 8 = 24 runs. If a question counts the clean runs before the bug appears instead, subtract the run that shows it: 1/p - 1 = 7. Say which count you mean.

An expected wait of 8 does not mean 8 runs are enough. The chance of seeing the bug within 8 runs is 1 - (7/8)^8, about 65.6%, the complement trick from counting and symmetry. The median wait is 6 runs, below the mean, because a few very long waits pull the average up.

All probability questions

Practicing with the questions on this page

Before computing anything, write the quantity as a sum of simpler pieces and ask whether linearity gives the answer in one line. If a question asks for a spread, list the covariance terms before you decide they are zero. If it asks whether to play, give the fair price, the edge as a share of the cost, and a sentence about risk. Check each answer against a quick simulation, then against a bound: an expected count can't exceed the number of things being counted, and a variance can't be negative.