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

Part 1 · ProbabilityLesson 112 minQuant Bible §2.1

Conditional probability and Bayes’ theorem

Updating a belief when evidence arrives, and why a rare base rate beats a convincing witness.

Trading is updating a belief under uncertainty, many times a second. A market maker shows a price, someone trades against it, and the market maker has to ask: given that this just happened, what is the thing actually worth? That question has a precise answer, and it is the most tested idea in quant interviews.

Conditional probability: shrink the world

Two events are rarely unrelated. The chance someone is sick changes once you know they are coughing. For events AA and BB, the probability of AA given that BB happened is

P(A∣B)=P(A∩B)P(B),P(B)>0.P(A \mid B) = \frac{P(A \cap B)}{P(B)}, \qquad P(B) > 0.

Read it geometrically. Once you learn that BB happened, every outcome outside BB is impossible, so BB becomes your whole world. P(A∣B)P(A\mid B) is the share of that new world where AA also happened.

Bayes’ theorem: flip the condition

The intersection can be written two ways, P(A∣B)P(B)=P(A∩B)=P(B∣A)P(A)P(A\mid B)P(B) = P(A\cap B) = P(B\mid A)P(A). Divide by P(B)P(B) and you get Bayes’ theorem:

P(H∣E)⏟posterior=P(E∣H)⏞likelihood  P(H)⏞priorP(E∣H)P(H)+P(E∣¬H)P(¬H)⏟evidence P(E)\underbrace{P(H \mid E)}_{\text{posterior}} = \frac{\overbrace{P(E \mid H)}^{\text{likelihood}}\;\overbrace{P(H)}^{\text{prior}}}{\underbrace{P(E \mid H)P(H) + P(E \mid \neg H)P(\neg H)}_{\text{evidence } P(E)}}

Here HH is a hypothesis (the cab was blue, the coin is rigged) and EE is the evidence you saw. The bottom line is the law of total probability: the evidence can arrive either because HH is true or because it isn’t, so you add both routes.

The useful part is that the right-hand side is usually easy to get. “How often does a good witness say blue when the cab is blue?” is a property of the witness. “How common are blue cabs?” is a fact about the town. Bayes turns those into the number you actually want.

Figure 1.1A population of 1,000, in 3D
Every block is one person (or cab, or coin). The hypothesis-true group is the blue block on the left. Blocks that show the evidence rise up. The posterior is simply: of all the raised blocks, what share are blue? Try the presets, especially the 1,000 coins, and rotate to compare the two raised groups.
Figure 1.2The same idea as areas
The square is everyone. The vertical cut is the prior: blue is hypothesis true, orange is false. Inside each column, the solid part is how often the evidence shows up. The posterior is the solid blue area divided by all the solid area. Try the presets.

Three classic traps

The taxi and the witness

A cab hit someone at night. 85% of the town’s cabs are green, 15% are blue. A witness says the cab was blue, and tests show he identifies colours correctly 80% of the time. Most people answer “80% likely blue”. Bayes says:

P(blue∣says blue)=0.8×0.150.8×0.15+0.2×0.85=0.120.29≈0.41.P(\text{blue}\mid\text{says blue}) = \frac{0.8 \times 0.15}{0.8\times 0.15 + 0.2 \times 0.85} = \frac{0.12}{0.29} \approx 0.41.

Of every 100 cabs, 12 are blue and correctly called blue, but 17 are green and wrongly called blue. The rare base rate wins.

Steve the librarian

Steve is shy, tidy and loves detail. Librarian or farmer? The description fits librarians better: say 40% of librarians but only 10% of farmers. But there are about 20 farmers for every librarian, so out of 21 people the expected matches are 1×0.4=0.41 \times 0.4 = 0.4 librarians against 20×0.1=220 \times 0.1 = 2 farmers. Steve is a farmer with probability about 2/2.4≈83%2 / 2.4 \approx 83\%.

A 99% accurate test

1% of people have a disease. A test catches 99% of sick people and falsely flags 1% of healthy ones. If you test positive, the chance you are sick is

P(S∣+)=0.99×0.010.99×0.01+0.01×0.99=0.5.P(S\mid +) = \frac{0.99 \times 0.01}{0.99 \times 0.01 + 0.01 \times 0.99} = 0.5.

A coin flip, despite “99% accuracy”. A second independent positive changes things fast: with kk positives the likelihoods are raised to the power kk, and two positives already give 99%. Set the repeats slider in the figure to see it.

The fast way: odds times likelihood ratio

Bayes is cleaner in odds. Divide the formula for HH by the one for ¬H\neg H and the evidence term cancels:

P(H∣E)P(¬H∣E)=P(H)P(¬H)×P(E∣H)P(E∣¬H).\frac{P(H\mid E)}{P(\neg H\mid E)} = \frac{P(H)}{P(\neg H)} \times \frac{P(E\mid H)}{P(E\mid \neg H)}.

Posterior odds are prior odds times the likelihood ratio. For the coins: prior odds 1:9991:999, likelihood ratio 1024:11024:1, posterior odds 1024:9991024:999. Independent pieces of evidence just multiply their likelihood ratios together, which is how you do this in your head.

In code

def posterior(prior, p_e_given_h, p_e_given_not_h, repeats=1):
  a = prior * p_e_given_h ** repeats
  b = (1 - prior) * p_e_given_not_h ** repeats
  return a / (a + b)

print(posterior(0.15, 0.8, 0.2))           # taxi: 0.414
print(posterior(0.01, 0.99, 0.01))         # one positive test: 0.5
print(posterior(0.01, 0.99, 0.01, 2))      # two positives: 0.99
print(posterior(1/1000, 1.0, 0.5, 10))     # 1000 coins: 0.506

# Check the taxi answer by simulation
import numpy as np
rng = np.random.default_rng(0)
blue = rng.random(1_000_000) < 0.15
says_blue = np.where(blue, rng.random(blue.size) < 0.8, rng.random(blue.size) < 0.2)
print(blue[says_blue].mean())              # ≈ 0.414

Check yourself

A rare event has prior probability 1%. A signal fires 90% of the time when the event is coming and 9% of the time when it isn’t. The signal fires. Roughly how likely is the event now?

Progress is saved in this browser only.