**Prob and Stats**

# Comprehensive Guide on Bayes' Theorem

*schedule*Aug 10, 2023

*toc*Table of Contents

*expand_more*

**mathematics behind data science**with 100+ top-tier guides

Start your free 7-days trial now!

# Bayes' Theorem

If $A$ and $B$ are events and $\mathbb{P}(A)\ne0$, then:

Where:

$\mathbb{P}(B|A)$, which is the probability of $B$ given $A$, is called the posterior probability.

$\mathbb{P}(A|B)$ is called the likelihood.

$\mathbb{P}(B)$ is called the prior probability.

$\mathbb{P}(A)$ is called the marginal probability.

Proof. Recall the definition of conditional probability:

Therefore, $\mathbb{P}(A,B)$ can be written as:

Equating the two equations and making $\mathbb{P}(B|A)$ the subject gives us the Bayes' theorem:

This completes the proof.

# Bayes' Theorem using law of total probability

If we have events $A$, $B_1$ and $B_2$ where $B_1$ and $B_2$ partitions the sample space, then:

Where $j$ can be either $1$ or $2$.

Proof. Suppose we have events $A$, $B_1$ and $B_2$. Bayes' theorem tells us that:

Now, suppose $B_1$ and $B_2$ form a partition of the sample space. From the law of total probabilitylink, we know that:

Substituting this into \eqref{eq:jtfupsOLvfmYwQhazH1} gives:

This completes the proof.

## Drawing a ball from two bags

Suppose we have the following two bags:

bag $\text{I}$, which contains $1$ red and $3$ green balls

bag $\text{II}$, which contains $2$ red and $1$ green balls.

We pick one bag at random and then draw a single ball. Given that the drawn ball is red, compute the probability that it came from bag $\mathrm{II}$.

Solution. We define the following events:

event $\mathrm{I}$ - bag $\mathrm{I}$ is chosen.

event $\mathrm{II}$ - bag $\mathrm{II}$ is chosen.

event $\color{red}\text{red}$ - a red ball is drawn.

The probability we are after is therefore $\mathbb{P}(\mathrm{II}|{\color{red}\mathrm{red}})$. Using Bayes' theorem, we have that:

We are not provided with $\mathbb{P}({\color{red}\mathrm{red}})$, so we must use the second form of the Bayes' theorem in which we express the denominator $\mathbb{P}({\color{red}\mathrm{red}})$ using the law of total probability:

Let's start with the denominator:

Next, the numerator is:

Substituting the denominator and numerator into the Bayes' theorem gives:

Therefore, the probability that the red ball comes from bag $\mathrm{II}$ is around $0.73$.

# Bayes rule as an update rule based on evidence

Let's now revisit the Bayes' theorem from the above example:

Note the following terminologies:

$\mathbb{P}(\mathrm{II}|{\color{red}\text{red}})$ is the posterior probability.

$\mathbb{P}({\color{red}\text{red}}|\mathrm{II})$ is the likelihood.

$\mathbb{P}(\mathrm{II})$ is the prior probability.

$\mathbb{P}({\color{red}\text{red}})$ is the marginal probability.

Suppose we are not given the information (or evidence) that the ball is red. In this case, the probability that the ball is from the second bag is $\mathbb{P}(\mathrm{II})$. This prior probability can be thought of as the probability before the evidence is considered.

Suppose we are now given evidence that the ball is red. The posterior probability $\mathbb{P}(\mathrm{II}|{\color{red}\text{red}})$ represents the updated probability based on this new evidence. In this way, the Bayes' theorem can be thought of as an update rule of probabilities whenever new evidence becomes available.

## Probability of carrying a disease after testing

Suppose we have a testing kit for a disease that affects $1$ in $1000$ people. The accuracy of the testing kit is as follows:

if the person actually has the disease, then the test will return positive $99\%$ of the time.

if the person does not have the disease, then the test will return positive $2\%$ of the time.

If a random person is chosen and the test returns positive, then what is the probability that this person actually has the disease?

Solution. We define the following events:

event $A$ - test returns a positive.

event $B_1$ - the person has the disease.

event $B_2$ - the person does not have the disease.

Events $B_1$ and $B_2$ partition the sample space, that is, $\mathbb{P}(B_1)+\mathbb{P}(B_2)=1$. Our goal is to compute $\mathbb{P}(B_1|A)$, which is the probability that the person actually has the disease given that the test returned positive.

The Bayes's theorem tells us that:

Therefore, even if the test returns positive, the probability that the person actually has the disease is only around $4.7\%$. This is surprising because the testing kit seems to be very accurate given the information provided in the question.

This counterintuitive result can be explained by the fact that the disease is rare and only affects $1$ out of $1000$ people, and so even if the test is accurate, there is still a high chance that the person does not carry the disease. To mathematically show this, let's make the Bayes' theorem more explicit:

Here, $\neg$ is to be read as "not". Because the disease is rare, $\mathbb{P}(\text{infected})$ is very small, which makes the green terms small. In contrast, the red term is much larger because $\mathbb{P}(\neg\;\text{infected})$ is so high - close to $1$. Therefore, probability $\mathbb{P}(\text{infected}|\text{test pos})$ ends up being small.

# General formula for Bayes' theorem

If the events $B_1$, $B_2$, $\cdots$, $B_k$ form a partition for the sample space $S$ such that $\mathbb{P}(B_i)\ne0$ for $i=1$, $2$, $\cdots$, $k$, then for any event $A$ in $S$, we have:

For $j=1,2,\cdots,k$.

Solution. Recall the general law of total probabilitylink, which states that if events $B_1$, $B_2$, $\cdots$, $B_k$ form a partition for the sample space $S$ such that $\mathbb{P}(B_i)\ne0$ for $i=1$, $2$, $\cdots$, $k$, then for any event $A$ in $S$, we have:

Now, from Bayes' theorem, we know that:

Substituting \eqref{eq:iz4HXwsDfKxxk2xH9zI} into the denominator of \eqref{eq:VtpXP99Fib9DpX86Nws} gives us the general Bayes' theorem:

This completes the proof.

## Drawing a ball from three bags

Suppose we have the following three bags:

bag $\text{I}$, which contains $1$ red and $3$ green balls

bag $\text{II}$, which contains $2$ red and $1$ green balls.

bag $\mathrm{III}$, which contains $3$ red and $4$ green balls.

We pick one bag at random, and then draw a single ball. Given that the drawn ball is green, compute the probability that it came from bag $\mathrm{II}$.

Solution. Let's define the following events:

event $\mathrm{I}$ - choosing bag $\mathrm{I}$.

event $\mathrm{II}$ - choosing bag $\mathrm{II}$.

event $\mathrm{III}$ - choosing bag $\mathrm{III}$.

event $\color{green}\text{green}$ - drawing a green ball.

The general Bayes' theorem tells us that:

Therefore, the probability that the drawn green ball is from bag $\mathrm{II}$ is around $0.20$.