Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Logistic Regression

1. Generalization Error

Definition: The generalization error of a model ff is the expected loss on new data:

Egen(f)=E(x,y)∼P[(f(x)−y)2]E_{\text{gen}}(f) = \mathbb{E}_{(x,y)\sim P}[(f(x) - y)^2]

This measures the model’s true predictive performance (also called population risk, true risk, or out-of-sample error).

2. Binary Classification Setup

For binary classification with labels y∈{0,1}y \in \{0, 1\}:

We need a model that outputs probabilities in [0,1][0, 1] rather than arbitrary real numbers.

3. Logistic Regression Model

Sigmoid Function:

σ(z)=11+e−z=ez1+ez\sigma(z) = \frac{1}{1 + e^{-z}} = \frac{e^z}{1 + e^z}

Properties:

Model Hypothesis:

fw(x)=σ(w⊤x)=11+e−w⊤xf_w(x) = \sigma(w^\top x) = \frac{1}{1 + e^{-w^\top x}}

where w∈Rd+1w \in \mathbb{R}^{d+1} are the parameters and x∈Rd+1x \in \mathbb{R}^{d+1} is the input feature vector (with x0=1x_0 = 1 for bias term).

4. Maximum Likelihood Estimation (MLE) Interpretation

Assumptions:

Probability Mass Function:

P(y∣x;w)={μ(x;w)if y=11−μ(x;w)if y=0P(y|x; w) = \begin{cases} \mu(x; w) & \text{if } y = 1 \\ 1 - \mu(x; w) & \text{if } y = 0 \end{cases}

This can be written compactly as:

P(y∣x;w)=μ(x;w)y⋅(1−μ(x;w))1−yP(y|x; w) = \mu(x; w)^y \cdot (1 - \mu(x; w))^{1-y}

Log-Likelihood for training set {(xi,yi)}i=1m\{(x_i, y_i)\}_{i=1}^m:

L(w)=∑i=1m[yilog⁡(μi)+(1−yi)log⁡(1−μi)]L(w) = \sum_{i=1}^m [y_i \log(\mu_i) + (1-y_i) \log(1-\mu_i)]

where μi=σ(w⊤xi)\mu_i = \sigma(w^\top x_i)

5. Cross-Entropy Loss

Per-example Loss:

cost(yi,fw(xi))={−log⁡(fw(xi))if yi=1−log⁡(1−fw(xi))if yi=0\text{cost}(y_i, f_w(x_i)) = \begin{cases} -\log(f_w(x_i)) & \text{if } y_i = 1 \\ -\log(1 - f_w(x_i)) & \text{if } y_i = 0 \end{cases}

This is the cross-entropy between two Bernoulli distributions:

Cross-Entropy Formula:

H(P,Q)=−[P(y=1)log⁡Q(y=1)+P(y=0)log⁡Q(y=0)]H(P, Q) = -[P(y=1) \log Q(y=1) + P(y=0) \log Q(y=0)]

=−[yilog⁡(fw(xi))+(1−yi)log⁡(1−fw(xi))]= -[y_i \log(f_w(x_i)) + (1-y_i) \log(1 - f_w(x_i))]

6. Training Objective

Minimize (average) cross-entropy loss:

J(w)=1m∑i=1m[yilog⁡(fw(xi))+(1−yi)log⁡(1−fw(xi))]J(w) = \frac{1}{m} \sum_{i=1}^m [y_i \log(f_w(x_i)) + (1-y_i) \log(1 - f_w(x_i))]

Or equivalently, maximize the log-likelihood.

7. Gradient Descent for Logistic Regression

Gradient:

∇wJ(w)=1m∑i=1m(fw(xi)−yi)xi\nabla_w J(w) = \frac{1}{m} \sum_{i=1}^m (f_w(x_i) - y_i) x_i

Proof: Let’s derive this step by step.

Given J(w)=−1m∑i[yilog⁡(σi)+(1−yi)log⁡(1−σi)]J(w) = -\frac{1}{m} \sum_i [y_i \log(\sigma_i) + (1-y_i) \log(1-\sigma_i)] where σi=σ(w⊤xi)\sigma_i = \sigma(w^\top x_i)

First, note that σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1 - \sigma(z)) (property of sigmoid)

For a single example (x,y)(x, y) with σ=σ(w⊤x)\sigma = \sigma(w^\top x):

∂∂w[ylog⁡(σ)+(1−y)log⁡(1−σ)]=y⋅1σ⋅∂σ∂w+(1−y)⋅11−σ⋅(−∂σ∂w)=[yσ−1−y1−σ]⋅∂σ∂w=y(1−σ)−(1−y)σσ(1−σ)⋅∂σ∂w=y−σσ(1−σ)⋅∂σ∂w=y−σσ(1−σ)⋅σ(1−σ)⋅x=(y−σ)⋅x\begin{align*}\frac{\partial}{\partial w}[y \log(\sigma) + (1-y) \log(1-\sigma)] &= y \cdot \frac{1}{\sigma} \cdot \frac{\partial \sigma}{\partial w} + (1-y) \cdot \frac{1}{1-\sigma} \cdot \left(-\frac{\partial \sigma}{\partial w}\right) \\ &= \left[\frac{y}{\sigma} - \frac{1-y}{1-\sigma}\right] \cdot \frac{\partial \sigma}{\partial w} \\ &= \frac{y(1-\sigma) - (1-y)\sigma}{\sigma(1-\sigma)} \cdot \frac{\partial \sigma}{\partial w} \\ &= \frac{y - \sigma}{\sigma(1-\sigma)} \cdot \frac{\partial \sigma}{\partial w} \\ &= \frac{y - \sigma}{\sigma(1-\sigma)} \cdot \sigma(1-\sigma) \cdot x \\ &= (y - \sigma) \cdot x\end{align*}

Therefore:

∇wJ(w)=1m∑i=1m(σ(w⊤xi)−yi)xi=1m∑i=1m(fw(xi)−yi)xi\nabla_w J(w) = \frac{1}{m} \sum_{i=1}^m (\sigma(w^\top x_i) - y_i) x_i = \frac{1}{m} \sum_{i=1}^m (f_w(x_i) - y_i) x_i

Update Rule (Gradient Descent):

w←w−α∇wJ(w)w \leftarrow w - \alpha \nabla_w J(w)

where α\alpha is the learning rate.

Stochastic Gradient Descent (SGD): For a randomly selected training example (xi,yi)(x_i, y_i):

w←w−α(fw(xi)−yi)xiw \leftarrow w - \alpha (f_w(x_i) - y_i) x_i

8. Linear Regression vs. Logistic Regression

AspectLinear RegressionLogistic Regression
TaskRegressionClassification
Hypothesisw⊤x∈Rw^\top x \in \mathbb{R}σ(w⊤x)∈[0,1]\sigma(w^\top x) \in [0, 1]
ObjectiveMinimize MSE: 12m∑(yi−w⊤xi)2\frac{1}{2m}\sum(y_i - w^\top x_i)^2Minimize Cross-Entropy
SolutionClosed-form or GDGD (no closed form)
OutputContinuous valueProbability