Probability: how often something happens

On this page
  1. What a probability is
  2. Rule 1: “not” (the complement)
  3. Rule 2: “or” (the addition rule)
  4. Rule 3: “given” (conditional probability)
  5. Rule 4: “and” (the multiplication rule)
  6. Rule 5: Bayes’ rule (turning “given” around)
  7. Check yourself
  8. The code
  9. Summary
  10. References

ProbabilityHow often an event happens if you could repeat a trial without end: the share of trials where it happens. sits under almost everything in machine learning and finance: judging whether a strategy’s result is luck, how a learner decides, how risk adds up. Kevin Murphy’s Probabilistic Machine Learning starts its maths with it in chapter 2. This post covers the five rules, each with a plain meaning, a market example, the formula, and the numbers worked out.

The examples come from the daily moves of SPY, the S&P 500 fund, over 2020 to 2025: 1,466 trading days of adjusted closesA historical price rewritten to fold in stock splits and dividends, so it shows the true value of holding the stock.. A day is up if the close is above the day before, otherwise down. Every count below is from that one series, so you can check them with the code at the end.

What a probability is

A probability is a number from 0 (never happens) to 1 (always happens). The everyday way to read it: out of many tries, what share ends in this outcome?

Of the 1,466 days, 808 were up:

a probability we worked outan up daya down day, a big dropthe signal we condition on

P(up)=up daysall daysP(\good{\text{up}}) = \frac{\text{up days}}{\text{all days}} P(up)=8081,466P(\good{\text{up}}) = \frac{808}{1{,}466} P(up)=0.551P(\good{\text{up}}) = 0.551 P(up)=55.1%P(\good{\text{up}}) = 55.1\%

Read P(up)P(\good{\text{up}}) as “the probability of an up day”. Slightly more than half: the market drifts up, but only just, one day at a time.

Rule 1: “not” (the complement)

Plain meaning: the chance something does not happen is everything else.

P(not A)=1−P(A)P(\text{not } A) = 1 - P(A)

This rule is most useful for “at least one” questions, which are hard to count directly.

Example: a trading week has five days. What is the chance that at least one of them is down?

Counting “at least one” means one down day, two, three… That’s messy. The opposite is simple: all five up. If days were independent, each up with probability 0.551:

P(five up days)=0.5515=0.0509P(\good{\text{five up days}}) = 0.551^{5} = 0.0509 P(at least one down day)=1−0.0509=0.9491P(\bad{\text{at least one down day}}) = 1 - 0.0509 = 0.9491

A 95%\key{95\%} chance that a week contains a down day. The record agrees: of 293 five-day blocks, 95.2% had one. Small risks add up fast when you repeat them, even in a market that is “usually up”: that only means up on more days than not.

Rule 2: “or” (the addition rule)

Plain meaning: add the two chances, then take away the overlap, because you counted it twice.

P(A or B)=P(A)+P(B)−P(A and B)P(A \text{ or } B) = P(A) + P(B) - P(A \text{ and } B)

Example: 44.9% of days are down. 8.1% of days are big: the market moved more than 2% either way. 4.5% are both, a big down day. What share of days would make a nervous investor check the news, because the day was down or big?

P(down or big)P(\bad{\text{down}} \text{ or } \text{big}) =0.449+0.081−0.045=0.485\quad = 0.449 + 0.081 - 0.045 = 0.485

That’s 711 of the 1,466 days. Without subtracting the overlap, you would say 53.0% and count the 66 big down days twice.

If two things can’t happen together (like “up” and “down”), the overlap is zero and you simply add.

Rule 3: “given” (conditional probability)

Plain meaning: once you know something, you only look at the cases where it’s true.

P(A∣B)=P(A and B)P(B)P(A \mid B) = \frac{P(A \text{ and } B)}{P(B)}

Read the bar ∣\mid as “given”. P(up∣signal)P(\good{\text{up}} \mid \risk{\text{signal}}) means “the chance of an up day, among days when the signal was on”.

