Bayes´ theorem explained with Examples

This article will be about The Bayes theorem, how to use the Bayes´ theorem and when we have to apply this theorem, with formula and examples.

¿What is the Bayes´ theorem?

The Bayes´ theorem is a method used in probability which is useful to find a conditional probability, this means that we are going to calculate an outcome when another outcome already happened.

The Bayes´ theorem is used when there are multiple outcomes that are related, a useful tool that helps us to understand the relation between outcomes are the tree diagrams, in case you do not know how to make a tree diagram, go to the example section, there will be some examples where the outcomes are represented in a tree diagram.

Bayes´ theorem

Basic Bayes´ theorem example

To understand better in which situations we are going to make use of the Bayes´ theorem we are going to develop the following example: A survey was made to a big group of people where they were asking the gender and if they eventually exercises, the results of the survey were the following: the 40% of the respondents were men and the other 60% were women, and 80% out of the men and 50% out of the women said that they use to exercise eventually.

Taking as reference the previous example, the questions that normally would done would be: ¿What is the probability that when any person is chosen this is a man and this man exercises eventually? In cases like this we would have to find the intersection between the 2 outcomes p(M n E) being M the probability that this person is a man and E the probability that this man exercises, and this would be calculated just by multiplying the 2 probabilities: p(M n E) = 0.4*0.8=0.32.

But the Bayes´ theorem is used when we have the opposite question, like this: ¿What is the probability that when a person that Exercises is chosen this is also a man?, what we are looking for with this types of problems is the opposite of what we already have, because what we have is the percentage of men a women that exercises, but what we want is know the opposite perspective, that is the probability that somebody that exercises is either a man or a woman, and is in this types of cases where we are going to make use of the Bayes´ theorem.

bayes´ theorem diagram

Seeing this from a diagram we can appreciate that the normal probability are calculated from right to left, like the probability of D when A had happened (p(D|A) ), but with the Bayes´ theorem we are going to find the probabilities from left to right, like the probability of A when D already happened (p(A|D) ).

Formula of the Bayes´ theorem

  • Formula
  • p(a | b) =
    p( b | a) * p(a)/p (b)

Examples of the Bayes´ theorem

Example 1: As the first example we are going to solve the previous one: 40% of the respondents were men and 60% were women, and 80% of the men and 50% of the women answered that they usually exercises. Knowing the results of the survey, find the probability that when we select a random person that usually exercises this is a man.

p(s) = person that practice sport p(m) = a man

First we are going to draw the tree diagram for this example to have a clearer look of our data

bayes theorem example 1

As we can see in the diagram p(s | m) means the probability that a man practice a sport, and this is a data that the problem already gave to us (80%), so we are only going to replace it in the formula, and also the problem gives us the data of p(h) that is the men percentage and is 40%.

Something really important is to calculate p(s), this is the probability that somebody not matter its gender practice a sport, and this is a data that we do not have, so we are going to calculate it, to calculate it we have to apply the multiplication rule in each branch of the diagram tree, it means that we have to multiply the percentage of men by the percentage of men that practice a sport and we have to multiply the percentage of women by the percentage of women that practice a sport and then we have to sum both results and this will be p(s).

  • p(s) = (0.4* 0.8) + (0.6 * 0.5)
  • p(s) = 0.32 + 0.3
  • p(d) = 0.62
  • Now with all the necessary data we use the Bayes´ theorem
  • m = man, s = sport
  • p(m | s) =
    p( s | m) * p(m)/p (s)
  • p(m | s) =
    0.8 * 0.4/0.62
  • p(m | s) =
    0.32/0.62
  • p(m | s) = 0.5161 * 100%
  • p(m | s) = 51.61%

Example 2: In a factory they produce cans of two sizes, of 25ml and 40ml, if we know that they make the same amount of cans for both presentations and that 1% of the 25ml cans and 4% of the 40ml cans are defective ¿What is the probability that when we choose a defective can this is a 40ml can?

bayes theorem example 2

p(40ml) = 40 ml can p(d) = defective can

  • We find that a defective can is a 40ml can
  • p(40ml | d) =
    p( d | 40ml) * p(40ml)/p (d)
  • p(40ml | d) =
    0.04 * 0.5/0.01*0.5 + 0.04*0.5
  • p(40ml | d) =
    0.02/0.005 + 0.02
  • p(40ml | d) =
    0.02/0.025
  • p(40ml | d) = 0.80 * 100%
  • p(40ml | d) = 80%

Example 3: In the elections of a country there are two candidates for president, the candidate A and the candidate B, and on the results of the elections we know that 75% of the population is middle class and a 25% of the population is high class, if 90% of the high class and 5% of the middle class voted for the candidate A, and we choose somebody that voted for the candidate A, calculate the probability that this person is middle class.

bayes theorem example 3

p(cA) = voted for a candidate p(m) = middle class person

  • Se encuentra la probabilidad alguien que votó por el candidato A sea de clase media o baja
  • p(m | cA) =
    p( cA | m) * p(m)/p (cA)
  • p(m | cA) =
    0.05 * 0.75 /0.05*0.75 + 0.25*0.9
  • p(mb | cA) =
    0.0375/0.0375 + 0.225
  • p(mb | cA) =
    0.0375/0.2625
  • p(m | cA) = 0.1429 * 100%
  • p( m | cA) = 14.29%