The data

◆ The Blueprint
The widest band

A line is the set of points where \(f(x) = w_1x_1 + w_2x_2 - \rho = 0\). Write \(w\cdot x = w_1x_1 + w_2x_2\), and \(\|w\| = \sqrt{w_1^2 + w_2^2}\) for the length of \(w\). Multiplying \(w\) and \(\rho\) by the same number does not move the line, so their size is fixed by requiring the nearest points to have \(f(x) = \pm 1\). The distance from the line to those points is then \(1/\|w\|\), so the widest band has the smallest \(\|w\|\). With \(y_i = +1\) for blue and \(-1\) for ember, it solves

$$\min_{w,\,\rho}\ \tfrac12\|w\|^2 \quad\text{subject to}\quad y_i\,(w\cdot x_i - \rho) \ge 1 \ \text{ for every point } i.$$
Points inside the band

The slack \(\xi_i = \max(0,\ 1 - y_i f(x_i))\) is 0 for a point outside the band on its own side, between 0 and 1 for a point inside the band on its own side, and above 1 for a point rated incorrectly. With slack allowed, the SVM solves

$$\min_{w,\,\rho}\ \tfrac12\|w\|^2 + \text{cost}\sum_i \xi_i .$$

As cost increases, the margin shrinks or stays the same. When some line separates the classes, a large enough cost gives the widest separating band. For the 24 points, every cost above about 1.6 gives that band; the first fit uses cost = 100.

Why only the support vectors matter

The solution is a weighted sum of the points, \(w = \sum_i \alpha_i y_i x_i\) with \(0 \le \alpha_i \le \text{cost}\). The support vectors are the points with \(\alpha_i > 0\): those on the band's edges, inside the band, or rated incorrectly. Every other point has \(\alpha_i = 0\), so removing it does not change \(w\) or \(\rho\). The name comes from the separable case: each point is a vector of its measurements, and when a line separates the classes, the band's edges pass through the support vectors and through no other point.

In R

svm(y ~ x1 + x2, data = d, kernel = "linear", cost = 100, scale = FALSE) fits the band. fit$index lists the support vectors, and t(fit$coefs) %*% fit$SV and fit$rho give \(w\) and \(\rho\). The default scale = TRUE standardizes each column before fitting; scale = FALSE keeps \(w\) in the data's units. Part 1 of The Script.

The rings

Distance from the center

◆ The Blueprint
The feature map

A feature map turns each point's measurements into a longer list of features. The map used here adds one, the squared distance from the center:

$$\varphi(x_1, x_2) = (x_1,\ x_2,\ x_1^2 + x_2^2).$$

A linear SVM on the three features fits \(f = w_1x_1 + w_2x_2 + w_3z - \rho\), and its boundary \(f = 0\) is a plane.

From the plane to the circle

Every lifted point is on the bowl \(z = x_1^2 + x_2^2\). Substituting that into \(f = 0\), dividing by \(w_3\), and completing the square gives

$$\Big(x_1 + \frac{w_1}{2w_3}\Big)^2 + \Big(x_2 + \frac{w_2}{2w_3}\Big)^2 = \frac{\rho}{w_3} + \frac{w_1^2 + w_2^2}{4w_3^2},$$

a circle centered at \((-w_1/2w_3,\ -w_2/2w_3)\). The band's edges, \(f = \pm 1\), are circles with the same center:

The fit

The line of step 1 and the plane both use cost = 1, e1071's default.

Why the squared distance

Any feature that grows with the distance from the center would separate these rings. The squared distance makes the boundary a circle, and the dot product of two lifted points has a short formula: \(\varphi(a)\cdot\varphi(b) = a\cdot b + \|a\|^2\|b\|^2\).

In R

train$z <- train$x1^2 + train$x2^2 adds the feature, and svm(ring ~ x1 + x2 + z, data = train, kernel = "linear", cost = 1, scale = FALSE) fits the plane. Part 2 of The Script.

The lifted points

The two points
◆ The Blueprint
The fit uses the data only through dot products

The SVM's optimization problem can be rewritten with a weight \(\alpha_i\) for each point in place of \(w\) (the dual problem):

$$\max_{\alpha}\ \sum_i \alpha_i - \tfrac12\sum_i\sum_j \alpha_i\alpha_j\,y_iy_j\;x_i\cdot x_j \quad\text{subject to}\quad 0 \le \alpha_i \le \text{cost},\ \ \sum_i \alpha_i y_i = 0,$$

and a new point \(x\) is rated by \(f(x) = \sum_i \alpha_i y_i\, x_i\cdot x - \rho\). The points appear only inside dot products. Fitting on lifted points replaces each \(x_i\cdot x_j\) with \(\varphi(x_i)\cdot\varphi(x_j)\), and a kernel computes that number from \(x_i\) and \(x_j\) directly. The coefficient \(c_i\) is \(\alpha_i y_i\).

The kernel used here
$$K(a, b) = \varphi(a)\cdot\varphi(b) = a_1b_1 + a_2b_2 + (a_1^2 + a_2^2)(b_1^2 + b_2^2) = a\cdot b + \|a\|^2\|b\|^2$$
Which functions are kernels

A function \(K\) is a positive semidefinite kernel when, for any set of points, the matrix of values \(K(x_i, x_j)\) is symmetric, \(K(a, b) = K(b, a)\), and positive semidefinite, \(\sum_i\sum_j u_iu_jK(x_i, x_j) \ge 0\) for every list of numbers \(u\). For every such \(K\), some feature map \(\varphi\) gives \(K(a, b) = \varphi(a)\cdot\varphi(b)\), and \(\varphi\) can have infinitely many features, as the radial kernel's does.

Polynomial features

\((a\cdot b + 1)^2 = \varphi(a)\cdot\varphi(b)\) with \(\varphi(a) = (1,\ \sqrt2 a_1,\ \sqrt2 a_2,\ a_1^2,\ \sqrt2 a_1a_2,\ a_2^2)\). With \(p\) predictors, degree \(d\) has \(\binom{p+d}{d}\) features: 6 and 10 here, and 176,851 for degree 3 with 100 predictors. Computing \(K\) still takes one dot product of length \(p\), an addition, and a power.

Radial features

\(e^{-\gamma\|a-b\|^2} = e^{-\gamma\|a\|^2}\,e^{-\gamma\|b\|^2}\,e^{2\gamma\, a\cdot b}\), and \(e^{2\gamma\, a\cdot b} = \sum_{k\ge 0} (2\gamma\, a\cdot b)^k / k!\) is a weighted sum of polynomial kernels of every degree. Its feature map therefore has infinitely many features.

In R

e1071 accepts only its built-in kernels. kernlab::ksvm() also accepts a matrix of kernel values, so part 3 of The Script fits the rings from \(K\) alone, with no \(z\). It finds the same five support vectors, and its decision values agree with the plane's to within , a difference that comes from the two solvers' stopping tolerance.

The boundary

The decision surface

Fits so far
◆ The Blueprint
e1071's kernels
$$\text{linear: } a\cdot b \qquad \text{polynomial: } (\gamma\, a\cdot b + c_0)^{d} \qquad \text{radial: } e^{-\gamma\|a-b\|^2}$$

e1071 also offers the sigmoid kernel, \(\tanh(\gamma\, a\cdot b + c_0)\). The polynomial fits use \(\gamma = 1\) and \(c_0 = 1\) (coef0), and every fit uses cost = 1. e1071's defaults are \(\gamma = 1/(\text{number of predictors})\), \(c_0 = 0\), degree 3, cost 1, and scale = TRUE.

The decision surface

Every fit rates a point by \(f(x) = \sum_i c_i K(x_i, x) - \rho\), a sum over the support vectors. The surface plots \(f\) above the \(x_1\)–\(x_2\) plane, and the boundary is the curve where \(f = 0\). Each surface has its own vertical scale, marked on its axis. Rescaling changes a surface's height but not its shape or where it crosses 0. Over the plotted square, the largest \(|f|\) is .

Boundary shapes by kernel

With the linear kernel, \(f\) is a plane and the boundary is a line. With a polynomial kernel of degree \(d\), \(f\) is a polynomial of degree \(d\) in \(x_1\) and \(x_2\): with degree 2 the boundary can be an ellipse, a parabola, or a hyperbola, and with degree 3 it can also be S-shaped. With the radial kernel, each term of \(f\) is a bell centered at a support vector with height \(c_i\), so the boundary can bend around single support vectors, and gamma sets how tightly it bends.

Gamma's distance scale

The radial kernel falls to \(e^{-1} \approx 0.37\) at distance \(1/\sqrt{\gamma}\): 3.2 for \(\gamma = 0.1\), 1 for \(\gamma = 1\), 0.32 for \(\gamma = 10\), and 0.1 for \(\gamma = 100\). At \(\gamma = 100\) a support vector affects \(f\) only very near itself, so the boundary encloses small groups of training points.

In R

svm(y ~ x1 + x2, data = train, kernel = "radial", gamma = 1, cost = 1, scale = FALSE) fits the radial SVM with gamma 1, and kernel = "polynomial", degree = 3, gamma = 1, coef0 = 1 gives the polynomial of degree 3. predict(fit, newdata, decision.values = TRUE) returns the decision values \(f(x)\). Part 4 of The Script.

Cross-validation error

The boundary

Held-out error of the cells you checked

◆ The Blueprint
10-fold cross-validation

The 300 training points are shuffled and cut into 10 folds of 30. For each setting of cost and gamma, the SVM is fit 10 times, each time on nine folds, and the remaining fold's points are rated. The cell shows the share of the 300 points rated incorrectly when their own fold was left out.

$$\text{cross-validation error} = \frac{1}{10}\sum_{k=1}^{10} \text{error on fold } k$$
Why the chosen cell's error goes up on new points

Each cross-validation error is an estimate with its own noise, and the lowest of 35 noisy estimates tends to be low by chance. The held-out points played no part in the choice, so their error rate is an unbiased estimate of how the chosen SVM does on new points.

Errors above 50%

Two cells, cost 0.1 with gamma 30 and with gamma 100, have cross-validation errors above 50%. At cost 0.1 no coefficient is larger than 0.1 in size, and the bells are narrow, so in most folds the fit rates every left-out point as whichever class has more points in the nine training folds. When a fold has more points of one class, the nine training folds have more of the other, so most of that fold is rated incorrectly. Fit to all 300 training points, 150 of each class, the same two settings rate 16.9% and 34.5% of the held-out points incorrectly.

The simulation

The data are two moons as on the Kernels tab, with noise standard deviation 0.35 before the moons are scaled by 1.6. set.seed(12) gives the 300 training points, and the draws that follow give the 1,000 held-out points. The folds come from set.seed(112).

In R

tu <- tune.svm(y ~ x1 + x2, data = train, kernel = "radial", gamma = c(0.1, 0.3, 1, 3, 10, 30, 100), cost = c(0.1, 1, 10, 100, 1000), scale = FALSE) runs 10-fold cross-validation over the grid. tu$performances contains every cell's error, and tu$best.parameters gives the cost and gamma with the lowest. Part 5 of The Script.

e1071, function by function
CallWhat it returns
svm(y ~ x1 + x2, kernel = "radial", gamma = 1, cost = 1, scale = FALSE)y must be a factor for classification. Kernels: "linear", "polynomial" (with degree, gamma, coef0), "radial" (with gamma), and "sigmoid".
fit$index, fit$SV, fit$coefs, fit$rhoThe support vectors' row numbers and coordinates, their coefficients \(c_i\), and \(\rho\).
predict(fit, newdata, decision.values = TRUE)Predicted classes, with \(f(x)\) in the attribute "decision.values". The column is named for the two classes, such as inner/outer: a positive \(f(x)\) rates a point as the first class named.
tune.svm(y ~ ., data, gamma = ..., cost = ...)10-fold cross-validation over every combination; summary() prints the grid of errors.
kernlab::ksvm(as.kernelMatrix(K), y, C = 1)An SVM fit from a matrix of kernel values, with no coordinates at all. Its C is e1071's cost.
svm_in_R.R