Skip to content

The Dual Support Vector Machine

Everything so far has been written in terms of w\mathbf{w}, which lives in RD\mathbb{R}^D. That is the primal SVM, and its size grows with the number of features. Section 12.3 rewrites the same problem in terms of one multiplier per example — and in doing so produces the form that Section 12.4 can kernelise.

Attach αn0\alpha_n \geq 0 to the margin constraint 12.26b and γn0\gamma_n \geq 0 to the non-negativity constraint 12.26c:

L(w,b,ξ,α,γ)=12w2+Cn=1Nξnn=1Nαn(yn(w,xn+b)1+ξn)n=1Nγnξn(12.34)\mathfrak{L}(\mathbf{w}, b, \boldsymbol\xi, \boldsymbol\alpha, \boldsymbol\gamma) = \tfrac12\lVert\mathbf{w}\rVert^2 + C\sum_{n=1}^{N}\xi_n - \sum_{n=1}^{N}\alpha_n\big(y_n(\langle\mathbf{w},\mathbf{x}_n\rangle + b) - 1 + \xi_n\big) - \sum_{n=1}^{N}\gamma_n\xi_n \qquad \text{(12.34)}

Differentiating in the three primal variables:

Lw=wnαnynxn,Lb=nαnyn,Lξn=Cαnγn(12.35)–(12.37)\frac{\partial\mathfrak{L}}{\partial\mathbf{w}} = \mathbf{w}^\top - \sum_n \alpha_n y_n \mathbf{x}_n^\top, \qquad \frac{\partial\mathfrak{L}}{\partial b} = -\sum_n \alpha_n y_n, \qquad \frac{\partial\mathfrak{L}}{\partial\xi_n} = C - \alpha_n - \gamma_n \qquad \text{(12.35)–(12.37)}

Setting the first to zero gives the result the rest of the chapter rests on:

w=n=1Nαnynxn(12.38)\mathbf{w} = \sum_{n=1}^{N}\alpha_n y_n \mathbf{x}_n \qquad \text{(12.38)}

the representer theorem — the optimal weight vector is a linear combination of the training examples. Setting the second to zero adds nαnyn=0\sum_n \alpha_n y_n = 0, which makes it an affine combination. Setting the third to zero and using γn0\gamma_n \geq 0 gives αnC\alpha_n \leq C.

Substituting back eliminates w\mathbf{w}, bb and ξ\boldsymbol\xi entirely (Equations 12.39, 12.40) and leaves

minα 12i=1Nj=1Nyiyjαiαjxi,xji=1Nαisubject toiyiαi=0,0αiC(12.41)\min_{\boldsymbol\alpha}\ \tfrac12\sum_{i=1}^{N}\sum_{j=1}^{N} y_i y_j \alpha_i \alpha_j \langle\mathbf{x}_i, \mathbf{x}_j\rangle - \sum_{i=1}^{N}\alpha_i \quad\text{subject to}\quad \sum_i y_i\alpha_i = 0, \quad 0 \leq \alpha_i \leq C \qquad \text{(12.41)}

The examples appear only through xi,xj\langle\mathbf{x}_i, \mathbf{x}_j\rangle. That is the whole point of the exercise, and page 1208 collects the winnings.

The eight-point dataset has a dual solution that can be written down by hand. With w=(1,0)\mathbf{w} = (1, 0) and support vectors (3,1)(3, 1), (3,1)(3, -1) and (1,0)(1, 0), Equation 12.38 and the constraint nαnyn=0\sum_n \alpha_n y_n = 0 give α0=α1=14\alpha_0 = \alpha_1 = \tfrac14 and α4=12\alpha_4 = \tfrac12.

the_dual.py
import numpy as np
from scipy.optimize import minimize
 
X = np.array([[3.0,  1.0], [3.0, -1.0], [6.0,  1.0], [6.0, -1.0],
              [1.0,  0.0], [0.0,  1.0], [0.0, -1.0], [-1.0, 0.0]])
Y = np.array([ 1.0,  1.0,  1.0,  1.0,  -1.0, -1.0, -1.0, -1.0])
BIG = 1e6                                # stands in for the hard margin
 
