Course contents 04 / 22

04 / 22 · Foundations

Conditional probability and Bayes' formula

Update probabilities by keeping the outcomes compatible with an observation. One exact finite population explains the chain rule, Bayes and odds, conditional independence, prior changes and why a flag's reporting rule changes its meaning.

Prerequisites

Kolmogorov's axioms

On this page 6

A probability table describes what can happen before an observation. Once a flag appears, some records are excluded and the remaining weights must be rescaled. If an interface chooses which flag to show, even that selection rule becomes part of the observation. The axioms note supplied the probability measure; we now ask different questions of that same measure.

We use a completely specified fictional population of 72 job records. Each has a processing mode H or L and two binary flags U and V. The counts, reporting policies, diagrams and exact code were composed for this project with AI assistance. They are not observed service statistics, an external dataset or evidence about a real classifier. Each record is equally likely to be selected; repeated experiments sample independently with replacement when repetition is mentioned.

ModeU and VU onlyV onlyNeitherTotal
H1264224
L21063048
Total1416103272

Write HH for the event that the chosen record has mode H. Its complement is mode L. We also use UU and VV for the events that those flags are present. A pair of flags is a property of one selected record, not two independent selections.

Conditioning shrinks the sample space#

For any events AA and BB with P(B)>0P(B)>0, define

P(A∣B)=P(A∩B)P(B).P(A\mid B)=\frac{P(A\cap B)}{P(B)}.

Only outcomes in BB remain relevant. Their original relative weights are preserved, and division by P(B)P(B) makes their total one. In a uniform finite population this becomes the number satisfying both conditions divided by the number satisfying the condition after the bar.

There are 30 records with U, of which 18 have mode H. There are 24 mode-H records, of which the same 18 have U. Thus

P(H∣U)=1830=35,P(U∣H)=1824=34.P(H\mid U)=\frac{18}{30}=\frac35,\qquad P(U\mid H)=\frac{18}{24}=\frac34.

The numerator is the same intersection; the denominator changes. The direction of the conditional is therefore part of the question, not a typographic choice.

Figure

One numerator, different denominators

All records

Total: 72

H: 24L: 48

P(H) = 24/72 = 1/3

Keep U

Total: 30

H ∩ U: 18L ∩ U: 12

P(H | U) = 18/30 = 3/5

Keep H

Total: 24

U ∩ H: 18¬U ∩ H: 6

P(U | H) = 18/24 = 3/4

Both conditions use an intersection of 18 records. Conditioning on U gives denominator 30; conditioning on H gives 24. Bar widths show shares within the retained population; labelled numbers count records.

For fixed BB, the map A↦P(A∣B)A\mapsto P(A\mid B) satisfies the probability axioms. Nonnegative weights stay nonnegative, the whole space has conditional probability one, and disjoint events remain disjoint after intersecting with BB. Hence complements, unions and bounds still work, provided the conditioning event stays fixed. For example, P(Hc∣U)=1−3/5=2/5P(H^c\mid U)=1-3/5=2/5.

The fraction definition does not assign a value when P(B)=0P(B)=0. Conditional densities and conditional expectations later handle conditioning on continuous variables under additional definitions; they are not obtained by evaluating an undefined 0/00/0.

The chain rule#

Multiplying the definition by its denominator gives P(A∩B)=P(A)P(B∣A)P(A\cap B)=P(A)P(B\mid A) when P(A)>0P(A)>0. Repeating the same step gives

P ⁣(⋂j=1nAj)=P(A1)∏j=2nP ⁣(Aj∣⋂i<jAi),P\!\left(\bigcap_{j=1}^n A_j\right) =P(A_1)\prod_{j=2}^n P\!\left(A_j\mid\bigcap_{i<j}A_i\right),

when every conditioning prefix has positive probability. This is an identity, not an independence assumption. If a prefix has probability zero, the full intersection is already zero; one should stop there instead of inserting undefined later conditional fractions.

For the three events H, U and V, the table gives

P(H∩U∩V)=2472⋅1824⋅1218=1272=16.P(H\cap U\cap V)=\frac{24}{72}\cdot\frac{18}{24}\cdot\frac{12}{18}=\frac{12}{72}=\frac16.

