MIT 6.790 — Lecture 8

Classification and Logistic Regression

§ Classification

Over the past few lectures on linear regression, we've assumed that the output space Y\mathcal{Y} is R\mathbb{R}. In classification problems, we instead take Y\mathcal{Y} to be discrete; for now, let's further assume Y\mathcal{Y} is binary, meaning Y={+1,−1}\mathcal{Y} = \{+1, -1\}.

This is the setup for logistic regression, covered in Lecture 25 of 18.650. These notes will assume knowledge of that lecture.

§ Bayes Classifier

Here's the most common choice of loss function for (binary) classification problems.

Definition. The zero-one loss is L(y^,y):=1[y^≠y]\mathcal{L}(\hat{y}, y) := \mathds{1}[\hat{y} \neq y]. The loss equals zero if the classification was right, and one otherwise.

In this case, it's clear that the best classifier is the Bayes classifier.

Theorem. (Bayes Classifier) The best (lowest-risk) classifier under zero-one loss is:

h(x):={+1 if P(Y=+1∣X=x)>12.−1 otherwise.h(x) := \begin{cases} +1 & \text{ if } \mathbb{P}(Y = +1 \mid X = x) > \frac{1}{2}. \\ -1 & \text{ otherwise}. \end{cases}

Of course, the challenge is that we don't know what the distribution [Y∣X=x][Y \mid X = x] exactly looks like.

§ Nearest Neighbor Classifier

Here's a simple design of a binary classifier.

Definition. Given an input space X\mathcal{X}, impose some distance metric d ⁣:X×X→R≥0d \colon \mathcal{X} \times \mathcal{X} \to \mathbb{R}^{\geq 0}. Then predict the label h(x)h(x) like so:

Identify the set Nk(x)N_k(x) of the indices of the kk training points closest to xx. Then return h(x)=sgn(∑i∈Nk(x)y(i))h(x) = \mathrm{sgn}\left(\sum_{i \in N_k(x)} y^{(i)}\right).

This is called the nearest neighbor classifier (or NN, or KNN). It's simple, but slow: making a single prediction requires scanning the entire training set.

§ ERM: Linear Classifier

Let's try an ERM approach instead; here's the set of hypotheses we'll restrict ourselves to.

Definition. A linear classifier is a map h ⁣:X→{+1,−1}h \colon \mathcal{X} \to \{+1, -1\} via h(x):=sgn(w⊤x)h(x) := \mathrm{sgn}\left(w^{\top}x\right), for some parameter ww.

Remark. We'll often pad every vector in X\mathcal{X} with an extra one, as usual; recall Lecture 24 of 18.650.

Then ERM tells us we should choose ww to minimize the empirical risk risk^(hw)\widehat{\mathrm{risk}}(h_w), given below:

risk^(hw):=1n∑i=1nL(w⊤x(i)⋅y(i))    where    L(z):=1[z<0].\widehat{\mathrm{risk}}(h_w) := \frac{1}{n} \sum_{i = 1}^n \mathcal{L}\left(w^{\top}x^{(i)} \cdot y^{(i)}\right) ~~~ \text{ where } ~~~ \mathcal{L}(z) := \mathds{1}[z < 0].

Note that L(z)\mathcal{L}(z) is just a one-variable form of zero-one loss. Unfortunately, the empirical risk described above is hard to optimize (via gradient descent, say) because L(z)\mathcal{L}(z) is not convex or differentiable. Here's a better alternative:

Definition. A convex variant of zero-one loss is logistic loss, given by Llogistic(z):=log⁡2(1+e−z)\mathcal{L}_{\mathrm{logistic}}(z) := \log_2(1 + e^{-z}).

§ Addendum: Convexity

We provide a brief reminder of convexity here; it's important to know for optimization purposes.

Definition. A twice-differentiable function f ⁣:Rn→Rf \colon \mathbb{R}^n \to \mathbb{R} is convex if any of the following (equivalent) statements is true.

  1. We have ∇2f(x)⪰0\nabla^2 f(x) \succeq 0 for all xx; that is, ∇2f(x)\nabla^2 f(x) is positive semidefinite.

    • Note that ∇2f(x)\nabla^2 f(x) here is the Hessian, which is the n×nn \times n matrix given by [∇2f(x)]i,j:=∂2f∂xi∂xj(x)\left[\nabla^2 f(x)\right]_{i, j} := \frac{\partial^2 f}{\partial x_i \partial x_j}(x).

  2. We have f(λ⋅u+(1−λ)⋅v)≤λ⋅f(u)+(1−λ)⋅f(v)f\left(\lambda \cdot u + (1 - \lambda) \cdot v\right) \leq \lambda \cdot f(u) + (1 - \lambda) \cdot f(v) for all u,v∈Rnu, v \in \mathbb{R}^n and λ∈[0,1]\lambda \in [0, 1].

    • That is, any line segment connecting two points on the graph of ff lies above the graph.

  3. We have f(u)≥f(v)+∇f(v)⊤(u−v)f(u) \geq f(v) + \nabla f(v)^{\top}(u - v) for all u,v∈Rnu, v \in \mathbb{R}^n.

    • That is, the tangent plane to ff at vv lives underneath the graph of ff everywhere.

Here's why we care about convexity so much:

Theorem. (Local = Global) For any convex objective over a convex domain, any local minimum is a global minimum.

So techniques that seek out local minima (e.g., gradient descent) are really seeking out the global minimum. You can check that logistic loss is convex.

§ MLE: Logistic Regression

As in Lecture 25, we'll switch our hypothesis to be a map pw ⁣:X→[0,1]p_w \colon \mathcal{X} \to [0, 1], where the goal is to have pw(x)p_w(x) be a solid approximation of P(Y=+1∣X=x)\mathbb{P}(Y = +1 \mid X = x). Then our classification model is hw(x):=sgn(pw(x)−12)h_w(x) := \mathrm{sgn}\left(p_w(x) - \frac{1}{2}\right), naturally.

Specifically, we'll assume that pwp_w takes the form pw(x)=σ(w⊤x)p_w(x) = \sigma\left(w^{\top}x\right), where σ(z):=ez1+ez\sigma(z) := \frac{e^z}{1 + e^z} is the sigmoid function.

Theorem. (Logistic Loss) The log (base 2) likelihood of pw(x)p_w(x) is:

ℓ(w)=−∑i=1nlog⁡2(1+exp⁡(−y(i)w⊤x(i)))=−∑i=1nLlogistic(w⊤x(i)⋅y(i)).\ell(w) = -\sum_{i = 1}^n \log_2\left( 1 + \exp\left(-y^{(i)}w^{\top}x^{(i)}\right) \right) = - \sum_{i = 1}^n \mathcal{L}_{\mathrm{logistic}}\left( w^{\top} x^{(i)} \cdot y^{(i)} \right).

The w⊤x(i)⋅y(i)w^{\top}x^{(i)} \cdot y^{(i)} term is called the margin, whose sign determines correctness and whose magnitude determines confidence.

The proof is just plain computation, so we omit it. Note that this differs slightly from the result in Lecture 25 of 18.650 because of the convention that y(i)∈{+1,−1}y^{(i)} \in \{+1, -1\} instead of y(i)∈{0,1}y^{(i)} \in \{0, 1\}.

The interesting part is the coincidence that logistic loss shows up once again. The following are therefore identical:

§ Addendum: Log-Odds and Linear-Logistic Equivalence

Definition. The log-odds is defined as the value f(x):=log⁡[P(Y=+1∣X=x)P(Y=−1∣X=x)]f(x) := \log \left[ \frac{\mathbb{P}(Y = +1 \mid X = x)}{\mathbb{P}(Y = -1 \mid X = x)} \right].

Theorem. (Linear-Logistic Equivalence) Given a classification model, consider the following two procedures.

Then the two procedures seek the same parameter: w1=w2w_1 = w_2.

Proof: Obvious; the log-odds function f(x)f(x) is just the inverse of σ(… )\sigma(\dots), in that log⁡[σ(w1⊤x)1−σ(w1⊤x)]=w1⊤x\log\left[ \frac{\sigma(w_1^{\top}x)}{1 - \sigma(w_1^{\top}x)} \right] = w_1^{\top}x.   ■\blacksquare

So all of the discussion about features and kernels concerning linear regression applies equally well to logistic regression.