Python helpers — run once, the checks below reuse them
import numpy as npfrom sklearn.svm import SVCdef hard_svm(X, y): # huge C ≈ hard margin return SVC(kernel="linear", C=1e8).fit(X, y)# m.coef_ = W, m.intercept_ = b, m.dual_coef_ = α_i y_i for the support vectors m.support_
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 X1..Xn: C={∑iλiXi:λi≥0,∑λi=1}.
Separating hyperplane: yi(WTXi+b)>0∀i.
W is normal to the hyperplane; the distance of the hyperplane from the origin is ∣b∣/∥W∥; the distance of a pointX from it is ∥W∥∣WTX+b∣.
Rescaling freedom:(W,b)→(cW,cb) leaves the hyperplane unchanged, so fix the scale so that the closest points satisfy yi(WTXi+b)=1 (margin parameter γ=1). All constraints become
yi(WTXi+b)≥1∀i.
Margin=d++d−=∥W∥1+∥W∥1=∥W∥2.
Support vectors: points with yi(WTXi+b)=1 (exactly on the margin).
Optimisation problem
max∥W∥2s.t. yi(WTXi+b)≥1⟺min21WTW s.t. yi(WTXi+b)≥1,i=1..n.
(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 X+ with WTX++b=1 and X− with WTX−+b=−1. Subtract: WT(X+−X−)=2. Projecting X+−X− on the unit normal W/∥W∥ gives the width: ∥W∥WT(X+−X−)=∥W∥2. ∎
Key insights
Maximising margin ≡ minimising ∥W∥.
Interior points (with yif(Xi)>1) 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:W=(3,4), b=−5. Distance of X=(2,1) from the hyperplane?
Asked: Distance.
Concept: Point-to-hyperplane distance.
Approach:∣WTX+b∣/∥W∥.
Solution:WTX+b=6+4−5=5; ∥W∥=5. Distance =1.
Final answer:1.
Key lesson
Distance is the signed value divided by ∥W∥.
Example 2 — Easy · Margin from ∥W∥
Problem: If the support vectors satisfy yi(WTXi+b)=1 and W=(3,4), what is the margin? What ∥W∥ gives margin 2?
Asked: Two numbers.
Concept: Margin =2/∥W∥.
Approach: Formula.
Solution:2/5=0.4. Margin 2⇒∥W∥=1.
Final answer:0.4; ∥W∥=1.
Key lesson
Big margin ⇔ small weight vector.
Example 3 — Moderate · One point per class
Problem: Positive point (2,2), negative point (0,0). Find the hard-margin hyperplane by imposing both constraints with equality and symmetry W=(a,a).
Asked:W,b, margin.
Concept: Constraints active on both points.
Approach: Solve for a,b.
Solution:(2,2): 4a+b=1; (0,0): −b=1⇒b=−1. So a=0.5. W=(0.5,0.5), b=−1; boundary x1+x2=2. ∥W∥=0.7071; margin =2.828 = distance between the two points 8=2.828 ✓.
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:W=(1,1), b=−3; positives (2,2),(3,3); negatives (1,1),(0,0). Check feasibility, find the support vectors and the margin.
Asked: Verification.
Concept: Constraints and SVs.
Approach: Compute yi(WTXi+b).
Solution:(2,2): 4−3=1 ✓ SV. (3,3): 6−3=3≥1 ✓ (not SV). (1,1): −(2−3)=1 ✓ SV. (0,0): −(0−3)=3≥1 ✓. Margin =2/2=1.414.
Final answer: Feasible; SVs ={(2,2),(1,1)}; margin 2.
Python check
X = np.array([[2,2],[3,3],[1,1],[0,0]]); y = np.array([1,1,-1,-1])print(y * (X @ [1, 1] - 3)) # [1 3 1 3] -> SVs are rows 0 and 2
Key lesson
Only points with value exactly 1 are support vectors.
Example 5 — Very Hard · XOR is not separable
Problem: Show by the convex-hull criterion that the XOR set — positives (0,0),(1,1), negatives (0,1),(1,0) — is not linearly separable, and that a different labelling of the same four points (positives (0,0),(1,0), negatives (0,1),(1,1)) is separable.
Asked: Hull intersection.
Concept: Hull criterion.
Approach: Find a common convex combination.
Solution: The positive hull contains 21(0,0)+21(1,1)=(0.5,0.5). The negative hull contains 21(0,1)+21(1,0)=(0.5,0.5). The hulls share a point ⇒ not separable. Second labelling: positive hull ={(t,0):0≤t≤1}; negative hull ={(t,1)}: disjoint (x2=0 vs x2=1) ⇒ separable, e.g. x2=0.5 (W=(0,−2), b=1 gives y(WTX+b)=1 on all four points).
Final answer: XOR hulls intersect at (0.5,0.5); the second labelling is separable.
C31. The Lagrange Dual of the Hard-Margin SVM and the KKT Conditions
Intuition
Constrained optimisation: introduce a “price” αi≥0 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 XiTXj, which is what makes kernels possible (C33).
Derivation
Lagrangian:
L(W,b,α)=21WTW+∑iαi[1−yi(WTXi+b)],αi≥0.
Minimise over (W,b), maximise over α.
∂W∂L=W−∑iαiyiXi=0⇒W=i∑αiyiXi,∂b∂L=−∑iαiyi=0⇒i∑αiyi=0.
Substitute: ∑iαiyiWTXi=WTW and ∑iαiyib=0, so
L=21WTW−WTW+∑αi=∑iαi−21WTW.Dual problem:αmaxLD(α)=i∑αi−21i∑j∑αiαjyiyjXiTXjs.t. αi≥0,i∑αiyi=0.
A concave quadratic program (QP); in matrix form: minimise 21αTQα−1Tα with Qij=yiyjXiTXj, yTα=0, α≥0.
KKT conditions: stationarity (above), primal feasibility yi(WTXi+b)≥1, dual feasibility αi≥0, and complementary slacknessαi[yi(WTXi+b)−1]=0.
Hence αi>0⇒ the point is on the margin (support vector); non-SVs have αi=0. b from any SV j: b=yj−WTXj.
Prediction: sign(WTX0+b)=sign(∑iαiyiXiTX0+b).
Key insights
W is a linear combination of support vectors only.
Strong duality (convex problem): 21∥W∥2=LD(α∗).
Complexity depends on n (size of the QP), not on m.
Worked examples
Example 1 — Very Easy · The 1-D dual
Problem: 1-D: x1=2(+1), x2=0(−1). Solve the dual.
Asked:α,W,b.
Concept: Dual QP.
Approach:∑αiyi=0⇒α1=α2=α.
Solution: Gram: x1x1=4, others 0. LD=2α−21(α2⋅4)=2α−2α2; dαdLD=2−4α=0⇒α=0.5. W=α1y1x1=0.5(2)=1. SV x1: b=y1−Wx1=1−2=−1. Boundary at x=1; margin 2/∣W∣=2 = distance between the points ✓.
Two different SVs must give the same b (a consistency check).
Example 4 — Hard · A support vector with α = 0
Problem: Points (2,2)(+), (0,0)(−), (2,0)(−). Find the optimal W,b,α and explain why one point on the margin has α=0.
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 (0,0) to (2,0). The point of it closest to (2,2) is (2,0), at distance 2. So the margin is 2, the boundary is the horizontal line halfway: W=(0,1), b=−1. Check: (2,2): 2−1=1; (0,0): −(0−1)=1; (2,0): −(0−1)=1 — all three lie on the margin. Dual: W=α1(2,2)−α3(2,0)=(2α1−2α3,2α1)=(0,1)⇒α1=0.5,α3=0.5. ∑αiyi=0⇒0.5−α2−0.5=0⇒α2=0.
Final answer:W=(0,1),b=−1, α=(0.5,0,0.5).
Python check
m = hard_svm([[2,2],[0,0],[2,0]], [1, -1, -1])print(m.coef_, m.intercept_, m.support_, m.dual_coef_) # [[0. 1.]] [-1.] [2 0] → point 1 (0,0) is NOT a support vector
Key lesson
αi>0⇒ on the margin, but a point on the margin can still have αi=0 (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:α∗, LD∗, primal value.
Concept: Strong duality.
Approach: Eliminate α3=α1+α2.
Solution: Gram entries: X1TX1=4, X2TX2=4, X1TX2=0, anything with X3=0 equals 0. LD=2(α1+α2)−2α12−2α22. ∂/∂α1=2−4α1=0⇒α1=0.5; likewise α2=0.5; α3=1. LD∗=2(1)−0.5−0.5=1. Primal: 21∥W∥2=21(1+1)=1 ✓ equal ⇒ strong duality. KKT: all three constraints tight (yifi=1 for each) ⇒ complementary slackness holds for every αi; ∑αiyi=0 ✓; αi≥0 ✓.
Final answer:α∗=(0.5,0.5,1), optimal value 1 for both problems.
Python check
X = np.array([[2,0],[0,2],[0,0]], float); y = np.array([1, 1, -1]); a = np.array([.5, .5, 1])Q = (y[:, None] * y[None, :]) * (X @ X.T)print(a.sum() - .5 * a @ Q @ a, .5 * (X.T @ (a * y)) @ (X.T @ (a * y))) # 1.0 1.0
Key lesson
Primal and dual optimal values coincide; use this as a numerical check.
Common mistakes
Forgetting the equality constraint ∑αiyi=0.
Using b=yj−WTXj with a non-support vector.
Exam traps
The lecture’s "0≤α≤∞" is just α≥0.
The lecture says ”αi>0 for support vectors and 0 otherwise” — true except for degenerate boundary points.
C32. Soft-Margin SVM (Slack Variables and the Parameter C)
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. C sets the exchange rate between a wide street and violations.
Formal definition
Slack γi≥0 (the lecture’s notation; often ξi):
minW,b,γ21WTW+C∑i=1nγis.t. γi≥0,yi(WTXi+b)≥1−γi.
Equivalent: γi=max(0,1−yif(Xi)) (hinge loss).
slack
meaning
γ=0
correct and outside the margin
0<γ<1
inside the margin but correctly classified (margin error)
γ=1
exactly on the boundary
γ>1
misclassified
Derivation of the dual
L=21WTW+C∑γi+∑αi[1−γi−yi(WTXi+b)]−∑μiγi,αi,μi≥0.∂W:W=∑αiyiXi; ∂b:∑αiyi=0; ∂γi:C−αi−μi=0. Substituting gives the same dual as hard margin,
LD=∑αi−21∑i,jαiαjyiyjXiTXj,
but with the box constraint0≤αi≤C (since μi=C−αi≥0). C→∞ recovers the hard margin.
KKT / what αi means: complementary slackness αi[yifi−1+γi]=0 and μiγi=0:
αi
μi
γi
Position
0
C
0
correct, outside margin (not an SV)
0<αi<C
>0
0
exactly on the margin (yifi=1)
C
0
≥0
inside the margin or misclassified
All points with αi>0 are support vectors (the lecture’s wording that margin-violators “are not support vectors” is non-standard). b is computed from a support vector with 0<αj<C: b=yj−WTXj.
Effect of C
Large C ⇒ violations are expensive ⇒ narrow margin, few violations, risk of overfitting. Small C ⇒ wide margin tolerant of violations, may underfit. Choose C by cross-validation (C13).
Worked examples
Example 1 — Very Easy · Compute the slacks
Problem:W=(1,0), b=−1. Compute γ for (3,0)+, (1.5,0)+, (0.5,0)+, (0,0)−, (1.8,1)−.
X = np.array([[3,0],[1.5,0],[.5,0],[0,0],[1.8,1]]); y = np.array([1, 1, 1, -1, -1])def objective(W, b, C): W = np.asarray(W, float); g = np.maximum(0, 1 - y * (X @ W + b)) return g, .5 * W @ W + C * g.sum()print(objective([1, 0], -1, 1)) # slacks [0 .5 1.5 0 1.8], objective 4.3
Key lesson
γ>1 ⇔ misclassified.
Example 2 — Easy · The objective value
Problem: Compute the soft-margin objective for Example 1 with C=1.
Asked: Value.
Concept:21∥W∥2+C∑γ.
Approach: Sum.
Solution:21∥W∥2=0.5; ∑γ=3.8. Total =4.3.
Final answer:4.3.
Key lesson
The penalty dominates when many points violate.
Example 3 — Moderate · The effect of C
Problem: Compare candidate A: W=(1,0),b=−1 (objective part: 21∥W∥2=0.5, ∑γ=3.8) with candidate B: W=(0.5,0),b=−0.5. Which wins for C=1 and C=10?
Asked: Winner vs C.
Concept: Trade-off.
Approach: Compute B’s slacks.
Solution: B: f=0.5x1−0.5. (3,0)+: γ=0. (1.5,0)+: γ=0.75. (0.5,0)+: γ=1.25. (0,0)−: γ=0.5. (1.8,1)−: γ=1.4. ∑γ=3.9; 21∥W∥2=0.125. C=1: A =4.3, B =4.025 ⇒ B. C=10: A =38.5, B =39.125 ⇒ A. Break-even: 0.375−0.1C=0⇒C=3.75.
Final answer: B for C<3.75, A for C>3.75.
Python check
for C in (1, 3.75, 10): print(C, objective([1, 0], -1, C)[1], objective([.5, 0], -.5, C)[1]) # B wins below 3.75, A above
Key lesson
Large C prefers fewer violations (narrower margin); small C prefers a wider margin.
Example 4 — Hard · Read the multipliers
Problem:C=1; labels y=(−1,−1,+1,+1,−1) and multipliers α=(1,0,0.6,1,0.6). (a) Check ∑αiyi=0. (b) Classify each point’s status. (c) Compute μi and which γi are forced to 0.
Asked: Checks and statuses.
Concept: KKT table.
Approach: Apply the table.
Solution: (a) −1−0+0.6+1−0.6=0 ✓. (b) Point 1: α=C ⇒ inside margin or misclassified. Point 2: α=0 ⇒ correct, outside margin. Point 3: 0<α<C ⇒ on margin. Point 4: α=C ⇒ violator. Point 5: on margin. (c) μi=C−αi=(0,1,0.4,0,0.4). μiγi=0 forces γi=0 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: x1=2(+), x2=0(−) with C=0.25. Solve both the dual and the primal and show they agree. Compare with the hard margin (W=1).
Asked:W, margin, slacks.
Concept: The box constraint binds.
Approach: Dual LD=2α−2α2 on 0≤α≤C.
Solution:Dual:LD increases on [0,0.5]; with C=0.25<0.5 the maximum over [0,0.25] is at α=C=0.25. W=αy1x1=0.25×2=0.5. Primal: with symmetry, objective =21W2+C(γ1+γ2) with γ1=1−(2W+b), γ2=1+b (valid when both >0): γ1+γ2=2−2W, so objective =21W2+C(2−2W); derivative W−2C=0⇒W=2C=0.5 ✓ agrees. Margin =2/W=4 (vs 2 hard margin). With b=−0.5: f(2)=0.5 ⇒ γ1=0.5; f(0)=−0.5 ⇒ γ2=0.5. Both have α=C ✓.
Final answer:W=0.5, margin 4, slacks 0.5 each; both points are margin violators with α=C.
Python check
m = SVC(kernel="linear", C=0.25).fit([[2], [0]], [1, -1])print(m.coef_, m.intercept_, m.dual_coef_) # [[0.5]] [-0.5] [[-0.25 0.25]] (α = C)
Key lesson
Small C widens the margin and tolerates slack; here it also makes b non-unique (any b∈[−1,0] gives the same objective).
Common mistakes
Writing αi≤C for the hard-margin case.
Forgetting that αi=C points are support vectors.
Exam traps
The dual objective is identical to hard margin; only the constraints 0≤αi≤C differ.
C=∞ ⇔ hard margin; the lecture’s range is C∈(0,∞].
The lecture’s remark “larger C 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 αi and C.
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
Dual in feature space: LD=∑αi−21∑ijαiαjyiyjK(Xi,Xj), 0≤αi≤C, ∑αiyi=0. The dual lives in Rn — its dimension does not change.
Prediction: sign(∑iαiyiK(Xi,X0)+b), with b=yj−∑iαiyiK(Xi,Xj) for any 0<αj<C.
Worked derivation: the polynomial kernel
K(X,Z)=(1+XTZ)2 for X,Z∈R2:
(1+x1z1+x2z2)2=1+2x1z1+2x2z2+x12z12+x22z22+2x1z1x2z2.
This equals ϕ(X)Tϕ(Z) for ϕ(X)=[1,2x1,2x2,x12,x22,2x1x2] (a map R2→R6): the product of corresponding entries gives each of the six terms above. In general (1+XTZ)p in Rm corresponds to a feature space of dimension (pm+p). The map for a given kernel is not unique (the lecture exhibits a second ϕ′; its printed 7-vector contains a repeated xi1xi2 component and omits the constant, so check it before use).
Key insights
Cost of K: O(m); cost of explicit ϕ: O(mp) 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:K(X,Z)=(1+XTZ)2 for X=(1,2), Z=(3,1).
Asked:K.
Concept: Kernel evaluation.
Approach: Inner product first.
Solution:XTZ=3+2=5; K=(1+5)2=36.
Final answer:36.
Key lesson
One inner product and a power — no feature map needed.
Example 2 — Easy · Check against φ
Problem: Verify K=ϕ(X)Tϕ(Z) for Example 1 using ϕ(X)=[1,2x1,2x2,x12,x22,2x1x2].
Asked: Same value via ϕ.
Concept: Equivalence of kernel and feature map.
Approach: Compute both vectors.
Solution:ϕ(1,2)=[1,1.4142,2.8284,1,4,2.8284]; ϕ(3,1)=[1,4.2426,1.4142,9,1,4.2426]. Products: 1,6,4,9,4,12. Sum =36 ✓.
Example 3 — Moderate · An explicit non-linear map (lecture)
Problem: Positives (1,1),(−1,1),(−1,−1),(1,−1); negatives (2,0),(0,2),(−2,0),(0,−2). Use ϕ(x)=x if ∥x∥<2, else ϕ(x)=(6−x1+(x1−x2)2,6−x2+(x1−x2)2). 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 ∥x∥=2<2 ⇒ unchanged. Negatives (norm =2): (2,0)→(8,10); (0,2)→(10,8); (−2,0)→(12,10); (0,−2)→(10,12). Positives have x1+x2∈{−2,0,2}; negatives 18,18,22,22. The line x1+x2=10 separates (W=(1,1), b=−10).
Final answer: Separated by x1+x2=10 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: A=(1,1)+, B=(−1,−1)+, C=(1,−1)−, D=(−1,1)−. With K=(1+XTZ)2 solve the hard-margin dual.
Asked:α,b, classifier.
Concept: Kernel SVM end-to-end.
Approach: Build the Gram matrix; use symmetry.
Solution:XTZ: same point =2 ⇒ K=9; opposite points: −2 ⇒ K=1; all other pairs 0 ⇒ K=1. So K=9111191111911119, y=(+,+,−,−). By symmetry all αi=α. ∑ijyiyjKij=36+[(∑y)2−∑y2]=36−4=32. LD=4α−16α2⇒α=81. Decision value at A: 81(9+1−1−1)=1; at C: 81(1+1−9−1)=−1. With b=0 every point satisfies yif=1 ✓ (all four are SVs). Expanding the kernel, only the cross term survives: f=x1x2.
Final answer:αi=81, b=0, f(X)=x1x2 (class + iff x1x2>0).
The classic XOR problem is linearly solvable in the feature x1x2; the kernel finds it automatically.
Example 5 — Very Hard · How big is the feature space?
Problem: (a) Feature-space dimension of (1+XTZ)p for m=2,p=2; m=10,p=3; m=1000,p=3. (b) Show the Gaussian kernel’s feature space is infinite-dimensional. (c) Cost comparison for m=1000, p=3.
Asked: Dimensions and argument.
Concept: Why kernels matter.
Approach:(pm+p); Taylor series.
Solution: (a) (24)=6; (313)=286; (31003)=167,668,501. (b) e−∥X−Z∥2/2σ2=e−∥X∥2/2σ2e−∥Z∥2/2σ2eXTZ/σ2, and eu=∑k=0∞k!uk with u=XTZ/σ2. Each term (XTZ)k is a polynomial kernel of degree k with its own feature space; the sum over all k gives infinitely many features. (c) K: about 1000 multiplications + a cube; ϕ: ≈1.7×108 coordinates per point.
Final answer:6, 286, 167,668,501; Gaussian: ∞.
Python check
from math import combprint(comb(4, 2), comb(13, 3), comb(1003, 3)) # 6 286 167668501
Key lesson
The trick turns an intractable (or impossible) computation into O(m).
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 n variables regardless of ϕ‘s dimension.
K(Xi,Xj)=ϕ(Xi)Tϕ(Xj); ϕ 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.
C34. Valid Kernels: Symmetry, Positive Semidefiniteness, Mercer; Popular Kernels
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
Symmetric:K(Xi,Xj)=K(Xj,Xi).
Positive semidefinite: for every n, points X1..Xn and scalars λi: ∑i,jλiλjK(Xi,Xj)≥0, i.e. the Gram matrix Kij is PSD (all eigenvalues ≥0).
Mercer’s theorem: for a symmetric K with ∬G(X)K(X,Y)G(Y)dXdY≥0 for all square-integrable G, there is a space H and map ϕ with K=ϕTϕ.
Popular kernels
Polynomial: K(Xi,Xj)=(1+XiTXj)p.
Gaussian / RBF: K(Xi,Xj)=exp(−∥Xi−Xj∥2/2σ2), σ>0 (the lecture prints e+∥⋅∥2/2σ2, a typo — see Example 4).
Sigmoid: K=tanh(aXiTXj+θ) — not PSD for all parameters.
Derivation: an explicit-feature kernel is PSD
If K(X,Z)=ϕ(X)Tϕ(Z) then ∑ijλiλjKij=∑iλiϕ(Xi)2≥0. ∎
Closure properties (standard)
Sum of kernels, positive scalar multiple, and product of kernels are kernels.
Python helper for the checks in this section
def is_psd(G, tol=1e-10): G = np.asarray(G, float); return np.allclose(G, G.T) and np.linalg.eigvalsh(G).min() >= -tol
Worked examples
Example 1 — Very Easy · Is x−z a kernel?
Problem: Is K(x,z)=x−z a valid kernel?
Asked: Validity.
Concept: Symmetry.
Approach: Swap arguments.
Solution:K(z,x)=z−x=−K(x,z)=K(x,z) unless x=z ⇒ 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: G1=(1221) or G2=(2112)?
Asked: PSD test.
Concept: Eigenvalues.
Approach: Eigenvalues of (abba) are a±b.
Solution:G1: 3,−1 ⇒ not PSD (the vector λ=(1,−1) gives λTG1λ=1−4+1=−2<0). G2: 3,1 ⇒ PSD.
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 tanh(axz+θ) with a=1,θ=1 fails PSD using points x=1 and x=2. (b) Show closure: if K1,K2 are kernels then K1+K2 is; give a valid composite such as (1+XTZ)2+e−∥X−Z∥2/2.
Asked: Counter-example and closure proof.
Concept: Mercer validity.
Approach: Determinant of the Gram matrix.
Solution: (a) K11=tanh(2)=0.96403; K22=tanh(5)=0.99991; K12=tanh(3)=0.99505. det=0.96394−0.99013=−0.0262<0 ⇒ one negative eigenvalue (≈−0.0132) ⇒ not PSD even though a,θ>0. (b) ∑λiλj(K1+K2)ij=∑λiλj(K1)ij+∑λiλj(K2)ij≥0+0 ✓. 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.