← Math
Math

Conditional probability and Bayes’ rule

The detector catches 95% of incidents. Is this alert real?

Use the population behind an alert to interpret its evidence.

Outcome

Count both populations, turn sensitivity and false-positive rate into the chance an alert is real, and update it correctly for more evidence.

Terminology (6)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Conditional probability Also P(A | B)
The chance of A among the cases where B happened.
Base rate Also prior
How common the condition is before looking at the evidence.
Sensitivity
P(alert | incident): the share of real incidents the detector catches.
False-positive rate
P(alert | no incident): the share of healthy checks that still raise an alert.
Posterior
The chance of the condition after the evidence: here P(incident | alert).
Likelihood ratio
Sensitivity divided by the false-positive rate. Each conditionally independent alert multiplies the odds of an incident by it.

Why this matters

Monitoring alerts, fraud flags, spam filters, test failures and security scanners all report evidence about a condition that is usually absent. A detector can be accurate on both kinds of cases and still be wrong most of the times it fires, because the healthy cases vastly outnumber the incidents. Paging policy, triage order and how much to trust a second signal all depend on the probability in the right direction.

This lesson assumes the probability and expected-count rules from earlier lessons.

What changes when you condition

Conditional probability restricts the cases you count. P(A | B) means the share of B cases in which A also happens. Reversing A and B changes that reference population: a detector’s sensitivity starts with incidents, while the probability that an alert is real starts with alerts.

Bayes’ rule connects those directions using the base rate, the chance of an incident before the alert. Combine the expected true and false alerts, then ask what share is real. The updated probability is the posterior. Further evidence can update it again; multiplying detector evidence requires the stated independence within both incident and healthy cases.

01Count both populations

Count both populations

The anomaly detector catches 95% of payment incidents and alerts on only 2% of healthy checks. Incidents are rare, 0.5% of checks. The on-call engineer must decide what an alert means for this service. That needs the incident population and the healthy population as well as the detector’s sensitivity.

Picture 10,000 checks. In this model, an expected 0.5% of them, or 50, happen during an incident, and the detector catches 95% of those. The other 9,950 are healthy, and 2% of them still raise an alert. These are expected counts, so 47.5 need not be an actual integer tally. Predict the three lines this program prints.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const checks = 10_000;
const incidents = 0.005; // share of checks during a real incident
const sensitivity = 0.95; // P(alert | incident)
const falseRate = 0.02; // P(alert | no incident)

const real = checks * incidents * sensitivity;
const mistaken = checks * (1 - incidents) * falseRate;

console.log(real.toFixed(2));
console.log(mistaken.toFixed(2));
console.log((real / (real + mistaken)).toFixed(4));
Why is the second line 199 and not 200?

02Flip the condition

Flip the condition

The alerts number 47.5 + 199 = 246.5, and 47.5 of them are real: P(incident | alert) = 47.5 / 246.5 ≈ 0.1927. In symbols, πs / (πs + (1 − π)f). The sensitivity, 0.95, is P(alert | incident): it says how often an incident is caught, not how often an alert is right.

An alert fires. What is the chance of a real incident?

When no alert can fire, 0 / 0 is NaN, which prints as NaN and fails ordered comparisons and equality with itself. Return null and show “no alerts possible” instead.

03The base rate decides

The base rate decides

Same detector (s = 0.95, f = 0.02), different base rates
IncidentsP(incident | alert)
5%0.714
0.5%0.193
0.05%0.023

The detector did not change; the rarer the incidents, the more its 2% false alarms dominate. A posterior measured on one service does not carry over to a service with a different base rate.

04Update again

Update again

A second detector with the same behavior also fires. Its result is independent of the first conditional on an incident, and also conditional on no incident. Start from the first posterior, 0.1927, as the new base rate: 0.1927 × 0.95 / (0.1927 × 0.95 + 0.8073 × 0.02) ≈ 0.919. In odds form each alert multiplies the odds by s / f = 47.5: 0.005 / 0.995 = 0.00503, then 0.239, then 11.34, which is 0.919 as a probability.

Two alerts fire, but both come from the same failing health check. How should the second one count?

05Stay finite

Stay finite

06Triage the alert

Triage the alert

The on-call review first estimates the true and false alert volumes. It then needs the fraction of all alerts that are real, including the case where an alert cannot occur. A second conditionally independent signal changes the evidence again; retain its log-likelihood contribution instead of restarting from the original prior or forming tiny powers. Earlier helpers and the complete caller remain visible.

In the frontend An honest alert banner in Next.js and Svelte Optional

An alert banner can tell the on-call engineer what an alert like this has meant recently: real incidents divided by all alerts from labelled history. That is the observed alert precision for that labelled cohort, an estimate of this conditional probability. The detector’s sensitivity answers another question. A zero observed incident count is a historical result, not proof that the next alert is false; missing labels and changes in the service can also bias the estimate.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// Labelled history for one stated cohort; a descriptive estimate, not certainty.
export function AlertBanner({ real, total }: { real: number; total: number }) {
  if (
    !Number.isSafeInteger(total) ||
    !Number.isSafeInteger(real) ||
    total < 0 ||
    real < 0 ||
    real > total
  )
    return <p role="status">Labelled alert history unavailable.</p>;

  if (total === 0) return <p role="status">No labelled alerts yet.</p>;

  return (
    <p role="alert">
      Anomaly detected. In the last 90 days, {real} of {total} labelled alerts like this
      were real incidents.
    </p>
  );
}

Check this idea later
Incidents become ten times rarer and the detector stays the same. What happens to P(incident | alert)?
A test has sensitivity 99% and false-positive rate 1%. Does a positive result mean 99% certainty?
Sources

Values above were computed in Node 22 and run in Go 1.27, Rust 1.99 and Apple clang 17. The payment service is a worked model.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.