Each factor changes the denominator to the population remaining after the preceding conditions. In a sequence model the same idea factors the probability of a fixed finite sequence into probabilities of each next symbol given its prefix. Whether a model estimates these factors well is a separate matter. See the n-gram note for that application; it is not needed for the calculations here.

The law of total probability#

A finite or countable partition consists of disjoint events whose union is the whole space. Split an event EE by a partition BiB_i. Summing the disjoint intersections gives

P(E)=∑iP(E∩Bi)=∑i:P(Bi)>0P(E∣Bi)P(Bi).P(E)=\sum_i P(E\cap B_i) =\sum_{i:P(B_i)>0}P(E\mid B_i)P(B_i).

Zero-probability partition cells contribute zero to the first sum and are omitted from the conditional form. Positive probability is needed to use the conditional fraction, not to call the sets a partition.

For U, use the H/L partition:

P(U)=34⋅13+14⋅23=512.P(U)=\frac34\cdot\frac13+\frac14\cdot\frac23=\frac5{12}.

The result matches 30 out of 72 records. The conditional rates are averaged using mode proportions; adding the two rates or averaging them equally would describe a different population.

Now let an interface show only one flag when any are present, and show nothing when neither is present. A displayed V is not necessarily equivalent to “V is present.” Consider two explicit rules.

Priority-U rule. If U is present, display U; otherwise display V when V is present. A V report comes only from the V-only column. Of its ten records, four are H. Therefore P(H∣report V)=4/10=2/5P(H\mid\text{report V})=4/10=2/5.

Uniform-present rule. If both flags are present, choose one of them uniformly using a fresh random choice independent of the record; if just one is present, display it. H contributes 4+12/2=104+12/2=10 units of report-V weight, while L contributes 6+2/2=76+2/2=7. The posterior is now P(H∣report V)=10/17P(H\mid\text{report V})=10/17. These weights sum probabilities of reporting V within each table cell; they are not a claim that a random run always splits every cell into exact halves.

Both policies use the same 72-record table. They differ because the observation includes a selection mechanism. Simply seeing V present with no selective reporting would instead give P(H∣V)=16/24=2/3P(H\mid V)=16/24=2/3. To choose among these three answers, state what was observed and how it was reported.

Bayes’ formula#

Computing the same intersection in both directions gives, for P(E)>0P(E)>0 and P(H)>0P(H)>0,

P(H∣E)=P(E∣H)P(H)P(E).P(H\mid E)=\frac{P(E\mid H)P(H)}{P(E)}.

Here P(H)P(H) is the prior, P(E∣H)P(E\mid H) the likelihood of the evidence under that hypothesis, and P(H∣E)P(H\mid E) the posterior. In our finite calculation the prior is simply the known population proportion. For the U flag, the numerator is (3/4)(1/3)=1/4(3/4)(1/3)=1/4, and the denominator is 5/125/12, giving 3/53/5. Bayes does not turn a likelihood into a posterior without that normalization and prior weight.

Assume both hypotheses have positive prior probabilities, and the evidence has positive likelihood under each hypothesis. Divide the H equation by its complementary equation. With odds O(H)=P(H)/P(Hc)O(H)=P(H)/P(H^c),

O(H∣E)=O(H)P(E∣H)P(E∣Hc).O(H\mid E)=O(H)\frac{P(E\mid H)}{P(E\mid H^c)}.

All denominators are positive in this example. The likelihood ratio for U is (3/4)/(1/4)=3(3/4)/(1/4)=3. For V it is (2/3)/(1/6)=4(2/3)/(1/6)=4. The table makes U and V conditionally independent separately in H and L, as verified in the next section. This lets the second flag use the same V likelihood ratio after U. Consequently the odds evolve as

12 → U  32 → V  6,P(H)=13 → P(H∣U)=35 → P(H∣U∩V)=67.\frac12\ \xrightarrow{\ U\ }\ \frac32\ \xrightarrow{\ V\ }\ 6, \qquad P(H)=\frac13\ \to\ P(H\mid U)=\frac35\ \to\ P(H\mid U\cap V)=\frac67.
Figure

Two distinct observations update the odds

Before flags

—
H: 24 · L: 48Odds 1:2

P(H) = 1/3

After U

× 3
H: 18 · L: 12Odds 3:2

P(H | U) = 3/5

After U and V

× 4
H: 12 · L: 2Odds 6:1

P(H | U ∩ V) = 6/7

Each row calculates H:L odds from the retained records. The V multiplier is four because the flags are conditionally independent within each mode. Showing the same U again would leave the second row unchanged.

Without conditional independence, the second multiplier would instead be P(V∣H∩U)/P(V∣Hc∩U)P(V\mid H\cap U)/P(V\mid H^c\cap U). Copying the already observed U flag supplies no new information: conditioning on U∩UU\cap U is conditioning on U. Multiplying by the U likelihood ratio again would double-count the same observation.

Taking logarithms converts positive likelihood-ratio products into sums. An exact posterior under a changed prior has a useful special case. Assume the class-conditional distribution of an observed feature xx stays unchanged, while the mode prior changes; this is label shift. Then

log⁡Onew(H∣x)=log⁡Oold(H∣x)+log⁡Onew(H)−log⁡Oold(H),\log O_{\rm new}(H\mid x)=\log O_{\rm old}(H\mid x) +\log O_{\rm new}(H)-\log O_{\rm old}(H),

on the common support where these odds are finite and positive. This follows by cancelling the unchanged class-conditional likelihood ratio; it does not follow merely from noticing a changed overall frequency. The label-shift assumption is discussed by Lipton, Wang and Smola in the reference below; their estimation procedure and experiments are not used here.

If the new H prior is 1/21/2 while both conditional flag distributions stay fixed, the prior odds rise from 1/21/2 to 1. The U posterior becomes 3/43/4, and the U-and-V posterior becomes 12/1312/13. If instead Pnew(U∣H)=1/2P_{\rm new}(U\mid H)=1/2 while Pnew(U∣Hc)=1/4P_{\rm new}(U\mid H^c)=1/4, the U posterior at that same prior is 2/32/3. The unchanged-likelihood correction predicting 3/43/4 no longer applies.

For a model, the formula presumes the old prediction is the relevant feature-conditional posterior. Ordinary calibration alone is weaker. A constant prediction of 1/31/3 is calibrated over the original population, since exactly a third of its records are H, but it misses the U-specific posterior 3/53/5. Correcting that constant’s prior odds gives 1/21/2, not the new U-specific posterior 3/43/4. Calibration grouped by predicted value must not be confused with recovery of every feature-conditional probability.

Independence#

Events AA and BB are independent when P(A∩B)=P(A)P(B)P(A\cap B)=P(A)P(B). If P(B)>0P(B)>0, this is equivalent to P(A∣B)=P(A)P(A\mid B)=P(A). Disjoint positive-probability events cannot be independent: their intersection has probability zero while the product is positive.

Independence of a whole collection requires the product identity for every finite subcollection, not only every pair. For a separate finite construction, choose uniformly among 48 tickets. Three binary marks A, B and C divide the tickets into the following groups; all eight combinations occur.

ABC000001010011100101110111
Tickets1157113353

The marginal probabilities are P(A)=1/2P(A)=1/2, P(B)=1/3P(B)=1/3, P(C)=1/4P(C)=1/4. Pair counts are 8, 6 and 4, so

P(A∩B)=16=1213,P(A∩C)=18=1214,P(B∩C)=112=1314.P(A\cap B)=\frac16=\frac12\frac13,\quad P(A\cap C)=\frac18=\frac12\frac14,\quad P(B\cap C)=\frac1{12}=\frac13\frac14.

Every pair is independent. But the triple has probability 3/48=1/16≠1/24=(1/2)(1/3)(1/4)3/48=1/16\ne1/24=(1/2)(1/3)(1/4). Pairwise independence has not made all three independent. The unequal margins and positive counts also make clear that no mark is a deterministic checksum of the other two.

Conditional independence applies the same definition within a fixed condition. In the job table,

P(U∩V∣H)=1224=34⋅23,P(U∩V∣Hc)=248=14⋅16.\begin{aligned} P(U\cap V\mid H)&=\frac{12}{24}=\frac34\cdot\frac23,\\ P(U\cap V\mid H^c)&=\frac2{48}=\frac14\cdot\frac16. \end{aligned}

So U and V are independent within each mode. Marginally, however,

P(U∩V)=736≠512⋅13=536,P(V∣U)=715≠P(V)=13.P(U\cap V)=\frac7{36}\ne\frac5{12}\cdot\frac13=\frac5{36}, \qquad P(V\mid U)=\frac7{15}\ne P(V)=\frac13.

Seeing U changes the mixture of H and L records, and those modes have different V rates. This is why conditional independence is the assumption needed for multiplying evidence likelihoods within a hypothesis; it is not a claim that the observations are independent before the hypothesis is known. The table verifies this exact construction. A few observed examples in a real application would not establish those identities.

What changed#

QuestionRequired operation
What remains after an event?Restrict its outcomes and renormalize.
What is the probability of several observations?Multiply conditional factors, with positive prefixes.
Which hidden mode could have produced them?Weight likelihoods by priors and normalize.
Can separate likelihood ratios be multiplied?Check conditional independence within each hypothesis, or use conditional ratios.
Does a displayed flag mean it is present?Include the reporting policy in the observation.

Limitations#

Every numerical result here is exact for the specified finite population and reporting policies. It is not an estimate of a real diagnostic system. A conditional fraction needs a positive conditioning probability; continuous point conditioning needs further definitions. Independence, label shift and a reporting rule are model assumptions that must be justified separately. Reweighting a prior cannot fix wrong class-conditional likelihoods or a predictor that only has marginal calibration. Conditioning also describes information, not the effect of intervening to set a flag.

What you should remember#

The event after the bar determines the denominator. The chain rule needs conditional factors; independent factors are a special case. Total probability partitions an event, while Bayes reverses a conditional by restoring the prior and normalizer. Odds updates must avoid repeated evidence and must condition correctly within each hypothesis. The same underlying records can yield different posteriors when the reporting mechanism changes.

Code#

The following standard-library calculation reproduces the two conditionals, both posteriors from the flags and both report-V policies. The fuller companion verifier also enumerates the weighted pairwise-independence construction and prior changes. Inputs are exact population counts, not random training or test samples.

from fractions import Fraction as F

rows = {'H': [12, 6, 4, 2], 'L': [2, 10, 6, 30]}
h_u = rows['H'][0] + rows['H'][1]
l_u = rows['L'][0] + rows['L'][1]
print('H given U, U given H:', F(h_u, h_u+l_u), F(h_u, sum(rows['H'])))
print('H given U and V:', F(rows['H'][0], rows['H'][0]+rows['L'][0]))
priority_h, priority_l = rows['H'][2], rows['L'][2]
random_h = F(rows['H'][0], 2) + rows['H'][2]
random_l = F(rows['L'][0], 2) + rows['L'][2]
print('H given report V:', F(priority_h, priority_h+priority_l),
      random_h/(random_h+random_l))
H given U, U given H: 3/5 3/4
H given U and V: 6/7
H given report V: 2/5 10/17

Next#

So far a flag defines a yes-or-no event. A count or waiting time is a random variable with several possible values. Discrete distributions introduces common finite and countable models and checks when their assumptions fit a mechanism. Joint distributions later returns to conditioning several numerical variables, including density-based conditioning.

References#

  • Kyle Siegrist, Random, Conditional Probability, definitions, multiplication rule, total probability and Bayes; Independence, two-event, mutual and conditional definitions. The author declares CC BY 2.0 on the home page. These are mathematical references; their worked exercises, diagrams and code are not imported.
  • Zachary C. Lipton, Yu-Xiang Wang, Alexander J. Smola. Detecting and Correcting for Label Shift with Black Box Predictors, 2018, abstract and Section 1 for the fixed class-conditional assumption and its limits. Reference only: no medical example, estimator implementation, experiment or reported result is used. The finite prior-change and calibration examples above are calculated from this note’s original table.