An Introduction to Generative Models - Naive Bayes for Binary Features

Generative models represent a powerful class of machine learning techniques. Unlike methods that directly map inputs x to outputs y, such as generalized linear models or perceptrons, generative models take a broader approach. They aim to model the joint distribution p(x,y;θ), which allows us to capture the underlying relationships between inputs and outputs in a holistic manner.

Generalized Linear Models vs. Generative Models

To recall, generalized linear models focus on the conditional distribution p(yx;θ). By contrast, generative models model p(x,y;θ). Once we have the joint distribution, we can predict labels for new data by leveraging the following rule:

y^=argmaxyYp(x,y;θ)

This prediction process connects naturally to conditional distributions. Using Bayes’ Rule, we can rewrite p(yx) as:

p(yx)=p(xy)p(y)p(x)

In practice, we often bypass computing p(x), as it is independent of y. Instead, predictions simplify to:

y^=argmaxyp(yx)=argmaxyp(xy)p(y)

With this foundation, let us explore one of the most straightforward and widely used generative models: Naive Bayes (NB).

If you’re unable to follow the above formulation, here’s a quick refresher on Bayes’ Rule to help you out.

Bayes’ Rule relates conditional probabilities to joint and marginal probabilities. It can be expressed as:

p(yx)=p(x,y)p(x)=p(xy)p(y)p(x),

where:

  • p(yx): Posterior probability of y given x,
  • p(x,y): Joint probability of x and y,
  • p(xy): Likelihood of x given y,
  • p(y): Prior probability of y,
  • p(x): Marginal probability of x, which ensures proper normalization.

Naive Bayes: A Simple and Effective Generative Model

To understand Naive Bayes, consider a simple yet practical problem: binary text classification. Imagine we want to classify a document as either a fake review or a genuine review. This setup offers a clear context to explore the mechanics of generative modeling.

Representing Documents as Features

To make this task computationally feasible, we use a bag-of-words representation. A document is expressed as a binary vector x, where:

x=[x1,x2,,xd].

Here, d represents the vocabulary size, and each xi indicates whether the i-th word in the vocabulary exists in the document (xi=1) or not (xi=0).

Modeling the Joint Probability of Documents and Labels

For a document x with label y, the joint probability p(x,y) can be expressed using the chain rule of probability:

p(xy)=p(x1,x2,,xdy)=p(x1y)p(x2y,x1)p(xdy,xd1,,x1). p(xy)=i=1dp(xiy,x<i),

However, modeling the dependencies between features (x1,x2,,xd) becomes intractable as the number of features grows and hard to estimate. This is where Naive Bayes introduces its defining assumption.

The Naive Bayes Assumption

Naive Bayes simplifies the problem by assuming that features are conditionally independent given the label y. Mathematically, this means:

p(xy)=i=1dp(xiy)

This assumption significantly reduces computational complexity while often delivering excellent results in practice. While the assumption of conditional independence may not hold in all cases, it is surprisingly effective in many real-world applications.


Parameterizing the Naive Bayes Model

To make predictions, we need to parameterize the probabilities p(xiy) and p(y).

Why? Parameterizing these distributions allows us to learn the necessary values (e.g., θ) from data in a structured way.

Binary Features

For simplicity, let us assume the features xi are binary (xi{0,1}). We model p(xiy) as Bernoulli distributions:

p(xi=1y=1)=θi,1,p(xi=1y=0)=θi,0

Similarly, the label distribution is modeled as:

p(y=1)=θ0

How do we arrive at these definitions? These definitions arise from the following assumptions and modeling principles:

  1. Binary Nature of Features: Since the features xi are binary (xi{0,1}), we need a probability distribution that models the likelihood of binary outcomes. The Bernoulli distribution is a natural choice for this.
  2. Parameterization with Bernoulli Distributions:
    • For p(xiy), the Bernoulli distribution models the probability that xi=1 for each possible value of y.
    • We introduce parameters θi,1 and θi,0, which represent the probability of xi=1 given y=1 and y=0, respectively.
  3. Label Distribution p(y):
    • The label y is also binary (y{0,1}), so we model p(y) using a Bernoulli distribution with a single parameter θ0, where θ0=p(y=1).
    • This parameter reflects the prior probability of the positive class.
  4. Learning from Data: These parameters (θi,1,θi,0,θ0) are learned from data using methods like Maximum Likelihood Estimation (MLE), ensuring that the model reflects the observed distribution of features and labels in the dataset.

