2.4.2SVM, Naive Bayes & Probabilistic Models

Hard vs soft margin classifiers

1,790 words8 min readdifficulty · medium

WHY do we need margins at all?

The decision boundary is a hyperplane: wx+b=0\mathbf{w}^\top \mathbf{x} + b = 0 We predict class +1+1 if wx+b>0\mathbf{w}^\top\mathbf{x}+b > 0, class 1-1 otherwise.

Deriving the margin from scratch

WHAT is the distance from a point x0\mathbf{x}_0 to the hyperplane wx+b=0\mathbf{w}^\top\mathbf{x}+b=0?

Why this step? Geometry: move from x0\mathbf{x}_0 along the unit normal w/w\mathbf{w}/\|\mathbf{w}\| until you hit the plane. The signed distance is d=wx0+bw.d = \frac{\mathbf{w}^\top \mathbf{x}_0 + b}{\|\mathbf{w}\|}.

We have freedom to scale w\mathbf{w} and bb by any constant — the hyperplane is unchanged. So we fix the scale by demanding that the closest points satisfy wx+b=1|\mathbf{w}^\top\mathbf{x}+b| = 1. This is the canonical form.

Why this step? It removes the ambiguity so the optimization is well-posed. With this choice the margin (distance to the nearest point) becomes γ=1w,full gap width=2w.\gamma = \frac{1}{\|\mathbf{w}\|}, \qquad \text{full gap width} = \frac{2}{\|\mathbf{w}\|}.

So maximizing the margin == minimizing w\|\mathbf{w}\| == minimizing 12w2\frac{1}{2}\|\mathbf{w}\|^2 (the square and 12\tfrac12 are just for a nicer, convex, differentiable objective).


Hard margin SVM

WHY the constraint yi(wxi+b)1y_i(\mathbf{w}^\top\mathbf{x}_i+b)\ge 1?

  • For a +1+1 point we want wxi+b1\mathbf{w}^\top\mathbf{x}_i+b \ge 1 (well on the positive side).
  • For a 1-1 point we want wxi+b1\mathbf{w}^\top\mathbf{x}_i+b \le -1.
  • Multiplying by yi{+1,1}y_i\in\{+1,-1\} folds both into the single clean inequality yi()1y_i(\dots)\ge 1.

Soft margin SVM

We introduce a slack variable ξi0\xi_i \ge 0 per point: how far point ii is allowed to violate its margin constraint.

yi(wxi+b)1ξi,ξi0.y_i(\mathbf{w}^\top\mathbf{x}_i + b) \ge 1 - \xi_i, \qquad \xi_i \ge 0.

HOW to read ξi\xi_i:

  • ξi=0\xi_i = 0: point is safely outside the margin (obeys hard constraint).
  • 0<ξi10 < \xi_i \le 1: inside the margin but still correctly classified.
  • ξi>1\xi_i > 1: misclassified (on the wrong side of the boundary).

We don't want unlimited cheating, so we penalize the total slack:

WHY the CC? CC is the trade-off knob between a wide margin and few violations.

  • Large CC → violations are very expensive → model tries hard to classify all points → narrow margin, low bias, high variance → approaches hard margin as CC\to\infty.
  • Small CC → cheap to violate → wide margin, more tolerance to noise, high bias, low variance.
Figure — Hard vs soft margin classifiers