Example: a simple signal: at yesterday’s close, was the price above its own 20-day average? (Technical traders call this being above the moving averageAn average of recent prices that follows the trend; it smooths out price moves but runs behind them in time..) Table 1 splits the days two ways, leaving out the first 19 days that have no average yet:

Table 1. SPY days by 20-day-average signal and direction, 2020 to 2025
ItemUpDownTotal
Signal on558431989
Signal off239219458
Total7976501,447
P(up∣signal on)=558989=56.4%P(\good{\text{up}} \mid \risk{\text{signal on}}) = \frac{558}{989} = 56.4\% P(up∣signal off)=239458=52.2%P(\good{\text{up}} \mid \risk{\text{signal off}}) = \frac{239}{458} = 52.2\%

Overall, 55.1% of days are up. With the signal on, 56.4%\key{56.4\%}; with it off, 52.2%\key{52.2\%}. The signal carries a little information, about four points’ worth.

Watch the direction. These are not the same question:

P(up∣signal)=558989P(\good{\text{up}} \mid \risk{\text{signal}}) = \frac{558}{989} P(up∣signal)=56.4%P(\good{\text{up}} \mid \risk{\text{signal}}) = 56.4\% P(signal∣up)=558797P(\risk{\text{signal}} \mid \good{\text{up}}) = \frac{558}{797} P(signal∣up)=70.0%P(\risk{\text{signal}} \mid \good{\text{up}}) = 70.0\%

“The signal is on for 70% of up days” sounds impressive and means almost nothing: the signal is on for 68% of all days. “Days with the signal on are up 56.4% of the time” is the number that matters. People mix the two up all the time, in data and in strategy pitches.

Rule 4: “and” (the multiplication rule)

Plain meaning: the chance of both is the chance of the first, times the chance of the second once the first has happened. Bishop and Bishop’s Deep Learning: Foundations and Concepts calls this the product ruleThe probability of X and Y together equals the probability of Y given X times the probability of X. (equation 2.9).

P(A and B)=P(B)×P(A∣B)P(A \text{ and } B) = P(B) \times P(A \mid B)

Check it on Table 1: P(signal)=989/1,447=68.3%P(\risk{\text{signal}}) = 989/1{,}447 = 68.3\%, and P(up∣signal)=56.4%P(\good{\text{up}} \mid \risk{\text{signal}}) = 56.4\%, so

P(up and signal)P(\good{\text{up}} \text{ and } \risk{\text{signal}}) =0.683×0.564=0.385=38.5%\quad = 0.683 \times 0.564 = 0.385 = 38.5\%

which matches Table 1 within rounding: 558 out of 1,447.

IndependenceTwo variables are independent when knowing one tells you nothing about the other: their joint probability is the product of their separate probabilities. is the special case where knowing one thing tells you nothing about the other. Then P(A∣B)=P(A)P(A \mid B) = P(A), and the rule becomes a plain multiplication, but only for independent events:

P(A and B)=P(A)×P(B)P(A \text{ and } B) = P(A) \times P(B)

I used this in the “at least one down day” example. Is it fair? For the direction of the day, nearly: P(up∣yesterday up)=53.8%P(\good{\text{up}} \mid \text{yesterday up}) = 53.8\% and P(up∣yesterday down)=56.8%P(\good{\text{up}} \mid \text{yesterday down}) = 56.8\%, both within a couple of points of the 55.1% base rateHow often something happens overall, before any extra evidence about the particular case is taken into account.. Yesterday’s direction tells you almost nothing about today’s.

The size of moves is another story. A drop of more than 2% happens on 4.5% of days. Given that yesterday was such a drop, it happens on 9.1%\bad{9.1\%} of days, twice as often. Big moves cluster (the technical name is volatility clusteringHigh volatility tending to be followed by more high volatility, and low volatility by more low volatility.), so multiplying “independent” probabilities for a run of bad days makes a crash look much less likely than it is.

Rule 5: Bayes’ rule (turning “given” around)

Plain meaning: Bayes’ rule turns “how likely is the evidence if it’s true” into the question you actually care about: “how likely is it true, now that I’ve seen the evidence”. It’s equation 2.10 in Bishop and Bishop, and Table 2 names its four parts:

P(A∣B)=P(B∣A)×P(A)P(B)P(A \mid B) = \frac{P(B \mid A) \times P(A)}{P(B)}
Table 2. Symbols in Bayes’ rule
SymbolNameMeaning
P(A)P(A)priorhow common AA is before you look at any evidence
P(B∣A)P(B \mid A)likelihoodhow often you see the evidence when AA is true
P(B)P(B)evidencehow often you see the evidence at all
P(A∣B)P(A \mid B)posteriorwhat you believe about AA after seeing the evidence

Example: a backtestTesting a trading strategy by simulating how it would have traded over some past period. that looks good. You test a trading idea on past prices and it comes out with a Sharpe ratioReturn above the risk-free rate divided by the standard deviation of the portfolio's return: reward per unit of risk. above 1 (the Sharpe ratio is return per unit of risk; ratios at or above 1 are very uncommon, and the Sharpe ratio post explains it). How likely is it that the idea has a real edge? Three assumptions, stated so you can argue with them:

  • Of the ideas people try, 1%\bad{1\%} have a real edge: P(edge)=0.01P(\text{edge}) = 0.01
  • A real edge passes the backtest 80%\risk{80\%} of the time: P(pass∣edge)=0.80P(\risk{\text{pass}} \mid \text{edge}) = 0.80
  • An idea with no edge still passes 5%\risk{5\%} of the time, by luck or by being tuned to the past: P(pass∣no edge)=0.05P(\risk{\text{pass}} \mid \text{no edge}) = 0.05

Most people reason “80% versus 5%, so it’s probably real”.

First, how often does a backtest pass at all? Passes come from real ideas or from lucky ones (this step is called the law of total probabilityThe chance of A equals the chance of A given B times the chance of B, plus the same for B not happening.; Bishop and Bishop derive it from the sum ruleThe probability of one variable is found by summing out the other variables, adding up over all of their values., equations 2.8 and 2.11):

P(pass)=0.80×0.01+0.05×0.99P(\risk{\text{pass}}) = 0.80 \times 0.01 + 0.05 \times 0.99 P(pass)=0.0080+0.0495P(\risk{\text{pass}}) = 0.0080 + 0.0495 P(pass)=0.0575P(\risk{\text{pass}}) = 0.0575

Then Bayes:

P(edge∣pass)=0.80×0.010.0575P(\text{edge} \mid \risk{\text{pass}}) = \frac{0.80 \times 0.01}{0.0575} P(edge∣pass)=0.00800.0575≈0.14P(\text{edge} \mid \risk{\text{pass}}) = \frac{0.0080}{0.0575} \approx 0.14

Under these assumptions, only about 14%\key{14\%} of good-looking backtests are real. It’s easier to see with counts (Figure 1). Out of 10,000 ideas:

  • 100 have an edge, and 80 of them pass.
  • 9,900 don’t, and 5% of them pass anyway: 495.

2026-10-31T01:55:48.039723 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/ 0 100 200 300 400 500 600 Number of ideas, out of 10,000 tested ideas that pass the backtest 575 backtests look good; 14% are real real edge: 80 luck or overfitting: 495
Figure 1. Of 10,000 backtests, which pass and which are real

The test is decent. The passes are still mostly false, because ideas without an edge outnumber real ones a hundred to one, and 5% of a huge number is still a big number. The cure is in the assumptions: lower the 5% (test on data the idea never saw) and raise the 1% (only test ideas with a reason to work).

Bishop and Bishop work through the same trap with a medical screening test (equations 2.20 to 2.22): a cancer that affects 1% of people, a test that catches 90% of cases and wrongly flags 3% of healthy people. A positive result means only about a 23%\key{23\%} chance of cancer.

This mistake has a name: base rate neglectJudging a chance by how accurate a test or signal is while forgetting how rare the thing being tested for is.. People look at how accurate the test is and forget how rare the thing is. It’s why one good backtest is never enough, and why the Sharpe ratio post keeps saying “never trust it on a short history”.

Check yourself

The signal is on for 70% of up days. Why does that say almost nothing about the signal?