def dual(XX, YY, C, tries=25):
    H = (YY[:, None] * YY[None, :]) * (XX @ XX.T)       # Y K Y
    out, bv = None, np.inf
    for s in range(tries):
        rg = np.random.default_rng(s)
        a0 = np.clip(np.abs(rg.normal(0.3, 0.3, len(XX))), 0, C)
        r = minimize(lambda a: 0.5 * a @ H @ a - a.sum(), a0,
                     jac=lambda a: H @ a - 1.0,
                     bounds=[(0.0, C)] * len(XX),
                     constraints=[{"type": "eq", "fun": lambda a: YY @ a,
                                   "jac": lambda a: YY}],
                     method="SLSQP", options={"maxiter": 30000, "ftol": 1e-14})
        if r.success and r.fun < bv - 1e-13:
            bv, out = r.fun, np.clip(r.x, 0.0, C)
    return out
 
a = dual(X, Y, BIG)
w = a * Y @ X                                            # Equation 12.38
print(f"alpha            : {np.round(a, 8)}")
print(f"w = sum a y x    : {np.round(w, 8)}")
print(f"sum y_n alpha_n  : {Y @ a:.3e}")
print(f"support vectors  : {list(np.where(a > 1e-9)[0])}")
text
alpha            : [0.25 0.25 0.   0.   0.5  0.   0.   0.  ]
w = sum a y x    : [1. 0.]
sum y_n alpha_n  : -5.306e-16
support vectors  : [0, 1, 4]

The largest deviation from the exact (14,14,0,0,12,0,0,0)(\tfrac14, \tfrac14, 0, 0, \tfrac12, 0, 0, 0) is 1.075×1081.075\times10^{-8}, and w\mathbf{w} recovers the primal answer to 1.950×1081.950\times10^{-8}.

The book’s remark is short: “The examples xn\mathbf{x}_n, for which the corresponding parameters αn=0\alpha_n = 0, do not contribute to the solution w\mathbf{w} at all. The other examples, where αn>0\alpha_n > 0, are called support vectors.”

Measured from both directions:

sparsity.py
sv = a > 1e-9
a2 = dual(X[sv], Y[sv], BIG)              # keep ONLY the support vectors
print(f"refit on {int(sv.sum())} points : alpha = {np.round(a2, 8)}")
print(f"  w = {np.round(a2 * Y[sv] @ X[sv], 8)}, gap "
      f"{np.linalg.norm(a2 * Y[sv] @ X[sv] - w):.3e}")
 
rg = np.random.default_rng(7)             # now ADD 500 easy examples
XL = np.vstack([X, rg.uniform([20, -5], [40, 5], (250, 2)),
                rg.uniform([-40, -5], [-20, 5], (250, 2))])
YL = np.r_[Y, np.ones(250), -np.ones(250)]
aL = dual(XL, YL, BIG, tries=6)
print(f"N = {len(XL)} : support vectors {int((aL > 1e-9).sum())}, "
      f"w gap {np.linalg.norm(aL * YL @ XL - w):.3e}")
print(f"  alpha mass on the 500 extras : {aL[8:].sum():.3e}")
text
refit on 3 points : alpha = [0.25 0.25 0.5 ]
  w = [ 1. -0.], gap 1.950e-08
N = 508 : support vectors 3, w gap 6.238e-09
  alpha mass on the 500 extras : 8.289e-12

Five of eight examples can be deleted without changing the answer, and five hundred more can be added without changing it either. The five hundred extras collectively carry 8.289×10128.289\times10^{-12} of dual mass. This is the same property page 1205 measured from the primal side: a point beyond its margin has zero hinge loss, and here it has zero multiplier.

figure Equation 12.38: the answer is a combination of the examples, and almost all coefficients are zero matplotlib
Left, the eight training points with the decision boundary and margins; the three support vectors carry green rings whose area is proportional to their multiplier, labelled alpha equals 0.25, 0.25 and 0.50, while the other five are labelled alpha equals zero. Right, a log-scale plot of counts against N as easy examples are added: a dotted grey line rising from 8 to 508 for the total, and a flat green line pinned at 3 for the number of support vectors. Left, the eight training points with the decision boundary and margins; the three support vectors carry green rings whose area is proportional to their multiplier, labelled alpha equals 0.25, 0.25 and 0.50, while the other five are labelled alpha equals zero. Right, a log-scale plot of counts against N as easy examples are added: a dotted grey line rising from 8 to 508 for the total, and a flat green line pinned at 3 for the number of support vectors.
The ringed areas are the multipliers, and they sum to ||w||² = 1. The right panel is the practical consequence: prediction cost scales with the number of support vectors, not with N.

