Naive Bayes

Introduction

Using the Enron email data set, we will create a Naive Bayesian network in this simple exercise to classify whether a given email is a spam or ham by looking at its word frequency as feature set.

Mathematics

Definitions

Let's define NN to be the number of total emails we have in the dataset and NsN_{s} to be the number of spam emails in the email set.

NsoN_{so} is the number of spam emails that contain the word "offer"

NoN_{o} is the number of emails that contain the word "offer"

Then the probability of having a spam email in the set is said to be:

P(SPAM=1)=NsNP(SPAM=1) = \frac{N_{s}}{N}

And the probability of having an email that contains the word offer is:

P(OFFER=1)=NoNP(OFFER=1) = \frac{N_{o}}{N}

Finally, the conditional probability of an email being a spam email given that it contains the word offer:

P(SPAM=1OFFER=1):=NsoNoP(SPAM=1\mid OFFER=1) := \frac{N_{so}}{N_{o}}

Postulate

If the probability of finding the word offer given that it's a spam email is higher than that of finding the word offer in a non-spam email:

P(OFFER=1SPAM=1)>P(OFFER=1SPAM=0)P(OFFER =1 \mid SPAM=1) > P(OFFER = 1 \mid SPAM=0)

then we can infer that:

P(SPAM=1OFFER=1)>P(SPAM=1)P(SPAM=1 \mid OFFER=1) > P(SPAM = 1)

Proof

P(SPAM=1OFFER=1)=P(OFFER=1SPAM=1)P(SPAM=1)P(OFFER=1)=NsoNsNsNNoN=NsoNoP(SPAM=1 \mid OFFER=1) = \frac{P(OFFER=1 \mid SPAM=1)P(SPAM=1)}{P(OFFER=1)} = \frac{\frac{N_{so}}{N_{s}}\frac{N_{s}}{N}}{\frac{N_{o}}{N}} = \frac{N_{so}}{N_{o}}

This is known as the Bayes' rule, famously stated as P(AB)=P(BA)P(A)P(B)P(A \mid B)=\frac{P(B \mid A)P(A)}{P(B)}

P(SPAM=0OFFER=1)=P(OFFER=1SPAM=0)P(SPAM=0)P(OFFER=1)P(SPAM=1OFFER=1)=P(OFFER=1SPAM=1)P(SPAM=1)P(OFFER=1)P(SPAM=0 \mid OFFER=1) = \frac{P(OFFER=1 \mid SPAM=0)P(SPAM=0)}{P(OFFER=1)} \\ P(SPAM=1 \mid OFFER=1) = \frac{P(OFFER=1 \mid SPAM=1)P(SPAM=1)}{P(OFFER=1)}

For abbreviation, let's define that:

P(SPAM=1):=P(S)P(OFFER=1SPAM=1):=P(OS)P(OFFER=1SPAM=0):=P(OSc)P(SPAM=1OFFER=1):=P(SO)P(SPAM=1) := P(S) \\ P(OFFER=1 \mid SPAM=1) := P(O \mid S) \\ P(OFFER=1 \mid SPAM=0) := P(O \mid S_{c}) \\ P(SPAM=1 \mid OFFER=1):= P(S \mid O)

Begin with

P(OS)>P(OSc)P(O \mid S) > P(O \mid S_{c})

Rewrite them using Bayes' rule:

P(SO)P(O)P(S)>P(ScO)P(O)P(Sc)\frac{P(S \mid O) P(O)}{P(S)} > \frac{P(S_{c} \mid O)P(O)}{P(S_{c})}

The $P(O)$ terms cancel out each other:

P(SO)P(S)>P(ScO)P(Sc)\frac{P(S \mid O)}{P(S)} > \frac{P(S_{c} \mid O)}{P(S_{c})}

By definition, we can rewrite the right hand side as the following:

P(SO)P(S)>1P(SO)1P(S)\frac{P(S \mid O)}{P(S)} > \frac{1 - P(S \mid O)}{1 - P(S)}

Re-organize the terms:

1P(S)P(S)>1P(SO)P(SO)\frac{1 - P(S)}{P(S)} > \frac{1 - P(S \mid O)}{P(S \mid O)}

Then we can easily see that:

1P(S)1>1P(SO)11P(S)>1P(SO)\frac{1}{P(S)} - 1 > \frac{1}{P(S \mid O)} - 1 \\ \frac{1}{P(S)} > \frac{1}{P(S \mid O)} \\

Q.E.D.

P(SO)>P(S)P(S \mid O) > P(S)

Feature Probability

First of all, we load the data into a class object called EmailSet and compute the feature probability for each word that has appeared in the email using FeatureProbability.from_email_set.

Edge cases

Notice that the word bacterial and compensating have rare occurence in the data set. The probability we compute has a very noisy estimate for their true value. In other words, they are statistically insignificant for us to draw any reliable conclusion. It is not safe to make the assumption that every email with teh word bacterial is a spam email.

Filter Low-reach Features

Let's apply a limit to filter the words that have very low occurence in our data set.

Using All Features

Unconditional Independence

Two variables are unconditionally independent, if knowing the result of one tells nothing of the other under any circumstance.

For example, let H to be the event of flipping a head, and S to be the event of rolling a 6.

P(SH)=P(S)P(H)P(HS)=P(H)P(SH)=P(S)P(HS)=P(S)P(H)P(S \wedge H) = P(S)P(H) \\ P(H \mid S) = P(H) \\ P(S \wedge H) = P(S)P(H \mid S) = P(S)P(H)

Conditional Independence

Let's denote the event of having a particular disease to be DD, event for showing positive on test 1 for detecting the disease to be T1T_{1}, and event for showing positive on test 2 for detecting the same disease to be T2T_{2}.

