IV.6 · Probability
How much should a flag change our belief?
Change a fictional quality model's prior and flag likelihoods. Watch both groups contribute evidence, then normalize their masses to obtain a posterior.
Evidence moves probability mass
| Group | Count | Mass |
|---|---|---|
| A and E | 90 | 0.009 |
| A and Eᶜ | 10 | 0.001 |
| Aᶜ and E | 495 | 0.0495 |
| Aᶜ and Eᶜ | 9405 | 0.9405 |
Fictional quality flag model. E is the positive flag. Expected counts in 10,000 items: A and E = 90; A and Eᶜ = 10; Aᶜ and E = 495; Aᶜ and Eᶜ = 9405. Observed positive evidence changes the mass of A from 0.01 to 2/13. The posterior masses sum to exactly one.
- Prior
- 0.01
- Evidence likelihood given A
- 0.9
- Evidence likelihood given Aᶜ
- 0.05
- False negative rate
- 0.1
- Normalizing evidence
- 0.0585
- Posterior
- 2/130.153846…
= exact model quantity · ≈ numerical companion or observed estimate
from 0 to 100, up to 2 decimal places
from 0 to 100, up to 2 decimal places
from 0 to 100, up to 2 decimal places
Before observing evidence
Prior mass of A is 0.01.
The example is a fictional item-quality flag, not a real diagnostic or medical recommendation. Before changing the prior, predict whether a flag rate of 90% among defective items implies that 90% of flagged items are defective.
Intuition: evidence comes from both groups
A flag can be produced by a defective item or a nondefective item. If defective items are rare, the large nondefective group can contribute more flags even when its own flag rate is small. Updating a belief requires comparing these two sources of evidence.
Begin with prior mass in each group. Multiply each by how likely that group is to produce the observed evidence. Their retained masses need not sum to one: their total is the probability of the evidence. Divide by that total to compare the groups among items that match the evidence.
Definition
Let A mean “the item is defective” and E mean “the item is flagged.” Write the prior , the flag likelihood , and the other flag likelihood . All three parameters lie in .
The positive-evidence masses are and . The evidence probability is their sum, and Bayes’ theorem gives
The posterior is the class probability after observing E. The false-positive rate is and the false-negative rate is , relative to this fictional class model. A likelihood conditions on the class; a posterior conditions on the evidence. They have different denominators.
Mechanics: prior, likelihood, retained mass, posterior
The stage’s canonical parameters specify one joint model. Its mass view, equations, inspector, population table, accessible interpretation, and trace follow that model and the selected evidence. Changing the prior changes group sizes; changing a likelihood changes the retained fraction within its group.
Use the evidence selector to switch to no flag. The likelihoods become and , and the retained total becomes . Replay the trace to follow the same update with the new evidence. Normalized posterior masses sum to one whenever the selected evidence has positive probability.
Derivation: Bayes from the joint mass
The multiplication rule computes the same intersection in two ways:
The two classes A and partition the sample space. Their evidence contributions therefore add to , which supplies the denominator. For negative evidence,
The joint model conserves original total mass: . Restriction retains only the two evidence-compatible cells; normalization rescales them to total one. It does not change the original four cells.
Worked examples
Use prior , , and . In a notional population of items, the expected counts are:
| Class | Flag E | No flag | Total |
|---|---|---|---|
| Defective A | 90 | 10 | 100 |
| Nondefective | 495 | 9405 | 9900 |
| Total | 585 | 9415 | 10000 |
These counts scale model probabilities; they are not a simulated sample or a promise that an actual batch contains those numbers. Other parameter choices can produce fractional expected counts, which should remain fractional rather than be silently rounded to people or items.
Worked example: Evidence moves probability mass
| Group | Count | Mass |
|---|---|---|
| A and E | 90 | 0.009 |
| A and Eᶜ | 10 | 0.001 |
| Aᶜ and E | 495 | 0.0495 |
| Aᶜ and Eᶜ | 9405 | 0.9405 |
Fictional quality flag model. E is the positive flag. Expected counts in 10,000 items: A and E = 90; A and Eᶜ = 10; Aᶜ and E = 495; Aᶜ and Eᶜ = 9405. Observed positive evidence changes the mass of A from 0.01 to 2/13. The posterior masses sum to exactly one.
- Prior
- 0.01
- Evidence likelihood given A
- 0.9
- Evidence likelihood given Aᶜ
- 0.05
- False negative rate
- 0.1
- Normalizing evidence
- 0.0585
- Posterior
- 2/130.153846…
= exact model quantity · ≈ numerical companion or observed estimate
from 0 to 100, up to 2 decimal places
from 0 to 100, up to 2 decimal places
from 0 to 100, up to 2 decimal places
Before observing evidence
Prior mass of A is 0.01.
With no flag, the defective share is . It is not ; that complement is the nondefective share among flagged items. Raising the prior to with the same positive likelihoods changes the posterior to . Base rates matter even when the flag mechanism does not change.
Executable reference code
Python constructs the exact four-cell joint table and returns evidence mass plus a posterior, using None for impossible evidence. Assertions verify counts, both evidence directions, base-rate sensitivity, and boundary cases. NumPy uses the same independently specified parameters to construct a floating-point joint array and compares it against literal exact fractions. Neither program depends on the browser implementation or imports TypeScript outputs.
"""Bayes' theorem for a fictional quality flag, not a real diagnostic tool.
All masses and expected counts are exact Fractions. Expected population counts
are a scaled model, not observations; arbitrary parameters can yield fractions.
"""
from fractions import Fraction as F
def joint_table(prior, true_flag, false_flag):
if any(not isinstance(x, F) or not 0 <= x <= 1 for x in (prior, true_flag, false_flag)):
raise ValueError("Prior and likelihoods must be Fractions in [0, 1]")
return {"A,flag": prior * true_flag,
"A,no flag": prior * (1 - true_flag),
"not A,flag": (1 - prior) * false_flag,
"not A,no flag": (1 - prior) * (1 - false_flag)}
def update(prior, true_flag, false_flag, positive=True):
if type(positive) is not bool:
raise ValueError("Select flag (True) or no flag (False) evidence")
joint = joint_table(prior, true_flag, false_flag)
suffix = "flag" if positive else "no flag"
a_mass = joint["A," + suffix]
not_a_mass = joint["not A," + suffix]
evidence = a_mass + not_a_mass
posterior = a_mass / evidence if evidence else None
return evidence, posterior
def main():
p, t, f = F(1, 100), F(9, 10), F(1, 20)
joint = joint_table(p, t, f)
assert sum(joint.values(), F(0)) == 1
assert joint["A,flag"] + joint["A,no flag"] == p
expected_counts = {cell: mass * 10000 for cell, mass in joint.items()}
assert expected_counts == {"A,flag": F(90), "A,no flag": F(10),
"not A,flag": F(495), "not A,no flag": F(9405)}
assert update(p, t, f) == (F(117, 2000), F(2, 13))
assert update(p, t, f, False) == (F(1883, 2000), F(2, 1883))
assert update(F(1, 10), t, f) == (F(27, 200), F(2, 3))
assert update(p, F(1, 5), F(1, 5))[1] == p
assert update(p, F(1, 5), F(1, 5), False)[1] == p
assert update(p, t, F(0))[1] == 1
assert update(p, F(0), F(0)) == (F(0), None)
assert update(p, F(1), F(1), False) == (F(0), None)
assert update(F(0), t, f)[1] == 0
assert update(F(1), t, f)[1] == 1
assert update(F(0), t, F(0))[1] is None
tiny = F(1, 10 ** 100)
assert update(p, tiny, tiny) == (tiny, p)
assert joint_table(F(1, 3), F(1, 2), F(1, 4))["A,flag"] * 10 == F(5, 3)
for params in ((F(-1), t, f), (p, F(2), f), (p, t, F(-1, 20)), (0.01, t, f)):
try:
update(*params)
except ValueError:
pass
else:
raise AssertionError("Invalid parameters were accepted")
print("Fictional quality flag: prior 1%, P(flag|A) 90%, P(flag|not A) 5%")
print("Exact expected counts for N = 10000:", expected_counts)
print("Positive evidence (mass, posterior):", update(p, t, f))
print("Negative evidence (mass, posterior):", update(p, t, f, False))
print("Prior 10%, unchanged likelihoods:", update(F(1, 10), t, f))
print("Impossible evidence:", update(p, F(0), F(0)), "; None means undefined")
if __name__ == "__main__":
main()"""A fictional quality flag as a joint array: rows are classes, columns evidence.
Compare against literal exact fractions with absolute tolerance 1e-12, relative
tolerance 0. Expected counts scale the model; they are not simulated observations.
"""
from fractions import Fraction as F
import numpy as np
ATOL = 1e-12
def update(prior, true_flag, false_flag, positive=True):
values = np.asarray([prior, true_flag, false_flag], dtype=float)
if not np.all(np.isfinite(values)) or np.any(values < 0) or np.any(values > 1):
raise ValueError("Prior and likelihoods must be finite and in [0, 1]")
if type(positive) is not bool:
raise ValueError("Select flag (True) or no flag (False) evidence")
p, t, f = values
priors = np.array([p, 1 - p])
likelihoods = np.array([[t, 1 - t], [f, 1 - f]])
# shortcut: direct float products can underflow, use scaling/log-space for tiny likelihoods.
joint = priors[:, None] * likelihoods
np.testing.assert_allclose(joint.sum(), 1.0, rtol=0, atol=ATOL)
np.testing.assert_allclose(joint.sum(axis=1), priors, rtol=0, atol=ATOL)
retained = joint[:, 0 if positive else 1]
evidence = retained.sum()
posterior = retained / evidence if evidence > 0 else None
return joint, evidence, posterior
def main():
p, t, f = 0.01, 0.90, 0.05
joint, evidence, posterior = update(p, t, f)
expected_counts = np.array([[90, 10], [495, 9405]])
np.testing.assert_allclose(joint * 10000, expected_counts, rtol=0, atol=ATOL)
np.testing.assert_allclose(evidence, float(F(117, 2000)), rtol=0, atol=ATOL)
np.testing.assert_allclose(posterior, [float(F(2, 13)), float(F(11, 13))], rtol=0, atol=ATOL)
np.testing.assert_allclose(posterior.sum(), 1.0, rtol=0, atol=ATOL)
_, negative_mass, negative_posterior = update(p, t, f, False)
np.testing.assert_allclose(negative_mass, float(F(1883, 2000)), rtol=0, atol=ATOL)
np.testing.assert_allclose(negative_posterior[0], float(F(2, 1883)), rtol=0, atol=ATOL)
np.testing.assert_allclose(update(0.1, t, f)[2][0], float(F(2, 3)), rtol=0, atol=ATOL)
np.testing.assert_allclose(update(p, 0.2, 0.2)[2][0], p, rtol=0, atol=ATOL)
np.testing.assert_allclose(update(p, 0.2, 0.2, False)[2][0], p, rtol=0, atol=ATOL)
assert update(p, 0.0, 0.0)[2] is None
assert update(p, 1.0, 1.0, False)[2] is None
assert update(0.0, t, f)[2][0] == 0.0
assert update(1.0, t, f)[2][0] == 1.0
np.testing.assert_allclose(update(p, t, 0.0)[2], [1.0, 0.0], rtol=0, atol=ATOL)
# A small positive evidence total is not invalid merely because it is below ATOL.
np.testing.assert_allclose(update(p, 1e-100, 1e-100)[2][0], p, rtol=0, atol=ATOL)
for params in ((-0.1, t, f), (p, 1.1, f), (p, t, -0.01), (np.nan, t, f), (p, np.inf, f)):
try:
update(*params)
except ValueError:
pass
else:
raise AssertionError("Invalid Bayes parameters were accepted")
print("Fictional quality joint masses ≈\n", joint)
print("Expected counts for N = 10000 ≈\n", joint * 10000)
print("Positive evidence mass ≈", evidence, "(exact 117/2000)")
print("Defective/nondefective posterior ≈", posterior, "(exact 2/13, 11/13)")
print("No-flag defective posterior ≈", negative_posterior[0], "(exact 2/1883)")
print("Zero represented evidence: undefined (None); underflow needs a separate policy")
if __name__ == "__main__":
main()Explore
Set both likelihoods equal and positive. Positive evidence then retains the same proportion of each class, leaving the posterior equal to the prior. If both are strictly less than one, the same is true after negative evidence. Next set the false-positive rate to zero with positive prior and positive true-flag likelihood: every possible flag comes from A, so its posterior is one.
Set both positive likelihoods to zero and choose flag evidence. The evidence is impossible and the posterior must be undefined. Set both to one and choose no flag for the parallel case. Try prior zero or one and identify when the selected evidence is still possible under the surviving class.
Common mistakes and counterexamples
Confusing with reverses the conditioning direction. Ignoring the nondefective evidence contribution commits the base-rate fallacy. Using the false-negative rate as the negative-evidence posterior also skips the denominator.
A flag is evidence under an assumed model, not certainty about an individual item or proof of a cause. The fictional rates are teaching parameters, not empirically calibrated quality guarantees. An observed count table would need its own sampling and estimation analysis before its rates could be treated as known.
Numerical considerations
Finite rational parameters give exact joint masses, expected counts, evidence totals, and posteriors. Decimal displays with are approximations of these exact quantities. The NumPy reference uses absolute tolerance and zero relative tolerance for these small specified examples, rather than asserting binary-decimal identity.
Very small but positive evidence remains mathematically valid; division may expose input uncertainty or floating-point underflow. A general numerical implementation may need scaling or log-space normalization, but must disclose that policy and preserve the distinction between impossible evidence and arithmetic loss. Adding epsilon to an impossible denominator changes the model and does not derive Bayes’ theorem.
Engineering and ML connections
Bayesian classification updates class priors with feature likelihoods. Changing deployment base rates can change posterior predictions even if the class-conditional likelihoods stay fixed. This is one reason a detection rate alone is insufficient to interpret flagged cases. Practical models must justify their likelihoods and priors; the algebra faithfully updates assumptions but cannot certify those assumptions.
Exercises
Summary and checkpoints
Multiply each prior group by its evidence likelihood, add the retained masses, then normalize. Check that you can explain the 90 versus 495 flagged counts, derive the exact posterior , update for negative evidence, predict a base-rate change, and leave impossible evidence undefined.