Because the signal is on for 68% of all days anyway. The question that matters runs the other way: among days with the signal on, how many were up? That is 56.4%, against 55.1% for all days, so the signal carries only a little information.

Why can multiplying probabilities make a run of bad days look much less likely than it is?

The plain multiplication only holds for independent events. Big moves cluster: a drop of more than 2% happens on 4.5% of days, but on 9.1% of days after such a drop. Treating those days as independent makes a crash look much less likely than it is.

A backtest passes 80% of real edges and only 5% of ideas with none. Why is a pass still probably luck?

Because real edges are rare: 1% of ideas. Out of 10,000 ideas, 80 real ones pass, but so do 495 with no edge. So only about 14% of passing backtests are real. This is base rate neglect.

The code

The counts from the price file, the Bayes calculation, and a simulation of 5 million ideas that checks the answer by brute force. It reads the blog’s price file from the examples repo, so it runs as pasted (pip install numpy pandas). px holds SPY’s daily closes, one per date; r the daily returnsThe percentage change in price from one day to the next: today's price divided by yesterday's, minus one.; up is True on up days.

import numpy as np
import pandas as pd

URL = "https://raw.githubusercontent.com/ankurtrapasiya/examples/main/data/prices.csv"
px = pd.read_csv(URL, index_col="date", parse_dates=True)["SPY"]   # SPY's daily closes, 2020-2025
r = px.pct_change().dropna()                       # daily returns: today's close / yesterday's - 1
up = r > 0                                         # True on up days, False on down days
print(up.mean())                                   # 0.551  P(up): the mean of True/False is the share of True
print(1 - up.mean() ** 5)                          # 0.949  P(at least one down day in five)

avg20 = px.rolling(20).mean()                      # 20-day average; missing for the first 19 days
signal = (px > avg20).where(avg20.notna()).shift(1)  # above it at yesterday's close; missing if no average
both = pd.DataFrame({"up": up, "signal": signal}).dropna()   # drop the days without a signal
print(len(both), pd.crosstab(both["signal"], both["up"]).values.tolist())   # 1,447 days, the table
print(both.groupby("signal")["up"].mean())         # 0.522 off, 0.564 on: P(up | signal)

prior, hit_rate, false_alarm = 0.01, 0.80, 0.05
p_pass = hit_rate * prior + false_alarm * (1 - prior)
print(hit_rate * prior / p_pass)                   # 0.139  P(edge | pass)

rng = np.random.default_rng(0)
edge = rng.random(5_000_000) < prior
passed = np.where(edge, rng.random(5_000_000) < hit_rate, rng.random(5_000_000) < false_alarm)
print(edge[passed].mean())                         # 0.139, the same by counting

It prints the numbers above: 0.551, then 0.949, then the 1,447 days and the table’s four counts, then 0.522 and 0.564 for the signal off and on, then 0.139 twice. Python prints the unrounded values (0.551160…). One line needs a word:

  • .where(avg20.notna()) marks the first 19 days, which have no 20-day average yet, as missing, so dropna() leaves them out. Without it, px > avg20 is simply False on those days and they would be miscounted as “signal off”.

Summary

Table 3 is the five rules in one place; the last column is the one to memorise.

Table 3. The five probability rules
RuleFormulaRemember it as
NotP(not A)=1−P(A)P(\text{not } A) = 1 - P(A)Use it for “at least one”
OrP(A or B)=P(A)+P(B)−P(A and B)P(A \text{ or } B) = P(A) + P(B) - P(A \text{ and } B)Don’t count the overlap twice
GivenP(A∣B)=P(A and B)/P(B)P(A \mid B) = P(A \text{ and } B) / P(B)Only look at the cases where BB is true
AndP(A and B)=P(B) P(A∣B)P(A \text{ and } B) = P(B)\,P(A \mid B)Plain multiplication only if independent
BayesP(A∣B)=P(B∣A) P(A)/P(B)P(A \mid B) = P(B \mid A)\,P(A) / P(B)Always ask how rare AA is

References

Tagged statistics, probability, finance← Home