Hinge loss view (WHY it's the same thing)

At the optimum, each ξi\xi_i is as small as allowed. The constraints force ξi=max(0, 1yi(wxi+b)).\xi_i = \max\bigl(0,\ 1 - y_i(\mathbf{w}^\top\mathbf{x}_i+b)\bigr).

Why this step? If the point already satisfies yi()1y_i(\dots)\ge 1, no slack needed → ξi=0\xi_i=0. Otherwise ξi\xi_i must cover the gap exactly. Substituting turns the constrained problem into an unconstrained one:


Worked examples


Support vectors (bonus insight)

Only points with ξi>0\xi_i>0 or lying exactly on the margin (yi()=1y_i(\dots)=1) have nonzero influence — these are the support vectors. Points comfortably outside the margin can be deleted without changing the boundary. WHY? In the dual, their Lagrange multipliers αi=0\alpha_i=0.


Recall Feynman: explain to a 12-year-old

Imagine drawing a fence between the cats and the dogs in a park. A hard fence rule says: no animal may touch or cross the fence, ever. That's fine until one confused cat wanders into dog-land — now you can't draw any fence at all! The soft rule says: try to keep them apart with the widest safe path, but if a few troublemakers cross, that's okay — just count how badly they cross and try to keep that small. The knob CC is how angry you get at troublemakers: big CC = very strict, small CC = chill and forgiving.


Flashcards

What is the margin width of an SVM in terms of w?
2w\frac{2}{\|\mathbf{w}\|} (half-margin 1/w1/\|\mathbf{w}\| to each side).
Why does maximizing margin equal minimizing w\|\mathbf{w}\|?
With canonical scaling wx+b=1|\mathbf{w}^\top x+b|=1 at closest points, margin =1/w=1/\|\mathbf{w}\|, so wider margin means smaller norm. ::: We minimize 12w2\tfrac12\|\mathbf{w}\|^2 for convexity/differentiability.
Write the hard margin constraint.
yi(wxi+b)1y_i(\mathbf{w}^\top\mathbf{x}_i+b)\ge 1 for all ii.
When does the hard margin SVM have NO solution?
When the data is not linearly separable (constraints are infeasible).
What is a slack variable ξi\xi_i?
A nonnegative amount by which point ii is allowed to violate its margin constraint: yi(wxi+b)1ξiy_i(\mathbf{w}^\top x_i+b)\ge 1-\xi_i.
Meaning of ξi=0\xi_i=0, 0<ξi10<\xi_i\le1, ξi>1\xi_i>1?
0 = outside margin (safe); (0,1] = inside margin but correct; >1 = misclassified.
Write the soft margin primal objective.
min12w2+Ciξi\min \tfrac12\|\mathbf{w}\|^2 + C\sum_i \xi_i s.t. yi(wxi+b)1ξi, ξi0y_i(\mathbf{w}^\top x_i+b)\ge 1-\xi_i,\ \xi_i\ge0.
Soft margin as unconstrained loss?
min12w2+Cimax(0,1yi(wxi+b))\min \tfrac12\|\mathbf{w}\|^2 + C\sum_i \max(0,\,1-y_i(\mathbf{w}^\top x_i+b)) (regularized hinge loss).
What does the parameter CC trade off?
Margin width vs. training violations. Large C = narrow margin/overfit; small C = wide margin/more tolerant.
What happens to soft margin as CC\to\infty?
It reduces to the hard margin SVM (violations become infinitely costly).
What is a support vector?
A point on the margin or violating it (ξi>0\xi_i>0); it has nonzero Lagrange multiplier and defines the boundary.
Formula for slack at the optimum?
ξi=max(0,1yi(wxi+b))\xi_i=\max(0,\,1-y_i(\mathbf{w}^\top x_i+b)) (the hinge).

Connections

  • Support Vector Machines — the parent method.
  • Kernel Trick — how soft margin extends to nonlinear boundaries.
  • Hinge Loss — the equivalent unconstrained objective.
  • Regularization and Bias-Variance TradeoffCC as inverse regularization strength.
  • Lagrangian Duality and KKT Conditions — where support vectors come from.
  • Logistic Regression — contrast: log-loss vs hinge-loss classifiers.

Concept Map

pick best by

distance to nearest point

fix scale

max margin equals

with constraint yi ge 1

needs

noisy point breaks it

fix by adding

allows violations

penalty per violation

Many separating lines

Maximize margin

Hyperplane w x plus b equals 0

Canonical form margin equals 1 over norm w

Minimize half norm w squared

Hard margin SVM

Linearly separable data

No feasible solution overfits

Slack variable xi ge 0

Soft margin SVM

Noise tolerant realistic model

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, SVM ka main idea ye hai ki do classes ke beech me hum aisi line (ya hyperplane) khichte hain jo dono ke sabse pass wale points se maximum door ho. Is gap ko margin bolte hain, aur wo 2/w2/\|\mathbf{w}\| hota hai. To margin bada karna matlab w\|\mathbf{w}\| chhota karna — isliye hum 12w2\tfrac12\|\mathbf{w}\|^2 minimize karte hain. Wide margin zyada robust hota hai, thoda sa noise aane par bhi prediction nahi badalti.

Hard margin kehta hai: koi bhi point margin ke andar ya galat side me nahi aana chahiye — 100% perfect separation. Problem ye hai ki agar data thoda bhi overlap kare ya ek bhi outlier ho, to koi solution hi nahi banta, aur model overfit ho jaata hai. Isliye real duniya me hard margin kaam nahi karta.

Yahan aata hai soft margin. Har point ke liye ek slack variable ξi\xi_i dete hain — matlab "thoda cheating allowed hai". ξi=0\xi_i=0 matlab point safe hai, 0<ξi10<\xi_i\le1 matlab margin ke andar par sahi side, aur ξi>1\xi_i>1 matlab galat classify ho gaya. Total cheating ko penalize karte hain CξiC\sum\xi_i se. Ye pura cheez hinge loss ke barabar hai: max(0,1yi(wxi+b))\max(0, 1-y_i(\mathbf{w}^\top x_i+b)).

CC ek knob hai. Bada CC = strict teacher, violations ko bahut mehnga maanega, narrow margin, overfit. Chhota CC = chill teacher, wide margin, noise ko ignore karega, better generalization. Jab CC\to\infty, soft margin wapas hard margin ban jaata hai. Exam me yaad rakho: "Hard = zero tolerance, Soft = slack with a Cost."

Test yourself — SVM, Naive Bayes & Probabilistic Models

Connections