Support Vector Machines from First Principles

A self-contained guide to one of the best off-the-shelf classifiers in machine learning.

Support Vector Machines (SVMs) were first proposed by Vladimir Vapnik and Alexey Chervonenkis in the mid-1960s and have since evolved into one of the most flexible and effective machine learning tools available [1]. At their core, SVMs rest on some genuinely beautiful mathematics, and in this post, we’ll walk through it together in manageable pieces. This post aims to be self-contained: detailed enough that you can learn about the topic in its entirety without juggling multiple resources, yet concise enough so you can avoid the trap of minutiae.

Use the table of contents below to navigate the post:

1  Hard-margin SVM

1.1  Intuition of margin

Consider a training dataset \(\mathcal{D} = \big \{ (x_i, y_i) \big \}_{i=1}^M\), where $x_i \in \mathbb{R}^D$ is the $i$-th feature vector, and $y_i \in \{+1,-1\}$ is its binary label. A hyperplane splits this $D$-dimensional feature space into two half-spaces. If we assume (for now) that our dataset is linearly separable, there are infinitely many valid hyperplanes. We would like to estimate the optimal separating hyperplane that will correctly classify the two classes with the smallest classification error. The SVM prefers a hyperplane that does not favor any particular class. Geometrically, this corresponds to choosing a hyperplane that maximizes the distance between the hyperplane and the closest data points from both classes [2]. Intuitively, a hyperplane that scrapes right past our data is sensitive to small perturbations, while one with a wide buffer zone generalizes better.

Geometry of a linear SVM.
Figure 1: The separating hyperplane (solid) maximizes the margin $\gamma$ between the two classes. The support vectors (circled) from each class sit right on the edge of the margin.

The data points that lie closest to the decision surface (or hyperplane) are called support vectors, and the distance from the hyperplane to the support vectors in either class defines the margin.

1.2  Optimal margin classifier

In a $D$-dimensional space, a hyperplane is defined as follows:

$$ \begin{align} h(x) &=w^Tx+b \tag{1}\\ &=w_1x_1 + w_2x_2+ \cdots + w_Dx_D+b \notag \end{align} $$

where $w \in \mathbb{R}^D$ is the normal vector orthogonal to the hyperplane, and $b \in \mathbb{R}$ is the bias term. For points lying on the hyperplane, we have

$$ h(x)=w^Tx+b = 0 \tag{2} $$

Recall that a hyperplane $h(x)$ divides the original $D$-dimensional space into two half-spaces, such that every point labeled $y_i = +1$ satisfies $h(x_i) > 0$, and every point labeled $y_i = -1$ satisfies $h(x_i) < 0$. As such, the hyperplane $h(x)$ acts as a linear classifier that predicts the label $y$ of a point $x$ according to the following decision rule:

$$ y = \begin{cases} +1 &\quad \text{if } h(x) > 0 \tag{3}\\ -1 &\quad \text{if } h(x) < 0 \end{cases} $$

Meanwhile, consider a point $x \in \mathbb{R}^D$ such that it does not lie on the hyperplane. Further let $x_p$ be the projection of $x$ onto the hyperplane along the normal vector $w$. Let $\gamma \gt 0$ be the distance from point $x$ to $x_p$ on the hyperplane.

Distance of a point to the hyperplane
Figure 2: Illustration of the directed distance $\gamma$ from a point $x$ to the hyperplane.

Since $w$ is the normal vector, the distance $\gamma$ is simply a scaling of $w$. Let’s assume that $w$ is of unit length $\lVert w \rVert = 1$, so following vector addition we can write:

$$ x = x_p + \gamma \frac{w}{\lVert w \rVert} \tag{4} $$

Here $\gamma$ gives the directed distance of the point $x$ from $x_p$ in terms of the unit weight vector $\frac{w}{\lVert w \rVert}$. Note that $\gamma$ is positive when it points in the same direction as $w$, and negative when it points in the opposite direction.

Substituting Eq. (4) into the hyperplane Eq. (1) yields

$$ \begin{align} h(x) &= w^T(x_p + \gamma \frac{w}{\lVert w \rVert})+b \notag\\ &= \underbrace{w^Tx_p + b}_{\text{0}} + \gamma \frac{w^Tw}{\lVert w \rVert} \notag \\ &= \gamma {\lVert w \rVert} \tag{5} \\ \end{align} $$

From Eq. (5), we can express the directed distance from the point to the hyperplane as

$$ \gamma = \frac {h(x)} {\lVert w \rVert} \tag{6} $$

Since the distance must be nonnegative, we can multiply $\gamma$ by the point’s label $y$, which is $+1$ when $h(x) > 0$ and $-1$ when $h(x)<0$. Thus, the distance of a point $x$ from the hyperplane $h(x)$ is given as

