4.2 Support vector machines I
In this section will discuss binary classification and in particular support vector machines (SVM). The approach taken here is different from the one in ISL. Our approach is easier to explain, generalizes to other method and perhaps also more modern. The approach in ISL however provides a different intuition and is also relevant when implementing the algorithms.
We are given training examples \((x_i,y_i)\), where \(x_i = (x_i^1,x_i^2,\ldots,x_i^p) \in \mathbb R^p\) and \(y_i\in \left\{-1,1\right\}\). The equation \[ f(x):=\beta_0 + x^1\beta + \ldots + x^p\beta_p = 0, \] defines a hyperplane (e.g. a line in \(\mathbb R^2\), a plane in \(\mathbb R^3\)). We are going to classify as +1 if the point is on one side of the hyperplane, \(\beta_0 + x^1\beta + \ldots + x^p\beta_p>0\), and -1 if it is on the other side, \(\beta_0 + x^1\beta + \ldots + x^p\beta_p<0\). In other words, the classification rule is \[ h(x) = \text{sign}(\beta_0 + x^1\beta + \ldots + x^p\beta_p). \] The value of \(f(x_i)\) tells us how far away from the hyperplane the point is and if \(y_if(x_i)>0\) the point is classified correctly. That is, if \(y_if(x_i)\) is large and positive, the point \(x_i\) is classified correctly and with a safe margin. If \(y_if(x_i)\) is large and negative, the point is classified incorrectly and is far away from being correctly classified. We will consider the hinge loss \[ l(y,f) = (1-yf)_+ , \] here \((\cdot)_+\) indicates the positive part and we will minimize the in-sample error \[ \underset{\beta_0,\beta}{\text{minimize}}\quad \frac{1}{n}\sum_{i=1}^n (1-y_if(x_i))_+. \]
As an example, we generate some training data from a mixture of normal distributions.
library(mvtnorm)
set.seed(42)
mu.p <- rmvnorm(10,mean = c(1,0), sigma = diag(2))
mu.n <- rmvnorm(10,mean = c(0,1), sigma = diag(2))
n.samples <- 100
data.matrix <- matrix(nrow = 2*n.samples, ncol = 3)
for (i in seq_len(n.samples)) {
mu = mu.p[sample(x= nrow(mu.p), size = 1),]
sample <- rmvnorm(1, mean = mu, sigma = diag(2)/5)
data.matrix[2*i-1,] <- c(sample, 1)
mu = mu.n[sample(x= nrow(mu.n), size = 1),]
sample <- rmvnorm(1, mean = mu, sigma = diag(2)/5)
data.matrix[2*i,] <- c(sample, -1)
}
data.df <- data.frame(data.matrix)
colnames(data.df) <- c("x1","x2","y")
data.df$y <- as.factor(data.df$y)Then use R to calculate the hyperplane that minimizes the in-sample error.
library(kernlab)
svm.model <- ksvm(y~x1+x2, data = data.df,
type = "C-svc",
kernel = "vanilladot",
C = 1000)
grid <- expand.grid(x1 = seq(-5,5, length = 500), x2 = seq(-5,5, length = 500))
grid$predicted <- as.factor(predict(svm.model, grid))
Figure 4.3: Training data and linear classification with hinge loss
We can also calculate the in-sample error
## [1] 0.285
Not so bad, but let us try to improve it. We select some basis functions, \(\varphi_m(x)\), \(m=1,\ldots, M\) and use the same classifier but with input features \(\varphi(x) = (\varphi_1(x),\ldots, \varphi_M(x))\). We can for example choose \(\varphi_m\) to be polynomials of increasing order. For order 2, we get the classifier below, an ellipsoid.
library(kernlab)
svm.model <- ksvm(y~poly(x1, x2, degree = 2), data = data.df,
type = "C-svc",
kernel = "vanilladot",
C = 1000)
grid <- expand.grid(x1 = seq(-5,5, length = 500), x2 = seq(-5,5, length = 500))
grid$predicted <- as.factor(predict(svm.model, grid))
Figure 4.4: Training data and quadratic classification with hinge loss
This time the in-sample error is
## [1] 0.275
Better. Let us continue with increasing order polynomials, and calculate the error.
library(kernlab)
maxDegree <- 20
error.df <- data.frame("degree" = 1:maxDegree, inError = NA, outError = NA)
svm.model.list <- vector(mode = "list", length = maxDegree)
for (degree in seq(1,maxDegree)) {
svm.model.list[degree] <- ksvm(y ~ poly(x1, x2, degree = degree), data = data.df,
type = "C-svc",
kernel = "vanilladot",
C = 1000)
error.df$inError[degree] <- mean(data.df$y != predict(svm.model.list[[degree]], data.df))
}
Figure 4.5: In sample error vs. degree of polynomial, using hinge loss
In-sample error gets smaller as we increase the order of the polynomial. For degree 20, the in-sample error is
## [1] 0.03
The classifier looks complex.
Figure 4.6: Training data and degree 20 polynomial classification
Since we know the data generating distribution, we can approximate the out-of-sample error for each classifier, by simulation.
library(mvtnorm)
set.seed(42)
n.samples <- 1e4
data.matrix <- matrix(nrow = 2*n.samples, ncol = 3)
for (i in seq_len(n.samples)) {
mu = mu.p[sample(x= nrow(mu.p), size = 1),]
sample <- rmvnorm(1, mean = mu, sigma = diag(2)/5)
data.matrix[2*i-1,] <- c(sample, 1)
mu = mu.n[sample(x= nrow(mu.n), size = 1),]
sample <- rmvnorm(1, mean = mu, sigma = diag(2)/5)
data.matrix[2*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)
for (degree in seq(1,maxDegree)) {
error.df$outError[degree] <- mean(data.test.df$y != predict(svm.model.list[[degree]],
data.test.df))
}
Figure 4.7: Out-of-sample error vs. degree of polynomial.
The in-sample error is decreasing as the degree of the polynomial increases, and so also the complexity of the model. When the degree is small, the in-sample error is a close approximation of the out-of-sample error, but as the degree increases the difference increases and for large degrees the in-sample error provides little information about the out-of-sample error. The out-of-sample error is at first decreasing, but unlike the in-sample-error, starts increasing after degree 4.
In the following sections we will investigate the connection between the in-sample and out-of-sample error.