Sub topic 7
From the primal to the dual problem
This chapter assumes you know the soft-margin SVM objective from the previous chapters, and that you can take gradients and read a constrained minimization problem. Lagrange multipliers help, and the chapter reviews what you need. The goal is to rewrite the SVM as a problem over one number per training point. That rewrite is what makes support vectors visible and the kernel trick possible.
Why rewrite the problem at all
The primal problem is stated in terms of w and b. The soft-margin form is:
- minimize
0.5 * ||w||^2 + C * sum(xi_i) - subject to
y_i * (w·x_i + b) >= 1 - xi_iandxi_i >= 0for every pointi
Here y_i is -1 or +1. The unknown w has one entry per feature. If you want features such as all degree-3 polynomial terms, w becomes enormous. The dual problem has one unknown per training example, and it touches the data only through dot products x_i·x_j. You can then replace those dot products with a kernel and never build the features.
Building the Lagrangian
Start with the hard-margin case because it is shorter. The constraints are y_i(w·x_i + b) - 1 >= 0. Give each constraint a multiplier alpha_i >= 0 and subtract the weighted constraints from the objective:
L(w, b, alpha) = 0.5 * w·w - sum_i alpha_i * [ y_i (w·x_i + b) - 1 ]
For fixed alpha, you minimize L over w and b. Setting the gradients to zero gives two conditions:
dL/dw = 0givesw = sum_i alpha_i y_i x_i.dL/db = 0givessum_i alpha_i y_i = 0.
The first condition says the weight vector is a combination of training points. Points with alpha_i = 0 do not contribute at all.
Substituting back: the dual
Plug w = sum_i alpha_i y_i x_i back into L. The b term vanishes because of sum_i alpha_i y_i = 0. What remains is a function of alpha alone:
W(alpha) = sum_i alpha_i - 0.5 * sum_i sum_j alpha_i alpha_j y_i y_j (x_i·x_j)
The dual problem is to maximize W(alpha) subject to alpha_i >= 0 and sum_i alpha_i y_i = 0. It is a concave quadratic program with linear constraints. The primal is convex with linear constraints, so strong duality holds. The optimal dual value equals the optimal primal value, and solving either gives the other.
The soft-margin dual
The soft-margin Lagrangian has a second set of multipliers mu_i >= 0 for the constraints xi_i >= 0:
L = 0.5 * w·w + C * sum_i xi_i - sum_i alpha_i [ y_i (w·x_i + b) - 1 + xi_i ] - sum_i mu_i xi_i
Setting dL/dxi_i = 0 gives C - alpha_i - mu_i = 0. Since mu_i >= 0, this forces alpha_i <= C. The slack variables disappear from the dual, and the only change from the hard-margin case is an upper bound on each multiplier:
- maximize
sum_i 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 <= Candsum_i alpha_i y_i = 0
As C goes to infinity, the box becomes alpha_i >= 0 and you recover the hard-margin dual. This is the dual view of what C does: it caps how much influence any single point can have.
KKT conditions and what they say about each point
At the optimum, the Karush-Kuhn-Tucker (KKT) conditions hold. Besides the stationarity conditions above, there are complementary slackness conditions:
alpha_i * [ y_i f(x_i) - 1 + xi_i ] = 0mu_i * xi_i = 0
Here f(x) = w·x + b. Together with alpha_i + mu_i = C, they sort every training point into one of three cases.
| Multiplier | Slack | Position | Role |
|---|---|---|---|
| `alpha_i = 0` | `xi_i = 0` | `y_i f(x_i) >= 1`, outside or on the margin | Does not affect the solution |
| `0 < alpha_i < C` | `xi_i = 0` | `y_i f(x_i) = 1`, exactly on the margin | Free support vector |
| `alpha_i = C` | `xi_i >= 0` | `y_i f(x_i) <= 1`, inside the margin or misclassified | Bounded support vector |
Support vectors are the points with alpha_i > 0. Delete any point with alpha_i = 0 and refit, and you get the same boundary.
The intercept b does not appear in the dual. You recover it from any free support vector, where y_i f(x_i) = 1, so b = y_i - sum_j alpha_j y_j (x_j·x_i). Solvers usually average this over all free support vectors for numerical stability.
Predicting with the dual
Substituting w into f gives:
f(x) = sum_i alpha_i y_i (x_i·x) + b
The sum effectively runs only over support vectors. The prediction compares the new point with each support vector by dot product, weights each comparison by alpha_i y_i, and adds b.
Checking it in scikit-learn
scikit-learn's SVC stores the dual solution. Its attribute dual_coef_ holds the products alpha_i * y_i for the support vectors only, and support_vectors_ holds the matching rows of X. The script below checks the derivation: the reconstruction of w, the equality constraint, the box constraint, the prediction formula, and the primal and dual objective values.
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.svm import SVC
X, y01 = make_blobs(n_samples=100, centers=2, cluster_std=2.0, random_state=0)
y = 2 * y01 - 1 # labels in {-1, +1}
C = 1.0
clf = SVC(kernel="linear", C=C, tol=1e-8).fit(X, y)
a_y = clf.dual_coef_[0] # alpha_i * y_i for support vectors
sv = clf.support_vectors_
b = clf.intercept_[0]
# Stationarity: w = sum_i alpha_i y_i x_i
w = a_y @ sv
print("w matches coef_:", np.allclose(w, clf.coef_[0]))
# Equality constraint: sum_i alpha_i y_i = 0
print("sum alpha_i y_i:", a_y.sum())
# Box constraint: 0 <= alpha_i <= C
alpha = np.abs(a_y)
at_bound = np.isclose(alpha, C, atol=1e-6)
print("support vectors:", len(alpha), "| at bound alpha=C:", at_bound.sum())
print("max alpha <= C:", alpha.max() <= C + 1e-9)
# Prediction using only dot products with support vectors
gram = X @ sv.T # shape (n_samples, n_support)
f_dual = gram @ a_y + b
print("dual prediction matches:", np.allclose(f_dual, clf.decision_function(X)))
# Strong duality: primal and dual objective values agree
margins = y * (X @ w + b)
primal = 0.5 * w @ w + C * np.maximum(0.0, 1.0 - margins).sum()
dual = alpha.sum() - 0.5 * w @ w
print("primal:", primal, "dual:", dual, "gap:", primal - dual)The gram line is the point of the chapter. The prediction needs only the matrix of dot products between inputs and support vectors. In the next chapter, you replace X @ sv.T with a kernel matrix and the rest of the code stays the same.
Common errors
- Reading
dual_coef_as alpha. It storesalpha_i * y_i, so signs differ by class. Take the absolute value to getalpha_i. - Assuming the label encoding is free. The dual formulas require labels in {-1, +1}. scikit-learn handles this internally, but any hand-written objective such as
hinge_objectiveneeds the conversion. - Expecting an exact zero gap. Solvers stop at a tolerance, so the primal-dual gap is small but not exactly zero. Tightening
tolshrinks it. - Treating the bound
alpha_i = Cas a label problem. Bounded support vectors include points inside the margin that are still correctly classified, not only misclassified points. - Thinking the dual is always faster. With a linear kernel and many samples, solving the primal directly (as
LinearSVCdoes) can be much cheaper than working with ann-by-nproblem.
Exercise
In the soft-margin dual, a training point has alphai = 0.4 and C = 1. Where does that point lie relative to the margin, and what is its slack xii?
Show answer
Because 0 < alphai < C, it is a free support vector. Complementary slackness requires yi f(xi) = 1 + 0 on the margin, and mui = C - alphai = 0.6 > 0 forces xii = 0. The point sits exactly on the margin with zero slack.
Exercise
Why does the term sumi alphai y_i = 0 appear in the dual but b does not?
Show answer
Setting dL/db = 0 produces the constraint sumi alphai y_i = 0, because b appears only linearly in the Lagrangian. Substituting that constraint back removes b from the objective. The intercept is recovered afterwards from a free support vector.