The dual returns α\boldsymbol\alpha, and Equation 12.38 returns w\mathbf{w} — but bb was eliminated. For an example on the margin, w,xn+b=yn\langle\mathbf{w}^*, \mathbf{x}_n\rangle + b = y_n, so

b=ynw,xn(12.42)b^* = y_n - \langle\mathbf{w}^*, \mathbf{x}_n\rangle \qquad \text{(12.42)}
text
from example 0 : b = -1.999999981275
from example 1 : b = -1.999999949778
from example 4 : b = -1.999999988509
the primal's b*: -2.0

The book adds: “In principle, there may be no examples that lie exactly on the margin. In this case, we should compute ynw,xn\lvert y_n - \langle\mathbf{w}^*,\mathbf{x}_n\rangle\rvert for all support vectors and take the median value to be the value of bb^*.”

That rule as written discards the sign. Taking the median of an absolute value returns a non-negative number, and bb^* here is 2-2:

CCbb from Equation 12.42the median-of-absolute rule
0.20.21.000000-1.000000+1.000000+1.000000
0.50.52.000000-2.000000+2.000000+2.000000
1.01.02.000000-2.000000+2.000000+2.000000
5.05.02.000000-2.000000+2.000000+2.000000

The magnitudes agree to 8.1×1098.1\times10^{-9} or better, so the intent is clear and the fix is to drop the absolute value — take the median of ynw,xny_n - \langle\mathbf{w}^*,\mathbf{x}_n\rangle itself.

The book’s side note reads:

It turns out that examples that lie exactly on the margin are examples whose dual parameters lie strictly inside the box constraints, 0<αi<C0 < \alpha_i < C.

The KKT conditions give three implications:

conditionconsequence
αn=0\alpha_n = 0ynf(xn)1y_n f(\mathbf{x}_n) \geq 1 — at or beyond the margin
0<αn<C0 < \alpha_n < Cynf(xn)=1y_n f(\mathbf{x}_n) = 1exactly on the margin
αn=C\alpha_n = Cynf(xn)1y_n f(\mathbf{x}_n) \leq 1 — at or inside the margin

The middle row is the note’s claim read left to right, and it is correct. The note is phrased the other way round — examples on the margin are examples with 0<αi<C0 < \alpha_i < C — and that direction is false, because the third row also permits ynf(xn)=1y_n f(\mathbf{x}_n) = 1 exactly.

text
  C = 0.5   (b from the primal: -1.000000)
    n       x_n    y     alpha    y f(x)              position
    0   [3.  1.]   +1  0.500000 -0.000000   inside / wrong side
    2   [6.  1.]   +1  0.009204  1.000000         ON the margin
    3   [6. -1.]   +1  0.004685  1.000000         ON the margin
    7   [-1. 0.]   -1  0.000000  1.333333     beyond the margin
      on the margin : alpha in [0.004685, 0.009204]   -- strictly inside
 
  C = 2.0   (b from the primal: -2.000000)
    n       x_n    y     alpha    y f(x)              position
    0   [3.  1.]   +1  2.000000  1.000000         ON the margin
    1   [3. -1.]   +1  2.000000  1.000000         ON the margin
    4   [1.  0.]   -1  2.000000  1.000000         ON the margin
    8   [4.5 0.]   -1  2.000000 -2.500000   inside / wrong side
      on the margin : alpha in [2.000000, 2.000000]   -- AT the bound

