The Mathematics Behind Linear Classifiers: Margins and the SVM

The mathematics under the boundary: why the weight vector is a direction, how maximising the margin becomes a quadratic programme, where hinge loss comes from, and what C really tunes.

Logistic regression and the support vector machine are usually taught as different animals. One outputs probabilities and comes from statistics; the other draws a widest-possible street between the classes and comes from optimisation theory. In scikit-learn they even live in different modules. Yet if you write down what each one actually computes, the difference collapses to a single line, and everything else, including the entire vocabulary of margins, support vectors, and the mysterious C parameter, follows from that one line.

The shared skeleton is this. Both models score a point with the same expression:

f(x)=wx+bf(x) = w^{\top} x + b

and both predict by sign. The prediction is class +1 when f(x)>0f(x) > 0 and class -1 when f(x)<0f(x) < 0. That is the whole model. Two classifiers, one function, identical at prediction time. What separates them is not the hypothesis but the loss: the rule by which they decide, during training, which of the infinitely many valid (w,b)(w, b) pairs deserves to be chosen. This article derives that choice, and shows what the geometry of the chosen boundary means.

The geometry of a hyperplane

The decision boundary is the set where the score vanishes:

=x:wx+b=0\mathcal{H} = {x : w^{\top}x + b = 0}

Two facts about this set matter, and both are one-line proofs. First, w is perpendicular to the boundary. Take any two points x1x_1 and x2x_2 lying on it. Both satisfy the equation, so subtracting gives:

wx1+b(wx2+b)=w(x1x2)=0w^{\top}x_1 + b – (w^{\top}x_2 + b) = w^{\top}(x_1 – x_2) = 0

The vector x1x2x_1 – x_2 lies along the boundary, and its inner product with w is zero, so w is normal to the hyperplane. The weight vector is not an abstract parameter list, it is a direction in the feature space, pointing perpendicular to the decision surface, and it is the direction along which the score changes fastest.

Second, the score measures distance. The perpendicular distance from any point x0x_0 to the boundary is:

d(x0,)=|wx0+b|wd(x_0, \mathcal{H}) = \frac{|w^{\top}x_0 + b|}{\lVert w \rVert}

So f(x)f(x) is not an arbitrary number. Divided by w\lVert w \rVert, it is literally how far the point sits from the boundary, and its sign says which side. Every quantity in what follows is a variation on this formula.

Functional and geometric margins

Encode the labels as yi1,+1y_i \in {-1, +1}, a convention chosen precisely because it lets correctness and distance combine into one product. Define the functional margin of a training point:

γ^i=yi(wxi+b)\hat{\gamma}_i = y_i\left(w^{\top}x_i + b\right)

Read the sign: when the label is +1 and the score is positive, the product is positive; when the label is -1 and the score is negative, the product is positive again. So γ^i>0\hat{\gamma}_i > 0 means correct, γ^i<0\hat{\gamma}_i < 0 means wrong, and the magnitude says how emphatically. One number carries both facts.

The functional margin has a defect, though, and the defect is instructive. Multiply both w and b by any positive constant c. The boundary x:c,wx+c,b=0{x : c,w^{\top}x + c,b = 0} is exactly the same set of points, so the classifier is unchanged, yet every functional margin has been multiplied by c. Confidence can be inflated arbitrarily without improving anything. The cure is to divide out the scale:

γi=yi(wxi+b)w \gamma_i = \frac{y_i\left(w^{\top}x_i + b\right)}{\lVert w \rVert}

This is the geometric margin, and comparing it with the distance formula above shows what it is: the signed perpendicular distance from the point to the boundary, positive when classified correctly. Unlike its functional cousin, it is invariant to rescaling and therefore means something.

Maximising the margin

Among all boundaries that separate the classes, which is best? The support vector machine’s answer is the one that stays furthest from the closest points on either side. Define the margin of the whole dataset as the smallest geometric margin:

γ=mini;γi \gamma = \min_{i} ; \gamma_i

and ask for the boundary maximising it:

maxw,b;mini;yi(wxi+b)w\max_{w, b} ; \min_{i} ; \frac{y_i\left(w^{\top}x_i + b\right)}{\lVert w \rVert}

As written this is unpleasant, containing a minimum inside a maximum and a ratio. The scale invariance we just complained about now becomes the tool that cleans it up. Since multiplying (w,b)(w, b) by any constant leaves the boundary and all geometric margins untouched, we are free to fix the scale however we like. Choose the normalisation that sets the smallest functional margin to exactly 1:

mini;yi(wxi+b)=1 \min_{i} ; y_i\left(w^{\top}x_i + b\right) = 1

This is not an assumption about the data. It is a choice of units, available for free because the ratio was invariant. With it, the dataset’s geometric margin becomes:

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

and the margin band, the empty street between the two classes, has width 2/w2/\lVert w \rVert. Maximising that width means minimising w\lVert w \rVert, and since squaring is monotone on non-negative numbers, we can minimise a convenient quadratic instead. The hard-margin SVM is:

