Hard-Margin Support Vector Machines: Choosing the Widest Safe Linear Boundary

Hard-margin Support Vector Machine hero image

Learning Objectives

By the end of this lesson, you should be able to:

  1. Explain why finding any separating boundary is not enough when several valid boundaries exist.
  2. Distinguish a raw linear score, a Logistic Regression probability, a functional margin, and a geometric distance.
  3. Derive the distance from a point to a linear decision boundary.
  4. Explain why the SVM margin must be normalized by w\lVert w\rVert.
  5. Build the hard-margin SVM objective from the maximum-margin idea.
  6. Interpret the constraints yi(wTxi+b)1y_i(w^Tx_i+b)\geq1 geometrically.
  7. Explain why support vectors, rather than all training observations equally, determine the final boundary.
  8. Describe what hard-margin SVM guarantees, when it has no solution, and what it does not provide.
  9. Train and inspect a linear SVM with a leakage-safe scikit-learn workflow.

Scope of this lesson

This lesson studies hard-margin SVM only. We assume that the training data is linearly separable. Slack variables, the penalty parameter CC, hinge-loss optimization, and soft-margin SVM are intentionally reserved for the next lesson.


1. The Remaining Problem: Which Correct Boundary Should We Choose?

Consider an automated quality-control system that classifies manufactured parts from two measurements:

Each part is represented by a feature vector:

x=[x1x2]x= \begin{bmatrix} x_1\\ x_2 \end{bmatrix}

The label is:

y={+1reject the part1accept the party= \begin{cases} +1 & \text{reject the part}\\ -1 & \text{accept the part} \end{cases}

A small teaching dataset is:

Surface-defect score x1x_1 Dimension deviation x2x_2 Label yy
0.0 0.0 -1
1.0 0.0 -1
0.0 1.0 -1
0.4 0.4 -1
3.0 2.0 +1
4.0 2.0 +1
3.0 3.0 +1
4.0 3.0 +1

The two classes can be separated by a straight line. That sounds like a problem the Perceptron has already solved.

The Perceptron searches for parameters ww and bb that make every training example land on the correct side of:

wTx+b=0w^Tx+b=0

If the data is linearly separable, the Perceptron eventually finds a separating boundary. Its convergence theorem does not say that the boundary is unique, centered between the classes, or stable under small changes in the inputs.

Logistic Regression addressed a different weakness. It kept the same linear score,

z=wTx+b,z=w^Tx+b,

but transformed that score into an estimated probability through the sigmoid function. This made the output more interpretable for ranking risk and selecting decision thresholds.

However, Logistic Regression optimizes log loss. It does not explicitly ask:

Among all lines that separate the training examples, which line leaves the largest geometric safety gap?

That is the question hard-margin Support Vector Machines answer.

Several valid separating boundaries

Diagram teaching notes

What appears: The same separable dataset with three different linear boundaries.
What to notice: Every line classifies the displayed training observations correctly, but the two outer lines pass very close to one class.
What to explain: Correctness is a feasibility condition, not yet a rule for choosing the best feasible boundary.
Connection: The next step is to replace “find any separator” with a measurable preference for one separator.

Concept Check

A model classifies every training example correctly. Does that prove the boundary is the most robust separating boundary?

No. It only proves that the boundary is feasible. Another feasible boundary may leave a larger gap between the classes.


2. What Carries Over from the Perceptron and Logistic Regression?

Hard-margin SVM is not a completely different kind of predictor. It begins with the same linear decision function:

f(x)=wTx+bf(x)=w^Tx+b

where:

The predicted class is:

y^=sign(wTx+b)\hat y=\operatorname{sign}(w^Tx+b)

Thus:

wTx+b>0w^Tx+b>0

produces class +1+1, while:

wTx+b<0w^Tx+b<0

produces class 1-1.

The decision boundary is the set of inputs for which:

wTx+b=0w^Tx+b=0

In two dimensions, this is a line. In three dimensions, it is a plane. In higher dimensions, it is a hyperplane.

2.1 What do ww and bb do geometrically?

The vector ww is perpendicular to the decision boundary. It is therefore called a normal vector to the hyperplane.

That last point will become essential.

Suppose:

w=[11],b=3w= \begin{bmatrix} 1\\ 1 \end{bmatrix}, \qquad b=-3

The boundary is:

x1+x23=0x_1+x_2-3=0

Now multiply both parameters by 55:

w=[55],b=15w'= \begin{bmatrix} 5\\ 5 \end{bmatrix}, \qquad b'=-15

The new equation is:

5x1+5x215=05x_1+5x_2-15=0

Dividing by 55 gives the original line. The geometry has not changed.

2.2 What is different from Logistic Regression?

Logistic Regression interprets:

σ(wTx+b)\sigma(w^Tx+b)

as an estimated probability under a probabilistic model.

Hard-margin SVM does not apply the sigmoid and does not fit a probability model. It uses the geometry of the score relative to the separating boundary.

A probability and a geometric distance answer different questions:

Common mistake — “Logistic Regression already chooses the best boundary.”

Logistic Regression chooses parameters that reduce log loss. Hard-margin SVM chooses parameters that maximize the smallest geometric distance from the training data to the boundary. The objectives are different, so their fitted boundaries need not be the same.

Common mistake — “A probability of 0.9 means the point is 0.9 units from the boundary.”

Probability and geometric distance use different scales and different mathematical meanings. Neither can be substituted for the other.


3. Why Correct Separation Alone Is Not Enough

For the quality-control data, consider boundaries of the form:

x1+x2=cx_1+x_2=c

The acceptable parts satisfy:

x1+x21x_1+x_2\leq1

for the closest displayed acceptable observations, while the closest reject observation satisfies:

x1+x2=5x_1+x_2=5

Therefore, any value:

1<c<51<c<5

creates a valid separating boundary.

Three possibilities are:

x1+x2=1.5x_1+x_2=1.5x1+x2=3x_1+x_2=3x1+x2=4.5x_1+x_2=4.5

All three classify the training examples correctly.

The first line passes only a short distance from the acceptable class. The third line passes only a short distance from the reject class. The middle line is centered between the closest observations from the two classes.

3.1 Fragility near a boundary

Suppose an acceptable part lies close to a boundary. A small amount of sensor noise could move its measured position across the line, changing the prediction even though the underlying part has not meaningfully changed.

A boundary with more empty space around it can tolerate a larger perturbation before the prediction flips.

This motivates a stronger objective:

Do not merely separate the training examples. Choose the separating hyperplane whose nearest training observation is as far away as possible.

This empty region is the margin.

3.2 What a wider margin does and does not mean

A wider training margin provides a geometric form of robustness. If a training point is a distance γ\gamma from the boundary, any perturbation shorter than γ\gamma cannot cross the boundary when measured in the same feature-space geometry.

This does not guarantee that:

The margin gives a principled boundary-selection rule. It is not a universal guarantee of good predictions.

Concept Check

Why not choose the boundary that is farthest from the positive class only?

Because doing so could place the boundary extremely close to the negative class. The relevant quantity is the distance to the closest observation from either class.


4. From a Linear Score to Distance from the Hyperplane

To maximize separation, we need to measure distance. The raw score:

wTx+bw^Tx+b

is not yet a geometric distance because its magnitude changes when ww and bb are rescaled.

4.1 Why the raw score cannot be the distance

Take the boundary:

x1+x23=0x_1+x_2-3=0

and the point:

x=[42]x= \begin{bmatrix} 4\\ 2 \end{bmatrix}

Using:

w=[11],b=3w= \begin{bmatrix} 1\\ 1 \end{bmatrix}, \qquad b=-3

the score is:

wTx+b=4+23=3w^Tx+b=4+2-3=3

Using the equivalent parameters:

w=[55],b=15w'= \begin{bmatrix} 5\\ 5 \end{bmatrix}, \qquad b'=-15

the score is:

wTx+b=20+1015=15w'^Tx+b'=20+10-15=15

The physical point and the geometric boundary are unchanged, so the distance cannot have changed from 33 units to 1515 units. We need a normalization that removes this arbitrary scaling.

Equivalent parameter scalings produce the same distance

Diagram teaching notes

What appears: One point and one boundary described by two different parameter pairs.
What to notice: The raw score changes by a factor of five, but the normalized distance does not change.
What to explain: A valid geometric quantity must be invariant to equivalent parameter scaling.
Connection: This motivates dividing the score by w\lVert w\rVert.

4.2 Deriving the distance formula

Let the hyperplane be:

H={z:wTz+b=0}H=\{z:w^Tz+b=0\}

For a point xx, the shortest path to the hyperplane must be perpendicular to the hyperplane. Because ww is normal to the hyperplane, the closest point xx^\star must lie somewhere along the direction of ww.

Write:

x=xtwx^\star=x-tw

where tt tells us how far to move in the normal direction.

Because xx^\star lies on the boundary:

wTx+b=0w^Tx^\star+b=0

Substitute x=xtwx^\star=x-tw:

wT(xtw)+b=0w^T(x-tw)+b=0

Distribute wTw^T:

wTxtwTw+b=0w^Tx-tw^Tw+b=0

Since:

wTw=w2w^Tw=\lVert w\rVert^2

we have:

wTx+btw2=0w^Tx+b-t\lVert w\rVert^2=0

Solving for tt:

t=wTx+bw2t=\frac{w^Tx+b}{\lVert w\rVert^2}

The displacement from xx to xx^\star is:

xx=twx-x^\star=tw

Its length is:

tw=tw\lVert tw\rVert=|t|\lVert w\rVert

Substitute the expression for tt:

distance(x,H)=wTx+bw2w\operatorname{distance}(x,H) = \left| \frac{w^Tx+b}{\lVert w\rVert^2} \right| \lVert w\rVert

Therefore:

distance(x,H)=wTx+bw\boxed{ \operatorname{distance}(x,H) = \frac{|w^Tx+b|}{\lVert w\rVert} }

Perpendicular distance from a point to a hyperplane

Diagram teaching notes

What appears: A point, its perpendicular projection onto the boundary, and the normal direction ww.
What to notice: The shortest segment is parallel to ww, not parallel to either coordinate axis.
What to explain: The numerator gives a signed algebraic score; the denominator converts it into geometric units.
Connection: The next section adds the class label so that “correct side” and “incorrect side” are handled in one expression.

4.3 Numerical distance calculation

For:

w=[11],b=3,x=[42]w= \begin{bmatrix} 1\\ 1 \end{bmatrix}, \qquad b=-3, \qquad x= \begin{bmatrix} 4\\ 2 \end{bmatrix}

the score is 33, and:

w=12+12=2\lVert w\rVert = \sqrt{1^2+1^2} = \sqrt{2}

Therefore:

distance(x,H)=322.121\operatorname{distance}(x,H) = \frac{3}{\sqrt{2}} \approx2.121

Using w=5ww'=5w and b=5bb'=5b:

distance(x,H)=1552=32\operatorname{distance}(x,H) = \frac{15}{5\sqrt{2}} = \frac{3}{\sqrt{2}}

The distance is unchanged, as required.

Common mistake — “A larger raw score always means a larger geometric distance.”

For one fixed fitted model, score magnitude and distance are proportional because w\lVert w\rVert is fixed. Across different parameter scalings or different models, raw scores cannot be compared as distances unless they are normalized by w\lVert w\rVert.


5. Functional Margin and Geometric Margin

The distance formula uses an absolute value, but classification also requires the side of the boundary.

For a labeled example (xi,yi)(x_i,y_i), define the functional margin:

γ^i=yi(wTxi+b)\hat\gamma_i = y_i(w^Tx_i+b)

where yi{1,+1}y_i\in\{-1,+1\}.

5.1 Why multiply by the label?

If yi=+1y_i=+1, correct classification requires:

wTxi+b>0w^Tx_i+b>0

Multiplying by +1+1 keeps the value positive.

If yi=1y_i=-1, correct classification requires:

wTxi+b<0w^Tx_i+b<0

Multiplying by 1-1 makes the result positive.

Therefore:

yi(wTxi+b)>0y_i(w^Tx_i+b)>0

means the example is on the correct side, while:

yi(wTxi+b)<0y_i(w^Tx_i+b)<0

means it is on the wrong side.

The functional margin combines both label cases into one expression.

5.2 Why the functional margin is still not geometric

If ww and bb are multiplied by 55, the functional margin is also multiplied by 55, even though the boundary does not move.

The geometric margin of example ii is therefore:

γi=yi(wTxi+b)w\gamma_i = \frac{y_i(w^Tx_i+b)}{\lVert w\rVert}

This is a signed distance measured toward the example's correct class side.

For a separating boundary, every training example has a positive geometric margin.

5.3 Margin of the complete training dataset

A boundary is only as safe as its closest training example. The margin of the dataset with respect to (w,b)(w,b) is:

γ(w,b)=mini=1,,nyi(wTxi+b)w\gamma(w,b) = \min_{i=1,\ldots,n} \frac{y_i(w^Tx_i+b)}{\lVert w\rVert}

where:

Hard-margin SVM seeks the boundary that maximizes this minimum distance.

Common mistake — “The average distance should be maximized.”

A large average could hide one observation that is almost touching the boundary. The minimum distance directly controls the narrowest part of the safety gap.

Common mistake — “SVM distance is a probability-like confidence score.”

The geometric margin is measured in the geometry of the feature space. It is not constrained to [0,1][0,1], does not sum across classes, and is not automatically calibrated as a probability.


6. From Maximum Margin to the Hard-Margin SVM Objective

We can now write the boundary-selection goal directly:

maxw,bminiyi(wTxi+b)w\max_{w,b} \min_i \frac{y_i(w^Tx_i+b)}{\lVert w\rVert}

subject to every example being correctly classified.

This expression captures the right idea, but it is inconvenient to optimize because:

The next step uses the scaling freedom deliberately.

6.1 Canonical scaling

For any separating hyperplane, all functional margins are positive. Because multiplying (w,b)(w,b) by a positive constant leaves the boundary unchanged, we may choose a scale at which the smallest functional margin is exactly 11:

miniyi(wTxi+b)=1\min_i y_i(w^Tx_i+b)=1

This convention is called canonical scaling.

Under this scale, every training example satisfies:

yi(wTxi+b)1y_i(w^Tx_i+b)\geq1

At least one observation satisfies equality.

Now the dataset's geometric margin becomes:

γ=miniyi(wTxi+b)w\gamma = \min_i \frac{y_i(w^Tx_i+b)}{\lVert w\rVert}

Because the minimum numerator is 11:

γ=1w\gamma=\frac{1}{\lVert w\rVert}

Therefore maximizing the margin is equivalent to minimizing w\lVert w\rVert.

6.2 Why minimize the squared norm?

Instead of minimizing:

w\lVert w\rVert

we minimize:

12w2\frac{1}{2}\lVert w\rVert^2

This produces the same optimal ww because squaring is monotone for nonnegative values. The square is smoother and easier to differentiate. The factor 1/21/2 cancels the 22 that appears when differentiating the squared norm:

w(12w2)=w\nabla_w\left(\frac{1}{2}\lVert w\rVert^2\right)=w

The factor does not change the location of the optimum.

6.3 The hard-margin primal problem

The complete optimization problem is:

minw,b12w2subject toyi(wTxi+b)1,i=1,,n\boxed{ \begin{aligned} \min_{w,b} \quad& \frac{1}{2}\lVert w\rVert^2 \\ \text{subject to} \quad& y_i(w^Tx_i+b)\geq1, \qquad i=1,\ldots,n \end{aligned} }

Every component has a distinct role.

Objective

12w2\frac{1}{2}\lVert w\rVert^2

Minimizing the norm makes:

1w\frac{1}{\lVert w\rVert}

as large as possible. This widens the distance from the decision boundary to the closest training observations.

Constraints

yi(wTxi+b)1y_i(w^Tx_i+b)\geq1

The constraints require every training example to:

  1. be classified correctly, and
  2. lie on or beyond its class's margin boundary.

For yi=+1y_i=+1:

wTxi+b1w^Tx_i+b\geq1

For yi=1y_i=-1:

wTxi+b1w^Tx_i+b\leq-1

The two margin hyperplanes are therefore:

wTx+b=+1w^Tx+b=+1

and:

wTx+b=1w^Tx+b=-1

The decision boundary lies halfway between them:

wTx+b=0w^Tx+b=0

Hard-margin SVM boundary and canonical margin hyperplanes

Diagram teaching notes

What appears: The decision boundary, the two canonical margin hyperplanes, the margin band, and circled closest observations.
What to notice: No training point lies inside the band. The closest points touch its edges.
What to explain: The constraints define feasibility; the norm objective widens the band while preserving feasibility.
Connection: The touching observations will be named support vectors because they support the optimal band.

6.4 Margin to one side versus full margin width

The distance from the decision boundary:

wTx+b=0w^Tx+b=0

to either margin hyperplane is:

1w\frac{1}{\lVert w\rVert}

The distance from the negative margin hyperplane to the positive margin hyperplane is:

2w\frac{2}{\lVert w\rVert}

Both quantities are called “the margin” in different explanations.

This lesson uses:

Always check which convention is being used.

6.5 Why the constraints are essential

Suppose we minimized only:

12w2\frac{1}{2}\lVert w\rVert^2

without classification constraints. The smallest possible norm would be obtained by:

w=0w=0

That does not define a useful separating boundary.

The objective alone prefers a small weight vector. The constraints force the small-norm solution to remain a valid separator.

Checkpoint Interpretation

Hard-margin SVM is not “minimize the weights” by itself. It is:

Find the smallest-norm weight vector among all parameter choices that keep every training observation on the correct side of the margin.


7. Manual Walkthrough: Solving a Two-Feature Example

Return to the quality-control dataset.

The closest acceptable observations lie on:

x1+x2=1x_1+x_2=1

The closest reject observation is:

x=[32]x= \begin{bmatrix} 3\\ 2 \end{bmatrix}

which lies on:

x1+x2=5x_1+x_2=5

The line centered between these two parallel lines is:

x1+x2=3x_1+x_2=3

Written as a decision function:

x1+x23=0x_1+x_2-3=0

This boundary is geometrically correct, but it is not yet in canonical scale. We need the closest positive example to have score +1+1 and the closest negative examples to have score 1-1.

Choose:

w=[0.50.5],b=1.5w= \begin{bmatrix} 0.5\\ 0.5 \end{bmatrix}, \qquad b=-1.5

Then:

wTx+b=0.5x1+0.5x21.5w^Tx+b = 0.5x_1+0.5x_2-1.5

The decision boundary is still:

0.5x1+0.5x21.5=00.5x_1+0.5x_2-1.5=0

Multiplying by 22 gives x1+x23=0x_1+x_2-3=0, so the same line is represented.

7.1 Verify the closest acceptable observations

For:

x=[10],y=1x= \begin{bmatrix} 1\\ 0 \end{bmatrix}, \qquad y=-1

the score is:

wTx+b=0.5(1)+0.5(0)1.5=1w^Tx+b = 0.5(1)+0.5(0)-1.5 = -1

The functional margin is:

y(wTx+b)=(1)(1)=1y(w^Tx+b) = (-1)(-1) = 1

The constraint is active exactly at equality.

For:

x=[01],y=1x= \begin{bmatrix} 0\\ 1 \end{bmatrix}, \qquad y=-1

the same calculation gives a functional margin of 11.

7.2 Verify the closest reject observation

For:

x=[32],y=+1x= \begin{bmatrix} 3\\ 2 \end{bmatrix}, \qquad y=+1

the score is:

wTx+b=0.5(3)+0.5(2)1.5w^Tx+b = 0.5(3)+0.5(2)-1.5=1.5+11.5=1.5+1-1.5=1=1

The functional margin is:

y(wTx+b)=(+1)(1)=1y(w^Tx+b) = (+1)(1) = 1

This constraint is also active.

7.3 Verify noncritical observations

For the reject observation:

x=[43]x= \begin{bmatrix} 4\\ 3 \end{bmatrix}

the score is:

0.5(4)+0.5(3)1.5=20.5(4)+0.5(3)-1.5=2

Its functional margin is 2>12>1. It lies beyond the positive margin hyperplane and is not one of the closest observations.

The constraint table is:

Point xx Label yy Score wTx+bw^Tx+b Functional margin y(wTx+b)y(w^Tx+b) Constraint status
(1,0)(1,0) -1 -1 1 Active
(0,1)(0,1) -1 -1 1 Active
(3,2)(3,2) +1 1 1 Active
(4,2)(4,2) +1 1.5 1.5 Strictly satisfied
(4,3)(4,3) +1 2 2 Strictly satisfied

7.4 Compute the margin

The norm of ww is:

w=0.52+0.52\lVert w\rVert = \sqrt{0.5^2+0.5^2}=0.50.7071= \sqrt{0.5} \approx0.7071

The distance from the boundary to the nearest training observation is:

γ=1w=10.70711.414\gamma = \frac{1}{\lVert w\rVert} = \frac{1}{0.7071} \approx1.414

The full margin width is:

2w2.828\frac{2}{\lVert w\rVert} \approx2.828

The optimization objective at this solution is:

12w2=12(0.5)=0.25\frac{1}{2}\lVert w\rVert^2 = \frac{1}{2}(0.5) = 0.25

7.5 Compare with off-center boundaries

For a line:

x1+x2=cx_1+x_2=c

the distance from a point with coordinate sum ss is:

sc2\frac{|s-c|}{\sqrt{2}}

For c=2c=2:

For c=3c=3:

For c=4c=4:

The centered boundary maximizes the minimum distance.

7.6 Complete prediction for a new part

Consider:

xnew=[3.62.4]x_{\text{new}} = \begin{bmatrix} 3.6\\ 2.4 \end{bmatrix}

The raw score is:

f(xnew)=0.5(3.6)+0.5(2.4)1.5f(x_{\text{new}}) = 0.5(3.6)+0.5(2.4)-1.5=1.8+1.21.5=1.8+1.2-1.5=1.5=1.5

Because the score is positive:

y^=+1\hat y=+1

The part is predicted to be rejected.

Its signed geometric distance is:

1.50.52.121\frac{1.5}{\sqrt{0.5}} \approx2.121

This is a distance in the chosen feature space, not a probability.

Common mistake — “Every future observation must be outside the margin.”

The hard-margin constraints apply to the training observations used to fit the model. A new observation can fall inside the margin band and still receive a class prediction based on the sign of its score.


8. How the Hard-Margin SVM Learns

The Perceptron learns through local mistake-driven updates. Hard-margin SVM is defined differently: it solves a constrained optimization problem over the complete training dataset.

8.1 The optimization is a quadratic program

The objective:

12w2\frac{1}{2}\lVert w\rVert^2

is quadratic in ww.

Each constraint:

yi(wTxi+b)1y_i(w^Tx_i+b)\geq1

is linear in the parameters ww and bb.

An optimization problem with a quadratic objective and linear constraints is a quadratic program.

8.2 Why constrained optimization is necessary

There are two simultaneous goals:

  1. Keep every training observation on the correct side of its margin boundary.
  2. Make the weight norm as small as possible so the geometric margin becomes large.

Neither goal can replace the other.

8.3 Convexity and the global solution

The squared norm is a convex function. The feasible region defined by linear inequalities is also convex.

This matters because a convex optimization problem has no inferior local minima. If a numerical solver converges to an optimum that satisfies the constraints, it has reached a global optimum of the stated hard-margin problem.

The optimal weight vector ww is unique because the objective is strictly convex in ww. In unusual degenerate configurations, more than one bias value may describe the same optimal orientation while satisfying the constraints, although typical datasets pin down bb through support vectors from both classes.

Convexity does not guarantee strong performance on unseen data. It guarantees that the mathematical training problem is solved globally.

8.4 What a solver does conceptually

A practical quadratic-programming or SVM solver conceptually performs the following work:

  1. Starts with candidate parameters or dual coefficients.
  2. Checks how the constraints and objective behave.
  3. Adjusts the candidate solution.
  4. Moves toward a feasible point with smaller w2\lVert w\rVert^2.
  5. Stops when optimization conditions are satisfied within a numerical tolerance.

Libraries often use specialized decomposition or coordinate-optimization methods rather than a simple full-batch gradient descent loop. Therefore, “one epoch” is not the defining unit of hard-margin SVM learning.

8.5 Language-independent algorithm

Input: linearly separable training examples (x_i, y_i) labels y_i in {-1, +1} 1. Place features on meaningful, comparable scales. 2. Define the optimization problem: minimize (1/2) * ||w||^2 subject to y_i * (w^T x_i + b) >= 1 for every training example i 3. Solve the constrained convex quadratic program. 4. Identify training examples satisfying: y_i * (w^T x_i + b) = 1 These are the support vectors. 5. For a new input x: score = w^T x + b predict +1 if score > 0 predict -1 if score < 0 6. When a geometric distance is needed: distance = score / ||w||

Optimization Checkpoint

Hard-margin SVM does not repeatedly repair only the latest mistake. It selects parameters by optimizing a global geometric objective under constraints from every training example.


9. Support Vectors: The Observations That Hold the Boundary in Place

At the optimum, some observations satisfy:

yi(wTxi+b)=1y_i(w^Tx_i+b)=1

These points lie exactly on one of the margin hyperplanes. They are called support vectors.

The name is geometric: these observations support the widest feasible margin band. If the band expanded any farther without rotating or shifting appropriately, it would run into one of them.

Observations satisfying:

yi(wTxi+b)>1y_i(w^Tx_i+b)>1

lie beyond the margin. They are correctly classified with extra geometric room.

9.1 Why non-support observations often do not move the boundary

Imagine moving a training observation that lies far beyond the margin. If it remains correctly classified and does not become one of the closest points, the current optimum still satisfies its constraint. The observation does not tighten the feasible region near the optimum, so the boundary may remain unchanged.

9.2 Why moving a support vector can change the boundary

A support vector is part of the closest geometry between the two classes. Moving it can:

The boundary does not have to change after every support-vector movement in every symmetric or redundant configuration, but support vectors are the observations with direct potential to determine the optimum.

Influence of non-support observations and support vectors

Diagram teaching notes

What appears: One scene moves a non-support observation; another moves a support vector.
What to notice: The first movement leaves the closest class geometry unchanged, while the second changes it.
What to explain: Constraints that are loose at the optimum exert no immediate pressure; active constraints define the edge of feasibility.
Connection: Lagrange multipliers provide the mathematical version of this “pressure” interpretation.

Common mistake — “All training observations contribute equally.”

All observations create constraints, but only active constraints directly hold the optimal margin in place. Non-support observations can matter if they move far enough to become active.

Common mistake — “The boundary changes whenever any training point moves.”

A small movement of a non-support observation may leave the solution exactly unchanged. The important question is whether the closest class geometry or active constraints change.


10. Why Support Vectors Appear: An Accessible Dual View

The primal formulation already defines hard-margin SVM:

minw,b12w2subject toyi(wTxi+b)1\min_{w,b} \frac{1}{2}\lVert w\rVert^2 \quad \text{subject to} \quad y_i(w^Tx_i+b)\geq1

A deeper question remains:

Can we show mathematically that the final weight vector is built from the observations that press against the margin?

Lagrange multipliers answer this question.

10.1 Rewriting the constraints

Write each constraint as:

yi(wTxi+b)10y_i(w^Tx_i+b)-1\geq0

Associate a nonnegative multiplier αi0\alpha_i\geq0 with it.

Intuitively, αi\alpha_i measures how strongly the ii-th constraint participates in holding the optimum in place.

The Lagrangian is:

L(w,b,α)=12w2i=1nαi[yi(wTxi+b)1]\mathcal{L}(w,b,\alpha) = \frac{1}{2}\lVert w\rVert^2 - \sum_{i=1}^{n} \alpha_i \left[ y_i(w^Tx_i+b)-1 \right]

The first term is the margin objective. The second term incorporates the constraints.

10.2 Stationarity with respect to ww

At the optimum, differentiate with respect to ww and set the result to zero:

wL=wi=1nαiyixi=0\nabla_w\mathcal{L} = w- \sum_{i=1}^{n}\alpha_i y_i x_i = 0

Therefore:

w=i=1nαiyixi\boxed{ w= \sum_{i=1}^{n}\alpha_i y_i x_i }

This equation says that the final normal vector is a weighted combination of training observations.

10.3 Stationarity with respect to bb

Differentiate with respect to bb:

Lb=i=1nαiyi=0\frac{\partial\mathcal{L}}{\partial b} = -\sum_{i=1}^{n}\alpha_i y_i = 0

Therefore:

i=1nαiyi=0\boxed{ \sum_{i=1}^{n}\alpha_i y_i=0 }

The weighted influence of the positive and negative classes must balance in this sense.

10.4 Complementary slackness

A key optimality condition is:

αi[yi(wTxi+b)1]=0\alpha_i \left[ y_i(w^Tx_i+b)-1 \right] = 0

This product can be zero in two main ways.

Case 1: The observation lies outside the margin

If:

yi(wTxi+b)>1y_i(w^Tx_i+b)>1

then:

yi(wTxi+b)1>0y_i(w^Tx_i+b)-1>0

For the product to be zero:

αi=0\alpha_i=0

That observation disappears from the weighted expression for ww.

Case 2: The multiplier is positive

If:

αi>0\alpha_i>0

then complementary slackness requires:

yi(wTxi+b)1=0y_i(w^Tx_i+b)-1=0

so:

yi(wTxi+b)=1y_i(w^Tx_i+b)=1

The observation lies on a margin hyperplane and is a support vector.

This is the mathematical reason only a subset of observations directly determines ww.

10.5 The dual optimization problem

Substituting the stationarity conditions into the Lagrangian gives the dual form:

maxαi=1nαi12i=1nj=1nαiαjyiyjxiTxjsubject toαi0i=1nαiyi=0\boxed{ \begin{aligned} \max_{\alpha} \quad& \sum_{i=1}^{n}\alpha_i - \frac{1}{2} \sum_{i=1}^{n} \sum_{j=1}^{n} \alpha_i\alpha_jy_iy_jx_i^Tx_j \\ \text{subject to} \quad& \alpha_i\geq0 \\ & \sum_{i=1}^{n}\alpha_i y_i=0 \end{aligned} }

Every symbol has already appeared:

The dual is not required to make predictions with a linear hard-margin SVM. Its main teaching value here is that it exposes two important facts:

  1. The model depends directly on support vectors with nonzero coefficients.
  2. The observations appear through dot products.

The second fact will become important when nonlinear SVMs are introduced later. No nonlinear transformation is needed for the current hard-margin linear lesson.

Concept Check

If a training observation satisfies yi(wTxi+b)=2.4y_i(w^Tx_i+b)=2.4, what does complementary slackness imply about its multiplier?

Because the constraint is strictly satisfied, αi=0\alpha_i=0. It is not an active support vector.


11. When Does Hard-Margin SVM Work?

Hard-margin SVM requires a feasible separator satisfying:

yi(wTxi+b)1y_i(w^Tx_i+b)\geq1

for every training observation after an appropriate positive rescaling of the parameters.

This is possible exactly when the training data is linearly separable in the chosen feature representation.

11.1 Conditions that support a meaningful solution

Hard-margin SVM is most appropriate when:

11.2 What the optimization guarantees

When the constraints are feasible, the hard-margin problem finds a global maximum-margin linear separator.

More precisely, it finds a feasible hyperplane that maximizes:

miniyi(wTxi+b)w\min_i \frac{y_i(w^Tx_i+b)}{\lVert w\rVert}

This gives a principled answer to the non-uniqueness problem left by the Perceptron.

11.3 What it does not guarantee

The maximum training margin does not guarantee:

The guarantee is about the stated optimization problem, not every property desired from a deployed classifier.

11.4 One conflicting observation can make the problem infeasible

Real datasets may contain overlap, measurement noise, or incorrect labels. A single negative-labeled observation placed inside the positive region can make perfect linear separation impossible.

A conflicting observation makes hard-margin separation infeasible

Diagram teaching notes

What appears: A previously separable dataset with one conflicting acceptable label inside the reject cluster.
What to notice: No straight line can place all positive examples on one side and all negative examples on the other.
What to explain: Hard-margin SVM does not trade margin against mistakes; it declares this set of constraints infeasible.
Connection: The next SVM extension will relax the perfect-separation requirement, but that extension is outside the current lesson.

Common mistake — “A wider margin always means zero training errors.”

In hard-margin SVM, zero training errors are required before the margin is optimized. A wide margin does not cause the zero-error condition; the constraints enforce it.

Common mistake — “Increase CC to make hard-margin SVM better.”

The mathematical hard-margin formulation has no penalty parameter CC. CC belongs to soft-margin formulations. A library may use a very large CC to numerically approximate hard-margin behavior on separable data, but increasing it indefinitely is not a general performance guarantee.


12. Limitations and the Next Improvements

The maximum-margin principle solves an important Perceptron limitation, but hard-margin SVM still has boundaries.

12.1 It cannot learn a nonlinear boundary in the original feature space

The prediction rule remains:

y^=sign(wTx+b)\hat y=\operatorname{sign}(w^Tx+b)

A single linear hyperplane cannot represent patterns such as concentric classes or XOR without changing the feature representation.

12.2 It has no feasible solution for nonseparable training data

Overlap, label noise, and contradictory observations can make the constraints impossible to satisfy.

This is the most immediate practical limitation of hard margin.

12.3 The solution can be controlled by a small number of extreme observations

Support-vector sparsity is useful, but it also means that unusual closest observations can strongly determine the margin and orientation.

The maximum-margin boundary is optimal for the observed feature-space geometry. If a support vector is a measurement error or unrepresentative outlier, the learned boundary may be undesirable for deployment.

12.4 It does not produce probabilities

The outputs of:

model.decision_function(X)

are signed scores. After normalization by w\lVert w\rVert, they are signed geometric distances in the model's feature space.

Neither output is automatically a class probability.

12.5 It is sensitive to feature scale

The SVM objective uses Euclidean length:

w\lVert w\rVert

and geometric distance in feature space. Changing a feature from meters to millimeters changes the numerical geometry unless preprocessing compensates for it.

A model can run without standardization, but then its margin reflects the arbitrary measurement units. Scaling is therefore usually essential unless the units are intentionally chosen to encode relative importance.

12.6 Short preview of later improvements

Hard-margin limitation Related improvement
Cannot learn nonlinear boundaries Kernel methods or explicit nonlinear features
Has no feasible solution on nonseparable data Soft-margin SVM
Can be dominated by extreme support observations Soft-margin and robust data-validation practices
Does not produce probabilities Probability calibration or a probabilistic classifier such as Logistic Regression
Is sensitive to feature scale Standardization inside the training pipeline

The next lesson should begin with the second row: how to preserve the maximum-margin idea when perfect separation is unrealistic.


13. Perceptron, Logistic Regression, and Hard-Margin SVM

All three methods can use the same linear score:

wTx+bw^Tx+b

Their central difference is not the prediction formula. It is what they optimize and how they interpret the output.

Property Perceptron Logistic Regression Hard-Margin SVM
Boundary shape Linear Linear Linear
Main training goal Find a separator through mistake-driven updates Minimize probabilistic log loss Maximize the minimum geometric margin
Requirement of perfect separability Needed for convergence guarantee Not required for fitting a regularized model Required for feasibility
Output used for prediction Sign of raw score Probability threshold Sign of raw score
Probability model No Yes No
Confidence interpretation Raw score only; not probability Estimated probability Geometric margin after normalization
Which observations influence fitting? Mistakes encountered during training Generally all observations contribute to loss Support vectors determine the final boundary directly
Boundary-selection principle Any separator may be found Best log-loss parameters Widest feasible separator
Sensitivity to feature scale Yes Important for optimization and regularization Fundamental to margin geometry

13.1 Probabilistic confidence versus geometric confidence

Suppose two models place an observation on the positive side.

Logistic Regression may report:

P(y=+1x)=0.85P(y=+1\mid x)=0.85

Hard-margin SVM may report a signed geometric distance of:

1.71.7

These values are not competing estimates of the same quantity.

13.2 Does Logistic Regression ever produce the same boundary?

It can, but it is not guaranteed. The two methods solve different objectives.

On some symmetric datasets, their decision boundaries may look nearly identical. On other datasets, especially when observations are distributed unevenly away from the class frontier, the boundaries can differ because Logistic Regression continues to account for all observations through log loss while hard-margin SVM is determined by its support vectors.


14. Practical Implementation with scikit-learn

scikit-learn's SVC implements CC-Support Vector Classification. It does not expose a separate “infinite-CC” hard-margin estimator.

For a clean, linearly separable teaching dataset, a very large value of C can approximate the hard-margin solution. This is an implementation device, not a new theoretical component of the hard-margin formulation.

The workflow below:

  1. creates named feature columns,
  2. splits before preprocessing,
  3. standardizes inside a Pipeline,
  4. trains a linear SVM,
  5. evaluates predictions,
  6. inspects support vectors,
  7. computes normalized geometric distances,
  8. predicts one new part.
from __future__ import annotations import numpy as np import pandas as pd from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC RANDOM_STATE = 42 TEST_SIZE = 0.25 HARD_MARGIN_APPROXIMATION_C = 1_000_000.0 FEATURE_COLUMNS = [ "surface_defect_score", "dimension_deviation", ] ACCEPT_LABEL = -1 REJECT_LABEL = 1 def create_dataset() -> tuple[pd.DataFrame, pd.Series]: """Create a small, linearly separable quality-control dataset.""" rows = [ # Acceptable parts (-1) (0.0, 0.0, ACCEPT_LABEL), (1.0, 0.0, ACCEPT_LABEL), (0.0, 1.0, ACCEPT_LABEL), (0.4, 0.4, ACCEPT_LABEL), (0.8, 0.2, ACCEPT_LABEL), (0.2, 0.8, ACCEPT_LABEL), (1.1, 0.3, ACCEPT_LABEL), (0.3, 1.1, ACCEPT_LABEL), (0.8, 0.8, ACCEPT_LABEL), (1.2, 0.5, ACCEPT_LABEL), (0.5, 1.2, ACCEPT_LABEL), (1.0, 1.0, ACCEPT_LABEL), # Reject parts (+1) (3.0, 2.2, REJECT_LABEL), (3.2, 2.4, REJECT_LABEL), (3.5, 2.0, REJECT_LABEL), (3.0, 3.0, REJECT_LABEL), (4.0, 2.2, REJECT_LABEL), (3.8, 3.0, REJECT_LABEL), (4.2, 2.8, REJECT_LABEL), (3.6, 3.6, REJECT_LABEL), (4.5, 3.2, REJECT_LABEL), (3.2, 3.8, REJECT_LABEL), (4.0, 4.0, REJECT_LABEL), (4.6, 3.8, REJECT_LABEL), ] frame = pd.DataFrame( rows, columns=[*FEATURE_COLUMNS, "quality_label"], ) X = frame[FEATURE_COLUMNS].copy() y = frame["quality_label"].copy() return X, y def build_model() -> Pipeline: """Build a leakage-safe scaling and linear-SVM pipeline.""" return Pipeline( steps=[ ("scaler", StandardScaler()), ( "classifier", SVC( kernel="linear", C=HARD_MARGIN_APPROXIMATION_C, probability=False, ), ), ] ) def signed_geometric_distance( model: Pipeline, X: pd.DataFrame, ) -> np.ndarray: """Return signed distance to the learned boundary in scaled space.""" scaler: StandardScaler = model.named_steps["scaler"] classifier: SVC = model.named_steps["classifier"] X_scaled = scaler.transform(X) raw_scores = classifier.decision_function(X_scaled) weight_norm = np.linalg.norm(classifier.coef_.ravel()) if weight_norm == 0: raise ValueError("The learned weight vector has zero norm.") return raw_scores / weight_norm def main() -> None: X, y = create_dataset() X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=TEST_SIZE, random_state=RANDOM_STATE, stratify=y, ) model = build_model() model.fit(X_train, y_train) train_predictions = model.predict(X_train) test_predictions = model.predict(X_test) print( "Training accuracy:", f"{accuracy_score(y_train, train_predictions):.3f}", ) print( "Test accuracy:", f"{accuracy_score(y_test, test_predictions):.3f}", ) classifier: SVC = model.named_steps["classifier"] weight_norm = np.linalg.norm(classifier.coef_.ravel()) margin_to_boundary = 1.0 / weight_norm full_margin_width = 2.0 / weight_norm print("Number of support vectors:", classifier.n_support_.sum()) print("Margin to nearest support hyperplane:", f"{margin_to_boundary:.3f}") print("Full margin width:", f"{full_margin_width:.3f}") support_positions = classifier.support_ support_rows = X_train.iloc[support_positions].copy() support_rows["label"] = y_train.iloc[support_positions].to_numpy() print("\nSupport-vector training rows:") print(support_rows.to_string(index=False)) new_part = pd.DataFrame( [ { "surface_defect_score": 3.4, "dimension_deviation": 2.5, } ] ) raw_score = float(model.decision_function(new_part)[0]) distance = float(signed_geometric_distance(model, new_part)[0]) prediction = int(model.predict(new_part)[0]) print("\nNew part:") print(f"Raw decision score: {raw_score:.3f}") print(f"Signed geometric distance: {distance:.3f}") print( "Prediction:", "Reject" if prediction == REJECT_LABEL else "Accept", ) if __name__ == "__main__": main()

14.1 Why scaling is inside the pipeline

The correct order is:

  1. split the original data,
  2. fit the scaler using only the training partition,
  3. transform the training partition,
  4. fit the SVM,
  5. apply the same fitted scaler to test and future observations.

Placing StandardScaler inside Pipeline makes this workflow automatic. Test-set statistics are not used to fit the scaler.

14.2 Why the distance is computed after scaling

The classifier is trained in standardized feature space. Its coef_ and decision_function() therefore describe that space.

The normalized quantity:

wTx+bw\frac{w^Tx+b}{\lVert w\rVert}

is a geometric distance in standardized units.

It is not the same as a distance measured directly in the original raw measurement units.

14.3 Inspecting support vectors

For SVC, the attribute:

classifier.support_

contains the positions of the training observations selected as support vectors.

The attribute:

classifier.n_support_

reports how many support vectors belong to each class.

For a linear kernel:

classifier.coef_

contains the learned weight vector.

14.4 Why probability=False

Hard-margin SVM does not intrinsically produce probabilities.

Enabling probability estimation in SVC adds a separate calibration procedure. That output should not be presented as part of the hard-margin derivation.

14.5 Interpreting the example output

One run of the included script produced:

Training accuracy: 1.000 Test accuracy: 1.000 Number of support vectors: 2 Margin to nearest support hyperplane: 0.781 Full margin width: 1.562 Support-vector training rows: surface_defect_score dimension_deviation label 1.0 1.0 -1 3.0 2.2 1 New part: Raw decision score: 1.436 Signed geometric distance: 1.121 Prediction: Reject

The perfect accuracy on this small synthetic split is expected because the data was deliberately constructed to be easy and separable. It is not evidence of real-world model quality.


15. Connecting the Code to the Mathematics

Mathematical concept Code Interpretation
Feature vector xx One row of X One manufactured part
Label y{1,+1}y\in\{-1,+1\} quality_label Accept or reject
Feature scaling StandardScaler() Defines a comparable standardized geometry
Train/test separation train_test_split() Keeps evaluation data outside fitting
Linear SVM SVC(kernel="linear", ...) Learns a linear hyperplane
Approximate hard-margin behavior Very large C on separable data Numerical implementation choice
Fit parameters model.fit() Solves the library's SVM optimization
Class prediction model.predict() Sign-based label
Raw score wTx+bw^Tx+b decision_function() Signed unnormalized decision score
Weight vector ww classifier.coef_ Normal to the boundary in scaled space
Support-vector positions classifier.support_ Training rows active at the margin
Margin to one side 1 / np.linalg.norm(coef_) 1/w1/\lVert w\rVert
Full margin width 2 / np.linalg.norm(coef_) 2/w2/\lVert w\rVert
Signed geometric distance score / np.linalg.norm(coef_) Distance in scaled feature space
Probability Not produced Hard-margin SVM is not probabilistic

Implementation warning

A very large C is safe only as a teaching approximation on clean, separable data. On noisy or nonseparable data, it can create numerical and modeling problems. That behavior belongs to the soft-margin discussion.


16. Key Takeaways

The prediction rule is still linear

y^=sign(wTx+b)\hat y=\operatorname{sign}(w^Tx+b)

Hard-margin SVM uses the same kind of decision boundary as the Perceptron and binary Logistic Regression.

The remaining problem was boundary selection

The Perceptron can find any separator. Logistic Regression optimizes probability-based log loss. Neither objective explicitly selects the widest geometric gap among all feasible separators.

Raw score is not geometric distance

signed distance=wTx+bw\operatorname{signed\ distance} = \frac{w^Tx+b}{\lVert w\rVert}

Normalization by w\lVert w\rVert removes arbitrary parameter scaling.

The geometric margin uses the label

γi=yi(wTxi+b)w\gamma_i = \frac{y_i(w^Tx_i+b)}{\lVert w\rVert}

The label makes positive geometric margin mean “on the correct side” for both classes.

Canonical scaling produces the hard-margin constraints

yi(wTxi+b)1y_i(w^Tx_i+b)\geq1

The nearest observations satisfy equality.

The hard-margin objective is

minw,b12w2subject toyi(wTxi+b)1\min_{w,b} \frac{1}{2}\lVert w\rVert^2 \quad \text{subject to} \quad y_i(w^Tx_i+b)\geq1

Minimizing the norm maximizes the margin while the constraints preserve perfect separation.

Support vectors determine the boundary

Support vectors satisfy:

yi(wTxi+b)=1y_i(w^Tx_i+b)=1

They are the active observations that hold the maximum-margin solution in place.

The method has a strict success condition

Hard-margin SVM requires linearly separable training data. If overlap, noise, or contradictory labels make the constraints infeasible, the method has no solution.

Geometric confidence is not probability

The decision score and normalized distance do not represent P(y=1x)P(y=1\mid x).

The next problem is realistic imperfection

Real data is rarely perfectly separable. The natural next question is:

How can we preserve the maximum-margin principle while allowing limited margin violations or classification errors?

That question leads to soft-margin SVM.

Hard-margin SVM summary infographic


Glossary

Active constraint
A constraint that holds with equality at the optimum. For hard-margin SVM, support-vector constraints are active.

Bias bb
The intercept that shifts the decision boundary without directly changing its normal direction.

Canonical scaling
A parameter scaling in which the smallest training functional margin equals 11.

Constraint
A requirement that every feasible solution must satisfy. Hard-margin SVM requires yi(wTxi+b)1y_i(w^Tx_i+b)\geq1.

Convex optimization
Optimization in which the objective and feasible region have geometry that prevents inferior local minima.

Decision boundary
The set of points satisfying wTx+b=0w^Tx+b=0.

Dual problem
An alternative optimization formulation expressed through Lagrange multipliers αi\alpha_i.

Functional margin
The scale-dependent quantity yi(wTxi+b)y_i(w^Tx_i+b).

Geometric margin
The normalized signed distance yi(wTxi+b)/wy_i(w^Tx_i+b)/\lVert w\rVert.

Hard-margin SVM
A maximum-margin linear classifier that requires every training example to be perfectly separated.

Hyperplane
A linear decision boundary in an arbitrary number of dimensions.

Lagrange multiplier αi\alpha_i
A nonnegative coefficient associated with a constraint. Nonzero coefficients identify observations that directly influence the SVM solution.

Margin hyperplanes
Under canonical scaling, the parallel hyperplanes wTx+b=+1w^Tx+b=+1 and wTx+b=1w^Tx+b=-1.

Normal vector ww
A vector perpendicular to the decision boundary.

Quadratic program
An optimization problem with a quadratic objective and linear constraints.

Support vector
A training observation that lies on a margin hyperplane and helps determine the optimal boundary.

Weight vector ww
The learned coefficients that determine the orientation and feature contributions of the linear decision function.