Why the Margin Can Be Set to One
Page 1202 spent the scale freedom one way: demand , and read the margin off as a separate variable . Section 12.2.2 spends it the other way — demand that the predictor’s value at the closest example be exactly — and Section 12.2.3 proves the two give the same classifier.
The derivation
Section titled “The derivation”Let be the closest example, scaled so that . Its projection is on the hyperplane:
Substitute Equation 12.8, :
and expand by bilinearity:
The second fraction is , so and
Two lines, and the margin is now a function of the parameters rather than a variable of its own. That is the entire content of §12.2.2 — the rest is bookkeeping.
import numpy as np
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])
w, b = np.array([1.0, 0.0]), -2.0
xa = X[0] # (3, 1), a closest positive
nw = np.linalg.norm(w)
r = (w @ xa + b) / nw
xp = xa - r * w / nw
print(f"<w, x_a> + b : {w @ xa + b:.10f}")
print(f"12.11: <w, x'_a> + b : {w @ xp + b:.3e}")
print(f"12.13: LHS : "
f"{w @ xa + b - r * (w @ w) / nw:.3e}")
print(f"12.14: 1/||w|| : {1/nw:.10f}")
print(f" measured distance : {np.linalg.norm(xa - xp):.10f}")<w, x_a> + b : 1.0000000000
12.11: <w, x'_a> + b : 0.000e+00
12.13: LHS : 0.000e+00
12.14: 1/||w|| : 1.0000000000
measured distance : 1.0000000000Equation 12.14 is not special to this example. Over random and random points, with the scale condition imposed, the worst disagreement between the measured distance and is .
The hard margin SVM
Section titled “The hard margin SVM”With the margin now equal to , the requirement that every point be at least a margin away becomes
and the objective is to make as large as possible:
The book then says: “Instead of maximizing the reciprocal of the norm as in (12.16), we often minimize the squared norm. We also often include a constant that does not affect the optimal but yields a tidier form when we compute the gradient.”
This is the hard margin SVM — “hard” because the constraints admit no violations at all.
from scipy.optimize import minimize
def solve(obj, seed=0):
rg = np.random.default_rng(seed)
return minimize(obj, np.r_[rg.normal(0, 1, 2), rg.normal(0, 1)],
constraints=[{"type": "ineq",
"fun": lambda v: Y * (X @ v[:2] + v[2]) - 1}],
method="SLSQP", options={"maxiter": 6000, "ftol": 1e-14})
r1 = solve(lambda v: np.linalg.norm(v[:2])) # 12.16, as written
r2 = solve(lambda v: 0.5 * v[:2] @ v[:2]) # 12.18, squared
print(f"minimising ||w|| : w = {np.round(r1.x[:2], 8)}, b = {r1.x[2]:.8f}")
print(f"minimising 0.5||w||^2 : w = {np.round(r2.x[:2], 8)}, b = {r2.x[2]:.8f}")
print(f"largest difference : {np.abs(r1.x - r2.x).max():.3e}")minimising ||w|| : w = [1. 0.], b = -2.00000000
minimising 0.5||w||^2 : w = [1. 0.], b = -2.00000000
largest difference : 4.441e-16The objective values differ — against — and the argmin does not, because is strictly increasing on . That is the whole justification, and it is worth noticing that it depends on being non-negative; the same move on a quantity that could be negative would be wrong.
Theorem 12.1, and what its proof turns on
Section titled “Theorem 12.1, and what its proof turns on”Theorem 12.1. Maximizing the margin , where we consider normalized weights as in (12.10), is equivalent to scaling the data, such that the margin is unity.
The proof is four steps, and only one of them is doing work:
- Square the objective. Maximising and maximising have the same argmax for .
- Reparametrise. Replace the normalised by for an unconstrained , giving Equation 12.22.
- Divide the constraint by . Legitimate because — which is where linear separability is used. Define and , Equation 12.23.
- Read off the norm. , Equation 12.24, so maximising is maximising , which is minimising .
Step 4 is the hinge. Checked over random draws, the worst deviation of from is .
And the theorem itself, on random datasets of dimension to and size to :
# solve both formulations on the same data, then compare the HYPERPLANES
rg = np.random.default_rng(3)
angs, rgaps = [], []
while len(angs) < 299:
D, n = int(rg.integers(2, 6)), int(rg.integers(6, 16))
mu = rg.normal(0, 1, D) * 3
XX = np.vstack([rg.normal(mu, 1.0, (n // 2, D)),
rg.normal(-mu, 1.0, (n - n // 2, D))])
YY = np.r_[np.ones(n // 2), -np.ones(n - n // 2)]
a = best_1221(XX, YY) # min 0.5||w||^2
c = best_1210(XX, YY) # max r, ||w|| = 1
if a is None or c is None:
continue
wa = a.x[:D] / np.linalg.norm(a.x[:D])
wc = c.x[:D] / np.linalg.norm(c.x[:D])
wc = wc if wa @ wc >= 0 else -wc
angs.append(np.degrees(np.arccos(np.clip(wa @ wc, -1, 1))))
rgaps.append(abs(c.x[D + 1] - 1.0 / np.linalg.norm(a.x[:D])))
print(f"worst angle between normals : {max(angs):.3e} degrees")
print(f"worst |r - 1/||w||| : {max(rgaps):.3e}")worst angle between normals : 7.492e-06 degrees
worst |r - 1/||w||| : 2.327e-13Seven millionths of a degree, across three hundred problems. The two formulations do not approximately agree — they return the same hyperplane, and the residual is solver tolerance rather than any difference in the mathematics.
-
pch.quizShowAnswer
B — From the scale choice of Section 12.2.2: x_a is the closest example and the data has been scaled so that its predictor value is exactly 1
-
pch.quizShowAnswer
C — Because t maps to half t squared is strictly increasing on non-negative t, so the argmin is unchanged — measured, the two solutions differ by 4.441e-16 while the objective values differ by a factor of two
-
pch.quizShowAnswer
B — The norm is not differentiable at the origin, whereas the squared norm is smooth with constant Hessian — making 12.18 a convex quadratic program. Over 500 starts, the norm form failed 3 times and needed up to 29 iterations; the squared form failed 0 times and needed at most 5
-
pch.quizShowAnswer
C — The ||w|| = 1 formulation failed to converge from all ten of its starting points, while the squared-norm one converged from the first — the non-convex sphere constraint again
Exercises
Section titled “Exercises”Exercise 1 – Derive r = 1/||w|| numerically
Section titled “Exercise 1 – Derive r = 1/||w|| numerically”Exercise 2 – Check step 12.24 of the proof
Section titled “Exercise 2 – Check step 12.24 of the proof”Exercise 3 – Count the iterations each objective needs
Section titled “Exercise 3 – Count the iterations each objective needs”Recall card
Section titled “Recall card”- Section 12.2.2 spends the scale freedom the other way: instead of demanding a unit-length w, it demands that the predictor’s value at the closest example be exactly one.
- Two lines of algebra then give r equal to one over the norm of w — substitute the projection into the hyperplane equation and use bilinearity. Verified to 3.553e-15 over twenty thousand random draws.
- The margin stops being a variable and becomes a function of the parameters, which is what makes the rest of the chapter possible.
- The hard margin SVM minimises half the squared norm subject to every point having margin at least one. Hard means no violations are allowed at all.
- Squaring the norm does not move the answer, only the objective value — measured, the two solutions differ by 4.441e-16 while the values differ by a factor of two.
- But squaring buys something real. The norm is not differentiable at the origin and the squared norm is smooth with constant Hessian, so 12.18 is a convex quadratic program.
- Measured over five hundred starts: the norm objective failed three times and took up to twenty-nine iterations; the squared objective never failed and never took more than five.
- Theorem 12.1 says the two formulations are equivalent, and its proof turns on one step — the reparametrised weight vector has norm exactly one over r, checked to 1.137e-13.
- That step needs r greater than zero, which is where linear separability enters the proof and nowhere else.
- Measured on three hundred random datasets, the two formulations agree to 7.492e-06 degrees. They do not approximately agree; the residual is solver tolerance.
- On one dataset in three hundred the normalised formulation did not converge at all, while the convex one did. Equivalent problems are not equally easy problems, and nobody solves Equation 12.10 in practice.
Next: The Soft Margin SVM — §12.2.4, what to do when no separating hyperplane exists at all.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading