Sub topic 7
From the primal to the dual problem
This chapter assumes you know the soft-margin SVM from the previous chapters. You should also be comfortable with dot products and with Lagrange multipliers for constrained minimization.
Why change the problem
The primal soft-margin problem has variables w, b, and one slack value per sample. It is written in terms of the feature vectors themselves. The dual problem has one variable per training sample, and the data enters only through dot products between pairs of samples. That structure is what makes the kernel trick possible.
Building the Lagrangian
The primal problem is:
minimize 0.5 * ||w||^2 + C * sum(xi_i)
subject to y_i * (w.x_i + b) >= 1 - xi_i, xi_i >= 0Attach a multiplier alpha_i >= 0 to each margin constraint, and a multiplier mu_i >= 0 to each constraint xi_i >= 0. Then set the derivatives of the Lagrangian to zero:
- With respect to
w:w = sum(alpha_i * y_i * x_i). - With respect to
b:sum(alpha_i * y_i) = 0. - With respect to
xi_i:C - alpha_i - mu_i = 0. Sincemu_i >= 0, this givesalpha_i <= C.
Substituting these back removes w, b, and xi from the Lagrangian. What remains depends only on alpha.
The dual problem
maximize sum(alpha_i) - 0.5 * sum_i sum_j alpha_i * alpha_j * y_i * y_j * (x_i . x_j)
subject to 0 <= alpha_i <= C, sum(alpha_i * y_i) = 0This is a quadratic program with box constraints and one equality constraint. The hard-margin dual is the same problem without the upper bound C.
The prediction for a new point x also uses only dot products:
f(x) = sum(alpha_i * y_i * (x_i . x)) + bReplacing each dot product x_i . x_j with a kernel k(x_i, x_j) gives a nonlinear SVM without ever computing the feature map.
Checking it in scikit-learn
By complementary slackness, alpha_i = 0 for points strictly outside the margin. Only support vectors have nonzero alpha_i. The script below rebuilds w from the dual coefficients and checks the constraints.
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.svm import SVC
X, y = make_blobs(n_samples=100, centers=2, cluster_std=1.2, random_state=0)
clf = SVC(kernel="linear", C=1.0).fit(X, y)
# dual_coef_ holds alpha_i * y_i for the support vectors only
w = clf.dual_coef_ @ clf.support_vectors_
print("w rebuilt from alphas matches coef_:", np.allclose(w, clf.coef_))
print("all alpha <= C:", np.abs(clf.dual_coef_).max() <= 1.0 + 1e-9)
print("sum(alpha_i * y_i) = 0:", np.isclose(clf.dual_coef_.sum(), 0.0, atol=1e-8))Reading the multipliers
The value of alpha_i tells you where a point sits:
| Value of alpha_i | Position of the point |
|---|---|
| 0 | Outside the margin, correctly classified |
| Between 0 and C | Exactly on the margin |
| Equal to C | Inside the margin or misclassified |
Common errors
- Forgetting that the dual ties
bto the equality constraint.bis not a dual variable. It is recovered afterward from any point with0 < alpha_i < C. - Reading
dual_coef_as unsigned. - Expecting
coef_with a nonlinear kernel.wlives in the implicit feature space, so onlydual_coef_is available.
Exercise
In the soft-margin dual, what bounds does each alpha_i have, and where does the upper bound come from?
Show answer
Each alphai satisfies 0 <= alphai <= C. The upper bound comes from the derivative with respect to the slack xii, which gives C - alphai - mui = 0, and mui >= 0 forces alpha_i <= C.
Exercise
Why does the dual form allow a kernel but the primal form does not?
Show answer
The dual objective and the prediction function use the data only through dot products xi . xj. These can be replaced by k(xi, xj) without computing explicit features, whereas the primal needs w in feature space.