At C=2C = 2, three examples sit exactly on the margin with αn=C\alpha_n = Cat the box bound, not strictly inside it. This is not a solver artefact: the optimum is unique there. Writing α0=α1=t\alpha_0 = \alpha_1 = t, α4=s\alpha_4 = s, α8=u\alpha_8 = u, the constraints force s=2tus = 2t - u and 4t3.5u=14t - 3.5u = 1, and the dual objective is 12w2αn\tfrac12\lVert\mathbf{w}\rVert^2 - \sum\alpha_n with αn=1+3.5u\sum\alpha_n = 1 + 3.5u. Maximising that pushes uu to its bound C=2C = 2, giving (t,s,u)=(2,2,2)(t, s, u) = (2, 2, 2) — all three at the bound, and αn=8\sum\alpha_n = 8, matching the measured dual value of 7.5=0.58-7.5 = 0.5 - 8.

figure The margin note is a one-way implication, and the book states it both ways matplotlib
Two scatter plots of the multiplier against y times f of x. At C equals 0.5 the two green diamonds marking on-margin examples sit near the bottom, far below the dashed red line at alpha equals C. At C equals 2 the green diamond marking on-margin examples sits exactly on the dashed red line at alpha equals 2, and the panel title reads the note FAILS here. Two scatter plots of the multiplier against y times f of x. At C equals 0.5 the two green diamonds marking on-margin examples sit near the bottom, far below the dashed red line at alpha equals C. At C equals 2 the green diamond marking on-margin examples sits exactly on the dashed red line at alpha equals 2, and the panel title reads the note FAILS here.
Left, the textbook picture the note describes. Right, the same data at C = 2, where the on-margin examples have their multipliers pinned at the box bound. The correct statement runs only one way: strictly inside the box implies on the margin, not the converse.
pch.quizTag Check your understanding
  1. pch.quizShowAnswer

    C — That sum of y_n alpha_n is zero, which makes it an affine combination rather than merely a linear one

  2. pch.quizShowAnswer

    B — Because a point beyond its margin has zero hinge loss in the primal and zero multiplier in the dual — it contributes nothing to Equation 12.38

  3. pch.quizShowAnswer

    C — The absolute value discards the sign, so the rule returns a non-negative number — on this data b* is -2 and the rule returns +2

  4. pch.quizShowAnswer

    B — The note's implication holds in one direction only: 0 < alpha < C implies on the margin, but an on-margin example may also have alpha = C. The note is phrased as the converse

Exercise 1 – Solve the dual and recover w

Section titled “Exercise 1 – Solve the dual and recover w”

Exercise 2 – Delete everything that is not a support vector

Section titled “Exercise 2 – Delete everything that is not a support vector”

Exercise 3 – Test the margin note in both directions

Section titled “Exercise 3 – Test the margin note in both directions”
  • The primal SVM’s size grows with the number of features; the dual’s grows with the number of examples. Section 12.3 rewrites the same problem in terms of one multiplier per example.
  • Equation 12.38 is the representer theorem: the optimal weight vector is a linear combination of the training examples, and the constraint from the derivative in b makes it an affine one.
  • In the dual the examples appear only through their inner products. That is what makes Section 12.4’s kernel substitution a one-line change.
  • On the running example the dual is exact: the multipliers are a quarter, a quarter and a half, and they recover w = (1, 0) to 2e-08.
  • An identity the book does not state: the total dual mass equals the squared norm of w, which is the inverse squared margin. Verified to 4.8e-08 across 200 random datasets. A wide margin means small multipliers.
  • Sparsity is the point. Five of the eight examples can be deleted without changing the answer, and 500 easy ones can be added without changing it either — they carry 8.3e-12 of dual mass between them.
  • That is the dual’s version of page 1205’s finding: a point beyond its margin has zero hinge loss and zero multiplier.
  • Recovering b needs an example on the margin, and the book’s fallback rule takes a median of absolute values — which discards the sign and returns plus two where the answer is minus two.
  • Strong duality holds to machine precision, because the primal is convex with affine constraints. That is what makes solving the dual instead of the primal legitimate rather than merely convenient.
  • The book’s margin note about the box constraints runs only one way. Strictly inside the box implies exactly on the margin; the converse is false, and at C = 2 three on-margin examples sit at the bound.
  • That case is a unique optimum, not solver noise — the constraints force a single feasible family and the objective pushes the multipliers to the bound.

Next: The Convex Hull View — §12.3.2, the same dual reached by a completely different geometric argument.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading