Lemonmath
Tiếng Việt

The law of total probability and Bayes' theorem

Lesson 6.2 · 20 min read · Updated

A rare disease affects 1%1\% of the population. A test gives a positive result for 95%95\% of people who have the disease, but it also gives a false positive for 5%5\% of people who do not. Lan takes the test and gets a positive result. Is the chance that Lan has the disease 95%95\%?

Many people, adults included, answer "yes". The right answer is much lower. This lesson gives us the tools to find it.

The law of total probability

Explore: picture 10 00010\,000 people

Instead of talking about probabilities, we imagine 10 00010\,000 people taking the test.

  • 1%1\% of them, that is 100100 people, have the disease. Of these, 95%95\%, that is 9595 people, test positive.
  • 9 9009\,900 people do not have the disease. Of these, 5%5\%, that is 495495 people, test positive by mistake.

The total number of positive results is 95+495=59095+495=590. Of those 590590 people, only 9595 really have the disease:

95590≈0.161.\frac{95}{590}\approx0.161.

So a positive result means only about a 16%16\% chance of the disease. To get 590590 we added the positive results from two cases: with the disease and without it. That is the idea behind the law of total probability.

Why is it true? The event AA happens either together with BB or together with B‾\overline{B}, so A=(A∩B)∪(A∩B‾)A=(A\cap B)\cup(A\cap\overline{B}), and these two parts are mutually exclusive. By the addition rule, P(A)=P(A∩B)+P(A∩B‾)P(A)=P(A\cap B)+P(A\cap\overline{B}). Each term comes from the multiplication rule, for example P(A∩B)=P(B)⋅P(A∣B)P(A\cap B)=P(B)\cdot P(A\mid B).

On a tree diagram the formula is easy to remember: multiply along each path that leads to AA, then add those paths.

0.60.40.020.980.050.95ItemMachine IMachine IIDefective: 0.012Good: 0.588Defective: 0.02Good: 0.38
Tree diagram for the two machines

Bayes' theorem

The next question is often a "backwards" one: we have seen the result AA, so how likely is it that it came from case BB? We need P(B∣A)P(B\mid A), while we are only given P(A∣B)P(A\mid B).

Why is it true? By definition, P(B∣A)=P(A∩B)P(A)P(B\mid A)=\dfrac{P(A\cap B)}{P(A)}. The numerator is rewritten with the multiplication rule, P(A∩B)=P(B)⋅P(A∣B)P(A\cap B)=P(B)\cdot P(A\mid B); the denominator with the law of total probability. On a tree diagram: the numerator is one path, the denominator is the sum of all paths leading to AA.

Choosing a box and then drawing a ball is a two-stage problem of the same kind.

With more than two cases, we use the general form of both formulas.

A test for a rare disease

Now we answer the question from the start with the formulas.

That is why, after a positive screening test, doctors usually order a second test.

Key terms

Summary

  • Total probability: P(A)=P(B)P(A∣B)+P(B‾)P(A∣B‾)P(A)=P(B)P(A\mid B)+P(\overline B)P(A\mid\overline B); in general, add P(Bi)P(A∣Bi)P(B_i)P(A\mid B_i) over a partition.
  • Bayes: P(B∣A)=P(B)P(A∣B)P(A)P(B\mid A)=\dfrac{P(B)P(A\mid B)}{P(A)}.
  • On a tree diagram: multiply along each path; P(A)P(A) is the sum of the paths to AA; Bayes is "one path divided by the sum".
  • For a rare disease, the false positives from the huge healthy group can dominate, so P(disease∣positive)P(\text{disease}\mid\text{positive}) can be small.
  • P(A∣B‾)P(A\mid\overline B) is not 1−P(A∣B)1-P(A\mid B).

Practice

Each exercise comes with what you need and some steps to start. Try it yourself first, then open the solution.

Exercise 1

Given P(B)=0.3P(B)=0.3, P(A∣B)=0.6P(A\mid B)=0.6 and P(A∣B‾)=0.2P(A\mid\overline B)=0.2, find P(A)P(A) and P(B∣A)P(B\mid A).

What you need and how to start

What you need: the law of total probability and Bayes' theorem.

Steps:

  1. P(B‾)=0.7P(\overline B)=0.7.
  2. Find P(A)P(A), then divide 0.3⋅0.60.3\cdot0.6 by P(A)P(A).
Show solution

P(A)=0.3⋅0.6+0.7⋅0.2=0.18+0.14=0.32P(A)=0.3\cdot0.6+0.7\cdot0.2=0.18+0.14=0.32. P(B∣A)=0.180.32=916=0.5625P(B\mid A)=\dfrac{0.18}{0.32}=\dfrac9{16}=0.5625.

Answer. P(A)=0.32P(A)=0.32; P(B∣A)=0.5625P(B\mid A)=0.5625.

Exercise 2

A school has two campuses: 70%70\% of its students are at campus 1 and 30%30\% at campus 2. The proportions who cycle to school at the two campuses are 20%20\% and 50%50\%. A student of the school is chosen at random. Find the probability that the student cycles. If the student cycles, what is the probability that the student is at campus 2?

What you need and how to start

What you need: the two campuses are two complementary cases.

Steps:

  1. Add the two paths that lead to "cycles".
  2. Divide the path through campus 2 by the total.
Show solution

P(cycles)=0.7⋅0.2+0.3⋅0.5=0.14+0.15=0.29P(\text{cycles})=0.7\cdot0.2+0.3\cdot0.5=0.14+0.15=0.29. P(campus 2∣cycles)=0.150.29=1529≈0.517P(\text{campus 2}\mid\text{cycles})=\dfrac{0.15}{0.29}=\dfrac{15}{29}\approx0.517.

Answer. 0.290.29 and 1529≈0.517\dfrac{15}{29}\approx0.517.

Exercise 3

Box I holds 44 red and 66 blue balls; box II holds 77 red and 33 blue balls. A die is rolled: a 11 or a 22 chooses box I, anything else chooses box II; then one ball is drawn at random from the chosen box. Find the probability of a red ball and the probability that a red ball came from box I.

What you need and how to start

What you need: P(box I)=26=13P(\text{box I})=\dfrac26=\dfrac13.

Steps:

  1. P(red)=13⋅410+23⋅710P(\text{red})=\dfrac13\cdot\dfrac4{10}+\dfrac23\cdot\dfrac7{10}.
  2. Use Bayes' theorem.
Show solution

P(red)=430+1430=1830=35P(\text{red})=\dfrac{4}{30}+\dfrac{14}{30}=\dfrac{18}{30}=\dfrac35. P(box I∣red)=4/3018/30=29P(\text{box I}\mid\text{red})=\dfrac{4/30}{18/30}=\dfrac29.

Answer. 35\dfrac35 and 29\dfrac29.

Exercise 4

The first box holds 33 white and 22 black balls; the second box holds 22 white and 22 black balls. One ball is moved at random from the first box to the second, and then one ball is drawn at random from the second box. Find the probability that this last ball is white.

What you need and how to start

What you need: two cases: the ball moved is white or black; after the move, the second box has 55 balls.

Steps:

  1. Moving a white ball (probability 35\dfrac35): the second box has 33 white out of 55.
  2. Moving a black ball (probability 25\dfrac25): the second box has 22 white out of 55.
Show solution

P(white)=35⋅35+25⋅25=925+425=1325P(\text{white})=\dfrac35\cdot\dfrac35+\dfrac25\cdot\dfrac25=\dfrac9{25}+\dfrac4{25}=\dfrac{13}{25}.

Answer. 1325\dfrac{13}{25}.

Exercise 5

The probability of rain on a given morning is 0.250.25. If it rains, the bus is late with probability 0.60.6; if it does not rain, the bus is late with probability 0.20.2. Is each claim true or false?

a) The probability that the bus is late is 0.30.3.

b) The probability that the bus is late when it does not rain is 0.80.8.

c) Given that the bus is late, the probability of rain is 0.50.5.

d) Given that the bus is not late, the probability of rain is less than 0.250.25.

What you need and how to start

What you need: the law of total probability and Bayes' theorem; P(not late∣rain)=1−0.6P(\text{not late}\mid\text{rain})=1-0.6.

Steps:

  1. Find P(late)P(\text{late}).
  2. Apply Bayes' theorem to "late" and to "not late".
Show solution

a) True: 0.25⋅0.6+0.75⋅0.2=0.15+0.15=0.30.25\cdot0.6+0.75\cdot0.2=0.15+0.15=0.3.

b) False: that probability is given as 0.20.2; 0.80.8 is the probability of not being late when it does not rain.

c) True: 0.150.3=0.5\dfrac{0.15}{0.3}=0.5.

d) True: P(rain∣not late)=0.25⋅0.40.7=17≈0.143<0.25P(\text{rain}\mid\text{not late})=\dfrac{0.25\cdot0.4}{0.7}=\dfrac17\approx0.143<0.25.

Answer. a) True; b) False; c) True; d) True.

Exercise 6

Three factories supply parts to a phone maker in the proportions 40%40\%, 35%35\%, 25%25\%, with defect rates 2%2\%, 3%3\%, 4%4\%. A part chosen at random is defective. Find the probability that it came from the first factory. Give your answer to 2 decimal places.

What you need and how to start

What you need: the general form of the law of total probability and of Bayes' theorem with three cases.

Steps:

  1. Find P(defective)P(\text{defective}) as a sum of three products.
  2. Divide 0.4⋅0.020.4\cdot0.02 by that sum.
Show solution

P(defective)=0.008+0.0105+0.01=0.0285P(\text{defective})=0.008+0.0105+0.01=0.0285. The probability is 0.0080.0285=1657≈0.2807\dfrac{0.008}{0.0285}=\dfrac{16}{57}\approx0.2807.

Answer. 0.280.28.

Exercise 7

In a company's inbox, 30%30\% of emails are spam. The phrase "you have won" appears in 40%40\% of spam emails but in only 2%2\% of ordinary emails. A new email contains "you have won". Find the probability that it is spam, as a percentage to 1 decimal place.

What you need and how to start

What you need: Bayes' theorem; this is how a simple spam filter works.

Steps:

  1. Find P(phrase)=0.3⋅0.4+0.7⋅0.02P(\text{phrase})=0.3\cdot0.4+0.7\cdot0.02.
  2. Divide 0.3⋅0.40.3\cdot0.4 by that result.
Show solution

P(phrase)=0.12+0.014=0.134P(\text{phrase})=0.12+0.014=0.134. P(spam∣phrase)=0.120.134=6067≈0.896P(\text{spam}\mid\text{phrase})=\dfrac{0.12}{0.134}=\dfrac{60}{67}\approx0.896.

Answer. About 89.6%89.6\%.

Exercise 8 Review

Machine A makes 300300 items, 99 of them defective; machine B makes 200200 items, 1010 of them defective. All the items are mixed and one is picked at random. Find the probability that it is defective in two ways: by direct counting and with the law of total probability. If the item is defective, what is the probability that machine B made it?

What you need and how to start

What you need: conditional probability by counting (see conditional probability).

Steps:

  1. Counting: 9+10500\dfrac{9+10}{500}.
  2. Formula: P(A)=300500=0.6P(A)=\dfrac{300}{500}=0.6, P(defective∣A)=9300=0.03P(\text{defective}\mid A)=\dfrac{9}{300}=0.03, and similarly for machine B.
Show solution

Counting: 19500=0.038\dfrac{19}{500}=0.038. Formula: 0.6⋅0.03+0.4⋅0.05=0.018+0.02=0.0380.6\cdot0.03+0.4\cdot0.05=0.018+0.02=0.038, the same. P(machine B∣defective)=1019P(\text{machine B}\mid\text{defective})=\dfrac{10}{19} (counting among the 1919 defective items).

Answer. 0.0380.038; 1019\dfrac{10}{19}.

Exercise 9 Review

Box I holds 44 red and 22 white balls; box II holds 22 red and 44 white balls. One box is chosen at random (each with probability 12\dfrac12), and then 22 balls are drawn at random together from that box. Find the probability of getting 22 red balls. If 22 red balls are drawn, what is the probability that box I was chosen?

What you need and how to start

What you need: counting with combinations (see permutations, arrangements and combinations): each box gives (62)=15\binom{6}{2}=15 ways to draw 22 balls.

Steps:

  1. P(2 red∣box I)=(42)15P(\text{2 red}\mid\text{box I})=\dfrac{\binom{4}{2}}{15} and P(2 red∣box II)=(22)15P(\text{2 red}\mid\text{box II})=\dfrac{\binom{2}{2}}{15}.
  2. Use the law of total probability, then Bayes' theorem.
Show solution

P(2 red)=12⋅615+12⋅115=730P(\text{2 red})=\dfrac12\cdot\dfrac6{15}+\dfrac12\cdot\dfrac1{15}=\dfrac7{30}. P(box I∣2 red)=6/307/30=67P(\text{box I}\mid\text{2 red})=\dfrac{6/30}{7/30}=\dfrac67.

Answer. 730\dfrac7{30} and 67\dfrac67.

Exercise 10 Challenge

A disease affects 2%2\% of the population. A test is positive for 90%90\% of people with the disease and gives a false positive for 4%4\% of people without it. a) A person tests positive; find the probability that the person has the disease. b) The person retakes the test and is positive again (the two results are independent for each person); find the probability of the disease again.

What you need and how to start

What you need: Bayes' theorem; with two independent tests, the probability of two positives is 0.920.9^2 with the disease and 0.0420.04^2 without it.

Steps:

  1. a) Numerator 0.02⋅0.90.02\cdot0.9; the denominator adds 0.98⋅0.040.98\cdot0.04.
  2. b) Replace 0.90.9 by 0.810.81 and 0.040.04 by 0.00160.0016.
Show solution

a) 0.0180.018+0.0392=0.0180.0572≈0.315\dfrac{0.018}{0.018+0.0392}=\dfrac{0.018}{0.0572}\approx0.315.

b) 0.02⋅0.810.02⋅0.81+0.98⋅0.0016=0.01620.017768≈0.912\dfrac{0.02\cdot0.81}{0.02\cdot0.81+0.98\cdot0.0016}=\dfrac{0.0162}{0.017768}\approx0.912.

Answer. a) ≈0.315\approx0.315; b) ≈0.912\approx0.912.

Mini test

Mini test

Marked like the Vietnamese graduation exam: Part I 0.25 points per question; Part II up to 1 point per question (1, 2, 3, 4 correct statements give 0.1, 0.25, 0.5, 1 point); Part III 0.5 points per question.

Part I. Multiple choice

Question 1. Given P(B)=0.4P(B)=0.4, P(A∣B)=0.5P(A\mid B)=0.5 and P(A∣B‾)=0.25P(A\mid\overline B)=0.25, the value of P(A)P(A) is
Question 2. Machine I makes 70%70\% of the items with a defect rate of 1%1\%; machine II makes 30%30\% with a defect rate of 4%4\%. An item is defective; the probability that machine II made it is closest to
Question 3. Box I holds 22 red and 33 white balls; box II holds 44 red and 11 white ball. A box is chosen at random and one ball is drawn. The probability of a red ball is
Question 4. A disease affects 0.5%0.5\% of the population; a test is positive for 99%99\% of people with it and gives a false positive for 2%2\% of people without it. A person tests positive; the probability that the person has the disease is closest to

Part II. True or false

Question 1. Workshop I makes 60%60\% of the items with a defect rate of 3%3\%; workshop II makes 40%40\% with a defect rate of 6%6\%. An item is chosen at random.
a) The probability that the item is defective is 0.0420.042.
b) The probability that the item is defective, given that it is from workshop II, is 0.0240.024.
c) Given that the item is defective, the probability that it is from workshop II is 0.40.4.
d) Given that the item is not defective, the probability that it is from workshop I is greater than 0.60.6.

Part III. Short answer

Question 1. 40%40\% of phone users use iOS and 60%60\% use Android. The proportions using app X in the two groups are 30%30\% and 25%25\%. A phone user is chosen at random; find the probability that the user uses app X.

Question 2. Box I holds 33 red and 77 blue balls; box II holds 66 red and 44 blue balls. A die is rolled: a 66 chooses box I, anything else chooses box II; then one ball is drawn and it is red. Find the probability that it came from box I. Give your answer to 2 decimal places.

Show answers and explanations
  1. Part I, question 1: D. P(A)=0.4⋅0.5+0.6⋅0.25=0.2+0.15=0.35P(A)=0.4\cdot0.5+0.6\cdot0.25=0.2+0.15=0.35. A misses the B‾\overline B branch, B adds the two conditional probabilities, and C wrongly uses P(B‾)=0.4P(\overline B)=0.4.
  2. Part I, question 2: C. P(defective)=0.007+0.012=0.019P(\text{defective})=0.007+0.012=0.019, so P(II∣defective)=0.0120.019=1219≈0.63P(\text{II}\mid\text{defective})=\dfrac{0.012}{0.019}=\dfrac{12}{19}\approx0.63. A is P(defective∣II)P(\text{defective}\mid\text{II}), B is the starting probability, and D is the numerator before dividing.
  3. Part I, question 3: B. 12⋅25+12⋅45=35\dfrac12\cdot\dfrac25+\dfrac12\cdot\dfrac45=\dfrac35. A multiplies the two conditional probabilities, C uses only the box I branch, and D forgets to multiply by 12\dfrac12 (a probability cannot exceed 11).
  4. Part I, question 4: C. 0.005⋅0.990.005⋅0.99+0.995⋅0.02=0.004950.02485≈0.199\dfrac{0.005\cdot0.99}{0.005\cdot0.99+0.995\cdot0.02}=\dfrac{0.00495}{0.02485}\approx0.199. A confuses P(positive∣disease)P(\text{positive}\mid\text{disease}) with P(disease∣positive)P(\text{disease}\mid\text{positive}), B is 1−0.021-0.02, and D is the starting probability.
  5. Part II, question 1: a) True; b) False; c) False; d) True. a) True: 0.018+0.0240.018+0.024. b) False: that probability is given as 0.060.06; 0.0240.024 is the probability of "from II and defective". c) False: 0.0240.042=47≈0.571\dfrac{0.024}{0.042}=\dfrac47\approx0.571. d) True: 0.6⋅0.970.958≈0.608\dfrac{0.6\cdot0.97}{0.958}\approx0.608.
  6. Part III, question 1: 0.27. 0.4⋅0.3+0.6⋅0.25=0.12+0.15=0.270.4\cdot0.3+0.6\cdot0.25=0.12+0.15=0.27.
  7. Part III, question 2: 0.09. P(red)=16⋅0.3+56⋅0.6=0.05+0.5=0.55P(\text{red})=\dfrac16\cdot0.3+\dfrac56\cdot0.6=0.05+0.5=0.55, so P(I∣red)=0.050.55=111≈0.09P(\text{I}\mid\text{red})=\dfrac{0.05}{0.55}=\dfrac1{11}\approx0.09.