Logistic Regression Deep Dive

5 minute read

Logistic regression is possibly the most well-known machine learning model for classification tasks. The classic case is binary classification, but it can easily be extended to multiclass or even multilabel classification settings. It’s quite popular in the data science, machine learning, and statistics community for many reasons:

  • The mathematics are relatively simple to understand
  • It is quite interpretable, both globally and locally; it is not considered a “black box” algorithm.
  • Training and inference are very fast
  • It has a minimal number of model parameters, which limits its ability to overfit

In many cases, logistic regression can perform quite well on a task. However, more complex architectures will typically perform better if tuned properly. Thus, practioners and researchers alike commonly use logistic regression as a baseline model.

Logistic regression’s architecture is a basic form of a neural network and it is optimized the same way as a neural network. This makes it a valuable case study for those interested in deep learning.

In this post I will be deriving logistic regression’s objective function from Maximum Likelihood Estimation (MLE) principles, deriving the gradient of the objective with respect to the model parameters, and visualizing how a gradient descent update shifts the decision boundary for a misclassified point.

Objective Function

Consider a dataset of observations where are the observed features and are the corresponding binary labels.

Logistic regression is a discriminative classifier (as opposed to a generative one), meaning that it models the posterior directly. It models this posterior as a linear function of “squashed” into by the sigmoid function (denoted as ).

The model is parameterized by a vector . For notational convenience we will assume each is concatenated with a so that the model’s bias term is contained in the final component of the vector .

Using the notational convenience that , we can write the model equation more generally:

Notice the equivalence with the Bernoulli PMF. This means logistic regression is modeling the data with a Bernoulli likelihood. Assuming data are iid, the likelihood of the full dataset is given by:

There is no closed form solution to logistic regression, so the log likelihood is optimized instead via gradient descent. The log likelihood objective function is given by:

Gradient of the Objective Function

In this section, we’ll be deriving the gradient of the objective function with respect to the model parameters . The derivation makes liberal use of the chain rule and other basic multivariate calculus rules.

The gradient of a sum is equal to the sum of the gradients, so for simplicity let’s consider a single data point.

Simplify notation:

The derivative of the sigmoid function will come in handy:

Derivation:

Visualizing a Gradient Update

A nice property of linear models like logistic regression is that the model parameters and the data share the same space. Take a look at the gradient:

Since and are both scalars, the gradient (for a particular data point) is just a scaled version of . To visualize this, let’s set the stage with a simple 2 dimensional dataset and model. Consider the following binary classification data:

Note: For simplicity in the example below, I’m using a logistic regression model without a bias term. This results in a non-affine decision boundary (ie the hyperplane must pass through the origin).

Let’s say the logistic regression model has been trained on this data and has learned model parameters . In the default setting (before any threshold tuning), the logistic regression decision boundary is defined by , which is equivalent to (you can verify this equivalence by looking at the sigmoid function plot below). This means data is classified as Class 1 if and Class 0 if .

In effect, defines a hyperplane (the decision boundary) in , . and are plotted with the data below.

Now, let’s introduce a new data point, belonging to Class 1 (orange), which is incorrectly classified by the learned model:

Let’s consider what happens if the model is trained on just this incorrectly classified data point. Computing the gradient:

Now we can plot the gradient alongside the data and the existing decision boundary.

The gradient update equation at iteration is

Note: If you’ve seen this equation in the past, the addition sign might throw you off. Usually we minimize the negative log likelihood, but in this case we’re going to maximize the positive log likelihood.

Where is a learning rate. Let’s compute the updated model parameters:

Armed with the updated model parameters, we can draw the updated decision boundary:

As you can see, adding a fraction of the gradient to effectively rotated the decision boundary towards the misclassified point.

Recalling the gradient equation, let’s consider some other cases for the gradient and how we can interpret the math. Hopefully you can visualize how the gradient will attempt to rotate or even “flip” the decision boundary based on the class and location of the data point.

description comments
model incorrect on a class 1 point 1 0.1 The case illustrated above
model correct on a class 1 point 1 0.99 Model is confident and correct; very small gradient update
model incorrect on a class 0 point 0 0.7 Rotates (or pushes) the decision boundary the opposite direction
model correct on a class 0 point 0 0.01 Model is confident and correct; very small gradient update