Search

Search papers and lessons

Law of Total Probability

Level 2 · 8 min read

If you split every outcome into non-overlapping cases, the chance of an event is the weighted average of its chance within each case.

On this page

  • Colour = topic mix
  • Probability
  • Sign in to see your progress

TL;DR

Sometimes the easiest way to find the chance of an event is to split the whole situation into separate cases, work out the chance of the event inside each case, and then average those chances, weighted by how likely each case is. The split has to be clean: the cases must not overlap, and together they must cover everything that can happen.

This is the rule that turns "the answer depends on which case we are in" into a single number.

Where you'll see it: in computing the overall error rate of a classifier from its error rates on each class, in Bayes' rule where the denominator is built with this law, and in marginalising a latent variable out of a model.

Intuition

Averaging over the cases

Suppose a factory has two production lines. Line A makes 60% of the parts and Line B makes the other 40%. Line A ships 2% defective parts and Line B ships 5%. You pick one part at random from the whole output. What is the chance it is defective?

You cannot read that off directly, because the answer depends on which line made the part. But you do know the answer within each line, and you know how much of the output each line contributes. The overall chance is just the defect rate of each line, weighted by that line's share:

0.6×0.02+0.4×0.05=0.032.0.6 \times 0.02 + 0.4 \times 0.05 = 0.032.

So 3.2% of all parts are defective. Notice that this is not the simple average of 2% and 5%, which would be 3.5%. Line A's parts count for more because there are more of them. The weights matter.

That is the whole idea. Split by a question you can answer (which line made it?), get the answer in each piece, and add the pieces back up with the right weights.

flowchart TD
  W[All parts] --> A[Made by Line A, 60 percent]
  W --> B[Made by Line B, 40 percent]
  A --> DA[Defective within A: 2 percent]
  B --> DB[Defective within B: 5 percent]
  DA --> T[Total defective: 0.6 times 2 plus 0.4 times 5 equals 3.2 percent]
  DB --> T

Why the split must be clean

The method only works if every part belongs to exactly one line. If a part could come from both lines, it would be counted twice. If some parts came from neither line, they would be missing from the total. The cases have to tile the whole sample space like pieces of a jigsaw: no overlaps, no gaps. Mathematicians call such a set of cases a partition.

The math

Partitions

Definition: Partition

A collection of events B1,B2,…,BnB_1, B_2, \dots, B_n is a partition of the sample space Ω\Omega if the events are pairwise disjoint (no two of them can happen together, so Bi∩Bj=∅B_i \cap B_j = \varnothing for i≠ji \neq j) and their union covers everything, B1∪B2∪⋯∪Bn=ΩB_1 \cup B_2 \cup \dots \cup B_n = \Omega. Each BiB_i also needs P(Bi)>0P(B_i) \gt 0, so that the conditional probabilities given BiB_i exist.

The factory lines form a partition: each part came from exactly one line, and every part came from some line. The BiB_i are the "cases" and the P(Bi)P(B_i) are the weights.

The law

For any event AA and any partition B1,…,BnB_1, \dots, B_n (the sum uses summation notation),

P(A)=∑i=1nP(A∣Bi) P(Bi).P(A) = \sum_{i=1}^{n} P(A \mid B_i)\, P(B_i).

Each term is "the chance of AA inside case ii" times "how likely case ii is". The sum adds up these pieces. Note that P(A∣Bi)P(A \mid B_i) is a conditional probability (see conditional probability), so the law leans on the multiplication rule P(A∩Bi)=P(A∣Bi)P(Bi)P(A \cap B_i) = P(A \mid B_i) P(B_i).

Why it is true

Start with AA and cut it up by the cases. Because the BiB_i cover Ω\Omega, every outcome in AA lies in at least one BiB_i, so

A=(A∩B1)∪(A∩B2)∪⋯∪(A∩Bn).A = (A \cap B_1) \cup (A \cap B_2) \cup \dots \cup (A \cap B_n).

Because the BiB_i are disjoint, these pieces do not overlap. Probability adds over non-overlapping events, so

P(A)=∑i=1nP(A∩Bi).P(A) = \sum_{i=1}^{n} P(A \cap B_i).

Finally, apply the multiplication rule to each term, P(A∩Bi)=P(A∣Bi) P(Bi)P(A \cap B_i) = P(A \mid B_i)\, P(B_i), and the law follows. Each step is one line, and none of them needs more than the definition of conditional probability.

Checking the factory example

Let BAB_A be "made by Line A" and BBB_B be "made by Line B". Let DD be "defective". Then

P(D∣BA)=0.02,P(BA)=0.60,P(D∣BB)=0.05,P(BB)=0.40,\begin{aligned} P(D \mid B_A) &= 0.02, & P(B_A) &= 0.60, \\ P(D \mid B_B) &= 0.05, & P(B_B) &= 0.40, \end{aligned}

and

P(D)=P(D∣BA)P(BA)+P(D∣BB)P(BB)=0.012+0.020=0.032.P(D) = P(D \mid B_A) P(B_A) + P(D \mid B_B) P(B_B) = 0.012 + 0.020 = 0.032.

The law gives the same answer as the intuition above, now written in symbols.

The continuous version

When the cases form a continuum, the sum becomes an integral. If YY is a random variable with density p(y)p(y) and XX depends on YY, then the density of XX is

p(x)=∫p(x∣y) p(y) dy.p(x) = \int p(x \mid y)\, p(y)\, dy.

This is the same idea: p(x∣y)p(x \mid y) is the density of XX inside the case Y=yY = y, the integral averages over all cases, and p(y)p(y) is the weight. Statisticians call it marginalising out YY.

Worked examples

Example
Two bags, one draw

