Logistic Regression

Introduction

Logistic regression is a parametric algorithm for solving classification problems whereby the label, y\in{\{0,1\}}. Logistic regression and perceptron are similar whereby a hyperplane is defined to divide the feature space into regions labeled according to the classification. The hyperplane is defined by the inner product between parameters the and the input vectors (summation of the weighted inputs), \mathbf{w}^T\mathbf{x}. Then, the resultant of the inner product is passed to a function that ensures the predicted value to be 0 \leq \hat{f}(\mathbf{x}) \leq 1 via

\hat{f}(\mathbf{x})=\text{sigm}(\mathbf{w}^T\mathbf{x})

\text{sigm}(z) refers to the sigmoid function, also known as logistic function which is defined as

\text{sigm}(z)=\frac{1}{(1+e^{-z})}

Figure 1 shows the graph of the sigmoid function. The sigmoid function has an S-shaped curve, which maps its input to a value within the range of 0 and 1. Thus, logistic regression has a probabilistic connotation. The curve has two horizontal asymptotes, one end is approaching 1 and the other end is approaching 0. Due to this property, the sigmoid function is differentiable at every point.

Figure 1. The sigmoid function is over -10 to +10.

As mentioned above, a logistic regression model outputs values within the range of 0 and 1. If we threshold the output at 0.5, a classification rule can be defined as follows:

\hat{f}(\mathbf{x}) = \begin{cases} 1 & \text{sigm}(z) > 0.5 \\ 0 & \text{sigm}(z) \leq 0.5 \end{cases}

Figure 2 shows the output probabilities of 50 simulated inputs to the sigmoid function. The input values vary from -17 to +22 and their predicted values are indicated by the red circles. Using a threshold value of 0.5, the classification of the data is indicated by the green and blue markers. As can be seen in the figure, output probabilities above the threshold value are classified as 1. Otherwise, they are classified as 0.

Figure 2. The output probabilities of the sigmoid function and their classification using a threshold value of 0.5.

Numerical Example

Consider the logistic regression model \hat{f}(\mathbf{x})=g(z) where z=-4+x_1+x_2. Predict the labels of the following inputs.

\mathbf{x}=\begin{bmatrix} 1&1\\1.5&1\\ 3&3\\3&4 \end{bmatrix}

z^1 = -4 + 1 + 1 = -2, g(z^1) = \frac{1}{1+e^{2}} = 0.119, \hat{y}^1 = 0

z^2 = -4 + 1.5 + 1 = -1.5, g(z^2) = \frac{1}{1+e^{1.5}} = 0.182, \hat{y}^2 = 0

z^3 = -4 + 3 + 3 = 2, g(z^3) = \frac{1}{1+e^{-2}} = 0.881, \hat{y}^3 =1

z^4 = -4 + 3.5 + 3 = 3, g(z^4) = \frac{1}{1+e^{-3}} = 0.953, \hat{y}^4 =1

Multiclass Classification Setting

So far, we have seen logistic regression for binary classification. Real-world problems may involve more than two classes. For multiclass classification, we need to train multiple logistic regression models, one for each of the C classes. For example, consider a classification problem as illustrated in Figure 3 (top). The data points belong to three classes, RED, BLUE and GREEN. We build three logistic regression models for each of the three classes. First, we treat RED instances as class 1, and all the other instances as class 0. Then, we build the logistic regression model to distinguish RED instances from the rest. Next, we treat BLUE instances as class 1 and all the other instances as class 0, and build the logistic regression model to distinguish BLUE instances from the rest. We perform the same method for GREEN instances. Eventually, we have three logistic regression models as shown in Figure 3 (bottom).

Given a test point, \mathbf{\acute{x}}, all models perform the prediction on the test point and produce C predicted values.

\hat{f}_c(\mathbf{\acute{x}})=\mathbf{w}_c^T\mathbf{\acute{x}}

Then, the predicted values are normalized using the softmax function. The softmax function turns the predicted values into probabilities. Each predicted value will be in the range of 0 and 1 and the sum of the values equal to 1.

\hat{f}_c(\mathbf{\acute{x}})=\frac{e^{\hat{f}_c(\mathbf{\acute{x}})}} {\sum_{k=1}^C e^{\hat{f}_k(\mathbf{\acute{x}})} }

The test point is assigned to the class with the largest probability.

Figure 3. Logistic regression in a multiclass classification setting.
Parameter Estimation

Now, how do we estimate the parameters of the logistic regression? We will discuss the estimation of the parameters for two-class logistic regression to simplify the explanation. The loss function of logistic regression is the negative log-likelihood or cross entropy as follows:

J(\mathbf{w})=- \sum_{i=1}^N y^i \log{\hat{y}^i} + (1-y^i) \log{(1-\hat{y}^i)}

where \hat{y}^i = \hat{f}(\mathbf{x}^i) = \sigma (z^i), \sigma denotes the sigmoid function and z^i=\mathbf{w}^T \mathbf{x}^i

We take the derivative of the loss function with respect to the parameters and set to zero. Using chain rule, the derivate of the loss function is

\frac{\partial J}{\partial w_j} = \sum_{i=1}^{N} \frac{\partial J}{\partial \hat{y}^i} \cdot \frac{\partial \hat{y}^i}{\partial z^i} \cdot \frac{\partial z^i}{\partial w_j}

\frac{\partial J}{\partial w_j} = \sum_{i=1}^{N} \frac{\hat{y}^i-y^i}{\hat{y}^i (1-\hat{y}^i)} \hat{y}^i(1-\hat{y}^i)x_j^i = \sum_{i=1}^{N} (\hat{y}^i-y^i) x_j^i

\sum_{i=1}^{N} (\hat{y}^i-y^i) x_j^i=0

However, the maximum likelihood estimation of the parameters are not in closed form. Thus, the equation needs to be solved using optimization algorithm such as gradient descent, Newton’s method etc. Using gradient descent, the parameters are estimated as follows:

\mathbf{w} := \mathbf{w} - \alpha \mathbf{g}

where \alpha is the learning rate and \mathbf{g}=\frac{\partial J}{\partial \mathbf{w}} is the gradient or the derivative of the loss function with respect to the parameters.

Using Newton’s method, the parameters are estimated as follows:

\mathbf{w} := \mathbf{w} - \alpha \mathbf{H}^{-1} \mathbf{g}

where \mathbf{H}^{-1} is the Hessian matrix or the second-order partial derivatives of the loss function with respect to the parameters.