4.4 Generalization error

In this section we will study the generalization error, that is the difference between \(E_{out}(h)\) and \(E_{in}(h)\).

We will only consider binary classification and we will assume that there is a function \(h\) such that \(Y=h(X)\). This is a simplification since it means that we only need to consider the distribution of \(X\). To make things concrete we study an example where \(\mathcal X = \mathbb R^2\) and a linear classification, where observations are classified according to which side of a straight line they fall.

Before looking at the data, let us choose a classification algorithm completely willy-nilly. Let us choose: \[ h(x_1,x_2)=\begin{cases} 1 \text{ if } x_1 >0\\ -1 \text{ otherwise}. \end{cases} \] Now we generate some data:
Training data and classification rule

Figure 4.8: Training data and classification rule

We can now calculate the in-sample error:

H <- function(a, b){
  function(x1, x2){
    if(a*x1 + b*x2 > 0) "1"
    else "-1"
  }
}

h <- H(1,0)

error <- function(data, classifier){
  missClass <- 0
  for (i in seq_len(nrow(data))) {
    x1 <- data[i,]$x1
    x2 <- data[i,]$x2
    y <- data[i,]$y
    if(classifier(x1,x2) != y )
      missClass <- missClass + 1
  }
  missClass/nrow(data)
}

error(data.df, h)
## [1] 0.32

In this case, \(h\) is not a function of the training data so the in-sample error is an unbiased estimate of the out-of-sample error. Since we know the distribution of \(X,Y\) we can calculate the out-of-sample error by simulation.

n.samples <- 1e4

data.matrix <- matrix(nrow = n.samples, ncol = 3)

for (i in seq_len(n.samples)) {
  if (runif(1)>0.5) {
    sample <- rmvnorm(1, mean = mu.p, sigma = diag(sd.p))
    data.matrix[i,] <- c(sample, 1)
  }
  else{
    sample <- rmvnorm(1, mean = mu.n, sigma = diag(sd.n))
    data.matrix[i,] <- c(sample, -1)
  }
}

data.test.df <- data.frame(data.matrix)
colnames(data.test.df) <- c("x1","x2","y")
data.test.df$y <- as.factor(data.test.df$y)
error(data.test.df, h)
## [1] 0.3548

Using Hoeffding’s inequality, we can give a guarantee of the difference between the in-sample and the out-of-sample error. Let us review: A pair \(X,Y\) is drawn from some distribution. We then apply the function \(h\) and with some probability we make an error on the classification, this probability is \(E_{out}(h)\) and corresponds to the probability \(p\) in Hoeffding. What we have is a sample of size \(n\) and an estimate \(E_{in}(h)\) of the probability of an error. This corresponds to \(\bar Y_n\) in Hoeffding. Therefore this situation is exactly like the Bernoulli experiment from the previous section. We can say that with probability at least \(1-\delta\), \[ E_{out}(h) \leq E_{in}(h) + \sqrt{\frac{\ln \frac{2}{\delta}}{2n}}. \] Let us say we want to have confidence 95%, that is \(\delta = 0.05\), we then have the generalization bound

delta = 0.05
error(data.df,h) + sqrt(log(2/delta)/(2*nrow(data.df)))
## [1] 0.5120646

That is, with confidence 95%, the out-of-sample error is not larger than this.

Above we picked an \(h\) without looking at the data and so it can not really be considered statistical learning. Therefore, for the generalization bound to be useful, we need to handle the situation where \(h\) is chosen from some collection \(\mathcal H\). Let us first consider the case where \(\mathcal H\) is finite, that is there is a finite number of functions \(h\) in the collection. Call this number \(|\mathcal H|\). The statement of the learning bound then becomes

Let \(\mathcal H\) be finite. Then for any \(\delta>0\), with probability at least \(1-\delta\): \[ \forall h\in\mathcal H,\quad E_{out}(h)\leq E_{in}(h) + \sqrt{\frac{ \ln\frac{2|\mathcal H|}{\delta}}{2n}}. \]

The proof of this is again an application of Hoeffding. Let \(h_1,\ldots,h_{|\mathcal H|}\) be the elements of \(\mathcal H\). Then \[\begin{align} &P\left( \exists h\in\mathcal H : \left| E_{in}(h) - E_{out}(h) \right|>\varepsilon \right)\\ =& P\left( \left\{ \left| E_{in}(h_1) - E_{out}(h_1) \right| > \varepsilon \right\} \cup \ldots \cup \left\{ \left| E_{in}(h_{|\mathcal H |}) - E_{out}( (h_{| \mathcal H |}) \right| > \varepsilon \right\} \right)\\ \leq & \sum_{i=1}^{|\mathcal H|} P\left(\left| E_{in}(h_i) - E_{out}(h_i) \right|>\varepsilon \right)\\ \leq & 2 |\mathcal H | e^{-2n\varepsilon^2}. \end{align}\] Setting this equal to \(\delta\) and solving for \(\varepsilon\) gives the result.

Now we can handle also the case with a finite number of \(h\). But it is still not very useful. Most classifications methods will have an infinite \(\mathcal H\). For example, in the example above, we would consider all different straight lines and no only \(x_1 = 0\). The above argument will not work for infinite \(\mathcal H\). There is however some hope. We used that, for two events \(A\) and \(B\), \[ P(A\cup B) = P(A) + P(B) - P(A\cap B) \leq P(A) + P(B). \] But this inequality is not very tight. When \(A\) and \(B\) tend to happen at the same time \(P(A \cup B)\approx P(A)\) and so the right hand side will be roughly twice the left hand side. We can expect this to happen also for the generalization errors. That is, many \(h\) in \(\mathcal H\) are similar, so if \(\left| E_{in}(h_1) - E_{out}(h_1) \right|>\varepsilon\) it is likely that also \(\left| E_{in}(h_2) - E_{out}(h_2) \right|>\varepsilon\).