Probability: how often something happens
On this page
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
Read 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.
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:
A 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.
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?
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.
Read the bar as “given”. 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:
| Item | Up | Down | Total |
|---|---|---|---|
| Signal on | 558 | 431 | 989 |
| Signal off | 239 | 219 | 458 |
| Total | 797 | 650 | 1,447 |
Overall, 55.1% of days are up. With the signal on, ; with it off, . The signal carries a little information, about four points’ worth.
Watch the direction. These are not the same question:
“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).
Check it on Table 1: , and , so
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 , and the rule becomes a plain multiplication, but only for independent events:
I used this in the “at least one down day” example. Is it fair? For the direction of the day, nearly: and , 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 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:
| Symbol | Name | Meaning |
|---|---|---|
| prior | how common is before you look at any evidence | |
| likelihood | how often you see the evidence when is true | |
| evidence | how often you see the evidence at all | |
| posterior | what you believe about 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, have a real edge:
- A real edge passes the backtest of the time:
- An idea with no edge still passes of the time, by luck or by being tuned to the past:
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):
Then Bayes:
Under these assumptions, only about 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.
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 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, sodropna()leaves them out. Without it,px > avg20is 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.
| Rule | Formula | Remember it as |
|---|---|---|
| Not | Use it for “at least one” | |
| Or | Don’t count the overlap twice | |
| Given | Only look at the cases where is true | |
| And | Plain multiplication only if independent | |
| Bayes | Always ask how rare is |
References
- Christopher M. Bishop and Hugh Bishop, Deep Learning: Foundations and Concepts (Springer, 2024), chapter 2, section 2.1: sum and product rules (equations 2.8, 2.9), Bayes’ theorem (2.10, 2.11), the screening-test example (2.20 to 2.22)
- Kevin P. Murphy, Probabilistic Machine Learning: An Introduction (MIT Press, 2022), chapter 2: probability