Week 09 · Conditional probability and Bayes’ rule

Use natural frequencies to see how the same spam signal means different things at different base rates.

Watch on YouTube

Subject contents · Week 8 · Week 10

Use natural frequencies to see how the same spam signal means different things at different base rates.

Ask how much a warning should change your belief

A mail filter flags a message. What should you conclude from the flag? Even a detector that catches most spam can flag many ordinary messages when spam is uncommon. To interpret the warning, you need the detector’s behavior in both groups and the sizes of the groups.

In this lesson, distinguish the probability of a warning given the true label from the probability of a label given the warning. You will calculate both with a table of message counts. The same approach helps when interpreting alerts from fraud models, spam filters, or monitoring systems.

The idea

A conditional probability is the fraction of outcomes of one kind among those that meet a condition. P(A | B) is P(A and B) divided by P(B), provided P(B) is positive. The order matters. P(flag | spam) measures how often spam is flagged. P(spam | flag) measures how often a flagged message is spam.

Bayes’ rule switches the direction of the condition. Multiply the base rate P(spam) by P(flag | spam) to get the probability of a true flag. Divide by the probability of any flag. That denominator includes flags from spam and from ordinary mail.

P(spam | flag) = P(flag | spam) P(spam) / P(flag)

P(flag) = P(flag | spam) P(spam) + P(flag | ordinary) P(ordinary)

The easiest way to check the denominator is to imagine a population and count how many flagged cases come from each group.

Build Bayes’ rule from the same group of messages

Write S for “this message is spam” and F for “this message is flagged.” The vertical bar in P(S | F) reads “given.” It restricts the denominator to flagged messages. Multiplying P(F | S) by P(S) gives the fraction of all messages that are both spam and flagged. That same fraction also equals P(S | F) times P(F). Equating the two expressions and dividing by P(F) gives Bayes’ rule.

The denominator P(F) must include all ways to get a flag. Spam and ordinary mail are disjoint groups and cover all messages in this example. Compute flags within each group, then add the two contributions. If no messages can be flagged, P(F) is zero and the conditional probability is undefined.

The table below traces the first scenario. The column totals are the quantities to inspect when your condition is “flagged.”

True classFlaggedUnflaggedTotal
Spam9010100
Ordinary4959,4059,900
Total5859,41510,000

Work it out

Take 10,000 messages. Suppose a detector flags 90% of spam and 5% of ordinary mail. With 1% spam, there are 100 spam messages and 9,900 ordinary messages. The expected flagged counts are 90 and 495. Of the 585 flags, only 90 are spam, so P(spam | flag) is about 15.4%.

Raise the spam rate to 10% and keep the two flag rates fixed. Now 900 flags are true and 450 are false. The fraction of flags that are spam rises to 66.7%. The signal behaves the same way within each group. The groups now contribute different numbers of messages.

Two horizontal bars compare the share of flags that are true and false at spam base rates of 1% and 10%. The true share rises from 15.4% to 66.7%.

Each bar contains only flagged messages. The labels are expected message counts out of the original 10,000.

Run the Python lab

def spam_given_flag(base_rate, sensitivity, false_positive_rate):
    true_flag = sensitivity * base_rate
    false_flag = false_positive_rate * (1 - base_rate)
    any_flag = true_flag + false_flag
    if any_flag == 0:
        raise ValueError("Cannot condition on an impossible flag")
    return true_flag / any_flag

for rate in (0.01, 0.10):
    result = spam_given_flag(rate, 0.90, 0.05)
    print(f"spam base rate {rate:.0%}: {result:.1%} of flags are spam")

assert abs(spam_given_flag(.01, .90, .05) - 90 / 585) < 1e-12
assert abs(spam_given_flag(.10, .90, .05) - 900 / 1350) < 1e-12

Expected output

spam base rate 1%: 15.4% of flags are spam
spam base rate 10%: 66.7% of flags are spam

Check which denominator the lab uses

sensitivity is the fraction flagged among spam. false_positive_rate is the fraction flagged among ordinary messages. base_rate determines how much each group contributes. Divide the probability of a true flag by the total flag probability to compute the share of flags that are correct.

Keep the detector’s rates fixed and change only the base rate. In the first example, P(F | S) is 90/100, while P(S | F) is 90/585. Both use the same 90 true flags. The different denominators explain the different answers. In practice, estimate the base rate for the population where you deploy the detector, and check whether the two within-group rates also change.

Try it yourself

  1. At the 1% base rate, lower the ordinary-mail flag rate from 5% to 1%. Work out the expected flag counts and the new posterior probability.
  2. Explain why there is no defined answer if the detector never flags any messages.

Check your work. The expected flagged counts are 90 true and 99 false, giving 90/189 ≈ 47.6%. If flags never occur, the probability of the condition is zero.

Further reading

Deep Learning · Applied Math and Machine Learning Basics

Subject contents · Week 8 · Week 10