$$ \tilde{\gamma} = \frac{y h(x)}{\lVert w \rVert} \tag{7} $$

Now, if we choose $x$ to be the point closest to the hyperplane, the distance $\tilde\gamma$ is the margin. We define the smallest distance between the hyperplane and all training points as

$$ \gamma^{*} = \min_{i} {\tilde{\gamma}_i} = \min_{i} \frac {y_i(w^T x_i + b)} {\lVert w \rVert} \tag{8} $$

Note that scaling $w$ and $b$ in the hyperplane Eq. (2) by the same constant $c>0$ does not change the hyperplane. Hence, we scale $(w, b)$ by the canonical normalization such that

$$ \min_{i} y_i(w^T x_i + b) = 1 \tag{9} $$

A support vector satisfies $y_i(w^T x_i + b)=1$, while any point that is not a support vector satisfies $y_i(w^T x_i + b) > 1$. The geometric margin is $\gamma^{*} = \frac{1}{\lVert w \rVert}$ and the width of the margin band is $\frac{2}{\lVert w \rVert}$.

With the canonical normalization above, we can pose the following constrained optimization problem:

$$ \max_{w,b} \; \gamma^{*} \quad \iff \quad \max_{w,b} \; \frac{1}{\lVert w \rVert} \tag{10} $$ $$ \text{s.t.}\quad y_i(w^T x_i + b) \ge 1,\quad \forall\, i=1,\ldots,M $$

Maximizing the problem in Eq. (10) is equivalent to minimizing the following problem:

$$ \min_{w,b} \; {\lVert w \rVert} \tag{11} $$ $$ \text{s.t.}\quad y_i​(w^Tx_i​+b)≥1, \quad \forall\, i=1,\ldots,M $$

Rather than minimizing the $l_2$-norm in Eq. (11), we often choose to minimize the squared norm because it yields a strictly convex quadratic objective. Thus, the new optimization problem becomes

$$ \min_{w,b} \; {\lVert w \rVert}^2 \tag{12} $$ $$ \text{s.t.}\quad y_i​(w^Tx_i​+b)≥1, \quad \forall\, i=1,\ldots,M $$

The SVM formulation in Eq. (12) is known as the primal optimization problem. The duality principle implies that an optimization problem may be viewed from either of two perspectives, the primal problem or the dual problem. In the next section, we will see how to transform the primal problem in Eq. (12) into an equivalent dual optimization problem.

2.  Duality of SVM

2.1  Primal and dual problems

2.1.1  Lagrangian

Consider a general optimization problem (the primal problem) defined as

$$ \min_{x} \; f_0(x) \tag{13} $$ $$ \text{s.t.}\quad f_i(x) \le 0,\quad \forall\, i=1,\ldots,M $$ $$ \qquad h_i(x) = 0,\quad \forall\, i=1,\ldots,P $$

where $x \in \mathbb{R}^N$ is the optimization variable, $f_0(x)$ is the objective function, $f_i(x) \le 0$ are $M$ inequality constraints, and $h_i(x) = 0$ are $P$ equality constraints.

The general idea in Lagrangian duality is to take the problem’s constraints into account by augmenting the objective function with a weighted sum of the constraint functions. We define the Lagrangian associated with the problem in Eq. (13) as

$$ \mathcal{L}(x,\alpha, \beta)= f_0(x) + \sum_{i=1}^{M}\alpha_if_i(x)+ \sum_{i=1}^{P}\beta_ih_i(x) \tag{14} $$

We refer to $\alpha_i$ as the Lagrange multiplier associated with the $i$-th inequality constraint $f_i(x) \le 0$, and $\beta_i$ as the Lagrange multiplier associated with the $i$-th equality constraint $h_i(x) = 0$.

The optimal value of the primal problem, denoted by $p^{*}$, is given by

$$ p^* = \inf_x \sup_{\alpha \ge 0, \beta} \mathcal{L}(x,\alpha, \beta) \tag{15} $$

2.1.2  Lagrange dual function

Given a Lagrangian, we define its Lagrange dual function as

$$ \begin{align} g(\alpha, \beta) &= \inf_x \mathcal{L}(x,\alpha, \beta) \notag \\ &= \inf_x \; \Biggl\{ f_0(x) + \sum_{i=1}^{M}\alpha_if_i(x)+ \sum_{i=1}^{P}\beta_ih_i(x) \Biggr\} \tag{16} \end{align} $$

The Lagrange dual function is the pointwise infimum of a family of affine functions of $(\alpha, \beta)$, and thus it is concave even when the original problem in Eq. (13) is nonconvex.

