4.1 Classification

In this section we discuss the theory of classification, without going in to detail about any particular classification method.

We are given training data \((x_i,y_i)\quad i=1,\ldots, n\) that we assume are a random sample from an unknown distribution \(P_{X,Y}\). The goal is to find a classifier, that is a function \(h:\mathcal X \mapsto \mathcal Y= \left\{-1,1\right\}\), based on the training data. There is a loss function \(l:\mathcal Y\times \mathcal Y \mapsto \mathbb R\) and most commonly this is the 0-1 loss, \[ l(h(x),y) :=1_{h(x)\neq y}(x,y). \] Then the out-of-sample error is \[ E_{out}(h) = E_{X,Y}\left[ l(h(X),Y) \right] = P(h(X)\neq Y). \] That is, it is the probability of classifying a random observation \(X\) incorrectly, when using the classifier \(h.\)

To simplify notation we also define the conditional class probability, \[ \eta(x):=P\left( Y=1\mid X=x \right). \]
In the hypothetical case where we know the distribution of \(X,Y\), the \(h\) that minimizes this out-of-sample error is called the Bayes classifier. We claim that it is \[\begin{align*} h^\star(x) &= \begin{cases} 1 & \text{if } \eta(x)\geq 1/2\\ -1 & \text{if } \eta(x)< 1/2 \end{cases}\\ &= \text{sign} (\eta(x) - 1/2). \end{align*}\]

That is, we should classify to the class that has the highest probability, conditioned on \(X\). The minimal out-of-sample error is called the Bayes risk.

Let us prove this. The claim is that for any other classifier \(h(x)\), the out-of-sample error is at least as large, that is \[ E_{out}(h) = P\left( h(X)\neq Y \right) \geq P\left( h^\star(X)\neq Y \right) = E_{out}(h^\star). \] Or, equivalently, \(P\left( h^\star(X)= Y \right)\geq P\left( h(X) = Y \right)\). First note that if we condition on \(X=x\), then either \(h(x)=-1\) or \(h(x)=1\). Therefore, \[\begin{align*} P\left( h(X) = Y \mid X=x\right) &= 1_{h(x) = 1}(x)P\left(Y=1 \mid X=x\right) + 1_{h(x) = -1}(x)P\left(Y=-1 \mid X=x\right)\\ &= 1_{ h(x) = 1}(x)\eta(x) + (1-1_{ h(x) = 1}(x))(1-\eta(x))\\ &= 1_{ h(x) = 1}(x)\left( 2\eta(x)-1 \right) +1-\eta(x). \end{align*}\] and the same is true if we replace \(h\) by \(h^\star\). Then, \[ P\left( h^\star(X) = Y \mid X=x\right) -P\left( h = Y \mid X=x\right) = \left(1_{ h^\star(x) = 1}(x) - 1_{ h(x) = 1}(x)\right)\left( 2\eta(x)-1 \right). \] Now if \(x\) is such that \(\eta(x)\geq 1/2\), then \[ \underbrace{\left(\underbrace{1_{ h^\star(x) = 1}(x)}_{=1} - \underbrace{1_{ h(x) = 1}(x)}_{= 0 \text{ or } 1}\right)}_{\geq 0}\underbrace{\left( 2\eta(x)-1 \right)}_{\geq 0}\geq 0. \] On the other hand, if \(x\) is such that \(\eta(x)< 1/2\), then \[ \underbrace{\left(\underbrace{1_{ h^\star(x) = 1}(x)}_{=0} - \underbrace{1_{ h(x) = 1}(x)}_{= 0 \text{ or } 1}\right)}_{\leq 0}\underbrace{\left( 2\eta(x)-1 \right)}_{< 0}\geq 0. \] In any case, this implies that \[ P\left( h^\star(X) = Y \mid X=x\right) -P\left( h = Y \mid X=x\right) \geq 0. \] Since this is true for any \(x\), it also holds that \[ P\left( h^\star(X) = Y \right) -P\left( h(X) = Y \right) \geq 0, \] as we claimed.

Since the Bayes classifier is not available in practice, one needs to take another approach. One is to estimate the function \(\eta(x)\) and then construct a classifier by plugging in to the Bayes classifier. That is, \[ h(x) = \text{sign} (\hat\eta(x) - 1/2). \] Logistic regression can be thought of as an example of this.

Another option is to find a function \(h\) that directly minimizes the in-sample error, \[ E_{in} = \frac{1}{n} \sum_{i=1}^n I(h(x_i)\neq y_i). \] It would be done by specifying a class of candidate classifiers \(\mathcal H\) from which we pick the best \(h\). For example, let us assume that \(x=(x^1,x^2,\ldots, x^p)\in \mathbb R^p\) and we might then consider all the linear classifiers, \[ \mathcal H = \left\{ h(x) = \text{sign}(\beta_0 + x^1\beta_1 + \ldots + x^p\beta_p) \mid \beta \in \mathbb R^p, \beta_0\in\mathbb R \right\}. \] Note that if \(y_i(\beta_0 + x^1\beta_1 + \ldots + x^p\beta_p)>0\), then \(y_i\) is classified correctly and if \(y_i(\beta_0 + x^1\beta_1 + \ldots + x^p\beta_p)<0\), then \(y_i\) is misclassified. We can therefore think of \(y_i(\beta_0 + x^1\beta_1 + \ldots + x^p\beta_p)\) as measuring how close \(y_i\) is to being classified correctly or incorrectly. In fact, we can write the 0-1 loss as \[ l(h(x),y) = I_{y(\beta_0 + x^1\beta_1 + \ldots + x^p\beta_p)\leq 0}(x,y). \] More generally we can consider a function \(f(x)\), a classifier \(h(x) = \text{sign}(f(x))\) and a loss function that depends on the margin \(yf(x)\).

It turns out that \(E_{in}\), as defined above, is difficult to use for training. The reason for this can be understood in different ways. Mathematically, the 0-1 loss is non-convex, and non-convex functions are in general difficult to optimize. In terms of classification, consider the picture below. Two points are misclassified, but we can see that by moving the classification boundary, we can find a classifier that only misclassifies one point. However, the training algorithm will try to move the boundary a very small step, and see if that gives an improvement. If we use the 0-1 loss, the in-sample error will be the same as long as the boundary is not moved far enough. Therefore it is better to use a loss function that also measures how far away each point is from being classified correctly/incorrectly.

Example of linear classification

Figure 4.1: Example of linear classification

Below we discuss two alternative loss functions that produce two much used methods for classification, the hinge loss and the negative log-likelihood. They are plotted in the picture below.
Loss functions for classification

Figure 4.2: Loss functions for classification

The hinge loss is \[ l(f(x),y)=(1-yf(x))_+, \] where \((\cdot)_+\) indicates the positive part. This function takes care of our complaints about the 0-1 loss function. If a point \(x_i\) is correctly classified, and it is far away from being misclassified, so that \(y_if(x_i)\) is large and positive, the loss is 0. However if it is close to being misclassified it incurs a loss, even if it is correctly classified. It might however worry some that we are using a different function for training (e.g. the hinge loss) and evaluation (0-1). Let us therefore see what the population minimizer of the hinge loss is. The population minimizer for the 0-1 loss is the Bayes classifier, and for the hinge loss, \[ f^\star_{hinge} := \underset{f}{\text{argmin}}~ E\left[(1-Yf(X))_+ \right]. \] Let us fix an arbitrary \(x\) and then we should find \(f(x)\) that minimizes \[ E\left[(1-Yf(x))_+ \mid X=x\right] = (1-f(x))_+\eta(x) + (1+f(x))_+(1-\eta(x)). \] We should always have \(-1\leq f(x)\leq 1\), because otherwise we could truncate \(f(x)\) to get a smaller loss. So with that assumption, \[ E\left[(1-Yf(x))_+ \mid X=x\right] = (1-f(x))\eta(x) + (1+f(x))(1-\eta(x)) = 1+(1-2\eta(x))f(x). \] Then we realize that depending on the sign of \(1-2\eta(x)\) we should choose \(f(x)\) to be as large positive or negative as possible, that is \[ f^\star_{hinge} = \begin{cases} 1&\text{if } \eta\geq 1/2\\ -1&\text{if } \eta< 1/2, \end{cases} \] which is exactly the Bayes classifier.

The other loss function we will discuss is the loss function of logistic regression. In logistic regression we model the conditional probability as \[ \eta(x) = \frac{1}{1+e^{-f(x)}}. \] The likelihood of the observation \((x,y)\) is therefore, \[ L(x,y) = \begin{cases} \eta(x)&\text{if } y=1\\ 1-\eta(x)&\text{if } y=-1. \end{cases} \] Then the negative log-likelihood (which should be minimized) is, \[ -\log_2(L(x,y)) = \begin{cases} \log_2\left(1+e^{-f(x)}\right)&\text{if } y=1\\ \log_2\left(1+e^{f(x)}\right)&\text{if } y=-1 \end{cases} = \log_2(1+e^{-yf(x)})=:l(f(x),y) \] Here we took base 2 logarithm since \(\log_2(1+e^0)=1\) and then the loss function is on the same scale as the hinge and 0-1. Also for logistic regression you can show that if you classify according to the class with highest probability, the population minimizer is again the Bayes classifier.