Module 6 — Support Vector Machines (Hard Margin, Soft Margin, Kernels)

Sources: Lec 13 — SVM (Hard Margin); Lec 14 — SVM (Soft Margin); Lec 15 — SVM Using Kernels. · Prev: Module 5 · Next: Module 7 — Clustering · Index: 00_INDEX_and_Coverage

Notation

, ; weight , bias ; hyperplane ; classifier .

flowchart LR
    A["Hard margin<br/>separable data<br/>C30–C31"] -->|"allow slack γ"| B["Soft margin<br/>0 ≤ α ≤ C<br/>C32"]
    B -->|"replace X·X' by K(X, X')"| C["Kernel SVM<br/>non-linear boundary<br/>C33–C34"]

C30. Hard-Margin SVM: Geometry, Margin and Support Vectors

Intuition

Many lines separate two classes. Choose the one that stays as far as possible from both classes — a fat “street” with the boundary down the middle. It is safer on new data. The few points that touch the edges of the street (support vectors) alone determine the answer.

 x2
  |   B   B                B = negative, A = positive
  |     B   ╲  ╲  ╲        |<— margin 2/‖W‖ —>|
  |  - - - - ╲ - ╲ - -   ← W·x + b = −1
  |           ╲  ╲        ← W·x + b =  0   (decision boundary)
  |  - - - - - -╲- ╲ -   ← W·x + b = +1
  |        A   A   A
  +--------------------→ x1     (points on the dashed lines = support vectors)

Formal definition

  • Data are linearly separable iff the convex hulls of the two classes do not intersect. Convex hull of : .
  • Separating hyperplane: .
  • is normal to the hyperplane; the distance of the hyperplane from the origin is ; the distance of a point from it is .
  • Rescaling freedom: leaves the hyperplane unchanged, so fix the scale so that the closest points satisfy (margin parameter ). All constraints become
  • Margin .
  • Support vectors: points with (exactly on the margin).

Optimisation problem

(The lecture calls the second form the “dual”; strictly it is an equivalent primal. The true dual comes in C31.)

Derivation of the margin

Take a support vector with and with . Subtract: . Projecting on the unit normal gives the width: . ∎

Key insights

  • Maximising margin minimising .
  • Interior points (with ) are irrelevant to the solution.
  • Hard margin needs separable data (C32 relaxes this).
  • Geometric view: the optimal margin equals the distance between the two convex hulls.

Worked examples

Example 1 — Very Easy · Distance to a hyperplane

  • Problem: , . Distance of from the hyperplane?
  • Asked: Distance.
  • Concept: Point-to-hyperplane distance.
  • Approach: .
  • Solution: ; . Distance .
  • Final answer: .

Key lesson

Distance is the signed value divided by .

Example 2 — Easy · Margin from

  • Problem: If the support vectors satisfy and , what is the margin? What gives margin ?
  • Asked: Two numbers.
  • Concept: Margin .
  • Approach: Formula.
  • Solution: . Margin .
  • Final answer: ; .

Key lesson

Big margin ⇔ small weight vector.

Example 3 — Moderate · One point per class

  • Problem: Positive point , negative point . Find the hard-margin hyperplane by imposing both constraints with equality and symmetry .
  • Asked: , margin.
  • Concept: Constraints active on both points.
  • Approach: Solve for .
  • Solution: : ; : . So . , ; boundary . ; margin = distance between the two points ✓.
  • Final answer: , , margin .

Key lesson

With one point per class the optimal margin equals the distance between the points, and the boundary is their perpendicular bisector.

Example 4 — Hard · Feasibility and support vectors

  • Problem: , ; positives ; negatives . Check feasibility, find the support vectors and the margin.
  • Asked: Verification.
  • Concept: Constraints and SVs.
  • Approach: Compute .
  • Solution: : ✓ SV. : ✓ (not SV). : ✓ SV. : ✓. Margin .
  • Final answer: Feasible; SVs ; margin .

Key lesson

Only points with value exactly are support vectors.

Example 5 — Very Hard · XOR is not separable

  • Problem: Show by the convex-hull criterion that the XOR set — positives , negatives — is not linearly separable, and that a different labelling of the same four points (positives , negatives ) is separable.
  • Asked: Hull intersection.
  • Concept: Hull criterion.
  • Approach: Find a common convex combination.
  • Solution: The positive hull contains . The negative hull contains . The hulls share a point ⇒ not separable. Second labelling: positive hull ; negative hull : disjoint ( vs ) ⇒ separable, e.g. (, gives on all four points).
  • Final answer: XOR hulls intersect at ; the second labelling is separable.
   XOR (not separable)         separable
 (0,1)−     (1,1)+            (0,1)−   (1,1)−
    ╲       ╱                 ─────────────── x2 = 0.5
     ● (0.5,0.5) ← hulls      (0,0)+   (1,0)+
    ╱       ╲   cross
 (0,0)+     (1,0)−

Key lesson

Separability is geometric: intersecting hulls ⇒ no hyperplane.

Common mistakes

  • Forgetting the in distances.
  • Writing the margin as (that is one side only).

Exam traps

  • Support vectors lie on the margin; margin .
  • Maximise ≡ minimise .

Connections: Perceptron (C49), logistic (C27), Lagrange dual (C31), soft margin (C32).

Mastery checklist

  • Derive margin .
  • Identify support vectors.
  • State the hull criterion.

C31. The Lagrange Dual of the Hard-Margin SVM and the KKT Conditions

Intuition

Constrained optimisation: introduce a “price” for violating each constraint. At the optimum only constraints that are tight have nonzero price. The dual problem is over the prices and — crucially — the data enter only through inner products , which is what makes kernels possible (C33).

Derivation

Lagrangian: Minimise over , maximise over . Substitute: and , so Dual problem: A concave quadratic program (QP); in matrix form: minimise with , , .

KKT conditions: stationarity (above), primal feasibility , dual feasibility , and complementary slackness Hence the point is on the margin (support vector); non-SVs have . from any SV : . Prediction: .

Key insights

  • is a linear combination of support vectors only.
  • Strong duality (convex problem): .
  • Complexity depends on (size of the QP), not on .

Worked examples

Example 1 — Very Easy · The 1-D dual

  • Problem: 1-D: , . Solve the dual.
  • Asked: .
  • Concept: Dual QP.
  • Approach: .
  • Solution: Gram: , others . ; . . SV : . Boundary at ; margin = distance between the points ✓.
  • Final answer: , , .

Key lesson

The constraint ties the multipliers together.

Example 2 — Easy · Two points in 2-D

  • Problem: 2-D: , . Solve.
  • Asked: .
  • Concept: Dual with a Gram matrix.
  • Approach: .
  • Solution: , , . . ; . . . Margin ✓.
  • Final answer: each, , .

Key lesson

Verify with the geometric answer.

Example 3 — Moderate · From α back to W and b

  • Problem: Points , , with multipliers . Compute and classify and .
  • Asked: , two labels.
  • Concept: Recover the primal from the dual.
  • Approach: .
  • Solution: . SV : ; check with : ✓. . . . Check ✓.
  • Final answer: , ; labels .

Key lesson

Two different SVs must give the same (a consistency check).

Example 4 — Hard · A support vector with α = 0

  • Problem: Points , , . Find the optimal and explain why one point on the margin has .
  • Asked: Solution and explanation.
  • Concept: Complementary slackness is one-directional.
  • Approach: Geometry first (margin = distance between the hulls), then recover .
  • Solution: The negative hull is the segment from to . The point of it closest to is , at distance . So the margin is , the boundary is the horizontal line halfway: , . Check: : ; : ; : — all three lie on the margin. Dual: . .
  • Final answer: , .

Key lesson

on the margin, but a point on the margin can still have (a “degenerate” SV). Removing it does not change the solution.

Example 5 — Very Hard · Strong duality check

  • Problem: For the data of Example 3, solve the dual from scratch and verify strong duality and all KKT conditions.
  • Asked: , , primal value.
  • Concept: Strong duality.
  • Approach: Eliminate .
  • Solution: Gram entries: , , , anything with equals . . ; likewise ; . . Primal: ✓ equal ⇒ strong duality. KKT: all three constraints tight ( for each) ⇒ complementary slackness holds for every ; ✓; ✓.
  • Final answer: , optimal value for both problems.

Key lesson

Primal and dual optimal values coincide; use this as a numerical check.

Common mistakes

  • Forgetting the equality constraint .
  • Using with a non-support vector.

Exam traps

  • The lecture’s "" is just .
  • The lecture says ” for support vectors and otherwise” — true except for degenerate boundary points.

Connections: C30, C32 (box constraint), C33 (kernels replace ).

Mastery checklist

  • Derive the dual from the Lagrangian.
  • State the KKT conditions.
  • Recover from .

C32. Soft-Margin SVM (Slack Variables and the Parameter )

Intuition

Real data overlap. Allow some points to be inside the street or on the wrong side, but pay a penalty proportional to how badly they violate the margin. sets the exchange rate between a wide street and violations.

Formal definition

Slack (the lecture’s notation; often ): Equivalent: (hinge loss).

slackmeaning
correct and outside the margin
inside the margin but correctly classified (margin error)
exactly on the boundary
misclassified

Derivation of the dual

; ; . Substituting gives the same dual as hard margin, but with the box constraint (since ). recovers the hard margin.

KKT / what means: complementary slackness and :

Position
correct, outside margin (not an SV)
exactly on the margin ()
inside the margin or misclassified

All points with are support vectors (the lecture’s wording that margin-violators “are not support vectors” is non-standard). is computed from a support vector with : .

Effect of

Large ⇒ violations are expensive ⇒ narrow margin, few violations, risk of overfitting. Small ⇒ wide margin tolerant of violations, may underfit. Choose by cross-validation (C13).

Worked examples

Example 1 — Very Easy · Compute the slacks

  • Problem: , . Compute for , , , , .
  • Asked: Slacks and categories.
  • Concept: Hinge slack.
  • Approach: , .
  • Solution: : , , (correct, outside margin). : , (margin error). : , (misclassified). : , , (on margin). : , , (misclassified).
  • Final answer: .

Key lesson

⇔ misclassified.

Example 2 — Easy · The objective value

  • Problem: Compute the soft-margin objective for Example 1 with .
  • Asked: Value.
  • Concept: .
  • Approach: Sum.
  • Solution: ; . Total .
  • Final answer: .

Key lesson

The penalty dominates when many points violate.

Example 3 — Moderate · The effect of C

  • Problem: Compare candidate A: (objective part: , ) with candidate B: . Which wins for and ?
  • Asked: Winner vs .
  • Concept: Trade-off.
  • Approach: Compute B’s slacks.
  • Solution: B: . : . : . : . : . : . ; . : A , B ⇒ B. : A , B ⇒ A. Break-even: .
  • Final answer: B for , A for .

Key lesson

Large prefers fewer violations (narrower margin); small prefers a wider margin.

Example 4 — Hard · Read the multipliers

  • Problem: ; labels and multipliers . (a) Check . (b) Classify each point’s status. (c) Compute and which are forced to .
  • Asked: Checks and statuses.
  • Concept: KKT table.
  • Approach: Apply the table.
  • Solution: (a) ✓. (b) Point 1: ⇒ inside margin or misclassified. Point 2: ⇒ correct, outside margin. Point 3: ⇒ on margin. Point 4: ⇒ violator. Point 5: on margin. (c) . forces for points 2, 3, 5. Support vectors: points 1,3,4,5.
  • Final answer: 4 support vectors; two on the margin (3, 5), two violators (1, 4).

Key lesson

The multipliers tell you where each point sits without looking at coordinates.

Example 5 — Very Hard · A small C widens the margin

  • Problem: 1-D: , with . Solve both the dual and the primal and show they agree. Compare with the hard margin ().
  • Asked: , margin, slacks.
  • Concept: The box constraint binds.
  • Approach: Dual on .
  • Solution: Dual: increases on ; with the maximum over is at . . Primal: with symmetry, objective with , (valid when both ): , so objective ; derivative ✓ agrees. Margin (vs hard margin). With : ⇒ ; ⇒ . Both have ✓.
  • Final answer: , margin , slacks each; both points are margin violators with .

Key lesson

Small widens the margin and tolerates slack; here it also makes non-unique (any gives the same objective).

Common mistakes

  • Writing for the hard-margin case.
  • Forgetting that points are support vectors.

Exam traps

  • The dual objective is identical to hard margin; only the constraints differ.
  • ⇔ hard margin; the lecture’s range is .
  • The lecture’s remark “larger pays more attention to points near the decision boundary” is about the same effect (fewer tolerated violations).

Connections: C30, C31, hinge loss, cross-validation (C13).

Mastery checklist

  • Define slack and its four cases.
  • Derive the box-constrained dual.
  • Classify points from and .

C33. The Kernel Trick

Intuition

If the data are not linearly separable in the original space, lift them to a higher-dimensional space where a straight cut works. Better still, never actually go there: the dual only needs inner products, and a kernel function computes the inner product in the lifted space directly from the original coordinates.

 1-D: − − [+ + +] − −          lift x → (x, x²)        x²
       not separable        ───────────────────►        |  −          −
                                                         |    + + +        a straight line
                                                         |  ───────────    now separates
                                                         +--------------→ x

Formal definition

Feature map (, possibly infinite). Kernel .

  • Dual in feature space: , , . The dual lives in — its dimension does not change.
  • Prediction: , with for any .

Worked derivation: the polynomial kernel

for : This equals for (a map ): the product of corresponding entries gives each of the six terms above. In general in corresponds to a feature space of dimension . The map for a given kernel is not unique (the lecture exhibits a second ; its printed 7-vector contains a repeated component and omits the constant, so check it before use).

Key insights

  • Cost of : ; cost of explicit : or infinite.
  • A kernel SVM is a linear SVM in feature space, a non-linear classifier in input space.

Worked examples

Example 1 — Very Easy · Evaluate a kernel

  • Problem: for , .
  • Asked: .
  • Concept: Kernel evaluation.
  • Approach: Inner product first.
  • Solution: ; .
  • Final answer: .

Key lesson

One inner product and a power — no feature map needed.

Example 2 — Easy · Check against φ

  • Problem: Verify for Example 1 using .
  • Asked: Same value via .
  • Concept: Equivalence of kernel and feature map.
  • Approach: Compute both vectors.
  • Solution: ; . Products: . Sum ✓.
  • Final answer: .

Key lesson

The kernel hides a 6-D dot product.

Example 3 — Moderate · An explicit non-linear map (lecture)

  • Problem: Positives ; negatives . Use if , else . Transform all points and find a separating line.
  • Asked: Images and a hyperplane.
  • Concept: Explicit non-linear map.
  • Approach: Compute the four negative images.
  • Solution: Positives have ⇒ unchanged. Negatives (norm ): ; ; ; . Positives have ; negatives . The line separates (, ).
  • Final answer: Separated by in the transformed space (the original space has no separating line — the negatives form a ring around the positives).

Key lesson

A well-chosen turns a non-separable set into a separable one.

Example 4 — Hard · XOR with a quadratic kernel

  • Problem: XOR: , , , . With solve the hard-margin dual.
  • Asked: , classifier.
  • Concept: Kernel SVM end-to-end.
  • Approach: Build the Gram matrix; use symmetry.
  • Solution: : same point ⇒ ; opposite points: ⇒ ; all other pairs ⇒ . So , . By symmetry all . . . Decision value at : ; at : . With every point satisfies ✓ (all four are SVs). Expanding the kernel, only the cross term survives: .
  • Final answer: , , (class iff ).

Key lesson

The classic XOR problem is linearly solvable in the feature ; the kernel finds it automatically.

Example 5 — Very Hard · How big is the feature space?

  • Problem: (a) Feature-space dimension of for ; ; . (b) Show the Gaussian kernel’s feature space is infinite-dimensional. (c) Cost comparison for , .
  • Asked: Dimensions and argument.
  • Concept: Why kernels matter.
  • Approach: ; Taylor series.
  • Solution: (a) ; ; . (b) , and with . Each term is a polynomial kernel of degree with its own feature space; the sum over all gives infinitely many features. (c) : about multiplications + a cube; : coordinates per point.
  • Final answer: , , ; Gaussian: .

Key lesson

The trick turns an intractable (or impossible) computation into .

Common mistakes

  • Computing explicitly when a kernel is available.
  • Forgetting to use the kernel in prediction as well as training.

Exam traps

  • The dual has variables regardless of ‘s dimension.
  • ; need not be unique.

Connections: C31, C32, C34.

Mastery checklist

  • Write the kernelised dual and predictor.
  • Derive the quadratic-kernel feature map.
  • Solve XOR with a kernel.

Intuition

Not every similarity function is an inner product in some space. A function qualifies only if every Gram matrix it generates is symmetric and positive semidefinite (PSD) — i.e. behaves like a matrix of dot products.

Formal conditions

  1. Symmetric: .
  2. Positive semidefinite: for every , points and scalars : , i.e. the Gram matrix is PSD (all eigenvalues ).
  3. Mercer’s theorem: for a symmetric with for all square-integrable , there is a space and map with .

Popular kernels

  • Polynomial: .
  • Gaussian / RBF: , (the lecture prints , a typo — see Example 4).
  • Sigmoid: — not PSD for all parameters.

Derivation: an explicit-feature kernel is PSD

If then . ∎

Closure properties (standard)

Sum of kernels, positive scalar multiple, and product of kernels are kernels.

Worked examples

Example 1 — Very Easy · Is a kernel?

  • Problem: Is a valid kernel?
  • Asked: Validity.
  • Concept: Symmetry.
  • Approach: Swap arguments.
  • Solution: unless ⇒ not symmetric.
  • Final answer: No.

Key lesson

Check symmetry first (it is the cheapest test).

Example 2 — Easy · Which Gram matrix is legal?

  • Problem: Which Gram matrix can come from a kernel: or ?
  • Asked: PSD test.
  • Concept: Eigenvalues.
  • Approach: Eigenvalues of are .
  • Solution: : ⇒ not PSD (the vector gives ). : ⇒ PSD.
  • Final answer: only.

Key lesson

A negative eigenvalue disqualifies.

Example 3 — Moderate · Prove the polynomial kernel is PSD

  • Problem: Prove that is PSD.
  • Asked: Proof.
  • Concept: Explicit feature map.
  • Approach: Use the from C33.
  • Solution: with ⇒ .
  • Final answer: PSD.

Key lesson

Exhibiting is the cleanest proof of validity.

Example 4 — Hard · The sign typo in the Gaussian kernel

  • Problem: Points and (1-D), . Build the Gram matrix for (a) and (b) the lecture’s printed . Check PSD.
  • Asked: Which is valid.
  • Concept: PSD check; sign typo.
  • Approach: Eigenvalues of are .
  • Solution: (a) : eigenvalues ✓. (b) : eigenvalues ✗.
  • Final answer: Only the negative exponent gives a valid kernel.

Key lesson

Similarity must decrease with distance; the lecture’s printed sign is a typo.

Example 5 — Very Hard · A broken sigmoid kernel and closure

  • Problem: (a) Show the sigmoid kernel with fails PSD using points and . (b) Show closure: if are kernels then is; give a valid composite such as .
  • Asked: Counter-example and closure proof.
  • Concept: Mercer validity.
  • Approach: Determinant of the Gram matrix.
  • Solution: (a) ; ; . ⇒ one negative eigenvalue () ⇒ not PSD even though . (b) ✓. The composite is a sum of the polynomial and Gaussian kernels, both valid, hence valid.
  • Final answer: The sigmoid kernel is not guaranteed valid; sums of kernels are kernels.

Key lesson

Validity depends on parameters; verify before relying on the dual being concave.

Common mistakes

  • Believing any similarity score is a kernel.
  • Forgetting that the Gaussian kernel has a negative exponent.

Exam traps

  • PSD must hold for all and point sets.
  • RBF-kernel SVM a Gaussian RBF network whose centres are the support vectors (lecture): .

Connections: C33; Gaussian-kernel bandwidth and tuned by cross-validation (C13).

Mastery checklist

  • State symmetric + PSD + Mercer.
  • Test PSD by eigenvalues of a Gram matrix.
  • Write the Gaussian-kernel decision function.