2.1.3  Lagrange dual problem

Given the primal problem in Eq. (13), we define its Lagrange dual problem as

$$ \max_{\alpha, \beta} \; g(\alpha, \beta) \tag{17} $$ $$ \text{s.t.}\quad \alpha_i \ge 0, \quad \forall\, i=1,\ldots,M $$

The optimal value of the dual problem, denoted by $d^{*}$, is given by

$$ d^* = \sup_{\alpha \ge 0, \beta} \inf_x \mathcal{L}(x,\alpha, \beta) \tag{18} $$

The dual problem is always a convex optimization problem since the objective to be maximized is concave and the constraint is convex [3]. This is the case whether or not the primal problem in Eq. (13) is convex.

📖 Terminology

Concave maximization over a convex set is convex optimization.

2.2  Weak and strong duality

The optimal value of the Lagrange dual problem $d^{*}$ gives us the best lower bound on the optimal value $p^{*}$ of the primal problem, i.e.,

$$ d^{*} \le p^{*} \tag{19} $$

which holds for both convex and nonconvex problems. This property is called weak duality.

The difference $p^{*} - d^{*}$ is referred to as the optimal duality gap, which is always nonnegative [3]. If the optimal duality gap is zero, then we say that strong duality holds, i.e.,

$$ d^{*} = p^{*} \tag{20} $$

If strong duality holds, solving the dual is equivalent to solving the primal [4]. However, strong duality does not hold in general, but we usually (but not always) have strong duality if the primal problem is convex, i.e., of the form

$$ \min_{x} \; f_0(x) \tag{21} $$ $$ \begin{align} \text{s.t.}\quad & f_i(x) \le 0,\quad \forall\, i=1,\ldots,M \notag \\ & Ax=b \notag \end{align} $$

where $f_0, \ldots, f_M$ are convex functions. Beyond convexity, the conditions which guarantee strong duality are called constraint qualifications.

📝 Note

Convexity + Constraint Qualifications ⟹ Strong Duality

One simple but well-known constraint qualification is Slater’s condition: there exists an $x \in \operatorname{relint} D$ such that

$$ f_i(x) < 0, \quad \forall\, i=1,\ldots,M, \quad Ax=b \tag{22} $$

Such a point is often referred to as strictly feasible, since the inequality constraints hold with strict inequalities. Slater’s theorem states that strong duality holds if Slater’s condition holds and the optimization problem is convex.

Slater’s condition in Eq. (22) can be refined when some of the inequality constraint functions $f_i$ are affine. If the first $K$ constraint functions $f_1, \ldots, f_K$ are affine, then strong duality holds provided the following weaker condition holds: there exists an $x \in \operatorname{relint} D$ with

$$ f_i(x) \le 0, \quad \forall\, i=1,\ldots,K, \qquad f_i(x) < 0, \quad \forall\, i=K+1,\ldots,M, \qquad Ax=b \tag{23} $$

Detailed proof of the Slater’s theorem can be found in Boyd and Vandenberghe’s Convex Optimization book (see Sec. 5.3.2, pp. 234-236).

2.3  Karush-Kuhn-Tucker (KKT) conditions

For general problems: for an optimization problem with differentiable objective and constraint functions, if strong duality holds, any pair of primal and dual optimal points must satisfy the following KKT conditions:

$$ \begin{align} f_i(x) &\le 0, &&\forall\, i = 1,\ldots,M \tag{24}\\ h_i(x) &= 0, &&\forall\, i = 1,\ldots,P \tag{25}\\ \alpha_i &\ge 0, &&\forall\, i = 1,\ldots,M \tag{26}\\ \alpha_i f_i(x) &= 0, &&\forall\, i = 1,\ldots,M \tag{27}\\ \nabla f_0(x) + \sum_{i=1}^{M}\alpha_i\nabla f_i(x) + \sum_{i=1}^{P}\beta_i\nabla h_i(x) &= 0 \tag{28} \end{align} $$

For convex problems: for a convex optimization problem with differentiable objective and constraint functions, any points that satisfy the KKT conditions are primal and dual optimal, and have zero duality gap, i.e., $p^{*}=d^{*}$.

For convex problems satisfying Slater’s condition: for a convex optimization problem with differentiable objective and constraint functions that satisfies Slater’s condition, the KKT conditions provide necessary and sufficient conditions for optimality.

2.4  SVM dual form

Let’s revisit the primal SVM formulation in Eq. (12), which can be equivalently expressed as the following constrained problem:

$$ \min_{w,b} \; \frac{1}{2}{\lVert w \rVert}^2 \tag{29} $$ $$ \text{s.t.}\quad -y_i​(w^Tx_i​+b) + 1\le 0, \quad \forall\, i=1,\ldots,M $$

where we introduce a constant $\frac{1}{2}$, which does not affect the optimal $w, b$ but simplifies our later derivations. This is a differentiable, convex optimization problem that satisfies Slater’s condition; therefore, strong duality $(p^{*}=d^{*})$ holds, and the KKT conditions are necessary and sufficient for optimality.

We first define the Lagrangian of the primal SVM problem as

$$ \mathcal{L}(w,b,\alpha) = \frac{1}{2}{\lVert w \rVert}^2 - \sum_{i=1}^{M}\alpha_iy_i​(w^Tx_i​+b) + \sum_{i=1}^{M}\alpha_i \tag{30} $$

Recall that the Lagrange dual function can be formulated as

$$ \begin{align} g(\alpha) &= \inf_{w,b} \mathcal{L}(w, b,\alpha) \notag \\ &= \inf_{w,b} \; \Biggl\{ \frac{1}{2}{\lVert w \rVert}^2 - \sum_{i=1}^{M}\alpha_iy_i​(w^Tx_i​+b) + \sum_{i=1}^{M}\alpha_i \Biggr\} \tag{31} \end{align} $$

In order to obtain the extremum of the Lagrangian with respect to $w$ and $b$, we compute the gradient of $\mathcal{L}(w,b,\alpha)$ with respect to $w$ and $b$, and set them to zero,

$$ \nabla_w \mathcal{L}(w,b,\alpha) = w - \sum_{i=1}^{M}\alpha_iy_i​x_i = 0 \tag{32} $$

and we thus have

$$ w = \sum_{i=1}^{M}\alpha_iy_i​x_i \tag{33} $$

Similarly,

$$ \frac{\partial\mathcal{L}}{\partial b}(w,b,\alpha) = \sum_{i=1}^{M}\alpha_iy_i​ = 0 \tag{34} $$

Plugging Eq. (33) and Eq. (34) into Eq. (31), we get

$$ g(\alpha) = \inf_{w,b} \mathcal{L}(w,b,\alpha) = \sum_{i=1}^{M}\alpha_i - \frac{1}{2}\sum_{i=1}^{M}\sum_{j=1}^{M}\alpha_i\alpha_j y_i y_j (x_i^T x_j) \tag{35} $$

Hence, the dual problem can be written as

$$ \max_{\alpha} \; g(\alpha) = \sum_{i=1}^{M}\alpha_i - \frac{1}{2}\sum_{i=1}^{M}\sum_{j=1}^{M}\alpha_i\alpha_j y_i y_j (x_i^T x_j) \tag{36} $$ $$ \text{s.t.}\quad \begin{cases} \alpha_i \ge 0, \quad \forall\, i = 1, \ldots, M \\ \displaystyle\sum_{i=1}^{M} \alpha_i y_i = 0 \end{cases} $$

This is a convex quadratic programming (QP) problem that can be efficiently solved with the Sequential Minimal Optimization (SMO) algorithm. Exactly how to solve such a QP problem is beyond the scope of this post. The dual optimization problem is introduced here mainly because it is easy to kernelize.

However, recall that according to the KKT condition in Eq. (27), we have

$$ \alpha_i(-y_i​(w^Tx_i​+b) + 1) = 0 \tag{37} $$

which gives rise to two scenarios:

This is a very important insight, since if $\alpha_i > 0$, then $y_i​(w^Tx_i​+b) = 1$, i.e., the training data points $x_i$ whose $\alpha_i$’s are nonzero are the support vectors. Once we have the Lagrange multipliers $\{\alpha_i\}$, we can reconstruct the weight vector $w$ as a linear combination of support vectors

$$ w = \sum_{i \in \mathcal{S}}\alpha_iy_i​x_i \tag{38} $$

where $\mathcal{S}$ denotes the set of the indices of the support vectors. This “sparse” representation is advantageous for making new predictions since the final classifier depends only on the small subset of data points $x_i$ with $\alpha_i > 0$, i.e., the support vectors.

SVM Classifier: For any new point $z$, we predict the class as

$$\begin{align} \hat{y}&=\operatorname{sign}​(w^Tz​+b) \notag\\ \hat{y}&=\operatorname{sign}​\biggl(\sum_{i \in \mathcal{S}}\alpha_iy_i(​x_i^Tz​)+b\biggr) \tag{39} \end{align} $$

where the $\operatorname{sign}$(·) function returns $+1$ for any positive argument, and $-1$ for any negative argument.

3  Kernels

3.1  Feature maps

Imagine a binary classification problem as shown in Fig. 3(a). Each data sample is represented by a single feature $x \in \mathbb{R}$, and the dataset cannot be separated by a linear hyperplane. How can we extend the linear SVM model to solve such a nonlinear problem?

To make life easier, we can transform each data sample to a higher-dimensional feature space where it is more likely to exhibit linear patterns. In our classification problem, for example, we map each sample from the $1$-dimensional input space into a $2$-dimensional feature space via the feature mapping:

$$ \phi: \mathbb{R} \rightarrow \mathbb{R}^2, \quad x \mapsto \begin{bmatrix}x \\ x^2\end{bmatrix} \tag{40} $$

where $\phi$ is a feature map, which maps the original input attributes to the features. As shown in Fig. 3(b), each sample now has two features, and we can find a linearly separable hyperplane in the new feature space.

Mapping nonseparable 1D data to a linearly separable higher-dimensional feature space.
Figure 3: (a) The data samples lie in a $1$-dimensional input space. (b) The data samples become linearly separable in the new higher-dimensional feature space.

The idea of “lifting” our data into a higher-dimensional space is very cool. However, feature mapping usually causes the number of features to blow up! Explicitly constructing such a mapping is computationally inefficient, especially when the new feature space is very high-dimensional [5]. Fortunately, the kernel trick comes to the rescue.

3.2  Kernel trick

Let’s rewrite the SVM formulation in Eq. (36) by replacing the original input attributes $x$ with a feature map $\phi(x)$:

$$ \begin{align} \max_{\alpha} \; & \sum_{i=1}^{M}\alpha_i - \frac{1}{2}\sum_{i=1}^{M}\sum_{j=1}^{M}\alpha_i\alpha_j y_i y_j \langle\phi(x_i), \phi(x_j)\rangle \tag{41} \\ \text{s.t.}\quad & \begin{cases} \alpha_i \ge 0, \quad \forall\, i = 1, \ldots, M \notag\\ \displaystyle\sum_{i=1}^{M} \alpha_i y_i = 0 \end{cases} \end{align} $$

where $\phi(x_i)^T \phi(x_j)$ is equivalently written as $\langle\phi(x_i), \phi(x_j)\rangle$, emphasizing the inner product of the two feature vectors.

Polynomial kernel makes the data linearly separable in 3D.
Figure 4: (a) Data is not linearly separable in the $2$-dimensional input space. (b) Linearly separable data in the $3$-dimensional feature space after a degree-$2$ polynomial transformation.

Now let’s look at another classic binary classification problem as shown in Fig. 4(a). We are working with $2$-dimensional data samples that are not linearly separable in the input space. Each data sample is represented by

$$ x = \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} \tag{42} $$

For a polynomial kernel of degree $2$, the corresponding feature map $\phi$ is given by

$$ \phi: \mathbb{R}^2 \rightarrow \mathbb{R}^3, \quad \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} \mapsto \begin{bmatrix} {x_1}^2 \\[3pt] {x_2}^2 \\[3pt] {\sqrt{2}x_1x_2} \end{bmatrix}\tag{43} $$

The inner product in the feature space is

$$ \begin{align} \phi(x_i)^T\phi(x_j) &= \begin{bmatrix}x_{i,1}^{2} & x_{i,2}^{2} & \sqrt{2}\,x_{i,1}x_{i,2}\end{bmatrix} \begin{bmatrix}x_{j,1}^{2}\\[6pt] x_{j,2}^{2}\\[6pt] \sqrt{2}\,x_{j,1}x_{j,2} \end{bmatrix} \notag \\[6pt] &=x_{i,1}^{2}x_{j,1}^{2}+x_{i,2}^{2}x_{j,2}^{2}+2x_{i,1}x_{i,2}x_{j,1}x_{j,2} \tag{44} \end{align} $$

Now consider a kernel function $k$ in a $2$-dimensional input space defined as follows:

$$ k(x_i,x_j) = (x_i^T x_j)^2 \tag{45} $$

Expanding it gives

$$ \begin{align} k(x_i,x_j) &=\biggl(\begin{bmatrix}x_{i,1} & x_{i,2}\end{bmatrix}\begin{bmatrix} x_{j,1}\\[6pt] x_{j,2}\\[6pt] \end{bmatrix}\biggr)^2 \notag\\[6pt] &=(x_{i,1}x_{j,1}+x_{i,2}x_{j,2})^2 \notag\\[6pt] &=x_{i,1}^{2}x_{j,1}^{2}+x_{i,2}^{2}x_{j,2}^{2}+2x_{i,1}x_{i,2}x_{j,1}x_{j,2} \notag\\[6pt] &=\phi(x_i)^T\phi(x_j) \tag{46} \end{align} $$

Did you notice what just happened? We just demonstrated that through the kernel function $k(x_i,x_j)$ we can implicitly compute the inner product $\langle\phi(x_i), \phi(x_j)\rangle$ directly from the original input space rather than constructing the explicit feature mapping. This is known as the kernel trick. The key advantage of the kernel trick is computational efficiency.

An example would help this make more sense.

Let

$$ x_i = \begin{bmatrix} 1 \\ 2 \end{bmatrix}, \qquad x_j = \begin{bmatrix} 3 \\ 4 \end{bmatrix} \tag{47} $$

and its corresponding inner product in the feature space is

$$ \begin{align} \phi(x_i)^T\phi(x_j) &= \begin{bmatrix} (1)^2 & (2)^2 & \sqrt{2} \cdot (1 \cdot 2) \end{bmatrix} \begin{bmatrix}(3)^2 \\[6pt] (4)^2 \\[6pt] \sqrt{2} \cdot (3 \cdot 4) \end{bmatrix} \notag \\[6pt] &=(1 \cdot 1) \cdot (3 \cdot 3) + (2 \cdot 2) \cdot (4 \cdot 4) + \sqrt{2} \cdot (1 \cdot 2) \cdot \sqrt{2} \cdot (3 \cdot 4) \notag\\[6pt] &= 9+64+48 \notag\\[6pt] &=121 \tag{48} \end{align} $$

Explicitly constructing both feature vectors and then taking their inner product requires 13 operations (11 multiplications, 2 additions).

Now consider direct kernel evaluation:

$$ \begin{align} k(x_i,x_j) &=\biggl(\begin{bmatrix}1 & 2\end{bmatrix}\begin{bmatrix} 3\\[6pt] 4\\[6pt] \end{bmatrix}\biggr)^2 \notag\\[6pt] &=(1 \cdot 3 + 2 \cdot 4)^2 \notag\\[6pt] &=11 \cdot 11 \notag\\[6pt] &=121 \tag{49} \end{align} $$

Direct kernel evaluation requires only 4 operations (3 multiplications, 1 addition). In other words, a suitable kernel function cheaply computes the inner product $\langle\phi(x_i), \phi(x_j)\rangle$ in the feature space without the lifting.

Let’s formalize the concept of a kernel function. Let $\mathcal{X}$ denote an input space. A function $k:\mathcal{X} \times \mathcal{X} \rightarrow \mathbb{R}$ is a kernel function over $\mathcal{X}$ if there exist a real Hilbert space $\mathcal{H}$ and a feature map $\phi:\mathcal{X} \rightarrow \mathcal{H}$ such that

$$ k(x_i,x_j) = {\langle \phi(x_i), \phi(x_j) \rangle}, \quad \forall \ x_i,x_j \in \mathcal{X} \tag{50} $$

where $\langle \cdot,\cdot \rangle$ denotes the inner product on $\mathcal{H}$.

But can any function be used as a kernel function? The answer is no. Not every function is a valid kernel. Mercer’s theorem provides the theoretical foundation for characterizing valid kernels (see Michael Jordan’s lecture notes). However, note that Mercer’s theorem only tells us whether a candidate similarity function is admissible for use as a kernel, not how well it would perform [6].

Some common kernels include:

3.3  The kernelized dual

Now let’s reformulate the dual problem in Eq. (41) by replacing the inner product $\langle\phi(x_i), \phi(x_j)\rangle$ with a kernel function $k(x_i,x_j)$:

$$ \begin{align} \max_{\alpha} \; & \sum_{i=1}^{M}\alpha_i - \frac{1}{2}\sum_{i=1}^{M}\sum_{j=1}^{M}\alpha_i\alpha_j y_i y_j k(x_i,x_j) \tag{51} \\ \text{s.t.}\quad & \begin{cases} \alpha_i \ge 0, \quad \forall\, i = 1, \ldots, M \notag\\ \displaystyle\sum_{i=1}^{M} \alpha_i y_i = 0 \end{cases} \end{align} $$

This reformulation is what allows SVMs to learn a linear decision boundary in feature space, which corresponds to a nonlinear decision boundary in the original input space.

Classifier: For any new point $z$, the predicted class can be expressed in terms of the kernel:

$$\begin{align} \hat{y}&=\operatorname{sign}​(\langle w, \phi(z) \rangle ​+b) \notag\\ &=\operatorname{sign}​\biggl(\sum_{i \in \mathcal{S}}\alpha_iy_i \langle \phi(​x_i),\phi(z​) \rangle +b\biggr) \notag \\ &=\operatorname{sign}​\biggl(\sum_{i \in \mathcal{S}}\alpha_iy_i k(x_i,z) +b\biggr) \tag{52} \end{align} $$

4  Regularized SVM

4.1 Slack variables

So far, we’ve seen that when a dataset cannot be separated by a hyperplane, we can use kernels to implicitly map the data to a higher-dimensional space. While it generally increases the likelihood that we can find a separating hyperplane in the new feature space, we can’t always guarantee a perfect separation when the dataset contains noise, outliers, or overlapping classes. In such cases, assuming perfect linear separability either makes the problem infeasible or results in a classifier with a very small margin and poor generalization.

This leads to the concept of a soft-margin SVM, which allows some classification errors while maximizing the margin. The soft-margin SVM relaxes the hard-margin constraints by introducing slack variables $\xi_i \ge 0$, which allow some training examples to fall within the margin region or even be misclassified. However, we also want to minimize the sum of the slack variables $\sum_{i} \xi_i$ to keep the number of such misclassified examples as small as possible. Thus, we reformulate our optimization problem as follows:

$$ \min_{w,b,\xi} \; \frac12\lVert w\rVert^2 + C\sum_{i=1}^M \xi_i \tag{53} $$
$$ \text{s.t.}\quad y_i(w^T x_i + b) \ge 1 - \xi_i, \quad \forall\, i=1,\ldots,M \tag{54} $$
$$ \xi_i \ge 0, \quad \forall\, i=1,\ldots,M \tag{55} $$

The value of $\xi_i$ indicates the degree of violation of the margin $\left(\frac{1}{\lVert w \rVert}\right)$ for each data point $i$:

Slack variables allow some points to violate the margin.
Figure 5: Soft-margin classifier with slack variables: $0 \lt \xi_i \lt 1$ for a point on the correct side of the hyperplane but within the margin, and $\xi_i \gt 1$ for a misclassified point.

The sum of the slack variables $\sum_{i} \xi_i$ measures the total margin violation across data points, and the regularization parameter $C \gt 0$ controls the trade-off between maximizing the margin and minimizing classification errors. Here’s how $C$ affects the margin and misclassifications:

4.2 Loss-function form

The primal soft-margin problem in Eq. (53) can be reformulated with the hinge loss as an unconstrained optimization problem. The constraint in Eq. (54) of our soft-margin formulation can be written more concisely as

$$ \xi_i \geq 1 - y_i(w^Tx_i+b) \tag{56} $$

Since we also require $\xi_i\geq0$, the minimum value that satisfies both constraints is

$$ \xi_i = \max(0, 1 - y_i(w^Tx_i+b)) \tag{57} $$

This expression is precisely the hinge loss. Substituting this expression into the objective of our soft-margin formulation yields an unconstrained formulation that combines the $l_2$-regularizer and the hinge loss:

$$ \min_{w,b} \; \underbrace{\frac{1}{2}\lVert w \rVert^2}_{\text{regularizer}} + \underbrace{C\sum_{i=1}^{M} \max(0, 1 - y_i(w^Tx_i+b))}_{\text{loss term}} \tag{58} $$

The first term in the new formulation is called the regularization term (or regularizer), and the second term is called the error term (or loss term). The regularizer keeps the weights small and well-behaved, which leads to a larger margin, whereas the loss term penalizes incorrect predictions and margin violations across the training set. The regularization parameter $C$ determines the trade-off between these two terms, balancing margin maximization against hinge-loss minimization.

4.3 Subgradient descent method

Before we continue, let’s reformulate the objective function in Eq. (58) in two steps:

Therefore, the new objective function is given by

$$ J(w,b)={\frac{\lambda}{2}{\lVert w \rVert}^2} + \frac{1}{M}\sum_{i=1}^{M} \max(0, 1 - y_i(w^Tx_i+b)) \tag{59} $$

The new regularization parameter $\lambda$ makes it more natural to vary the strength of regularization while keeping the objective the same.

In our objective function, both the regularizer and the hinge loss are convex with respect to model parameters $(w,b)$. Furthermore, for $\lambda>0$, the regularizer is strictly convex in $w$, which guarantees that the optimal weight vector $w^*$ is unique. Since a nonnegative sum of convex functions is convex [7], the overall SVM objective is also convex.

In general, we apply the standard gradient descent method to compute the global minimizer of an unconstrained differentiable convex objective. Since the hinge loss function in our SVM objective is nondifferentiable, we use subgradients instead of gradients. The resulting variant of the gradient descent method is called the subgradient descent method, which applies directly to nondifferentiable convex functions [8].

Hinge loss function.
Figure 6: The piecewise linear form of the hinge loss, with a "kink" at $y_i(w^Tx_i+b)=1$.

Recall that the hinge loss function is defined as

