Introduction To Conditional Probability and Bayes Theorem For Data Science Professionals
Introduction To Conditional Probability and Bayes Theorem For Data Science Professionals
Introduction To Conditional Probability and Bayes Theorem For Data Science Professionals
In this article, I will focus on conditional probability. For beginners in probability, I would strongly
recommend that you go through this article before proceeding further.
In this article, I will walk you through conditional probability in detail. Ill be using examples &
real-life scenarios to help you improve your understanding.
Table of Contents
1. Events Union, Intersection & Disjoint events
2. Independent, Dependent and Exclusive events
3. Conditional Probability
4. Bayes Theorem
5. Probability trees
6. Frequentist vs Bayesian definitions of probability
7. Open Challenges
1.1 EVENTS
An event is simply the outcome of a random experiment. Getting a heads when we toss a coin is
an event. Getting a 6 when we roll a fair die is an event. We associate probabilities to these events
by defining the event and the sample space.
The sample space is nothing but the collection of all possible outcomes of an experiment. This
means that if we perform a particular task again and again, all the possible results of the task are
listed in the sample space.
1
For example: A sample space for a single throw of a die will be {1,2,3,4,5,6}. One of these is
bound to occur if we throw a die. The sample space exhausts all the possibilities that can happen
when that experiment is performed.
We can define an event (C) of getting a 4 or 6 when we roll a fair die. Here event C is a union of
two events:
Event A = Getting a 4
Event B = Getting a 6
P (C) = P (A B)
In simple words we can say that we should consider the probability of (A B) when we are
interested in combined probability of two (or more) events.
Let C be the event of getting a multiple of 2 and 3 when you throw a fair die.
P (C) = P (A B)
2
We can now say that the shaded region is the probability of both events A and B occurring
together.
What if, you come across a case when any two particular events cannot occur at the same time.
For example: Lets say you have a fair die and you have only one throw.
Event A = {3,6}
Event B = {5}
As you can see, there is no case for which event A & B can occur together. Such events are called
disjoint event. To represent this using a Venn diagram:
3
2. Independent, Dependent & Exclusive Events
Suppose we have two events event A and event B.
If the occurrence of event A doesnt affect the occurrence of event B, these events are called
independent events.
Getting heads after tossing a coin AND getting a 5 on a throw of a fair die.
Choosing a marble from a jar AND getting heads after tossing a coin.
Choosing a 3 card from a deck of cards, replacing it, AND then choosing an ace as the
second card.
Rolling a 4 on a fair die, AND then rolling a 1 on a second roll of the die.
In each of these cases the probability of outcome of the second event is not affected at all by the
outcome of the first event.
Lets take an example here. Suppose we win the game if we pick a red marble from a jar containing
4 red and 3 black marbles and we get heads on the toss of a coin. What is the probability of
winning?
We need to find the probability of both getting a red marble and a heads in a coin toss.
P (A) = 4/7
P (B) = 1/2
We know that there is no effect of the color of the marble on the outcome of the coin toss.
P (A B) = P (A) * P (B)
4
In the above example, lets define event A as getting a Red marble from the jar. We then keep the
marble out and then take another marble from the jar.
Will the probabilities in the second case still be the same as that in the first case?
Lets see. So, for the first time there are 4/7 chances of getting a red marble. Lets assume you
got a red marble on the first attempt. Now, for second chance, to get a red marble we have 3/6
chances.
If we didnt get a red marble on the first attempt but a white marble instead. Then, there were 4/6
chances to get the red marble second time. Therefore the probability in the second case was
dependent on what happened the first time.
Quiz 1: If you have a Jack and your next card is dealt with a new deck of cards the probability of
you obtaining a jack again is? Are these events dependent or independent?
Mutually exclusive events are those events where two events cannot happen together.
The easiest example to understand this is the toss of a coin. Getting a head and a tail are mutually
exclusive because we can either get heads or tails but never both at the same in a single coin toss.
A set of events is collectively exhaustive when the set should contain all the possible outcomes of
the experiment. One of the events from the list must occur for sure when the experiment is
performed.
Consider the outcomes even (2,4 or 6) and not-6 (1,2,3,4, or 5) in a throw of a fair die. They
are collectively exhaustive but not mutually exclusive.
3. Conditional Probability
Conditional probabilities arise naturally in the investigation of experiments where an outcome of
a trial may affect the outcomes of the subsequent trials.
5
We try to calculate the probability of the second event (event B) given that the first event (event
A) has already happened. If the probability of the event changes when we take the first event into
consideration, we can safely say that the probability of event B is dependent of the occurrence of
event A.
Drawing a second ace from a deck given we got the first ace
Finding the probability of having a disease given you were tested positive
Finding the probability of liking Harry Potter given we know the person likes fiction
And so on.
We can write the conditional probability as , the probability of the occurrence of event
A given that B has already happened.
Lets play a simple game of cards for you to understand this. Suppose you draw two cards from a
deck and you win if you get a jack followed by an ace (without replacement). What is the
probability of winning, given we know that you got a jack in the first turn?
We need to find
P(A) = 4/52
6
Here we are determining the probabilities when we know some conditions instead of calculating
random probabilities. Here we knew that he got a jack in the first turn.
Suppose you have a jar containing 6 marbles 3 black and 3 white. What is the probability of
getting a black given the first one was black too.
P (A) = 3/6
P (B) = 2/5
Example: Rahuls favorite breakfast is bagels and his favorite lunch is pizza. The probability of
Rahul having bagels for breakfast is 0.6. The probability of him having pizza for lunch is 0.5. The
probability of him, having a bagel for breakfast given that he eats a pizza for lunch is 0.7.
Lets define event A as Rahul having a bagel for breakfast, Event B as Rahul having a pizza for
lunch.
P (A) = 0.6
P (B) = 0.5
7
If we look at the numbers, the probability of having a bagel is different than the probability of
having a bagel given he has a pizza for lunch. This means that the probability of having a bagel is
dependent on having a pizza for lunch.
Now what if we need to know the probability of having a pizza given you had a bagel for breakfast.
i.e. we need to know . Bayes theorem now comes into the picture.
4. Bayes Theorem
The Bayes theorem describes the probability of an event based on the prior knowledge of the
conditions that might be related to the event. If we know the conditional probability ,
we can use the bayes rule to find out the reverse probabilities .
For the previous example if we now wish to calculate the probability of having a pizza for
lunch provided you had a bagel for breakfast would be = 0.7 * 0.5/0.6.
8
If multiple events Ai form an exhaustive set with another event B.
Lets first compute the probability of having cancer given that the patient tested positive in the
first test.
P (cancer) = 0.00148
Since we do not have any other information, we believe that the patient is a randomly sampled
individual. Hence our prior belief is that there is a 0.148% probability of the patient having cancer.
The complement is that there is a 100 0.148% chance that the patient does not have CANCER.
Similarly we can draw the below tree to denote the probabilities.
9
Lets not try to calculate the probability of having cancer given that he tested positive on the first
test i.e. P (cancer|+)
To calculate the probability of testing positive, the person can have cancer and test positive or he
may not have cancer and still test positive.
This means that there is a 12% chance that the patient has cancer given he tested positive in the
first test. This is known as the posterior probability.
Lets now try to calculate the probability of having cancer given the patient tested positive in the
second test as well.
10
Now remember we will only do the second test if she tested positive in the first one. Therefore
now the person is no longer a randomly sampled person but a specific case. We know something
about her. Hence, the prior probabilities should change. We update the prior probability with the
posterior from the previous test.
Nothing would change in the sensitivity and specificity of the test since were doing the same test
again. Look at the probability tree below.
Lets calculate again the probability of having cancer given she tested positive in the second test.
To calculate the probability of testing positive, the person can have cancer and test positive or she
may not have cancer and still test positive.
Now we see, that a patient who tested positive in the test twice, has a 93% chance of having
cancer.
11
6. Frequentist vs Bayesian Definitions of probability
A frequentist defines probability as an expected frequency of occurrence over large number of
experiments.
The Bayesian view of probability is related to degree of belief. It is a measure of the plausibility
of an event given incomplete knowledge.
The frequentist believes that the population mean is real but unknowable and can only be estimated
from the data. He knows the distribution of the sample mean and constructs a confidence interval
centered at the sample mean. So the actual population mean is either in the confidence interval or
not in it.
This is because he believes that the true mean is a single fixed value and does not have a
distribution. So the frequentist says that 95% of similar intervals would contain the true mean, if
each interval were constructed from a different random sample.
The Bayesian definition has a totally different view point. They use their beliefs to construct
probabilities. They believe that certain values are more believable than others based on the data
and our prior knowledge.
The Bayesian constructs a credible interval centered near the sample mean and totally affected by
the prior beliefs about the mean. The Bayesian can therefore make statements about the population
mean by using the probabilities.
7. Open Challenges
In the cancer example taken above, try calculating the probability of a patient having cancer
provided the patient is tested positive in the third test as well.
In an exam, there is a problem that 60% of students know the correct answer. However,
there is 15% chance that a student picked the wrong answer even if he/she knows the right
answer And there is also a 25% chance that a student does not know the right answer but
guessed it correctly. If a student did get the problem right, what is the chance that this
student really knows the answer?
12