Support Vector Machines and Support Vector Regression › Example

Michael BrenndoerferOctober 26, 20265 min read
Machine Learning from ScratchAll chapters
Part 7 of 15Supervised Learning Beyond Linear Models
Chapter 56Support Vector Machines and Support Vector Regression › Example
Reading section 6 of 10 · Example · 5 minSection 6 of 10 · 5 min

Section 6 of 10 · 5 min read

Support Vector Machines and Support Vector Regression › ExampleSection view · Sections in this chapter6 / 10 ↓
Reading tools Definitions, links & notes

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Notes & comments

Two points suffice to work the margin calculation by hand. Let

x1=(1,1),  y1=+1,x2=(−1,−1),  y2=−1.\mathbf{x}_1 = (1, 1), \; y_1 = +1, \qquad \mathbf{x}_2 = (-1, -1), \; y_2 = -1.

where:

  • x1,x2∈R2\mathbf{x}_1, \mathbf{x}_2 \in \mathbb{R}^2: the two training inputs
  • y1,y2∈{−1,+1}y_1, y_2 \in \{-1, +1\}: their corresponding labels

The hard-margin problem for these two points is

min⁡w, b  12(w12+w22)s.t.  w1+w2+b≥1,w1+w2−b≥1.\begin{aligned} \min_{\mathbf{w},\, b} \;& \tfrac{1}{2}(w_1^2 + w_2^2) \\ \text{s.t.} \;& w_1 + w_2 + b \geq 1, \\ & w_1 + w_2 - b \geq 1. \end{aligned}

We can prove the symmetry rather than assume it. Let s=w1+w2s=w_1+w_2. The two constraints imply s≥1+∣b∣s\ge1+|b|, while w12+w22≥s2/2w_1^2+w_2^2\ge s^2/2, with equality only when w1=w2w_1=w_2. Hence the objective is at least (1+∣b∣)2/4≥1/4(1+|b|)^2/4\ge1/4. Equality is attained by b=0b=0 and w1=w2=1/2w_1=w_2=1/2, proving global optimality. At this solution both margin constraints are tight:

point 1:(+1)(w1+w2)=1,point 2:(−1)(−w1−w2)=1.\text{point 1:} \quad (+1)(w_1 + w_2) = 1, \qquad \text{point 2:} \quad (-1)(-w_1 - w_2) = 1.

Both equations reduce to w1+w2=1w_1 + w_2 = 1. Under the symmetry assumption w1=w2w_1 = w_2 we find w1=w2=0.5w_1 = w_2 = 0.5, so w=(0.5,0.5)\mathbf{w} = (0.5, 0.5) and ∥w∥=0.52+0.52=0.5≈0.707\|\mathbf{w}\| = \sqrt{0.5^2 + 0.5^2} = \sqrt{0.5} \approx 0.707. The margin width is

2∥w∥=20.5=20.707…≈2.828,\begin{aligned} \frac{2}{\|\mathbf{w}\|} &= \frac{2}{\sqrt{0.5}} \\ &= \frac{2}{0.707\ldots} \\ &\approx 2.828, \end{aligned}

and the decision boundary is the line x1+x2=0x_1 + x_2 = 0.

To connect this back to the dual, let α1,α2≥0\alpha_1, \alpha_2 \geq 0 be the multipliers. The KKT stationarity condition gives

w=∑iαiyixi=α1(1,1)+α2(−1)(−1,−1)=(α1+α2)(1,1).\mathbf{w} = \sum_i \alpha_i y_i \mathbf{x}_i = \alpha_1 (1, 1) + \alpha_2 (-1) (-1, -1) = (\alpha_1 + \alpha_2)(1, 1).

Since w=(0.5,0.5)\mathbf{w}=(0.5,0.5), stationarity requires α1+α2=0.5\alpha_1+\alpha_2=0.5. The balance condition gives α1=α2=0.25\alpha_1=\alpha_2=0.25. These hard-margin multipliers are feasible for the soft-margin box constraints when C≥0.25C\ge0.25, so the same zero-slack solution is optimal there. For 0<C<0.250<C<0.25, the dual optimum has α1=α2=C\alpha_1=\alpha_2=C and w=(2C,2C)\mathbf{w}=(2C,2C). Choosing b=0b=0 then gives both points functional margin 4C<14C<1. In fact every b∈[4C−1, 1−4C]b\in[4C-1,\,1-4C] is optimal in this regime, showing that the unpenalized bias need not be unique.

Returning to the hard-margin solution, which is also the soft-margin solution for C≥0.25C\geq0.25, we can check strong duality. Its primal objective is

12∥w∥2=12(0.52+0.52)=12(0.5)=0.25.\tfrac{1}{2}\|\mathbf{w}\|^2 = \tfrac{1}{2}\bigl(0.5^2 + 0.5^2\bigr) = \tfrac{1}{2}(0.5) = 0.25.

Because both points sit exactly on the margin, their slack values are zero and the hinge-loss sum vanishes, so the primal objective equals the margin term alone. This is the dual echo of the geometric statement that the two closest points fully determine the boundary.

We can verify the calculation with the dual objective. Note that

x1⊤x1=x2⊤x2=2,x1⊤x2=−2,\mathbf{x}_1^\top \mathbf{x}_1 = \mathbf{x}_2^\top \mathbf{x}_2 = 2, \qquad \mathbf{x}_1^\top \mathbf{x}_2 = -2,

so the cross term carries a negative label product y1y2=−1y_1 y_2 = -1, and the negative inner product x1⊤x2=−2\mathbf{x}_1^\top \mathbf{x}_2 = -2 makes the product positive. Thus

∑i,jαiαjyiyjxi⊤xj=α12y12x1⊤x1+2α1α2y1y2x1⊤x2+α22y22x2⊤x2=0.252⋅2+2⋅0.25⋅0.25⋅(1)(−1)(−2)+0.252⋅2=0.0625⋅2+0.125⋅2+0.0625⋅2=0.125+0.25+0.125=0.5.\begin{aligned} \sum_{i,j} \alpha_i \alpha_j y_i y_j \mathbf{x}_i^\top \mathbf{x}_j &= \alpha_1^2 y_1^2 \mathbf{x}_1^\top \mathbf{x}_1 + 2\alpha_1\alpha_2 y_1 y_2 \mathbf{x}_1^\top \mathbf{x}_2 + \alpha_2^2 y_2^2 \mathbf{x}_2^\top \mathbf{x}_2 \\ &= 0.25^2 \cdot 2 + 2 \cdot 0.25 \cdot 0.25 \cdot (1)(-1)(-2) + 0.25^2 \cdot 2 \\ &= 0.0625 \cdot 2 + 0.125 \cdot 2 + 0.0625 \cdot 2 \\ &= 0.125 + 0.25 + 0.125 = 0.5. \end{aligned}

and the dual objective equals

∑iαi−12∑i,jαiαjyiyjxi⊤xj=(0.25+0.25)−12(0.5)=0.5−0.25=0.25.\sum_i \alpha_i - \tfrac{1}{2} \sum_{i,j} \alpha_i \alpha_j y_i y_j \mathbf{x}_i^\top \mathbf{x}_j = (0.25 + 0.25) - \tfrac{1}{2}(0.5) = 0.5 - 0.25 = 0.25.

where:

  • αi\alpha_i: the optimal dual multipliers α1=α2=0.25\alpha_1 = \alpha_2 = 0.25 from the stationarity condition
  • yiy_i: the labels y1=+1y_1 = +1 and y2=−1y_2 = -1
  • xi⊤xj\mathbf{x}_i^\top \mathbf{x}_j: the pairwise inner products, x1⊤x1=x2⊤x2=2\mathbf{x}_1^\top \mathbf{x}_1 = \mathbf{x}_2^\top \mathbf{x}_2 = 2 and x1⊤x2=−2\mathbf{x}_1^\top \mathbf{x}_2 = -2
  • 0.50.5: the value of the double sum computed above

The dual objective value 0.250.25 matches the primal value 0.250.25, which is consistent with strong duality.

Now consider a slightly harder case that exercises the soft margin. Add a third point x3=(2,2)\mathbf{x}_3 = (2, 2) with y3=−1y_3 = -1. Keep the original candidate w=(0.5,0.5)\mathbf{w} = (0.5, 0.5) and b=0b = 0; rescaling it would change the functional margins used by the objective. For the third point,

y3f(x3)=(−1)(0.5⋅2+0.5⋅2)=−2.y_3 f(\mathbf{x}_3) = (-1)\bigl(0.5\cdot 2+0.5\cdot 2\bigr) = -2.

Its smallest feasible slack at this candidate is therefore

ξ3=max⁡(0,1−y3f(x3))=3.\xi_3=\max(0,1-y_3 f(\mathbf{x}_3))=3.

The original two points still have zero slack, so this candidate has objective 14+3C\tfrac14+3C. This is a feasible value, not proof that the candidate is optimal.

In fact, this dataset has a useful structure: the positive point lies between two negative points on the same line. For every C>0C>0, the exact soft-margin optimum is the constant classifier

w=(0,0),b=−1,(ξ1,ξ2,ξ3)=(2,0,0).\mathbf{w}=(0,0),\qquad b=-1,\qquad (\xi_1,\xi_2,\xi_3)=(2,0,0).

It predicts the negative class for all three points, pays hinge loss two for the positive point, and has objective 2C2C. We can prove optimality using the dual multipliers

(α1,α2,α3)=(C,C3,2C3).(\alpha_1,\alpha_2,\alpha_3) =\left(C,\frac C3,\frac{2C}{3}\right).

They satisfy the box constraints and α1−α2−α3=0\alpha_1-\alpha_2-\alpha_3=0, and they give

∑iαiyixi=(C+C3−4C3)(1,1)=(0,0).\sum_i\alpha_i y_i\mathbf{x}_i =\left(C+\frac C3-\frac{4C}{3}\right)(1,1) =(0,0).

The dual objective is consequently ∑iαi−12∥w∥2=2C\sum_i\alpha_i-\tfrac12\|\mathbf{w}\|^2=2C, matching the primal value. A feasible primal value matching a feasible dual value establishes global optimality.

Here CC changes the objective value without changing the fitted classifier. The usual margin-versus-loss tradeoff does not promise that the boundary moves whenever CC changes. Since w=0\mathbf{w}=0, this constant solution has no separating hyperplane with a finite margin width 2/∥w∥2/\|\mathbf{w}\|; the soft-margin objective is still well defined.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy