Conditional Probability
Conditional probability is concerned with calculating the Probability of an event, given that another event has occurred.
For example, you might expect the word "viagra" to appear more often in spam emails than in non-spam emails, sometimes called "ham". Let's use these illustrative probabilities:
The vertical bar means given. The first expression says that, given an email is spam, the probability that it contains the word "viagra" is .
More generally, we write:
This means the probability of event , given that event has occurred. The order matters: the probability that a spam email contains a word is a different question from the probability that an email containing that word is spam.
Calculating Conditional Probability
To compute conditional probability, we use:
This requires , since we cannot divide by zero.
The denominator is the probability of . The numerator is the probability that both and occur. Their ratio tells us how likely is within the cases where occurs.
General Multiplication Rule
We can rearrange the conditional probability formula to get the general multiplication rule:
To find the probability of both events, multiply the probability of by the probability of given .
Independent Events
In the special case where and are independent, knowing that occurred does not change the probability of :
So the general rule reduces to the Multiplication Rule of Probability:
For example, suppose we roll a fair six-sided die twice, with the rolls independent of each other. Knowing that the first roll was a five does not change the probability of getting a six on the second roll:
Computing Probability by Total Enumeration
Returning to the email example, suppose of emails are spam. Every email in this example is classified as either spam or ham, so the remaining are ham.
Let's use for the event that an email is spam, for ham, and for the event that it contains the word "viagra". We have:
What is the probability that a randomly selected email contains "viagra", without knowing whether it is spam or ham?
There are two ways this can happen: the email contains "viagra" and is spam, or it contains "viagra" and is ham. These cases cover every email containing the word and are mutually exclusive, so we can use the Addition Rule of Probability:
Using the general multiplication rule for each term:
Substituting our probabilities:
We can also see this by imagining 100,000 emails with exactly these proportions:
| Email type | Total emails | Emails containing "viagra" |
|---|---|---|
| Spam | 20,000 | 2,200 |
| Ham | 80,000 | 8 |
| Total | 100,000 | 2,208 |
That gives .
This calculation is an example of the law of total probability: split the possibilities into mutually exclusive cases that cover all outcomes, calculate each case's contribution, and add them together. Each conditional probability is weighted by how likely its case is.
Bayes Theorem
For a spam filter, we want to know the probability that an email is spam, given that it contains the word "viagra". So far, we have the probability in the other direction: how likely the word is to appear, given that the email is spam.
We can derive Bayes' rule from the conditional probability formula:
The order of events inside "and" does not matter. This is the commutative law for conjunction, one of the Laws of Logic:
So we can rewrite the numerator, then apply the general multiplication rule:
This is Bayes' rule. As with the original conditional probability formula, it requires .
Applying Bayes' Rule to Spam
Using for spam and for the email containing "viagra":
We already calculated , so:
Under our illustrative assumptions, an email containing "viagra" has about a probability of being spam. We can check this against the table above: of the 2,208 emails containing the word, 2,200 are spam.
Expanded Bayes' Rule
Sometimes the denominator, , is not given directly. We can calculate it using the law of total probability, splitting the possibilities into and its Complement Rule, , meaning "$B$ does not occur".
When both cases have nonzero probability:
Substituting this into Bayes' rule gives the expanded form:
Here, . In the spam example, the complement of spam is ham, so:
Use the shorter formula when the denominator is already known. If it isn't, calculate it separately by total enumeration or use the expanded formula. Both forms give the same result.