Soft-Margin SVMs and the C Parameter
Real data overlaps, so an SVM has to let some points sit inside its corridor. C is the fine it charges them. Turning C up buys a boundary that contorts to accommodate every last point; turning it down buys a smooth one that shrugs at outliers.
Prerequisites: Support Vector Machines, Convex Optimization
The original support vector machine demands that every training point stay outside a corridor of a fixed width. On paper that is elegant. On real data it is unusable, because a single mislabelled row on the far side of the gap makes the whole problem infeasible — there is no line at all that satisfies the constraints, and the optimiser returns nothing. Even when the data is separable in principle, one noisy point can drag the boundary halfway across the plane to accommodate itself.
The soft margin fixes this by letting points trespass and charging them for it. The knob that sets the charge is called , and it is the single most consequential number you will pick when fitting an SVM.
The mental picture is a parking fine. The corridor is a no-parking zone down the middle of the road. A low fine means the council leaves the zone wide and tolerates a few cars in it. A high fine means nobody dares park there, so the council narrows the zone until it fits between the parked cars. Neither is right or wrong; it is a policy choice about which nuisance you mind more.
The maths, one symbol at a time
Each training point gets its own slack , measuring how far inside the corridor it has strayed. Zero means it is safely outside; a value between 0 and 1 means it is inside the corridor but still on the correct side; anything above 1 means it is misclassified outright. The constraints relax from "score at least 1" to "score at least 1 minus your slack", and the objective picks up a bill:
In plain English: keep the weights small — which is the same as keeping the corridor wide, since its width is — and pay for every unit of trespass. The two terms pull in opposite directions and sets the exchange rate between them.
Written the other way round, the same problem is ordinary regularised loss minimisation:
In plain English: minimise hinge loss plus a penalty on the size of the weights. That makes the familiar ridge-style shrinkage parameter, so is inverse regularisation strength. Large means almost no shrinkage.
Worked example 1: what one outlier costs
Five points on a single feature axis. Four are well behaved: and are class ; and are class . The fifth, at , is labelled but sits deep in negative territory. The classifier is .
Option A — keep the wide corridor and pay the fine. Fit on the four clean points. The corridor edges land on and , giving and . Subtracting, , so and . Check the odd point: , and since it needs a score of at least , so its slack is . The bill is
The corridor width is .
Option B — squeeze the corridor so nobody trespasses. Make the odd point a corridor edge alongside : now and , so , giving and . Every point is now outside, all slacks are zero, and the bill is . The corridor width has collapsed to .
Now compare at two settings:
- : Option A costs , Option B costs 8. The SVM shrugs and keeps the wide street.
- : Option A costs , Option B costs 8. The SVM contorts for one point.
The switch happens at , so . Below that the outlier is ignored; above it, the outlier rewrites the model and the corridor gets four times narrower. Drag the flexibility control below through the same arc — the boundary goes from too rigid to sensible to fenced around individual points.
Worked example 2: C does not survive a change in sample size
Here is a trap that bites people who tune on a subsample. The penalty term does not grow with the number of rows. The slack term does — every extra row adds another slack to the pile. So the same means less regularisation on a bigger dataset.
Take a dataset of 500 rows and two candidate solutions:
| solution | total slack at | cost at | |
|---|---|---|---|
| smooth | 8 | 60 | |
| tight | 32 | 42 |
The smooth solution wins. Now double the data with more rows from exactly the same distribution. Each solution's total slack doubles, while its weight penalty is unchanged:
- smooth:
- tight:
The tight solution now wins, and nothing about the problem changed except the row count. To recover the earlier answer you must halve the fine: at the smooth solution costs and the tight one , restoring the original ranking. The practical rule is that the useful scales roughly like , so a value tuned on a two-year pilot will be far too aggressive when you refit on twenty years.
is not a measure of accuracy or confidence. It is the exchange rate between margin width and training mistakes, equal to for the ridge penalty you already know. Small = strong shrinkage = smooth boundary; large = weak shrinkage = boundary that chases outliers.
What this means in practice
Tune on a logarithmic grid — something like to in powers of ten — because the interesting range spans orders of magnitude and a linear grid wastes almost all its points. With a curved kernel you must search and the bandwidth jointly (see The RBF Kernel and Bandwidth Selection), since a narrow kernel and a large overfit in the same direction and can partly cancel each other during a one-at-a-time search.
The solution tells you where each point stands, through the dual weights described in The SVM Dual and Support Vectors. Points outside the corridor get and could be deleted without changing anything. Points exactly on the edge get . Points that trespassed are pinned at . Counting how many sit at the cap is a fast diagnostic: if most of your training set is pinned there, is too small and the model has essentially given up.
On financial data the honest default is a small . Signal-to-noise is low, labels are frequently ambiguous near the boundary, and a model that insists on classifying every training day correctly is fitting the noise. Start small, raise it only if validation error genuinely improves.
interacts with feature scaling, so the two must be decided together. The margin is measured in the units of your features; if one column is in basis points and another in raw prices, is dominated by whichever needs the smaller coefficient, and the fine you set no longer means what you thought. Standardise first, then tune — and retune it whenever you change the feature set, the scaler, or the sample length.
Related concepts
Practice in interviews
Further reading
- Cortes & Vapnik, Support-Vector Networks (1995)
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning (Ch. 12)
- Chang & Lin, LIBSVM: A Library for Support Vector Machines