SVM: Linear
- Support Vector Machine
- A classification algorithm that finds the best boundary to separate two classes () by maximizing the distance to the closest observations.
Observations that are included within the dashed lines are called as support vectors.
| Description | Formula |
|---|---|
| Equation for a line in two dimensions | |
| Equation for a hyperplane | |
| Observations above the hyperplane | |
| Observations below the hyperplane |
Observations below the hyperplane are classified as .
Observations above the hyperplane are classified as .
Since is either , is positive for correctly classified observations, negative for misclassified observations, and zero on the boundary.
For every correctly classified observation, this statement holds: .
This can be redefined with a margin, , such that: .
is the distance of the line to the closest observation.
Linearly Separable¶
Maximum Margin Classifier¶
implies no errors.
implies to find the hyperplane that gives the largest possible margin between the two classes.
These coefficients, , should give the largerst possible margin .
Noramlization of controls the margin width.
- Normalization
- Fixing the scale of the vector so that it has a specific length.
By notating , the coefficient scale is fixed to 1. Can also be written as .
Ensure that the constraint on is met by dividing by the norm.
This proves that when you normalize , the result has length 1.
can be multiplied by any positive constant, , because both sides will scale equally. The actual values of don’t uniquely matter, only their ratios do.
Formula Rearrangements¶
This is the original form that maximizes margin , while keeping a unit vector. It is hard to optimize because of the constraint.
Subject to: “Under the condition that...” Hence, Find the that makes as small as possible, but only consider solutions where every training point is correctly classified with margin .
Maimizing is the same thing as minimizing .
Drop the constraint. Absorb the unit norm constraitn into the inequality, and can be any scale.
If we assume ,
Minimizing is the same thing as minimizing . If is minimized at some , then is also minimized at the exact point at .
Final optimization for quadratic optimization problem with a linear constant:
Geometric margin and coefficient scale¶
Multiplying both and by the same positive constant does not change the separating hyperplane. The geometric distance of from it is
In the unit-norm formulation, the signed score is already a signed distance. In the canonical formulation, the closest correctly classified points satisfy , so the distance to either margin plane is . The full width between the two margin planes is .
This normalization is why minimizing maximizes the margin. The intercept is optimized too, but it is not included in the norm: shifting the plane does not change the length of its normal vector.
To minimizie as a constraint, the Lagragian trick merges the objective and constraint into a single expression.
| Term | Expression | Meaning |
|---|---|---|
| First term | ½‖β‖² | The original objective you want to minimize |
| Second term | ∑αᵢ[yᵢ(β₀ + xᵢᵀβ) - 1] | Penalizes violations of the constraint |
| αᵢ | Lagrange multipliers | One per training point, weights that control how much each constraint matters |
Set derivatives with respect to to zero (to find a minimum of a function):
Writing in vector notation avoiding index :
Thus, we know is a linear combination of the observations.
- Lagrange Multiplier
- A variable that convers a constrained optimization problem into an unconstriained one.
- measures how much the constraint is pushing back against the objective. The larger is, more influeential the constraint is.
In the hard-margin formulation, only observations on a margin plane can have .
Observations strictly beyond the margin planes have .
- Kuhn-Tucker Condition
- Rules that the optimal solution must satisfy when you have an optimization problem with inequality constraints.
- To optimize , ,
- If is a local optimum, subject to regularity conditions, then there exist a constant multiplier such that,
Complementary Slackness¶
Stemming from , complementary so that the two terms complement each other to make zero. At least one factor must be zero; both can be zero.
A point is on the boundary if .
Also, if , then is on the boundary. If is not on the boundary, then .
Beta Optimization¶
Hence, this proves that estimation of is only dependent on support vectors.
Not Linearly Separable¶

In cases like this, two classes are not completely separable.
Non-linearly separable classess can be managed with some errors. This is the slack variables, or .
All observations between the margin, and the separating hyperplane, and all misclassified observations (on the other side of the separating hyperplane) have a positive .
Errors, , of the correctly classified observations before the margin is 0.
Error Limit¶
The sum of cannot exceed .
is the tuning parameter; the cost parameter.
Larger value allows for more error and have larger margins. Also correspond to more support vectors because more observations lie within the larger margins.
Two meanings of ¶
Above, is a budget in . Increasing that budget permits more total margin violation. Another common formulation uses as a penalty coefficient:
Here, larger makes violations more expensive, so its direction is opposite to the budget convention. Always check the objective before interpreting the tuning parameter.
For the smallest feasible slack, :
: on or beyond the correct margin plane.
: correctly classified but inside the margin.
: on the decision boundary.
: misclassified.
In this penalty formulation, the dual coefficients satisfy . A point with lies on a margin plane; points inside the margin can have . Support vectors therefore need not all lie exactly on the margin in a soft-margin model.
Prediction¶
| Score | sign() | Predicted class |
|---|---|---|
| Positive | +1 | Class +1 |
| Negative | -1 | Class -1 |
| Zero | 0 | On the boundary |
How a new observation is being classified is decided by which side of the hyperplane the observation falls on.
Mathematically, the sign of the estimated value, , determines whether the observation is classified as +1 or -1, where:
where is the inner dot product, is the estimated coefficients arising during optimization.
The dot product is a similarity measure affected by both direction and magnitude, where is an observation from training data and is an observation from test data.
SVM: Kernels¶

Add quadratic derived variables, so that the linear separation becomes possible in an expanded space, but it is still nonlinear in the original space.
In the original space (Assume we have ), classes may not be separable.
Add new features like and now we’re in the high dimensional, expanded space. In this space, a linear boundary may separate patterns that could not be separated in the original coordinates.
But when you project that boundary back to the original space, it looks nonlinear.
Adding nonlinearities in .
The original feature space has variables, but expanded feature spaces have variables.
SVM is a linear method in the expanded features space.
SVM accommodates nonlinearity in the original feature space.
- Kernels
- A function that computes the similarity between two points in a higher dimensional space, without actually going there. Instead of expanding to high dimensions (which can be slow), directly computes what the dot product in that high dimensional space would have been.
How kernels expand feature space is not obvious.
Predictions¶
From , the predicted value is:
| Symbol | Type | Meaning |
|---|---|---|
| Vector | Training data (feature vector for observation ) | |
| Vector | A new vector that we want to predict | |
| Scalar | Class label (+1 or -1) | |
| Scalar | Lagrange multipliers arising during optimization | |
| Index set | Indices of support vectors |
Can be re-written as:
The solution depends only on the inner product of the observations, not on the observations themselves.
Non-Linear Expansion¶
Rather than adding quadratic terms to the -variables, it is possible to expand the feature space as a function of the original -variables.
Referes to the standard SVM without any additional -variables.
Has no additonal tuning parameters.
Often preferred in text mining applications with -gram variables.
Sparse matrices and indicator variables.
Tuning parameters: .
In practice, often set
RBF Kernel¶
Also called as Gaussian Kernel.
Tuning parameter: .
determines how fast the influence of neighboring observations on the classification of the test observation drops off as a function of distance. Larger leads to a local fit.
is a test observation.
When a test observation is very far from training observation, the kernel value approaches 0 as the exponent becomes increasingly negative.
Training observations far from test observation contribute very little to classification.
Most popular choice.
Corresponds to an infinite number of -variables.
Interpreting together with ¶
For the RBF kernel, two observations a distance apart have similarity . At , it is 1; as distance grows, the kernel value approaches 0. Larger makes each observation’s influence more local.
A large and a large penalty-form can give a highly irregular boundary. A small makes similarities vary more slowly, but its effect still depends on and the scaled distances. Tune them together using validation folds, fitting the scaling transformation within each fold.
A nonlinear feature map makes more boundaries available; it does not guarantee that every dataset becomes separable. Two observations with identical predictors and opposite labels remain indistinguishable under any deterministic feature map.
Tuning parameters: .
Not commonly used because of some undesirable theoretical properties.
| Kernel | When to use |
|---|---|
| Linear | Data is already high dimensional (e.g. text, genomics), large datasets, when you suspect linear separability |
| Polynomial | When you believe feature interactions matter, image recognition |
| RBF/Gaussian | Default choice, when you have no prior knowledge, works well in most cases |
| Sigmoid | Rarely used, sometimes in neural network contexts |
Platt’s Method¶
- Platt Scaling
- A method to convert SVM’s raw decision scores into probabilities.
Since logistic regression computes a probability which is rounded up or down for classification, SVM yield a score.
Positive scores: the uncalibrated decision rule predicts .
Negative scores: the uncalibrated decision rule predicts .
Converting Scores into Probabilities¶
This is Platt’s way of estimating probabilities based on the score.
If a probability is needed, we can use logistic regression on the score:
The probabilities can be estimated as usual by solving for .
Fitting the calibration step¶
The decision score gives a side of the boundary and a margin-related magnitude. A score of 2 does not mean a probability of 0.88 unless a particular fitted calibration map makes it so.
Fit the sigmoid mapping using held-out scores or out-of-fold predictions, then evaluate it on separate data. Fitting the map to the same in-sample scores used to train the SVM can make probability estimates too optimistic. Calibration can improve the meaning of probabilities without changing the underlying ordering when the fitted map is increasing.
Multi-Class SVM¶
Typically, SVM only allows for two classes.
Can extend two-class classifiers:
1. One vs. All¶
More common.
With classes, apply SVM once for each class.
Class vs. all remaining claases.
Each SVM classifier gives a score for class (vs. all others.)
Predict the class that has the highest score .
2. One vs. One¶
classes give ( choose 2) possible pairs.
SVM is conducted on each pair.
Keep track of which class wins each time.
Predict the class with the most wins.
Usage
Works well with large feature numbers.
Works well when number of features exceeds the number of observations.
Memory efficient for prediction, because only support vectors are needed.
Limitations
When the dataset is large, training time can be very long.
For nonlinear kernels, you may encounter convergence problems.
Does not work as well in the presence of a lot of noise variables.
Probability estimates are ad-hoc.
SVM is sensitive to scaling of -variables.