$$ \ell(x_i,y_i;w,b)={\max(0, 1 - y_i(w^Tx_i+b))} \tag{60} $$

At the point where $y_i(w^Tx_i+b)=1$, the hinge loss is nondifferentiable (the “kink”), so there is no unique gradient. However, the subgradient at this point can be either the gradient when $y_i(w^Tx_i+b) > 1$ or the gradient when $y_i(w^Tx_i+b) < 1$ or any value in between. The subgradient of the hinge loss with respect to model parameter $w$ is given by

$$ {\nabla}_{w}\ell(x_i,y_i;w,b)= \begin{cases} -y_ix_i &\quad \text{if } y_i(w^Tx_i+b) < 1 \\ 0 &\quad \text{otherwise} \end{cases} \tag{61} $$

and the subgradient with respect to $b$ is given by

$$ \frac{\partial}{\partial b}\ell(x_i,y_i;w,b)= \begin{cases} -y_i &\quad \text{if } y_i(w^Tx_i+b) < 1 \\ 0 &\quad \text{otherwise} \end{cases} \tag{62} $$

Therefore, the subgradient of the cost function with respect to $w$ is given by

$$ \nabla_{w}J(w,b) = \lambda w + \frac{1}{M}\sum_{i=1}^{M}\begin{cases} -y_ix_i & \text{if } y_i(w^Tx_i+b) < 1 \\ 0 & \text{otherwise} \end{cases} \tag{63} $$

and the subgradient with respect to $b$ is given by

$$ \frac{\partial}{\partial b}J(w,b)=\frac{1}{M}\sum_{i=1}^{M}\begin{cases} -y_i & \quad \text{if } y_i(w^Tx_i+b) < 1 \\ 0 & \quad \text{otherwise} \end{cases} \tag{64} $$

For each iteration $t$, the subgradient descent update rule for minimizing the cost function $J(w,b)$ is defined as

$$ \begin{aligned} w_{t+1} &\leftarrow w_{t} - \eta \nabla_{w}J(w_t,b_t) \\ b_{t+1} &\leftarrow b_{t} - \eta \frac{\partial}{\partial b}J(w_t,b_t) \end{aligned} \tag{65} $$

where $0<\eta<1$ is the learning rate.

We are now ready to apply stochastic subgradient descent to optimize the primal soft-margin objective. Check out the notebook for implementation:

didarulilm/ml-notebooks Support Vector Machines from First Principles. Jupyter Notebook

5  Closing thoughts

SVMs are a vast topic, and if some of the concepts take a few readings to sink in, that’s okay. Nobody absorbs all the details in one read.

If this all makes sense, you should now have a pretty good mental model of how SVMs work intuitively, and the mathematics behind them should feel less like a pile of formulas to memorize and more like a series of choices you might have made yourself.

If you found this post helpful, consider sharing it with someone who might benefit from it!

If you like this type of content and want to learn more or spot any errors in the post, make sure to reach out to me on X or LinkedIn.

That’s all for now, thanks for reading!

  1. A. Y. Chervonenkis, “Early History of Support Vector Machines,” in Empirical Inference: Festschrift in Honor of Vladimir N. Vapnik, Springer, 2013, pp. 13–20. doi: 10.1007/978-3-642-41136-6_3.
  2. M. J. Zaki and W. Meira Jr., Data Mining and Machine Learning: Fundamental Concepts and Algorithms, 2nd ed. Cambridge University Press, 2020. Available at: https://dataminingbook.info/book_html/
  3. S. Boyd and L. Vandenberghe, Convex Optimization. Cambridge University Press, 2004. Available at: https://stanford.edu/~boyd/cvxbook/bv_cvxbook.pdf
  4. M. Chiang, “Lecture 2: Convex Optimization and Lagrange Duality.” ELE539A: Optimization of Communication Systems, Princeton University, 2007. Available at: https://www.princeton.edu/~chiangm/optimization.pdf
  5. F. Li, “Lecture Notes on Support Vector Machine.” Shandong University, 2021. Available at: https://funglee.github.io/ml/slides/Lecture6-SVM-Notes.pdf
  6. M. Chandrasekaran, “Understanding the Kernel Trick.” 2025. Available at: https://monishver11.github.io/blog/2025/kernel-trick/
  7. A. Zisserman, “Lecture 2: The SVM Classifier.” C19 Machine Learning, University of Oxford, 2015. Available at: https://www.robots.ox.ac.uk/~az/lectures/ml/lect2.pdf
  8. S. Boyd and J. Park, “Subgradient Methods.” EE364b: Convex Optimization II, Stanford University, 2014. Available at: https://web.stanford.edu/class/ee364b/lectures/subgrad_method_notes.pdf