Naive Bayes classifier is constructed under Bayesian framework. To understand Bayesian framework, we will start to discuss conditional probability first. See picture below.
Picture above is Venn Diagram for probability of event A and B which intersect each other. Probability A can be calculate by dividing with (universe, all rectangle area). Likewise, Probability B is divided by . The intersection area is when both event A and event B occur together. The probability of it, event A and event B occur together, is called joint probability, denoted by . can be calculated by dividing by .
Conditional probability is the probability of certain event when another event already occur, for example probability of event A when event B already occur. We can write probability of event A when event B already occur by . We can calculate as follow.
Formula above makes sense and is easy to remember. Probability of event A which event B already occur means given area B (green circle), thus how large area A in area B compared to area B? Yes, by dividing the intersection area with area B, exactly same with equation above. This formula is then known as Bayes’ theorem. We can also write in another form, that is probability of event B that event A already occur as follows.
Since is the intersection between B and A, thus . We can write combine two equations above as follows.
Equation above is powerful equation is machine learning. Because, by knowing the probability of event A that event B already occurs, probability of event A and B, we can predict probability of event B given attribute A. Let put an example for clearer understanding. Let’s say in a village, Ronald is getting fever. We can predict the probability of Ronald is getting typhus disease. To do that, we have to collect some data in that village as follows.
- for example for all people that are getting typhus disease, 98% of the are getting fever
- for example in that village, 10% people are getting fever
- for example in that village, 5% people are getting fever
Thus, we can estimate the probability of Ronald getting typhus disease as follows.
That’s cool, right? For this case, this is still simple to make our prediction accurate. Because in this case, we only use one attribute to predict whether Ronald getting typhus or not, which is fever. For more accurate prediction, we need more attributes, such as fever or not, body temperature, cough or not, how long he has been getting fever, does someone in their family also ever get typhus, and so on. More related attribute we can collect, more accurate our prediction. Here is further Bayes’ formula for more attributes. Let attribute (such as fever, body temperature, and so on) denoted by and class prediction (such as typhus, not typhus and so on) denoted by . Here is our Bayes formula.
It will be difficult to collect data and in real life. For example, probability when people is getting fever, also getting cough, also has body temp 38C, also has family member that ever gets typhus and so on. Thus, we will make an assumption to make equation above implementable in real life. We will use conditional independent as our assumption. Before that, we will discuss what independent and conditional independent are.
Independent events means an event that is not effected by previous event, for example when we toss a coin, we get head in the first toss, and for the second toss, the result whether head or tail is not affected by the first toss. In mathematical formula, we can write independent probability as follows.
Thus, for conditional probability, the formula will be.
Under conditional independent assumption, our Bayes formula becomes.
Voila! We just successfully derived for Bayes formula for classifier with many attributes. We can just calculate for all , and the class prediction is the with maximal value of . This classifier then is called Naive Bayes. In formal from, we can write as follows.
And since the denominator for all are same, we can simplify as follows. And this is our final Naive Bayes classifier.
If you are interested, for the denominator, we can still expand using chain rule as follows. But again, we will not use it in this case. Just if you are curious :p
Using conditional property here, we can simplify become: