NewTurn any lesson into a live trading bot with one prompt, using AgenKitagenkit.xyz

Part 1 · ProbabilityLesson 212 minQuant Bible §2.2

Expected value and linearity

The weighted average that prices every game, and the linearity trick that solves half of all interview questions.

Every price in finance is an expected value of something. An option, a bond, a bet on a dice game: once you can write down what it pays in each outcome and how likely each outcome is, the fair price is a weighted average. This lesson is about computing that average, and about one property of it that makes hard questions easy.

The weighted average

A random variable XX has a probability mass function p(x)p(x) if it is discrete or a density f(x)f(x) if it is continuous. Its expected value (or mean, μ\mu) weights every value by its probability:

E[X]=∑xx p(x)orE[X]=∫x f(x) dx.E[X] = \sum_{x} x\,p(x) \qquad\text{or}\qquad E[X] = \int x\,f(x)\,dx .

The same recipe prices any payoff g(X)g(X): weight g(x)g(x) instead of xx.

E[g(X)]=∑xg(x) p(x)or∫g(x) f(x) dx.E[g(X)] = \sum_x g(x)\,p(x) \qquad\text{or}\qquad \int g(x)\,f(x)\,dx .

A fair six-sided die pays E[X]=(1+2+3+4+5+6)/6=3.5E[X] = (1+2+3+4+5+6)/6 = 3.5. If a game pays the roll, its fair price is $3.50. Pay less and you profit on average; pay more and you lose.

Pricing a choice: the re-roll game

Interviewers love to add a decision. Roll a die; you may re-roll once and must keep the second roll. What is the game worth? Work backwards. A fresh roll is worth 3.5, so keep anything above 3.5 and re-roll 1, 2 or 3:

E=36⋅3.5+16(4+5+6)=4.25.E = \tfrac{3}{6}\cdot 3.5 + \tfrac{1}{6}(4+5+6) = 4.25 .

If each re-roll costs $1, a re-roll is worth 3.5−1=2.53.5 - 1 = 2.5, so you only re-roll 1s and 2s: 26⋅2.5+16(3+4+5+6)=23/6≈3.83\tfrac{2}{6}\cdot 2.5 + \tfrac{1}{6}(3+4+5+6) = 23/6 \approx 3.83.

With unlimited $1 re-rolls the game is worth some value VV, and the rule is to keep any face above V−1V - 1. Keeping 4, 5, 6 gives V=2.5+12(V−1)V = 2.5 + \tfrac12(V-1), so V=4V = 4. Keeping 3 or more gives the same V=4V = 4, because a 3 is exactly break-even.

Figure 2.1The re-roll game on a real die
Each glossy column is what the game is worth if you keep any roll of at least that face; the tallest is the optimal rule. Roll games with that rule and watch your average converge on the fair price. Turn on unlimited re-rolls and a cost to see the rule shift.
Figure 2.2Strategy lab for any die
The same calculation as a chart, including a 100-sided die. Each bar is what the game is worth if you keep any roll of at least that face. The highest bar is the optimal rule and the fair price. Switch to a 100-sided die or unlimited re-rolls, then check the theory with 20,000 simulated games.

Linearity: the trick that solves half the questions

Expectation is linear: E[aX+b]=aE[X]+bE[aX + b] = aE[X] + b. More surprisingly, it adds across any random variables, even dependent ones:

E[X1+X2+⋯+Xn]=E[X1]+E[X2]+⋯+E[Xn].E[X_1 + X_2 + \dots + X_n] = E[X_1] + E[X_2] + \dots + E[X_n].

So when a question asks for the expected number of somethings, write the count as a sum of indicators, Ii=1I_i = 1 if thing ii happens and 0 otherwise. Each indicator has E[Ii]=P(thing i)E[I_i] = P(\text{thing } i), and you add those up. You never need the joint distribution.

Example. Ten boys and ten girls stand in a random line. What is the expected number of adjacent boy–girl pairs?

There are 19 neighbouring slots. Slot ii is mixed with probability

P(mixed)=2⋅10⋅1020⋅19=1019,P(\text{mixed}) = \frac{2 \cdot 10 \cdot 10}{20 \cdot 19} = \frac{10}{19},

so the answer is 19×1019=1019 \times \tfrac{10}{19} = 10. The slots are dependent (a mixed slot changes who is left for the next one), and it doesn’t matter.

Figure 2.3Line-up simulator
Each shuffle lines up 10 boys (blue) and 10 girls (orange) and counts mixed neighbours (green links). Run thousands of line-ups: the average converges on 10, exactly as linearity predicts.

In code

import numpy as np
rng = np.random.default_rng(1)

# Fair value of "roll, optionally re-roll once" by simulation
first = rng.integers(1, 7, 1_000_000)
second = rng.integers(1, 7, 1_000_000)
payoff = np.where(first <= 3, second, first)
print(payoff.mean())                     # ≈ 4.25

# Linearity: expected mixed neighbours in a line of 10 boys + 10 girls
line = np.array([0] * 10 + [1] * 10)
mixed = []
for _ in range(100_000):
    p = rng.permutation(line)
    mixed.append(np.sum(p[1:] != p[:-1]))
print(np.mean(mixed))                    # ≈ 10.0

Check yourself

A deck is shuffled. What is the expected number of positions where the card is the same as in a fresh, unshuffled deck of 52?

Progress is saved in this browser only.