Support Vector Machines (SVM)
1. Geometric Intuition and the Margin ¶ The goal of a Support Vector Machine is to find a hyperplane w ⊤ x + b = 0 w^\top x + b = 0 w ⊤ x + b = 0 that separates two classes with the maximum margin .
1.1 Distance to the Hyperplane ¶ For any point x i x_i x i , the signed distance to the hyperplane is given by:
γ i = w ⊤ x i + b ∥ w ∥ \gamma_i = \frac{w^\top x_i + b}{\|w\|} γ i = ∥ w ∥ w ⊤ x i + b To ensure the distance is always positive for correctly classified points, we define the functional margin as y i ( w ⊤ x i + b ) y_i(w^\top x_i + b) y i ( w ⊤ x i + b ) . The geometric margin is then:
Margin = y i ( w ⊤ x i + b ) ∥ w ∥ \text{Margin} = \frac{y_i(w^\top x_i + b)}{\|w\|} Margin = ∥ w ∥ y i ( w ⊤ x i + b ) 1.2 The Canonical Hyperplane ¶ Since scaling w w w and b b b by a constant does not change the hyperplane, we can fix the functional margin of the points closest to the hyperplane (the support vectors) to be exactly 1:
y i ( w ⊤ x i + b ) = 1 y_i(w^\top x_i + b) = 1 y i ( w ⊤ x i + b ) = 1 Under this convention, the total margin between the two classes is 2 ∥ w ∥ \frac{2}{\|w\|} ∥ w ∥ 2 .
2. Primal Optimization Problems ¶ 2.1 Hard-Margin SVM (Linearly Separable) ¶ When the data is perfectly separable, we maximize the margin by minimizing the inverse:
min w , b 1 2 ∥ w ∥ 2 s.t. y i ( w ⊤ x i + b ) ≥ 1 , i = 1 , … , m \begin{aligned} \min_{w, b} & \quad \frac{1}{2}\|w\|^2 \\ \text{s.t.} & \quad y_i(w^\top x_i + b) \ge 1, \quad i=1, \dots, m \end{aligned} w , b min s.t. 2 1 ∥ w ∥ 2 y i ( w ⊤ x i + b ) ≥ 1 , i = 1 , … , m 2.2 Soft-Margin SVM (Non-Separable) ¶ To handle noise or overlapping distributions, we introduce slack variables ξ i ≥ 0 \xi_i \ge 0 ξ i ≥ 0 .
min w , b , ξ 1 2 ∥ w ∥ 2 + C ∑ i = 1 m ξ i s.t. y i ( w ⊤ x i + b ) ≥ 1 − ξ i , ξ i ≥ 0 \begin{aligned} \min_{w, b, \xi} & \quad \frac{1}{2}\|w\|^2 + C \sum_{i=1}^m \xi_i \\ \text{s.t.} & \quad y_i(w^\top x_i + b) \ge 1 - \xi_i, \quad \xi_i \ge 0 \end{aligned} w , b , ξ min s.t. 2 1 ∥ w ∥ 2 + C i = 1 ∑ m ξ i y i ( w ⊤ x i + b ) ≥ 1 − ξ i , ξ i ≥ 0 C C C (Hyperparameter): Controls the trade-off between maximizing the margin and minimizing classification error.
Hinge Loss Equivalence: For a fixed ( w , b ) (w, b) ( w , b ) , the optimal ξ i \xi_i ξ i is max ( 0 , 1 − y i ( w ⊤ x i + b ) ) \max(0, 1 - y_i(w^\top x_i + b)) max ( 0 , 1 − y i ( w ⊤ x i + b )) . Thus, the objective is equivalent to:
min w , b 1 2 ∥ w ∥ 2 + C ∑ i = 1 m HingeLoss ( y i , w ⊤ x i + b ) \min_{w, b} \frac{1}{2}\|w\|^2 + C \sum_{i=1}^m \text{HingeLoss}(y_i, w^\top x_i + b) w , b min 2 1 ∥ w ∥ 2 + C i = 1 ∑ m HingeLoss ( y i , w ⊤ x i + b ) 3. Lagrange Duality and the Dual Problem ¶ To solve the constrained optimization efficiently and enable the use of kernels, we derive the Wolfe Dual .
3.1 The Lagrangian ¶ For the soft-margin case, we define the Lagrangian L L L with multipliers α i ≥ 0 \alpha_i \ge 0 α i ≥ 0 and μ i ≥ 0 \mu_i \ge 0 μ i ≥ 0 :
L ( w , b , ξ , α , μ ) = 1 2 ∥ w ∥ 2 + C ∑ i = 1 m ξ i − ∑ i = 1 m α i [ y i ( w ⊤ x i + b ) − 1 + ξ i ] − ∑ i = 1 m μ i ξ i L(w, b, \xi, \alpha, \mu) = \frac{1}{2}\|w\|^2 + C \sum_{i=1}^m \xi_i - \sum_{i=1}^m \alpha_i [y_i(w^\top x_i + b) - 1 + \xi_i] - \sum_{i=1}^m \mu_i \xi_i L ( w , b , ξ , α , μ ) = 2 1 ∥ w ∥ 2 + C i = 1 ∑ m ξ i − i = 1 ∑ m α i [ y i ( w ⊤ x i + b ) − 1 + ξ i ] − i = 1 ∑ m μ i ξ i 3.2 Stationarity Conditions ¶ To find the dual, we take the partial derivatives of L L L and set them to zero:
∂ L ∂ w = 0 ⟹ w = ∑ i = 1 m α i y i x i \frac{\partial L}{\partial w} = 0 \implies w = \sum_{i=1}^m \alpha_i y_i x_i ∂ w ∂ L = 0 ⟹ w = ∑ i = 1 m α i y i x i
∂ L ∂ b = 0 ⟹ ∑ i = 1 m α i y i = 0 \frac{\partial L}{\partial b} = 0 \implies \sum_{i=1}^m \alpha_i y_i = 0 ∂ b ∂ L = 0 ⟹ ∑ i = 1 m α i y i = 0
∂ L ∂ ξ i = 0 ⟹ C − α i − μ i = 0 \frac{\partial L}{\partial \xi_i} = 0 \implies C - \alpha_i - \mu_i = 0 ∂ ξ i ∂ L = 0 ⟹ C − α i − μ i = 0
From (3), since μ i ≥ 0 \mu_i \ge 0 μ i ≥ 0 , we conclude that 0 ≤ α i ≤ C 0 \le \alpha_i \le C 0 ≤ α i ≤ C .
3.3 The Dual Objective ¶ Substituting the stationarity results back into the Lagrangian yields the Dual Problem :
max α ∑ i = 1 m α i − 1 2 ∑ i = 1 m ∑ j = 1 m α i α j y i y j ( x i ⊤ x j ) s.t. ∑ i = 1 m α i y i = 0 0 ≤ α i ≤ C , i = 1 , … , m \begin{aligned} \max_{\alpha} & \quad \sum_{i=1}^m \alpha_i - \frac{1}{2} \sum_{i=1}^m \sum_{j=1}^m \alpha_i \alpha_j y_i y_j (x_i^\top x_j) \\ \text{s.t.} & \quad \sum_{i=1}^m \alpha_i y_i = 0 \\ & \quad 0 \le \alpha_i \le C, \quad i=1, \dots, m \end{aligned} α max s.t. i = 1 ∑ m α i − 2 1 i = 1 ∑ m j = 1 ∑ m α i α j y i y j ( x i ⊤ x j ) i = 1 ∑ m α i y i = 0 0 ≤ α i ≤ C , i = 1 , … , m 4. KKT Conditions and Parameter Recovery ¶ 4.1 Complementary Slackness ¶ The Karush-Kuhn-Tucker (KKT) conditions must hold at the optimum:
α i [ y i ( w ⊤ x i + b ) − 1 + ξ i ] = 0 \alpha_i [y_i(w^\top x_i + b) - 1 + \xi_i] = 0 α i [ y i ( w ⊤ x i + b ) − 1 + ξ i ] = 0
μ i ξ i = ( C − α i ) ξ i = 0 \mu_i \xi_i = (C - \alpha_i) \xi_i = 0 μ i ξ i = ( C − α i ) ξ i = 0
4.2 Support Vector Roles ¶ α i = 0 \alpha_i = 0 α i = 0 : The point x i x_i x i is correctly classified and lies outside the margin. It does not affect w w w .
0 < α i < C 0 < \alpha_i < C 0 < α i < C : From condition (2), ξ i = 0 \xi_i = 0 ξ i = 0 . These points lie exactly on the margin (y i ( w ⊤ x i + b ) = 1 y_i(w^\top x_i + b) = 1 y i ( w ⊤ x i + b ) = 1 ). They are the “Free Support Vectors.”
α i = C \alpha_i = C α i = C : From condition (1), these points are “Bounded Support Vectors” and may lie inside the margin or be misclassified (ξ i > 0 \xi_i > 0 ξ i > 0 ).
4.3 Recovering b ∗ b^* b ∗ ¶ For any support vector x j x_j x j where 0 < α j < C 0 < \alpha_j < C 0 < α j < C , we know y j ( w ⊤ x j + b ) = 1 y_j(w^\top x_j + b) = 1 y j ( w ⊤ x j + b ) = 1 . Multiplying by y j y_j y j :
b ∗ = y j − ∑ i = 1 m α i y i ( x i ⊤ x j ) b^* = y_j - \sum_{i=1}^m \alpha_i y_i (x_i^\top x_j) b ∗ = y j − i = 1 ∑ m α i y i ( x i ⊤ x j ) To increase numerical stability, we typically average this over the set of all such support vectors M M M :
b ∗ = 1 ∣ M ∣ ∑ j ∈ M ( y j − ∑ i = 1 m α i y i ( x i ⊤ x j ) ) b^* = \frac{1}{|M|} \sum_{j \in M} \left( y_j - \sum_{i=1}^m \alpha_i y_i (x_i^\top x_j) \right) b ∗ = ∣ M ∣ 1 j ∈ M ∑ ( y j − i = 1 ∑ m α i y i ( x i ⊤ x j ) ) 5. The Kernel Trick ¶ The dual objective and the prediction rule depend only on the inner product x i ⊤ x j x_i^\top x_j x i ⊤ x j . This allows us to map data into a high-dimensional feature space ϕ ( x ) \phi(x) ϕ ( x ) without explicitly computing the coordinates.
5.1 Kernel Function ¶ We define a kernel K ( x i , x j ) = ϕ ( x i ) ⊤ ϕ ( x j ) K(x_i, x_j) = \phi(x_i)^\top \phi(x_j) K ( x i , x j ) = ϕ ( x i ) ⊤ ϕ ( x j ) . Common kernels include:
Polynomial: K ( x , z ) = ( x ⊤ z + c ) d K(x, z) = (x^\top z + c)^d K ( x , z ) = ( x ⊤ z + c ) d
Gaussian (RBF): K ( x , z ) = exp ( − ∥ x − z ∥ 2 2 σ 2 ) K(x, z) = \exp\left(-\frac{\|x - z\|^2}{2\sigma^2}\right) K ( x , z ) = exp ( − 2 σ 2 ∥ x − z ∥ 2 )
5.2 Mercer’s Theorem ¶ A function K ( x , z ) K(x, z) K ( x , z ) is a valid kernel if and only if for any finite set of points, the Kernel Matrix (Gram Matrix) G i j = K ( x i , x j ) G_{ij} = K(x_i, x_j) G ij = K ( x i , x j ) is symmetric and positive semi-definite.
5.3 Prediction Rule ¶ The final decision function for a new point x x x becomes:
f ( x ) = sign ( ∑ i ∈ SV α i y i K ( x i , x ) + b ) f(x) = \text{sign}\left( \sum_{i \in \text{SV}} \alpha_i y_i K(x_i, x) + b \right) f ( x ) = sign ( i ∈ SV ∑ α i y i K ( x i , x ) + b )