Applying Bayes' Theorem in Deduction

Examples

Recreational mathematics

Bayes' rule and computing conditional probabilities provide a solution method for a number of popular puzzles, such as the Three Prisoners problem, the Monty Hall problem, the Two Child problem and the Two Envelopes problem.


Drug testing

Figure 1: Using a frequency box to show P(User|Positive) visually by comparison of shaded areas

Figure 1: Using a frequency box to show P({\text{User}}\vert {\text{Positive}}) visually by comparison of shaded areas

Suppose, a particular test for whether someone has been using cannabis is 90% sensitive, meaning the true positive rate (TPR) = 0.90. Therefore, it leads to 90% true positive results (correct identification of drug use) for cannabis users.

The test is also 80% specific, meaning true negative rate (TNR) = 0.80. Therefore, the test correctly identifies 80% of non-use for non-users, but also generates 20% false positives, or false positive rate (FPR) = 0.20, for non-users.

Assuming 0.05 prevalence, meaning 5% of people use cannabis, what is the probability that a random person who tests positive is really a cannabis user?

The Positive predictive value (PPV) of a test is the proportion of persons who are actually positive out of all those testing positive, and can be calculated from a sample as:

PPV = True positive / Tested positive

If sensitivity, specificity, and prevalence are known, PPV can be calculated using Bayes theorem. Let P({\text{User}}\vert {\text{Positive}}) mean "the probability that someone is a cannabis user given that they test positive," which is what is meant by PPV. We can write:

{\begin{aligned}P({\text{User}}\vert {\text{Positive}})&={\frac {P({\text{Positive}}\vert {\text{User}})P({\text{User}})}{P({\text{Positive}})}}\\&={\frac {P({\text{Positive}}\vert {\text{User}})P({\text{User}})}{P({\text{Positive}}\vert {\text{User}})P({\text{User}})+P({\text{Positive}}\vert {\text{Non-user}})P({\text{Non-user}})}}\\[8pt]&={\frac {0.90\times 0.05}{0.90\times 0.05+0.20\times 0.95}}={\frac {0.045}{0.045+0.19}}\approx 19\%\end{aligned}}

The fact that P({\text{Positive}})=P({\text{Positive}}\vert {\text{User}})P({\text{User}})+P({\text{Positive}}\vert {\text{Non-user}})P({\text{Non-user}}) is a direct application of the Law of Total Probability. In this case, it says that the probability that someone tests positive is the probability that a user tests positive, times the probability of being a user, plus the probability that a non-user tests positive, times the probability of being a non-user. This is true because the classifications user and non-user form a partition of a set, namely the set of people who take the drug test. This combined with the definition of conditional probability results in the above statement.

In other words, even if someone tests positive, the probability that they are a cannabis user is only 19% - this is because in this group, only 5% of people are users, and most positives are false positives coming from the remaining 95%.

If 1,000 people were tested:

  • 950 are non-users and 190 of them give false positive (0.20 × 950)
  • 50 of them are users and 45 of them give true positive (0.90 × 50)

The 1,000 people thus yields 235 positive tests, of which only 45 are genuine drug users, about 19%. See Figure 1 for an illustration using a frequency box, and note how small the pink area of true positives is compared to the blue area of false positives.


Sensitivity or specificity

The importance of specificity can be seen by showing that even if sensitivity is raised to 100% and specificity remains at 80%, the probability of someone testing positive really being a cannabis user only rises from 19% to 21%, but if the sensitivity is held at 90% and the specificity is increased to 95%, the probability rises to 49%.

Test
Actual
Positive Negative Total
User 45 5 50
Non-user 190 760 950
Total 235 765 1000
90% sensitive, 80% specific, PPV=45/235 ≈ 19%

Test
Actual
Positive Negative Total
User 50 0 50
Non-user 190 760 950
Total 240 760 1000
100% sensitive, 80% specific, PPV=50/240 ≈ 21%

Test
Actual
Positive Negative Total
User 45 5 50
Non-user 47 903 950
Total 92 908 1000
90% sensitive, 95% specific, PPV=45/92 ≈ 49%

Cancer rate

Even if 100% of patients with pancreatic cancer have a certain symptom, when someone has the same symptom, it does not mean that this person has a 100% chance of getting pancreatic cancer. Assuming the incidence rate of pancreatic cancer is 1/100000, while 10/99999 healthy individuals have the same symptoms worldwide, the probability of having pancreatic cancer given the symptoms is only 9.1%, and the other 90.9% could be "false positives" (that is, falsely said to have cancer; "positive" is a confusing term when, as here, the test gives bad news).

Based on incidence rate, the following table presents the corresponding numbers per 100,000 people.


Symptom
Cancer
Yes No Total
Yes 1 0 1
No 10 99989 99999
Total 11 99989 100000

Which can then be used to calculate the probability of having cancer when you have the symptoms:

{\begin{aligned}P({\text{Cancer}}|{\text{Symptoms}})&={\frac {P({\text{Symptoms}}|{\text{Cancer}})P({\text{Cancer}})}{P({\text{Symptoms}})}}\\&={\frac {P({\text{Symptoms}}|{\text{Cancer}})P({\text{Cancer}})}{P({\text{Symptoms}}|{\text{Cancer}})P({\text{Cancer}})+P({\text{Symptoms}}|{\text{Non-Cancer}})P({\text{Non-Cancer}})}}\\[8pt]&={\frac {1\times 0.00001}{1\times 0.00001+(10/99999)\times 0.99999}}={\frac {1}{11}}\approx 9.1\%\end{aligned}}


Defective item rate

Condition

Machine
Defective Flawless Total
A 10 190 200
B 9 291 300
C 5 495 500
Total 24 976 1000

A factory produces items using three machines - A, B, and C - which account for 20%, 30%, and 50% of its output respectively. Of the items produced by machine A, 5% are defective; similarly, 3% of machine B's items and 1% of machine C's are defective. If a randomly selected item is defective, what is the probability it was produced by machine C?

Once again, the answer can be reached without using the formula by applying the conditions to a hypothetical number of cases. For example, if the factory produces 1,000 items, 200 will be produced by Machine A, 300 by Machine B, and 500 by Machine C. Machine A will produce 5% × 200 = 10 defective items, Machine B 3% × 300 = 9, and Machine C 1% × 500 = 5, for a total of 24. Thus, the likelihood that a randomly selected defective item was produced by machine C is 5/24 (~20.83%).

This problem can also be solved using Bayes' theorem: Let Xi denote the event that a randomly chosen item was made by the ith machine (for i = A,B,C). Let Y denote the event that a randomly chosen item is defective. Then, we are given the following information:

P(X_{A})=0.2,\quad P(X_{B})=0.3,\quad P(X_{C})=0.5.

If the item was made by the first machine, then the probability that it is defective is 0.05; that is, P(Y | X_A) = 0.05. Overall, we have

P(Y|X_{A})=0.05,\quad P(Y|X_{B})=0.03,\quad P(Y|X_{C})=0.01.

To answer the original question, we first find P(Y). That can be done in the following way:

P(Y)=\sum _{i}P(Y|X_{i})P(X_{i})=(0.05)(0.2)+(0.03)(0.3)+(0.01)(0.5)=0.024.

Hence, 2.4% of the total output is defective.

We are given that Y has occurred, and we want to calculate the conditional probability of X_C. By Bayes' theorem,

P(X_{C}|Y)={\frac {P(Y|X_{C})P(X_{C})}{P(Y)}}={\frac {0.01\cdot 0.50}{0.024}}={\frac {5}{24}}

Given that the item is defective, the probability that it was made by machine C is 5/24. Although machine C produces half of the total output, it produces a much smaller fraction of the defective items. Hence the knowledge that the item selected was defective enables us to replace the prior probability P(X_C) = \frac{1}{2} by the smaller posterior probability P(X_C | Y) = \frac{5}{24}.