minw,b;12w2subject toyi(wxi+b)1; for all i \min_{w, b} ; \tfrac{1}{2}\lVert w \rVert^{2} \quad \text{subject to} \quad y_i\left(w^{\top}x_i + b\right) \ge 1 ; \text{ for all } i

This is worth pausing on, because the transformation is the mathematical heart of the method. A geometric wish, “put the widest possible street between the classes”, has become a convex quadratic programme with linear constraints: a problem class with a unique global optimum and reliable solvers. No local minima, no restarts, no initialisation luck. The awkward maximin over a ratio and the tidy quadratic are the same problem wearing different clothes.

Note also what w\lVert w \rVert now means. It looks like a regularisation penalty, and it behaves like one, but it arrived here as the reciprocal of the margin. Shrinking the weights and widening the street are the same act.

Why only some points matter

The constrained problem has a Lagrangian with one multiplier αi0\alpha_i \ge 0 per training point, and the Karush-Kuhn-Tucker conditions include complementary slackness:

αi[yi(wxi+b)1]=0\alpha_i \left[y_i\left(w^{\top}x_i + b\right) – 1\right] = 0

The condition is a product forced to zero, so for each point at least one factor must vanish. If the point sits strictly outside the margin, its bracket is strictly positive, forcing αi=0\alpha_i = 0. Only points sitting exactly on the margin boundary, where the bracket is zero, may have αi>0\alpha_i > 0.

The stationarity condition then expresses the solution as a weighted sum of training points:

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

Combine the two. Every point with αi=0\alpha_i = 0 contributes nothing to w, so the fitted boundary depends only on the handful of points touching the margin. Those are the support vectors, and the name is now literal rather than metaphorical: they hold the street open, and everything else is irrelevant. Delete a thousand comfortably classified points and refit, and the boundary does not move by a millimetre. Move one support vector and the whole thing swings.

This is a genuine difference from logistic regression, where every point contributes a nonzero gradient forever and distant correct points continue to nudge the boundary. It also explains the SVM’s characteristic robustness and its characteristic fragility in one sentence: it ignores the bulk of the data and stakes everything on the boundary cases.

Soft margins, and where hinge loss comes from

Real data is not separable, and the hard-margin constraints then have no feasible solution at all. Introduce a slack variable ξi0\xi_i \ge 0 per point, permitting violation at a price:

minw,b,ξ;12w2+Ci=1nξisubject toyi(wxi+b)1ξi,ξi0 \min_{w, b, \xi} ; \tfrac{1}{2}\lVert w \rVert^{2} + C \sum_{i=1}^{n} \xi_i \quad \text{subject to} \quad y_i\left(w^{\top}x_i + b\right) \ge 1 – \xi_i, \quad \xi_i \ge 0

Now watch the slack variables disappear. At the optimum the solver will never pay more than it must, so each ξi\xi_i takes the smallest value satisfying its constraint. Rearranged, the constraint demands ξi1yif(xi)\xi_i \ge 1 – y_i f(x_i), and combined with ξi0\xi_i \ge 0 the minimum feasible value is:

ξi=max(0,;1yi(wxi+b))\xi_i = \max\left(0, ; 1 – y_i\left(w^{\top}x_i + b\right)\right)

Substituting back eliminates the constraints entirely and leaves an unconstrained objective:

minw,b;12w2+Ci=1nmax(0,;1yif(xi)) \min_{w, b} ; \tfrac{1}{2}\lVert w \rVert^{2} + C \sum_{i=1}^{n} \max\left(0, ; 1 – y_i f(x_i)\right)

The second term is the hinge loss, and here it is not a definition handed down but a derived quantity: it is exactly the cost of violating the margin constraints, read off from the geometry. Writing mi=yif(xi)m_i = y_i f(x_i) for the functional margin, the hinge is:

Lhinge(m)=max(0,;1m)L_{\text{hinge}}(m) = \max(0, ; 1 – m)

Its shape encodes the SVM’s whole attitude. Beyond $m = 1$ the loss is flat at exactly zero, so a point comfortably on the right side contributes nothing, which is complementary slackness reappearing as a picture. Below $m = 1$ the penalty grows linearly, and the kink at 1 is what makes the solution sparse in support vectors.

Hinge against logistic

Logistic regression optimises a different loss on the same margin variable:

Llog(m)=log(1+em)L_{\text{log}}(m) = \log\left(1 + e^{-m}\right)

Both are convex surrogates standing in for the loss we actually care about, the zero-one loss:

L01(m)=𝟙[m0]L_{01}(m) = \mathbb{1}[m \le 0]

which counts mistakes and is useless for optimisation, being flat everywhere with a discontinuity at zero. It has no gradient to follow, and minimising it exactly is NP-hard. Hinge and logistic are both convex upper bounds on it, which is the entire reason either exists.

The difference between them lives at m>1m > 1. The hinge is identically zero there; the logistic loss is small but strictly positive, since log(1+em)\log(1 + e^{-m}) never reaches zero for finite m. Differentiating shows the consequence:

Llogw=,yi,xi,σ(mi)\frac{\partial L_{\text{log}}}{\partial w} = -,y_i, x_i, \sigma(-m_i)

