Support Vector Machines and Support Vector Regression › Formula
Machine Learning from ScratchAll chapters Support Vector Machines and Support Vector Regression › FormulaSection view · Sections in this chapter4 / 10 ↓
Reading tools Definitions, links & notes
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
We start with a question that is more subtle than it first appears: among all hyperplanes that separate two classes, which one should we trust? A hyperplane is specified by a normal vector and a bias , and the signed decision value is
where:
- : the normal vector of the hyperplane, which fixes its orientation
- : the input point being classified
- : the bias term, which shifts the hyperplane away from the origin
- : the signed decision value, positive on one side of the hyperplane and negative on the other
This tells us which side of the boundary a point lies on; away from a zero score, the predicted signed label is . For a nonzero normal vector, multiplying and by a positive constant preserves both that prediction and the geometric boundary. Infinitely many boundaries may separate a dataset. In the separable hard-margin setting, the SVM selects the boundary with the widest empty buffer. Points with nonzero dual coefficients are called support vectors; in the hard-margin optimum they lie on the buffer's edge.
Margins, support vectors, and hinge loss
The perpendicular distance of a point to the decision hyperplane is
where:
- : the normal vector of the hyperplane
- : the query point whose distance we measure
- : the bias term
- : the Euclidean length of the normal vector
This expression exposes an important scaling issue: multiplying and by the same positive constant leaves the hyperplane and every perpendicular distance unchanged, because the numerator and denominator scale together. The unnormalized decision values and functional margins do scale by that constant. The length therefore becomes a measure of margin width only after we fix the functional-margin scale. Here is the Euclidean length of the normal vector; in the canonical hard-margin scale, the two margin hyperplanes have separation .
For a separating hyperplane, we can fix the scale so that the smallest functional margin is one. At the maximum-margin solution with both classes present, the bias centers the corridor between the nearest opposite-class projections, so the two supporting hyperplanes are and . Their perpendicular separation is , with each one a distance from the decision hyperplane. Maximizing this width is equivalent to minimizing , or . The hard-margin problem therefore asks for the smallest weight norm subject to every training point lying on or beyond its class's margin hyperplane:
where:
- : the objective being minimized, equivalent to maximizing the margin
- : the label of the -th training example
- : the -th training input
- : the signed decision value of the -th example
- : the constraint that every point lies on or beyond its margin boundary
The quantity
is the functional margin of the -th example. In the canonical hard-margin optimum, support vectors have , while points with lie beyond the corridor. In a soft-margin fit, indicates a margin violation, and support vectors can lie inside the margin or even be misclassified. The dual coefficient, rather than nearness alone, determines whether a point appears in the fitted decision function.
If the classes are linearly separable, the hard-margin constraints are feasible. If they are not, no hyperplane can keep the corridor empty, and insisting on the hard constraint makes the optimization problem infeasible. The SVM responds by turning the violation into a penalty. The hinge loss measures exactly how much a point fails the margin requirement:
where:
- : the functional margin of the -th training example
- : the amount by which the functional margin falls short of 1
The maximum with zero clamps the loss at zero, so points with contribute no penalty and points that intrude into the margin or are misclassified incur a linear cost.
The hinge loss is zero once the functional margin reaches 1, and it grows linearly as points cross into the corridor or onto the wrong side. Penalizing these margin violations gives the soft-margin regularized empirical-risk objective:
where:
- : the margin term, which shrinks as the margin widens
- : the penalty parameter controlling the exchange rate between margin width and training error
- : the hinge loss of the -th example
- : the sum over all training examples
The constant sets the exchange rate between weight norm and training hinge loss. On a fixed dataset with the same kernel and penalty convention, increasing cannot increase the optimized total hinge loss, while the optimized squared weight norm cannot decrease. Either quantity can stay unchanged over a range of , and individual residuals or classification errors need not move monotonically. The hinge form needs no separate slack variables and can be optimized by subgradient methods. Because the loss has a kink at margin one, the following expressions give one valid subgradient of :
where:
- : the regularized hinge-loss objective being minimized
- : the set of points currently violating the margin, i.e. those with active hinge loss
- : the exchange rate between margin width and training error
These expressions choose the zero hinge subgradient at points with . At such a kink, any multiple with is allowed for the weight part of the hinge term, with the corresponding for the bias. Strict margin violators have the full hinge contribution, while points with have none. This current strict-violation set is not the dual support-vector set: an optimal point on the margin can have nonzero dual weight even though its hinge loss is zero.
Soft margins and regularization
The classical soft-margin formulation is the constrained version of the regularized empirical-risk objective above. Writing as an explicit variable and minimizing over , , and subject to the relaxed margin constraints gives the same optimum as the hinge-loss problem. At the optimum, each slack records a functional-margin shortfall; a correctly classified point inside the margin can also have positive slack:
where:
- : the slack for the -th example, measuring how far it violates the margin
- : the margin term
- : the total violation penalty, scaled by
- : the relaxed margin constraint, allowing the -th example to fall short of the margin by at most
At any optimum, the slacks are pinned down by the KKT conditions:
where:
- : the optimal slack for the -th example
- : the functional margin of the -th example
- : the amount by which the functional margin falls short of 1
This is why the slack formulation and the hinge-loss formulation describe the same learning problem even though they look different. In the constrained problem the slacks are auxiliary variables, but the KKT conditions pin each one to the value the hinge loss would assign it, so eliminating the slacks recovers the regularized empirical-risk objective exactly. The slack view is valuable because it reveals the constrained optimization structure and prepares us for the Lagrangian, which is where kernels enter.
Introduce multipliers for the margin constraints and for the nonnegativity constraints. The Lagrangian is
where:
- : the primal weight vector and bias
- : the slack for the -th example, measuring how far it violates the margin
- : the Lagrange multiplier for the -th margin constraint
- : the Lagrange multiplier for the nonnegativity constraint
Setting derivatives to zero gives three structural facts. First, yields the weight as a linear combination of training points,
where:
- : the Lagrange multiplier for the -th margin constraint
- : the label of the -th training example
- : the -th training input
- : the sum over all training examples
This is the first sign that the solution may be sparse: any point with contributes nothing to the weight vector. Second, yields the balance condition
where:
- : the Lagrange multiplier for the -th margin constraint
- : the label of the -th training example
- : the sum over all training examples
Third, yields , so eliminating gives the box constraint
Substituting the three equations back into leaves only behind:
where:
- : the dual multiplier for the -th margin constraint, bounded in
- : the upper bound on each multiplier
- : the pairwise inner product between training examples
The dual problem is a convex quadratic program: the objective is a concave quadratic in that we maximize over the box and balance constraints, so the feasible region is convex and any local maximum is global. Apart from the box constraint and the balance condition , its dependence on the training input vectors is only through pairwise inner products . The decision function becomes
where:
- : the input point whose decision value we compute
- : the dual multiplier of the -th training example
- : the bias recovered from the KKT conditions
- : the inner product between the -th training point and the query point
The KKT complementarity conditions require whenever . A point with lies exactly on its margin hyperplane; a point with a strict violation has . At equality , the multiplier can include either endpoint, so touching the margin does not by itself prove . The support vectors are precisely the points with positive dual coefficient in this representation. Only those terms remain in the decision sum, but the support set need not be a small fraction of the data.
There is also a useful connection to ordinary L2 regularization. If we divide the primal objective by and let
the soft-margin objective becomes
Here the loss is averaged over the examples. Under this convention, : increasing reduces the relative strength of the L2 penalty. For a fixed training problem, the optimized total hinge loss cannot increase as grows, although the solution may stay unchanged over a range of values. If the loss is written as an unaveraged sum instead, the corresponding coefficient is . Larger and larger Ridge-style therefore move regularization in opposite directions; always check the loss normalization before comparing libraries or codebases.
Kernel functions and nonlinear boundaries
The dual exposes a clean structural idea. Anywhere the algorithm touches an inner product , we can replace it with a symmetric function that corresponds to an inner product in some feature space , possibly infinite-dimensional:
where:
- : the kernel function, symmetric in its two arguments
- : two input examples
- : the feature map from input space into the (possibly infinite-dimensional) feature space
The dual problem and decision function replace each inner product by the kernel. The weight representation belongs to the feature space: . A nonlinear kernel need not provide a weight vector in the original input coordinates. The decision function is:
where:
- : the dual multiplier of the -th training example
- : the label of the -th training example
- : the kernel similarity between the -th training point and the query point
- : the bias recovered from the KKT conditions
We can now fit nonlinear functions in the original input space without explicitly computing . This is the kernel trick. Three useful kernel examples are:
- Linear kernel, , which recovers the linear soft-margin formulation.
- Polynomial kernel, , with positive integer and . When , its feature map contains scaled monomials of degrees zero through ; when and , it contains only degree- monomials.
- Radial basis function (RBF) kernel, , with . Similarity falls to at Euclidean separation , so this is its characteristic distance scale.
A real-valued kernel is valid when it is symmetric and its Gram matrix is positive semidefinite for every finite input collection. This is the positive-type kernel condition that gives a Hilbert-space feature map. A PSD training matrix alone does not establish that condition on all inputs; Mercer's spectral expansion requires additional regularity assumptions. Logistic-style similarities are not valid kernels in general and can make the dual nonconcave. The linear kernel, the polynomial kernel under the conditions above, and the RBF kernel with are valid. Nonnegative weighted sums and pointwise products of valid kernels remain valid; the latter follows from the Schur product theorem applied to their Gram matrices.
Two controls govern the RBF kernel. A larger makes similarity more local, which can permit a more variable boundary when is large enough; it does not by itself force memorization. A smaller makes similarities vary more slowly. For fixed , taking all the way to zero makes the kernel approach the constant value one, so the fitted scores can flatten toward an intercept rather than recover a linear SVM. The penalty controls the cost of violations and interacts with this locality. Choose them jointly using validation, as covered in Hyperparameter Search and Nested Cross-Validation.
Epsilon-insensitive regression and practical limitations
Support Vector Regression extends everything above to a continuous target . The idea is to build a tube of half-width along the fitted function and ignore residuals smaller than . The loss is symmetric and piecewise linear:
where:
- : the residual for a training example, i.e. the difference between the observed target and the predicted value
- : the half-width of the insensitive tube
- : the amount by which the residual exceeds the tube
- : the loss, zero inside the tube and linear outside
Points inside the tube incur zero loss; points outside incur a linear penalty. This is the regression analogue of the margin neighborhood: small errors are treated as noise and ignored, while large errors become support vectors that shape the fit.
In QP form we introduce two slacks per point, one for errors above the tube, , and one for errors below, :
where:
- : the primal weight vector and bias
- : the slack for residuals above the tube ()
- : the slack for residuals below the tube ()
- : the penalty controlling how strongly residuals outside the tube are penalized
- : the half-width of the insensitive tube
The dual has two multiplier families . With , stationarity in the unpenalized bias gives , and stationarity in the weight gives . Its objective is
Here is the signed coefficient of training example , and the term penalizes the total multiplier mass in the dual maximization. An active tube constraint can still have zero multiplier. A PSD kernel makes this objective concave. The prediction is
where:
- : the dual multiplier for the constraint
- : the dual multiplier for the constraint
- : the kernel similarity between the -th training point and the query point
- : the bias recovered from the KKT conditions
Complementarity explains where nonzero coefficients can occur. For , a point strictly inside the tube has . A point on either edge may have a nonzero multiplier, while a point strictly outside has the corresponding multiplier at . Thus only points on or outside the tube can contribute, but their number need not be small. The tube half-width controls which residuals have zero loss, and controls the penalty beyond the tube. The linear growth of this loss limits sensitivity to large response residuals relative to squared error; it does not protect against every outlier or guarantee better generalization. At , the loss becomes absolute error, and the prediction still uses the net coefficient .
The practical limits depend on the formulation. Exact nonlinear kernel SVMs can be expensive on large datasets, while dedicated linear solvers avoid the full kernel matrix. Choose input scales that reflect the problem and evaluate preprocessing within the training folds. A binary classification score is ; when , its signed geometric distance is in the model's feature space. Neither quantity is automatically a probability. Validate the relevant hyperparameters, including , kernel settings such as , and for SVR, alongside any probability calibration needed downstream.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!