Bayes' theorem · Mathematics Class 11–12 | StemPy Lab
Course 16.2·Lesson 2 of 3
Bayes' theorem
Bayes' theorem turns a probability around. You know how likely a result is under each possible cause. The theorem tells you how likely each cause is once you have seen the result. This lesson gives the formula, the words prior, likelihood and posterior, and the method of natural frequencies, with factories, urns and a medical test.
about 30 min1 simulation1 practice question1 Python program5 questions to check yourself
This lesson is free to read. Enroll free to keep your place and take the tests.
Lesson 2 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
reverse a conditional probability with Bayes' theorem;
name the prior, the likelihood and the posterior;
check a Bayes answer with natural frequencies.
Which machine made the bolt?
In the last lesson we found that 2.9% of all bolts are faulty. Now a faulty bolt is lying on the table. Which machine made it?
A first guess is M3, because it has the highest fault rate. But M3 makes only a fifth of the bolts. A second guess is M1, because it makes the most bolts. Both guesses use only half of the information. Bayes' theorem uses both halves.
The theorem
Let E1,…,En be a partition and let A be an event with P(A)>0. Start from the definition of conditional probability and use the multiplication rule on the top. Then use the law of total probability on the bottom:
P(Ei∣A)=∑jP(Ej)P(A∣Ej)P(Ei)P(A∣Ei).
Three names are worth knowing.
The priorP(Ei) is the chance of the cause before you see the result.
The likelihoodP(A∣Ei) is the chance of the result if that cause is true.
The posteriorP(Ei∣A) is the chance of the cause after you see the result.
Indian books call the prior and posterior probabilities "a priori" and "a posteriori". A-Level, IB and AP use prior and posterior. The bottom of the fraction is the total probability of A, which is the sum of all the paths that end in A. The top is one of those paths.
A tree diagram that starts with 10 000 people. One branch goes to 100 ill people with probability 0.01 and one to 9900 healthy people with probability 0.99. From the ill people, 0.95 test positive and 0.05 test negative. From the healthy people, 0.1 test positive and 0.9 test negative. The ends show the counts: ill and positive 95, ill and negative 5, healthy and positive 990, healthy and negative 8910.
The machines again
The three paths ending in "faulty" are 0.01, 0.009 and 0.01, and they add up to 0.029. The posterior of each machine is its path divided by 0.029:
The three posteriors add up to 1, as they must, because the faulty bolt came from exactly one machine. Notice that M1 and M3 are equally likely. M1 makes more bolts, and M3 makes worse bolts, and the two effects cancel.
Natural frequencies
Many people find Bayes' theorem easier with counts. Imagine 10000 bolts. Then M1 made 5000 of them and 100 of those are faulty. M2 made 3000 and 90 are faulty. M3 made 2000 and 100 are faulty. In all, 290 bolts are faulty. Of these 290, exactly 100 came from M1, so the chance is 290100=2910. Same answer, no formula.
A screening test
In a town, 1 person in 100 has a certain illness. A test finds 95% of the people who have the illness. It wrongly flags 10% of the healthy people. Take 10000 people.
Ill people.100 are ill. The test flags 95 of them.
Healthy people.9900 are healthy. The test flags 990 of them.
All positive tests.95+990=1085.
The posterior. Of the 1085 people with a positive test, only 95 are ill. So P(ill∣+)=108595=21719≈0.0876.
A positive result from a good test leaves less than a 9% chance of being ill. The reason is the size of the groups. There are so many more healthy people that even a small error rate produces more false alarms than true ones.
Practise with new numbers
Try it in Python
The program has one function that does Bayes' theorem for any partition. It is used twice: once for the three machines and once for the screening test.
Common mistakes
Turning the condition around.P(positive∣ill) is not P(ill∣positive). They are linked by Bayes' theorem.
Forgetting the prior. A rare cause stays unlikely even when a test points to it.
A wrong denominator. The bottom is the sum of all the paths that end in A, not just the one path on top.
Mixing percentages and fractions. Change everything to decimals or to counts before you start.
Remember these
P(Ei∣A)=∑jP(Ej)P(A∣Ej)P(Ei)P(A∣Ei).
Prior, likelihood and posterior are the three names to know.
The posteriors of a partition add up to 1.
With natural frequencies, count the people on the paths that end in A, and then divide.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
A fair coin decides which box to use. Box 1 holds 4 red and 6 blue balls. Box 2 holds 7 red and 3 blue balls. A red ball is drawn. Find the probability that it came from box 1.
Factory A makes 60% of the screws and factory B makes the rest. A fault rate of 1% holds for A and 4% for B. A screw is found faulty. Find the probability that it came from B.
One person in 500 has a disease. A test finds 98% of the ill and wrongly flags 4% of the healthy. Out of 100000 people, how many test positive, and what is the chance that a person with a positive test is ill?
A test result is equally likely whether or not a person is ill. After a positive result, what is the posterior probability of being ill?
Three urns are chosen with equal probability. They hold red balls in the shares 51, 52 and 53. A red ball is drawn. Find the probability that it came from the third urn.
References
OpenStax, Introductory Statistics, Chapter 3, section on contingency tables and tree diagrams, where reversed conditional probabilities are found.
NCERT, Mathematics Part II for Class XII, Chapter 13, section on Bayes' theorem.
Enroll free in Total Probability and Bayes' Theorem
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.