Disclosure: This post may contain affiliate links, meaning I recommend products and services I've used or know well and may receive a commission if you purchase them, at no additional cost to you. Learn more.

# Understanding Bayes’ Theorem

##### Table of Contents

– John Maynard Keynes

“When the facts change, I change my mind. What do you do sir?”

Bayes’ Theorem is a method to update our beliefs based on new information acquired.

For example, if we were trying to find out the probability of a said person liking Evangelion, we could just take the percentage of the entire population who likes Evangelion and call it a day. But, if there was new information that said person actually likes Anime, we can adjust our probability upwards based on the fact that most likely, Evangelion lovers are also Anime lovers.

## The formula of Bayes’ Theorem

In it’s simplest form, the equation of Bayes’ theorem is:

For the above equation,

P(A|B) is the posterior probability; the probability of event A happening given that event B has happened. Relating to the example in our introduction, this would be the probability of liking Evangelion given the person also likes Anime. P(B|A) is the likelihood; the probability of event B happening given that event A has happened. This would be the probability of liking Anime given the person likes Evangelion. P(A) is the prior probability; the probability of event A happening. This would be the probability of liking Evangelion. P(B) is the evidence; the probability of event B happening. This would be the probability of liking Anime.Basically, in simple English sense, the above equation can written as:

### Bayes’ Theorem example calculation

Because examples are always good to explain complex problems, let’s use our previous example from the introduction to explain the calculations in Bayes’ Theorem.

For starters, let’s say the possibility of someone liking Evangelion, P(Evangelion) is somewhat rare with a 1% probability (not because it’s bad, but because it’s unconventional).

In addition, Anime fans, P(Anime) are fairly common with a 10% probability due to the weeb movement.

And as for Evangelion fans who like Anime, P(Anime|Evangelion), that’s about 95%.

With the information we have obtained above, we can find the probability of someone liking Evangelion if that said someone likes Anime.

Consequently, just by obtaining our new evidence, we manage to achieve a better probability, from a probability of 0.01 to 0.095.

## Classification using Bayes’ Theorem

In a classification problem where we have to categorise one from another, Bayes’ Theorem can prove to be very useful as we have previously seen in our blog post, “Fake Jobs Text Classification using Naive Bayes”.

As so, if we have a problem instance that needs to be classified, our equation can be written as:

This equation is then used to find the probability of each class in a problem, the class with the highest probability is then categorised as that class.

However, the issue arises when we start including multiple features within our data. The calculation of the likelihood, P(x|Class) becomes exponentially more expensive if these features relate to each other.

Because of this, we don’t use Bayes’ Theorem fully, we simplify it.

### Naive Bayes Classifier

Naive Bayes aims to simply the Bayes’ Theorem by making two huge assumptions:

- All features of our data are independent of one another
- All features have an equal contribution to the outcome

With that said, if we consider any two events A and B are independent, then,

Because of this, in a conditional independence, we can define the independence in a conditional model as:

So, if we define, Data = (x_1, x_2,…,x_n) and x_1, x_2, … x_n are representatives of the features in our data, we can substitute them for our Data.

And because we’re assuming that all features of our data are independent, we can actually split up the conditional probability so that P(x_1, x_2 | Class) can be written as P(x_1 | Class) P(x_2 | Class) .

Furthermore, as the denominator remains constant for all entries in the data, we can remove the denominator and introduce a proportionality.

Once we have derived our independent model, we just need to apply a *MAP* decision rule to find the *Class* with the highest probability. Which is essentially just like a MAX function in programming. This can be shown mathematically as,

Finally, all we’re required to do is to calculate P(Class) and P(x_i|Class). This can be done easily by calculating the appropriate probabilities from our data.

## Parameters and types of Naive Bayes classifiers

Now that we have derived our model, we need a way to calculate P(x_i|Class), which requires us to deal with our data.

When dealing with data, probability distributions may differ from one another. Different distributions can have very different characteristics, for example:

- Continuous distributions vs. Discrete distributions
- For continuous distributions, there could be different sets of possible values (e.g. numbers x, x>0, or x\in[0,1])
- For discrete distributions, there could be a finite vs. an infinite number of possible values

In essence, each distribution *within* a family of distributions is different in terms of the parameters of the distribution. These parameters are what help determine the mean and variance of the distribution, and the values of probabilities from it.

### Gaussian Naive Bayes

Typically when we deal with continuous data, we assume that continuous values associated with each feature are distributed across a Gaussian distribution, or another term you might be more familiar with, Normal distribution.

When the likelihood of the features in our data are assumed to be Gaussian, the conditional probability can be described as:

Where \mu_{Class} and \sigma_{Class}^2 is the mean and variance of our features respectively (our parameters) in respect to our Class.

### Multinomial Naive Bayes

In a Multinomial model, we estimate the conditional probability of a certain feature based on the frequency of the feature appearing for a specific Class. It also has a bag of words assumption that the position of the features does not matter.

In this case, the likelihood of observing a feature can be given as,

### Bernoulli Naive Bayes

For the Bernoulli model, features are considered independent booleans (binary variables). In contrast to the Multinomial model, it finds the binary feature occurrence as opposed to the feature frequency.

The likelihood of a observing a feature in this model can be give as,

## Summary

Despite the simplified assumptions on Naive Bayes, the Naive Bayes classifiers have performed surprisingly well in real-world applications. They require only a handful of training data to estimate the necessary parameters.

Naive Bayes algorithms are also fast and easy to implement in comparison to other models. The assumption on independent conditional probability helps greatly with speeding things up. However, in real-world scenarios, our features are most likely dependent on each other, hence could cripple the performance of our classifier.

**Note.** This was also a complimentary blog post to the “Fake Jobs Text Classification using Naive Bayes” where we used Naive Bayes for a natural language processing (NLP) problem.

## Further Reading

### Books

- Practical Statistics for Data Scientist (O’Reilly), by Peter and Andrew Bruce

(A must-have book for aspiring data scientist on applying statistical methods to data science)

### References

- J, Abdey. (2020), Probability and Statistics: To p, or not to p? (2020).
*In Probability and Statistics Coursera Course by the University of London*.