Thus, the definitions provide a straightforward and interpretable way to model binary features and labels within the Naive Bayes framework.

With these definitions, the joint probability p(x,y) can be written as (with NB assumption):

p(x,y)=p(y)i=1dp(xiy)

Substituting the probabilities for binary features:

p(x,y)=p(y)i=1dθi,yI{xi=1}+(1θi,y)I{xi=0}

Here, I{condition} is an indicator function that evaluates to 1 if the condition is true and 0 otherwise.

How to intuitively understand this equation? This equation represents the joint probability p(x,y) by combining the prior probability p(y) with the product of the individual probabilities p(xiy) for each feature xi. Here:

  1. For each feature xi, the term I{xi=1} ensures that the corresponding parameter θi,y is used if xi=1, while I{xi=0} ensures that (1θi,y) is used if xi=0.
  2. The product i=1d combines the contributions of all features under the Naive Bayes assumption of conditional independence.
  3. Finally, multiplying by p(y) incorporates the prior belief about the label y, providing the full joint distribution p(x,y).

By this decomposition, we can efficiently compute p(x,y) for classification tasks.


Learning Parameters with Maximum Likelihood Estimation (MLE)

The parameters θ of the Naive Bayes model are learned by maximizing the likelihood of the observed data. Given a dataset of N labeled examples {(x(n),y(n))}n=1N, the likelihood of the data is:

n=1Npθ(x(n),y(n))

Taking the logarithm of the likelihood to simplify optimization, we obtain the log-likelihood:

(θ)=n=1Nlogpθ(x(n),y(n))

For binary features, substituting the joint probability pθ(x,y) (as defined earlier) gives:

(θ)=n=1N[i=1dlog(I{xi(n)=1}θi,y(n)+I{xi(n)=0}(1θi,y(n)))]+logpθ(y(n))

Focusing on a specific feature xj and label y=1, the relevant portion of the log-likelihood is:

(1)(θ)=n=1Nlog[I{y(n)=1xj(n)=1}θj,1+I{y(n)=1xj(n)=0}(1θj,1)]

Step 1: Derivative of the Log-Likelihood

Taking the derivative of the log-likelihood with respect to θj,1:

(2)θj,1=n=1N[I{y(n)=1xj(n)=1}θj,1I{y(n)=1xj(n)=0}1θj,1]

Did you follow the derivative? You might be wondering how derivative log(a+b) can be written as 1a+1b, right? If so, that’s a completely valid question — I had the same thought myself. Here’s the explanation.

The transition from equation (1) to equation (2) involves taking the derivative of the log-likelihood with respect to θj,1. Let’s break it down:

θj,1=θj,1n=1N[log(θj,1I{xj(n)=1}+(1θj,1)I{xj(n)=0})]

Here, the derivative is applied to the logarithm term. Using the chain rule, we first compute the derivative of the logarithm, which is:

θj,1log(f(θj,1))=1f(θj,1)f(θj,1)θj,1,

where

f(θj,1)=θj,1I{xj(n)=1}+(1θj,1)I{xj(n)=0}.

For a single n, the term inside the logarithm is:

f(θj,1)={θj,1,if xj(n)=1,1θj,1,if xj(n)=0.

The derivative of f(θj,1) with respect to θj,1 is:

f(θj,1)θj,1={1,if xj(n)=1,1,if xj(n)=0.

Using the chain rule:

θj,1log(f(θj,1))={1θj,1,if xj(n)=1,11θj,1,if xj(n)=0.

Applying this to the summation over N:

θj,1=n=1N[I{y(n)=1xj(n)=1}1θj,1I{y(n)=1xj(n)=0}11θj,1].

This is exactly what equation (48) represents, showing the decomposition of the derivative into two terms for xj(n)=1 and xj(n)=0.

The simplification uses the indicator functions I to select the appropriate cases, where:

I{xj(n)=1}contributes1θj,1,

and

I{xj(n)=0}contributes11θj,1.

Key Insight:

At each step of the derivation, we are dealing with a single term inside the logarithm. As a result, when we take the derivative of the logarithm, the result is simply 1term, where the term is either θj,1 or 1θj,1, depending on the value of xj(n).

  • If xj(n)=1, the term inside the log is θj,1, and its derivative is 1θj,1.
  • If xj(n)=0, the term inside the log is 1θj,1, and its derivative is 11θj,1.

Thus, at each n, we compute the derivative as 1term, with the specific term depending on the value of xj(n). This makes the process more straightforward as we apply it term by term across all N data points.

I hope that makes sense now. Let’s continue.

Step 2: Setting the Derivative to Zero

To find the maximum likelihood estimate, we set the derivative to zero:

n=1N[I{y(n)=1xj(n)=1}θj,1]=n=1N[I{y(n)=1xj(n)=0}1θj,1]

Simplifying:

θj,1n=1NI{y(n)=1}=n=1NI{y(n)=1xj(n)=1}

The above simplification is quite straightforward. I encourage you to write it out for yourself and work through the steps. Simply multiply both sides by θj,1(1θj,1) to eliminate the denominators, then expand both sides and isolate θj,1.

Note:

n=1NI{y(n)=1xj(n)=1}+n=1NI{y(n)=1xj(n)=0}=n=1NI{y(n)=1}

Step 3: Solving for θj,1

Rearranging to isolate θj,1:

θj,1=n=1NI{y(n)=1xj(n)=1}n=1NI{y(n)=1}

Interpretation: This estimate corresponds to the fraction of examples with y=1 in which the j-th feature xj is active (i.e., xj=1). Intuitively, it represents the conditional probability p(xj=1y=1) under the Naive Bayes assumption.


Next Steps:
  1. Compute the other θi,y values: You should calculate the parameters for all other features in the model (for example, θi,0 and θi,1 for binary features). These values represent the probability of a feature given a class, so you’ll continue by maximizing the likelihood for each i and class y to estimate these parameters.

  2. Estimate p(y): You’ll also need to compute the class prior probability p(y), which is simply the proportion of each class in the training data. This can be done by counting how many times each class label appears and normalizing by the total number of examples.

θ0=n=1NI{y(n)=1}N
  • θ0 is the proportion of samples in the dataset that belong to the class y=1.
  • It serves as the prior probability of y=1.

Substituting the Probabilities for Binary Features:

The likelihood of the joint probability p(x,y) can be expressed as:

p(x,y)=p(y)i=1dθi,yI{xi=1}+(1θi,y)I{xi=0}

Where I{xi=1} is an indicator function that equals 1 when xi=1, and I{xi=0} equals 1 when xi=0.

Remember this equation; it’s the one we started with.

Once all parameters are estimated, you will have a fully parameterized Naive Bayes model. The model can then be used for prediction by computing the posterior probabilities for each class y given an input x. For prediction, you would use the formula:

y^=argmaxyYp(y)i=1dp(xiy)

Where p(xiy) are the feature likelihoods, and p(y) is the class prior. The class with the highest posterior probability is chosen as the predicted label. This approach allows Naive Bayes to make efficient, probabilistic predictions based on the learned parameters.

So, the fundamental idea is:

You are estimating the parameters θ for all possible features and classes. Once the parameters are learned, you apply Bayes’ rule to compute the posterior probability for each class y. Finally, you take the class that maximizes the posterior probability using:

y^=argmaxyp(yx)

This gives you the predicted class y^, based on the learned parameters from the training data.


Recipe for Learning a Naive Bayes Model:
  1. Choose p(xiy): Select an appropriate distribution for the features, e.g., Bernoulli distribution for binary features xi.
  2. Choose p(y): Typically, use a categorical distribution for the class labels.
  3. Estimate Parameters by MLE: Use Maximum Likelihood Estimation (MLE) to estimate the parameters, following the same strategy used in conditional models.
Where Do We Go From Here?

So far, we have focused on modeling binary features. However, many real-world datasets involve continuous features. How can Naive Bayes be extended to handle such cases? In the next blog, we’ll explore Naive Bayes for continuous features and see how this simple model adapts to more complex data types. See you there!