The factor σ(mi)\sigma(-m_i) decays but never vanishes, so every point, however confidently correct, keeps pulling on the boundary forever, with a weight that decays smoothly with confidence. The hinge’s gradient, by contrast, is exactly zero past the margin and yixi-y_i x_i inside it. One model listens to everyone quietly; the other listens only to the points near the line and ignores the rest completely.

That single difference generates everything else people say about these models. Logistic regression yields calibrated probabilities through σ(f(x))\sigma(f(x)), because its loss is the negative log-likelihood of a Bernoulli model and is proper for that purpose. The SVM yields no probabilities, because its loss makes no likelihood claim, which is why scikit-learn must bolt on Platt scaling to fake them. The SVM is sparse in support vectors; logistic regression is not sparse in anything. Neither is a defect. They are two positions on how much attention distant points deserve.

What C actually is

Divide the soft-margin objective by C and it becomes:

minw,b;i=1nmax(0,;1yif(xi))+12Cw2\min_{w, b} ; \sum_{i=1}^{n} \max\left(0, ; 1 – y_i f(x_i)\right) + \frac{1}{2C}\lVert w \rVert^{2}

Compare it with the standard regularised form iLi+λw2\sum_i L_i + \lambda \lVert w \rVert^2 and read off the correspondence:

λ=12C \lambda = \frac{1}{2C}

C is the reciprocal of the regularisation strength, and this is the origin of the direction that trips people up in scikit-learn, where large C means less regularisation. The mathematics makes it obvious rather than arbitrary. Large C means violations are expensive, so the optimiser will contort the boundary to avoid them, tolerating a large w\lVert w \rVert and therefore a narrow margin: low bias, high variance. Small C means violations are cheap, so the optimiser prefers a small w\lVert w \rVert, a wide margin, and a boundary that accepts some errors in exchange for stability: high bias, low variance.

The identity γ=1/w\gamma = 1/\lVert w \rVert ties the knot. Tuning C is tuning the margin width, and tuning the margin width is tuning the bias-variance trade-off. Three descriptions, one dial. The same reading applies to C in scikit-learn’s LogisticRegression, which uses the identical convention, so a grid over C on a log scale is a grid over margin widths.

The dual, and the door to kernels

Solving through the Lagrangian yields the dual problem, stated entirely in terms of the multipliers:

maxα;i=1nαi12i=1nj=1nαiαj,yiyj,xixj\max_{\alpha} ; \sum_{i=1}^{n} \alpha_i – \tfrac{1}{2} \sum_{i=1}^{n}\sum_{j=1}^{n} \alpha_i \alpha_j , y_i y_j , x_i^{\top} x_j
subject to0αiC,i=1nαiyi=0 \text{subject to} \quad 0 \le \alpha_i \le C, \qquad \sum_{i=1}^{n} \alpha_i y_i = 0

The upper bound αiC\alpha_i \le C is where the slack penalty ends up, which is a neat confirmation that C caps how much influence any single point may exert: no individual training example, however badly placed, can pull the boundary further than C allows. Outliers are structurally limited.

The dual’s more famous consequence is the appearance of the data only through inner products xixjx_i^{\top}x_j. Nowhere does the optimisation need the individual vectors, only their pairwise products. Substituting any valid kernel K(xi,xj)K(x_i, x_j) for that inner product therefore fits a linear boundary in a transformed space that may be far higher-dimensional, without ever computing the transformation. That is the kernel trick, and it exists because of an accident of algebra in the dual form, not because anyone designed it in.

What the mathematics tells you to do

Scale your features before fitting either model, because both objectives penalise w\lVert w \rVert, and a feature measured in thousands needs a tiny weight to have any effect while a feature measured in units needs a large one. The penalty does not know the difference and simply crushes whichever weight it can, so the regularisation you thought you were applying to complexity is applied to units instead.

Search C on a logarithmic grid, because the quantity it controls, 1/w1/\lVert w \rVert, is a scale rather than an offset, and equal ratios matter rather than equal differences. Expect large C to overfit narrow margins and small C to underfit wide ones, and read a chosen C as a statement about how much margin violation your data deserves.

Prefer logistic regression when you need probabilities, since the SVM’s loss makes no probabilistic claim and any probabilities you extract are a post-hoc fit. Prefer the SVM when the decision boundary is what matters and the bulk of the data is uninformative, since the hinge’s flat region will discard it for free.

The deeper point is the one the shared skeleton makes. These models are not different theories of classification. They are the same linear score under two different opinions about what a training point owes you once it is already on the right side of the line. Logistic regression says: keep contributing, quietly, forever. The SVM says: past the margin, be silent. Every practical difference between them, sparsity, calibration, robustness, and the meaning of C, is that one disagreement, worked out to its conclusions.

Hope this helps, Andrei.

View Comments (2)

Leave a Reply

Subscribe to My Newsletter

Subscribe to my email newsletter to get the latest posts delivered right to your email. Pure inspiration, zero spam.

Discover more from Discuss Data Science, Machine Learning and Analytics

Subscribe now to keep reading and get access to the full archive.

Continue reading