A bag is chosen by a fair coin toss. Bag 1 holds 3 red balls and 1 blue ball. Bag 2 holds 1 red ball and 3 blue balls. You draw one ball from the chosen bag. What is the chance it is red?

  1. The cases are "Bag 1" and "Bag 2". They are disjoint and cover everything, so they form a partition.
  2. The weights are P(Bag 1)=1/2P(\text{Bag 1}) = 1/2 and P(Bag 2)=1/2P(\text{Bag 2}) = 1/2.
  3. Within Bag 1, the chance of red is 3/43/4. Within Bag 2, it is 1/41/4.
  4. Apply the law: P(red)=34⋅12+14⋅12=38+18=12P(\text{red}) = \tfrac{3}{4} \cdot \tfrac{1}{2} + \tfrac{1}{4} \cdot \tfrac{1}{2} = \tfrac{3}{8} + \tfrac{1}{8} = \tfrac{1}{2}.

Interpretation: the two bags are mirror images, so overall a red ball is exactly as likely as a blue one. Each bag is lopsided, but the lopsidedness cancels because the bags are equally likely to be chosen.

Example
A spam filter's overall rate

A mail filter sorts messages into two groups by whether they contain the word "invoice". Suppose 30% of all messages contain "invoice", and 10% of those are spam. Among messages without "invoice", 2% are spam. What fraction of all messages are spam?

  1. Cases: CC = "contains invoice" and CcC^c = "does not contain invoice". These partition all messages.
  2. Weights: P(C)=0.30P(C) = 0.30 and P(Cc)=0.70P(C^c) = 0.70.
  3. Spam rates within each case: P(spam∣C)=0.10P(\text{spam} \mid C) = 0.10 and P(spam∣Cc)=0.02P(\text{spam} \mid C^c) = 0.02.
  4. Apply the law: P(spam)=0.10⋅0.30+0.02⋅0.70=0.030+0.014=0.044P(\text{spam}) = 0.10 \cdot 0.30 + 0.02 \cdot 0.70 = 0.030 + 0.014 = 0.044.

Interpretation: about 4.4% of all messages are spam. A naive guess that averages 10% and 2% would give 6%, which is wrong because most messages fall in the low-spam group.

Tradeoffs and failure modes

The law is a calculation, not a modelling choice, so it is hard to use wrongly in the formula itself. Most mistakes come from the setup.

SituationWhat goes wrongBetter choice
Cases overlapSome outcomes are counted twice and the total is too bigRedefine the cases so each outcome falls in exactly one
Cases leave gapsSome outcomes are missing and the total is too smallAdd a catch-all case such as "none of the above"
Averaging the rates without weightsSmall cases get the same say as big onesUse the case probabilities as weights
Conditioning on an event of probability zeroThe conditional probability is undefinedDrop the case, or use a density in the continuous version
Many or continuous casesSumming over every case is impracticalUse the integral form, or a model with a closed-form marginal

A common confusion is between P(A∣B)P(A \mid B) and P(B∣A)P(B \mid A). The law tells you how to get P(A)P(A) from the first kind of quantity. Turning the answer around to get P(B∣A)P(B \mid A) needs Bayes' rule, which uses this law in its denominator.

History

The idea of splitting a question into cases and averaging is much older than its formal name. It appears in early probability arguments, including the 18th-century work of Thomas Bayes on reasoning backwards from observations and Pierre-Simon Laplace's treatises on probability. Laplace used this kind of case-splitting routinely.

The modern treatment came in the 1930s. Andrey Kolmogorov's axioms, published in 1933, defined probability as a measure on a sample space. Once probability is defined that way, the law is a short theorem that follows from additivity over disjoint events, which is exactly the proof above. It caught on because it is the basic tool for combining conditional information, and nearly every later calculation with conditional probabilities uses it somewhere.

Check yourself

QuestionRecall: what two conditions must a set of events B1,…,BnB_1, \dots, B_n meet to be a partition of the sample space?Show answer

The events must be pairwise disjoint, so no two of them can happen together, and their union must cover the whole sample space, so one of them always happens. In addition, each BiB_i should have positive probability so that P(A∣Bi)P(A \mid B_i) is defined.

QuestionA machine has two shifts. Shift 1 runs 70% of the hours and has a fault rate of 1%. Shift 2 runs 30% of the hours and has a fault rate of 4%. What is the overall fault rate?Show answer

Use the law with the shifts as the partition: 0.70⋅0.01+0.30⋅0.04=0.007+0.012=0.0190.70 \cdot 0.01 + 0.30 \cdot 0.04 = 0.007 + 0.012 = 0.019. The overall fault rate is 1.9%. The simple average of 1% and 4% would be 2.5%, which is wrong because Shift 1 runs more often and should carry more weight.

QuestionWhy is the sum of conditional rates weighted by the case probabilities, and not simply averaged?Show answer

The overall probability counts every outcome of AA once. Each case contributes the share of AA that falls inside it, and that share is the case's probability times its internal rate. A case that is rarely true contributes only a small piece of the total, so an unweighted average would give it too much influence.

QuestionA test flags a part as faulty. Two suppliers provide parts. Supplier X provides 80% of parts, and the chance a part from X is flagged is 0.05. Supplier Y provides 20%, and the chance a part from Y is flagged is 0.15. A part is picked at random. What is the chance it is flagged, and which supplier's parts drive most of the flags?Show answer

By the law, the chance is 0.80⋅0.05+0.20⋅0.15=0.040+0.030=0.0700.80 \cdot 0.05 + 0.20 \cdot 0.15 = 0.040 + 0.030 = 0.070, or 7%. Supplier X contributes 0.040 of that 0.070, which is more than half, so most flags come from X. Supplier Y has a much higher rate, but it provides only a fifth of the parts, so its contribution is smaller.

Sources