Skip to content

Clustering, density estimation, and anomaly detection · Anomaly detection · UCL056 · Algorithm

One-class support vector machine

Learn a boundary around a reference distribution in kernel feature space, often from predominantly normal training examples, then flag observations falling outside that boundary.

Runs in CodeSplash Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.ocsvm. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Learn a boundary around a reference distribution in kernel feature space, often from predominantly normal training examples, then flag observations falling outside that boundary.

Question / hypotheses

Which observations deviate from the chosen reference notion of normality, and at what operational alert rate? This entry defines an estimation or optimization objective, not an intrinsic null/alternative hypothesis test.

Architecture / computational structure

Kernel feature mapping and a regularized separating hyperplane in that space.

Assumptions

Under the optimization assumptions, ν\nu bounds training-error and support-vector fractions in specified directions; it is not an exact future anomaly rate. RBF bandwidth and ν\nu interact strongly. Training contamination can distort the accepted region, and kernel methods can be costly at large sample sizes.

Formula / computational definition

Minimize 12∥w∥2+(νn)−1∑iξi−ρ\tfrac12\|w\|^2+(\nu n)^{-1}\sum_i\xi_i-\rho subject to wTϕ(xi)≥ρ−ξiw^T\phi(x_i)\ge\rho-\xi_i and ξi≥0\xi_i\ge0. The signed decision is f(x)=wTϕ(x)−ρf(x)=w^T\phi(x)-\rho. Kernel evaluations replace explicit feature vectors.

Parameters and notation

Learned or fitted quantities: Support coefficients, offset, and resulting boundary. User-set hyperparameters/design choices: Choose kernel, kernel bandwidth, ν\nu, scaling, and solver tolerance. Tune these against the stated objective and stability/validation criteria; do not select settings on the final evaluation set.

Fitting, fine-tuning, and adaptation

Neural fine-tuning is not applicable to this classical detector. Refit the reference model when its population changes, then recalibrate an alert threshold on a separate normal holdout. Tune sensitivity without using final-test anomaly labels repeatedly.

How to use and calibrate

Calibrate the operating threshold on separate normal reference data and evaluate unseen anomalies if available. Plot acceptance regions in simple cases and test sensitivity to scaling and bandwidth.

Example application

Learn a nonlinear normality boundary from a reference cohort. Define the reference population, measurement units, and success criterion before fitting; investigate whether discovered groups or alerts reflect the intended phenomenon.

Interpretation / pitfalls

Watch out

The reported result is conditional on the representation and objective. An alert is a screening result, not a diagnosis or a calibrated posterior probability. Evaluate both ranking and the decision threshold.

Python and R code examples

Use sklearn.svm.OneClassSVM; R e1071::svm(type='one-classification') provides a native route. Score signs and default scaling differ, so make preprocessing and score orientation explicit before comparing results.

Component: Kernel one-class decision function for supplied support weights. Python: executed successfully; R: not executed (Rscript unavailable). These small fixtures establish the arithmetic operation, not full-method accuracy or reproduction of paper results.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Kernel one-class decision function for supplied support weights. · needs numpy

import numpy as np
x=np.array([-2.,0.,2.]); support=np.array([-1.,1.]); alpha=np.array([.5,.5]); gamma=.5; rho=.4
K=np.exp(-gamma*(x[:,None]-support)**2); result=K@alpha-rho
print(result)
R

Component demo, not a trained model — Kernel one-class decision function for supplied support weights.

x=c(-2,0,2); support=c(-1,1); alpha=c(.5,.5); gamma=.5; rho=.4
K=exp(-gamma*outer(x,support,'-')^2); result=as.vector(K%*%alpha)-rho
print(result)

Sources

From the CodeSplash reference, entry UCL056.