Beyond the average: risk, variance and ruin
P75.expected-value.02 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Expected value: the average outcome of a choice, Probability Distributions
P75.expected-value.02 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Expected value: the average outcome of a choice, Probability Distributions
The last lesson chose the axe because its average was higher. But two choices can have exactly the same average and still be very different to make: one always gives you about the same, the other all or nothing. Most of the time the average is the right guide. Sometimes one bad result ends everything, and then the average is the wrong question. This lesson measures the difference, and shows when it decides.
A character can take only so much harm. Their hit points count it: each point of damage takes one away, and a character at 0 is out of the fight. In this platform's game a character who drops to 0 is knocked out or captured, never killed, but the fight is lost all the same.
Ada faces a troll with 10 hit points. She can strike in two ways:
Both average 5 damage per strike: 1 × 5 for the steady strike, 0.5 × 10 + 0.5 × 0 for the wild swing. By expected value they are equal.
What differs is how far the results fall from that average. The variance measures it: the expected value of the squared distance from the average. The steady strike always gives 5, so its variance is 0. The wild swing is 5 away from its average either way, so its variance is 0.5 × (10 − 5)² + 0.5 × (0 − 5)² = 25. Its square root, the standard deviation, is back in damage points: 5. “Probability Distributions” (Data Science & AI) uses the same two measures for any distribution.
Now the fight. Ada strikes first each round; then, if it is still standing, the troll strikes back and lands half the time. Ada has 1 hit point left, so one blow from the troll ends the fight.
Same average, but the risky choice wins more often, because the steady one is certain to give the troll a chance.
Give Ada a second hit point and the answer flips. The steady strike now wins every time, since the troll's one blow cannot finish her, while the wild swing wins 8/9 of the time, about 89 %: a run of misses can still let the troll land twice.
The risk of ruin is the chance of an outcome you cannot recover from: dropping to 0 hit points, or losing all your money. When it is in play, compare choices by that chance, not by their averages. Between choices with the same average: with room to absorb a bad result, the low-variance option is usually safer, as the steady strike is with two hit points. With no room left, when every extra round gives the other side another chance, a high-variance option that can end things at once can be the safer one, as the wild swing is with one hit point.
Why it matters — A good player, like a careful investor, asks first what result they cannot come back from.
The program below computes Ada's chance of winning exactly, with fractions and the same reasoning as above, and then by playing ten thousand fights with a fixed seed.
import random
from fractions import Fraction
from functools import cache
HALF = Fraction(1, 2) # the troll lands half its blows; the wild swing lands half the time
@cache
def win_chance(troll, lives, wild):
"""Ada's exact chance of winning, with the troll on `troll` hit points."""
if lives == 0:
return Fraction(0)
if troll <= 0:
return Fraction(1)
def troll_strikes(left):
if left <= 0:
return Fraction(1)
return HALF * win_chance(left, lives - 1, wild) + HALF * win_chance(left, lives, wild)
if not wild:
return troll_strikes(troll - 5)
# P = 1/2 · (a hit) + 1/2 · 1/2 · (a miss, then a blow) + 1/4 · P, solved for P.
other = HALF * troll_strikes(troll - 10) + HALF * HALF * win_chance(troll, lives - 1, wild)
return other / (1 - HALF * HALF)
def fights_won(lives, wild, fights=10_000, seed=1):
roller = random.Random(seed)
won = 0
for _fight in range(fights):
troll, left = 10, lives
while troll > 0 and left > 0:
troll -= (10 if roller.random() < 0.5 else 0) if wild else 5
if troll > 0 and roller.random() < 0.5:
left -= 1
won += troll <= 0
return won / fights
for lives in (1, 2):
print(lives, win_chance(10, lives, False), win_chance(10, lives, True),
fights_won(lives, False), fights_won(lives, True))
# 1 1/2 2/3 0.4977 0.6594 · 2 1 8/9 1.0 0.8821
Each simulated chance lands within one percentage point of the exact one.
The first rung asks for a judgement in words: with one hit point left, which strike would you choose, and why? The second, written from scratch, computes the average and the variance of a choice from its outcomes.
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Loading exercise…
Loading exercise…
Loading your notes...