Naive Bayes
Bayes Theorem¶
Bayes’ Theorem:
Update your belief when you have new evidence.
| Term | Meaning |
|---|---|
| Prior. What you believed about A before seeing any evidence. | |
| Likelihood. How probable evidence B is, if A were true. | |
| Posterior. Updated belief about A after seeing evidence B. | |
| Normalizer. Overall probability of seeing B regardless of A. |
In classification,
: Posterior probability.
“Given I observed this data point , what is the probability that it belongs to class ?”
We want to find out the class that maximizes this probability.
The goal is to predict class that maximizes the posterior probability:
Equation can be written this way because we can ignore the denominator as the denominator will be a constant.
: Prior probability of class .
How common class is in the population before seeing any data.
If 70% of training data is labeled as class A, then .
: Likelihood.
: Also called as conditional density.
Given that a point belongs to class , how probable is it to look like (how typical is this data point)?
For the Gaussian version, assume that the density is multivariate Gaussian with diagonal covariance. If class has a Gaussian distribution, is just the Gaussian PDF evaluated at .
: Sums over all classes. Normalizing constant that ensures all postieror probabilities across classes sum to 1.
Assumption¶
We assume that conditional on class , variables are independent. Hence we multiply the conditional density of each variable to get the joint density:
When combined with Bayes Theorem:
When has many features with -dimensions, estimating as a full multivariate Gaussian is expensive and requires a lot of data. So in naive bayes, we pretend that all features are independent of each other within each class.
Hence, conditional on the outcome, there is no muilticollinearity.
Once you already know the class, Naive Bayes only requires the features to be independent.
and are independent given .
When there are many features, multiplying lots of small number together to achieve causes numerical underflow; product becomes so small that gets rounded up to zero and everything beraks.
This problem can be avoided by taking log transformation and turn product into a sum.
Because log function is monotone, taking the log does not change the class that yields the maximum posterior probability.
Conditional independence is within a class¶
Two features can be associated in the full sample even when they are independent within each class. For example, two words may both occur more often in one document class. Their overall association can come from the mixture of classes.
The assumption is stronger than zero correlation: knowing one feature must give no additional information about another after conditioning on the class. It is a modeling assumption, not something that preprocessing automatically makes true. Duplicate features count the same evidence twice and can make the posterior too extreme.
For classification, use the log score
The prior must remain in this score. Only the denominator shared by all classes can be dropped when choosing the largest score.
Definition of Density
Instead of using probabilities of an event happening as in discrete variables, for continous variables, probability over an interval is stated instead.
Density function, , describes how concentrated the probability is around each value of .
Estimation¶
Estimation means computing the parameters of a model from training data since we don’t know the true values.
In Naive Bayes, estimate the prior probability from training data. Also called as proportional prior.
Called proportional because it’s just the proportion of each class in the training data.
Categorical Variable¶
If the -variable is categorical, density is a probability.
Continuous Variable¶
Estimate probability from a marginal density.
Typically, Gaussian distribution is assumed. In this case, it is called as Gaussian Naive Bayes.
LaPlace Smoothing¶
Zero Probability Problem¶
When you estimate probabilities from training data using , you often get a probability of 0 if a particular feature never appears in class .
So, one single 0 can wipe out the entire product and ignore evidence from all other features.
Intuition¶
LaPlace smoothing adds a small count (usually 1) to every possible value, pretending that there is at leaset one count.
refers to the number of catgories of the corresponding -variable.
Data above gets transformed to:
| compliant | independent | chain |
|---|---|---|
| no | 3+1 | 0+1 |
| yes | 7+1 | 5+1 |
| compliant | San Mateo | Los Angeles | San Francisco | Contra Costa | Marin |
|---|---|---|---|---|---|
| no | 1+1 | 2+1 | 0+1 | 0+1 | 0+1 |
| yes | 1+1 | 8+1 | 1+1 | 1+1 | 1+1 |
For
county, add for each category (in the denominator).
Generalized LaPlace Estimator¶
Instead of adding 1 count, we can add an arbitrary number of observations, , that controls the amount of shrinking.
If is very large, it gets summarized to .
can be choosen by minimizing error on validation data.
does not have to be an integer value.
Choosing the feature distribution¶
Gaussian Naive Bayes models a continuous feature with a class-specific mean and variance. The density value is not the probability of observing one exact real number.
Bernoulli Naive Bayes models binary presence or absence. Absence contributes to the likelihood as well as presence.
Multinomial Naive Bayes models token counts within a document. Repeated words contribute repeatedly, and smoothing uses total token counts in the class as the denominator base, rather than the number of documents.
The categorical smoothing formula above applies to one categorical feature at a time. Adding to each of its categories adds to the denominator, keeping the probabilities summed to 1.
Class priors also matter. If training observations were deliberately balanced by sampling equal numbers from each class, the sample proportions may not represent the population where predictions will be made. Interpret posterior probabilities with that sampling scheme in mind.
Usage
When a fast algorithm is needed.
Naive Bayes transforms multivariate problems into univariate by looking at each feature separately, and computes for each individually. Does not look at features jointly.
Both training and test data complexity are linear in size of data.
Hence, useful for high dimensional data with text classifications.
When there is only small training data available.
Because we are estimating simple univariate distribute per feature per class.
When features are genuinely mostly independent.
Limitations
When features are strongly correlated.
When feature interactions matter for decision.
When we need a well-calibrated probabilities.
Naive Bayes posterior probabilities are often overconfident due to the independence assumption.