You are on page 1of 12

# Introduction to Conditional Probability and

## Bayes theorem for data science professionals

Introduction
Understanding of probability is must for a data science professional. Solutions to many data
science problems are often probabilistic in nature. Hence, a better understanding of probability

In this article, I will focus on conditional probability. For beginners in probability, I would strongly

## A predictive model can easily be understood as a statement of conditional probability. For

example, the probability of a customer from segment A buying a product of category Z in next 10
days is 0.80. In other words, the probability of a customer buying product from Category Z, given
that the customer is from Segment A is 0.80.

In this article, I will walk you through conditional probability in detail. Ill be using examples &

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. Events Union, Intersection & Disjoint events

Before we explore conditional probability, let us define some basic common terminologies:

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.

## 1.2 Union of Events

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.

## Lets look at another example.

Let C be the event of getting a multiple of 2 and 3 when you throw a fair die.

## Probabilities are then defined as follows.

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.

## 1.4 Disjoint Events

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}

## Sample Space= {1,2,3,4,5,6}

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:

## Now that we are familiar with the terms Union,

intersection and disjoint events, we can talk about independence of events.

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.

## Lets see some examples of 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.

## In this case the probability of P (A B) = P (A) * P (B)

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?

## Event B is getting heads on the toss of a coin.

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)

## Next, can you think of examples of dependent events ?

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 and Exhaustive events

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.

## For example, in a throw of a die, {1,2,3,4,5,6} is an exhaustive collection because, it encompasses

the entire range of the possible outcomes.

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.

## 1. Drawing a red card or a jack from a given 52 cards deck.

2. Getting three heads or three tails when three coins are flipped.

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.

## Lets think of cases where this happens:

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.

## Event A is the probability of the event were trying to calculate.

Event B is the condition that we know or the event that has happened.

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

## P(A and B) = 4/52*4/51= 0.006

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.

## Lets take another example.

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

## 3.1 Reversing the condition

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 .

## The above statement is the general representation of the Bayes rule.

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.

## We can generalize the formula further.

8
If multiple events Ai form an exhaustive set with another event B.

## 5. Example of Bayes Theorem and Probability trees

Lets take the example of the breast cancer patients. The patients were tested thrice before the
oncologist concluded that they had cancer. The general belief is that 1.48 out of a 1000 people
have breast cancer in the US at that particular time when this test was conducted. The patients
were tested over multiple tests. Three sets of test were done and the patient was only diagnosed
with cancer if she tested positive in all three of them.

## Specificity of the test (99%) true negative Rate

Lets first compute the probability of having cancer given that the patient tested positive in the
first test.

## P (has cancer | first test +)

P (cancer) = 0.00148

## Specificity can be denoted as P (- | no cancer)

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|+)

## P (no cancer and +) = P (no cancer) * P(+) = 0.99852*0.01

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.

## 5.1 Bayes Updating

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.

## P (no cancer and +) = P (no cancer) * P (+) = 0.88 * 0.01

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.

## P(event) = n/N, where n is the number of times event A occurs in N opportunities.

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