The following case is NOT unconditionally independent because it is conditional

P(T1T2)P(T1)P(T_{1} \mid T_{2}) \neq P(T_{1})

Given that we know test 2 is showing a positive result, it does influence the probability of having a positive on test 1, even though test 2 could have been a false positive. It is because a positive result from either tests can influence the probability of having the disease. The tworesults we have from T1T_{1} and T2T_{2} are connected by the variable DD.

P(T1D)P(T1)P(T2D)P(T2)P(T_{1} \mid D) \neq P(T_{1}) \\ P(T_{2} \mid D) \neq P(T_{2})

Even though the two events are not unconditionally independent, under some cases, aka conditions, they can be independent of each other.

P(T1T2D)=P(T1D)P(T_{1} \mid T_{2} \wedge D) = P(T_{1} \mid D)

This is saying that if the condition of having the disease is satisfied, then the probabilities of T1T_{1} and T2T_{2} are independent from each other. And equivalently speaking,

P(T1T2D)=P(T1D)P(T2D)P(T_{1} \wedge T_{2} \mid D) = P(T_{1} \mid D) \cdot P(T_{2} \mid D)

Examples When Unconditional Independence is Violated

Let's go back to the email examples. This is an example of conditional independence.

P(LIMITED=1OFFER=1)P(LIMIT=1)P(LIMITED = 1 \mid OFFER = 1) \neq P(LIMIT = 1)

The two random variables are not independent of each other in this particular condition because:

  • The presence of the word offer suggests that the email is spam.

  • If the email is spam, then it is more likely to contain the word limited.

  • Therefore, the presence of the word offer makes the presence of the word limited more likely.

  • In conclusion, the words limited and offer are NOT unconditionally independent. They are conditional independent in the sense that in some conditions they are dependent of each other and some conditions they are truly independent.

P(LIMITED=1OFFER=1)>P(LIMITED=1)P(LIMITED = 1 \mid OFFER = 1) > P(LIMITED = 1)

However if we already knew the email is a spam email, then learning that we have the word offer doesn't add any new knowledge to our estimate of probability of seeing the word limited.

P(LIMITED=1OFFER=1SPAM=1)=P(LIMITED=1SPAM=1)P(LIMITED = 1 \mid OFFER = 1 \wedge SPAM = 1) = P(LIMITED = 1 \mid SPAM = 1)

Examples When Conditional Independence is Violated

The words limited and offer often appear together in spam email because they frequently appear as part of the compound phrase a limited time offer. In this sense, the probability of finding one word while other one is present is definitely not independent.

P(LIMITED=1SPAM=1OFFER=1)>P(LIMITED=1SPAM=1)P(LIMITED=1 \mid SPAM=1 \wedge OFFER=1) > P(LIMITED=1 \mid SPAM=1)

However, if we naively assume independence

P(LIMITED=1OFFER=1SPAM=1)=P(LIMITED=1SPAM=1)P(OFFER=1SPAM=1)P(LIMITED=1 \wedge OFFER =1 \mid SPAM=1) = P(LIMITED=1 \mid SPAM=1) \cdot P(OFFER=1 \mid SPAM=1)

It enables us to separate terms for ease of calculation.

Applying The Naive Assumption

For abbreviation, I will write LIMITED=1LIMITED = 1 as LL and LIMITED=0LIMITED = 0 as LcL_{c}. Same applies to other variables.

We are trying to calculate the probability ratio of the following.

P(SLO)P(ScLO)\frac{P(S \mid L \wedge O)}{P(S_{c} \mid L \wedge O)}

Using Bayes' rule, we can rewrite them as:

P(S)P(Sc)P(LOS)P(LOSc)\frac{P(S)}{P(S_{c})} \cdot \frac{P(L \wedge O \mid S)}{P(L \wedge O \mid S_{c})}

Using the naive assumption, we can break the terms apart and simplify the expression

P(LOS)=P(LS)P(OS)P(LOSc)=P(LSc)P(OSc)P(L \wedge O \mid S) = P(L \mid S) \cdot P(O \mid S) \\ P(L \wedge O \mid S_{c}) = P(L \mid S_{c}) \cdot P(O \mid S_{c})

In conclusion,

P(S)P(Sc)P(LS)P(LSc)P(OS)P(OSc)\frac{P(S)}{P(S_{c})} \cdot \frac{P(L \mid S)}{P(L \mid S_{c})} \cdot \frac{P(O \mid S)}{P(O \mid S_{c})}

More Generally Speaking

P(SWi...WN)P(ScWi...WN)=P(S)P(Sc)iNP(WiS)P(WiSc)\frac{P(S \mid W_{i} ... W_{N})}{P(S_{c} \mid W_{i} ... W_{N})} = \frac{P(S)}{P(S_{c})} \cdot \prod_{i}^{N} \frac{P(W_{i} \mid S)}{P(W_{i} \mid S_{c})}

If we wish to improve the performance of the classifier, we should also include that probability ratio of words that do not appear in the email. For example, in this Enron dataset, the word enron NOT appearing in the email could be a good indicator that the email is spam.

If a word doesn't appear in the text/email, we just need to calculate

  • the probability of not having the word given an email is spam

  • the probability of not having the word given an email is ham.

AbsenceRatio=P(WcS)P(WcSc)=1P(WS)1P(WSc)AbsenceRatio = \frac{P(W_{c} \mid S)}{P(W_{c} \mid S_{c})} = \frac{1 - P(W \mid S)}{1 - P(W \mid S_{c})}

Split into Training & Test Set

Let's split the data and see how well the model performs on unseen data!

Last updated