Logistic Regression
1. Generalization Error¶
Definition: The generalization error of a model f is the expected loss on new data:
Egen(f)=E(x,y)∼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=0: negative class (e.g., not spam, benign)
y=1: positive class (e.g., spam, malignant)
We need a model that outputs probabilities in [0,1] rather than arbitrary real numbers.
3. Logistic Regression Model¶
Sigmoid Function:
σ(z)=1+e−z1=1+ezez Properties:
σ(z)∈(0,1)
σ(0)=0.5
As z→∞, σ(z)→1
As z→−∞, σ(z)→0
Model Hypothesis:
fw(x)=σ(w⊤x)=1+e−w⊤x1 where w∈Rd+1 are the parameters and x∈Rd+1 is the input feature vector (with x0=1 for bias term).
4. Maximum Likelihood Estimation (MLE) Interpretation¶
Assumptions:
Probability Mass Function:
P(y∣x;w)={μ(x;w)1−μ(x;w)if y=1if y=0 This can be written compactly as:
P(y∣x;w)=μ(x;w)y⋅(1−μ(x;w))1−y Log-Likelihood for training set {(xi,yi)}i=1m:
L(w)=i=1∑m[yilog(μi)+(1−yi)log(1−μi)] where μi=σ(w⊤xi)
5. Cross-Entropy Loss¶
Per-example Loss:
cost(yi,fw(xi))={−log(fw(xi))−log(1−fw(xi))if yi=1if yi=0 This is the cross-entropy between two Bernoulli distributions:
Ground-truth: P(y=1∣xi,yi)=yi
Predicted: P(y=1∣xi;w)=fw(xi)
Cross-Entropy Formula:
H(P,Q)=−[P(y=1)logQ(y=1)+P(y=0)logQ(y=0)] =−[yilog(fw(xi))+(1−yi)log(1−fw(xi))] 6. Training Objective¶
Minimize (average) cross-entropy loss:
J(w)=m1i=1∑m[yilog(fw(xi))+(1−yi)log(1−fw(xi))] Or equivalently, maximize the log-likelihood.
7. Gradient Descent for Logistic Regression¶
Gradient:
∇wJ(w)=m1i=1∑m(fw(xi)−yi)xi Proof: Let’s derive this step by step.
Given J(w)=−m1∑i[yilog(σi)+(1−yi)log(1−σi)] where σi=σ(w⊤xi)
First, note that σ′(z)=σ(z)(1−σ(z)) (property of sigmoid)
For a single example (x,y) with σ=σ(w⊤x):
∂w∂[ylog(σ)+(1−y)log(1−σ)]=y⋅σ1⋅∂w∂σ+(1−y)⋅1−σ1⋅(−∂w∂σ)=[σy−1−σ1−y]⋅∂w∂σ=σ(1−σ)y(1−σ)−(1−y)σ⋅∂w∂σ=σ(1−σ)y−σ⋅∂w∂σ=σ(1−σ)y−σ⋅σ(1−σ)⋅x=(y−σ)⋅x Therefore:
∇wJ(w)=m1i=1∑m(σ(w⊤xi)−yi)xi=m1i=1∑m(fw(xi)−yi)xi Update Rule (Gradient Descent):
w←w−α∇wJ(w) where α is the learning rate.
Stochastic Gradient Descent (SGD):
For a randomly selected training example (xi,yi):
w←w−α(fw(xi)−yi)xi 8. Linear Regression vs. Logistic Regression¶
| Aspect | Linear Regression | Logistic Regression |
|---|
| Task | Regression | Classification |
| Hypothesis | w⊤x∈R | σ(w⊤x)∈[0,1] |
| Objective | Minimize MSE: 2m1∑(yi−w⊤xi)2 | Minimize Cross-Entropy |
| Solution | Closed-form or GD | GD (no closed form) |
| Output | Continuous value | Probability |