Bayes’ Theorem

Bayes' Theorem

Bayes’ Theorem states that $$ p(F | E) = \frac{p(E|F)p(F)}{p(E|F)p(F) + p(E|\bar F)p(\bar F)} $$

Short form:

Link to original

title: Example 1
A frog climbing out of a well is affected by the weather.
When it rains, he falls back down the well with a probability of $\frac{1}{10}$.
In dry weather, the probability he falls is $\frac{1}{25}$.
The probability of rain is $\frac{1}{5}$.
 
If we know that the frog fell back, what is the probability it was a rainy day?
- Let $F$ be the event that frog fell back.
- Let $R$ be the event that it was a rainy day.
 
We are looking to find $p(R | F)$.
 
$$
	\begin{aligned}
		p(R) &= \frac{1}{5} \\
		p(F|R) &= \frac{1}{10} \\
		p(F|\bar R) &= \frac{1}{25} \\
		p(\bar R) &= \frac{4}{5} \\
		\\
		p(R | F) &= \frac{
			p(F|R)p(R)
		}{
			p(F|R)p(R) + p(F|\bar R)p(\bar R)
		} \\
		&= \frac{
			\frac{1}{10} \cdot \frac{1}{5}
		}{
			\frac{1}{10} \cdot \frac{1}{5} + \frac{1}{25} \cdot \frac{4}{5}
		} \\
		&= \frac{\frac{1}{50}}{\frac{1}{50} + \frac{4}{125}} \\
		&= \frac{5}{13}
	\end{aligned}
$$
title: Example 2
There are two boxes, $\text{Box}_1$ and $\text{Box}_2$.
- $\text{Box}_1$ contains $2$ green balls and $7$ red balls.
- $\text{Box}_2$ contains $4$ green balls and $3$ red balls.
 
One of the boxes is picked at random and then a ball is picked at random.
If we know a red ball has been selected, what is the probability that it was taken $\text{Box}_1$?
- Let $R$ be the event that we have chosen a red ball.
- Let $F$ be the event that we have chosen a ball from $\text{Box}_1$.
 
We want to find $p(F|R)$:
- $p(R|F) = \frac{7}{9}$
- $\bar F$ is the event we have picked from $\text{Box}_2$.
  $p(F) = p(\bar F) = \frac{1}{2}$
- $p(R | \bar F) = \frac{3}{7}$ as $\text{Box}_2$ has $3$ red balls out of a total of $7$.
 
By Bayes' Theorem:
 
$$
	\begin{aligned}
		p(F | R) &= \frac{
			\frac{7}{9} \cdot \frac{1}{2}
		}{
			\frac{7}{9} \cdot \frac{1}{2}
			+ \frac{3}{7} \cdot \frac{1}{2}
		} \\
		&= \frac{
			\frac{7}{9}
		}{
			\frac{49+27}{63}
		} \\
		&= \frac{49}{76} \\
		&\approx 0.645
	\end{aligned}
$$
title: Example 3
Suppose $1$ in $100,000$ people have a rare disease for which there is a quite accurate diagnostic test:
- It has a $99\%$ accuracy when given to someone with the disease.
- It has a $99.5\%$ accuracy when given to someone without the disease.
 
What is the probability that someone who tests positive for the disease actually has the disease?
- Let $D$ be the event that a person has the disease.
- Let $T$ be the event that a person tests positive for the disease.
 
Hence,
- $p(T | D) = 0.99$
- $p(\bar T | \bar D) = 0.995$
  So we find that:
  $$
  	\begin{aligned}
		p(T | \bar D) &= 1 - p(\bar T | \bar D) \\
		&= 1 - 0.995 \\
		&= 0.005
	\end{aligned}
  $$
- $p(D) = \frac{1}{100,000} = 0.00001$
- $p(\bar D) = 0.99999$
 
$$
	\begin{aligned}
		p(D | T) &= \frac{0.99 \cdot 0.00001}{0.99 \cdot 0.00001 + 0.005 \cdot 0.99999} \\
		&\approx 0.002
	\end{aligned}
$$

Bayesian spam filters

Bayesian spam filters

A Bayesian spam filter uses information about previously seen e-mail messages to guess whether an incoming e-mail is spam. It looks for occurrences of particular words in messages.

For a word , the probability that appears in a spam message is estimating by determining:

  • the number of times appears in a message from a large set of messages known to be spam
  • the number of times appears in a large set of messages that are known not to be spam

Sometimes spam filters are not perfect:

  • A false negative occurs when a spam filter fails to identify a spam message as spam.
  • A false positive occurs when a spam filter identifies a message as spam that is not spam.
title: Example
Suppose we have found the word "Bitcoin" occurs in $250$ of $2000$ messages known to be spam, and in $5$ out of $1000$ messages known not to be spam.
 
Estimate the probability that an incoming message containing the word "Bitcoin" is spam, assuming that it is equally likely that an incoming message is or is not spam.
 
Let our threshold for spam be $0.9$, will we reject the message?
 
Let $S$ be the event that an incoming message is spam, and $R$ be the event that the message contains the word "Bitcoin". Then:
- $p(R | S) = \frac{250}{2000} = 0.125$
- $p(R | \bar S) = \frac{5}{1000} = 0.005$
- $p(S) = p(\bar S) = 0.5$
 
$$
	\begin{aligned}
		p(S | R) &= \frac{0.125 \cdot 0.5}{0.125 \cdot 0.5 + 0.005 \cdot 0.5} \\
		&= \frac{0.125}{0.13} \\
		&\approx 0.962
	\end{aligned}
$$
 
As $0.962 > 0.9$, we reject the message as spam.
Link to original