v2026.11.2
All Bundles
Bundle

Core machine learning types: the seedable Random generator, Matrix2D linear algebra and activations, matrix reference holders, the NeuralNetwork and CSV-backed MatrixReader. Compile with -lib ml.

EllipticEnvelope

Robust covariance outlier detection: the Minimum Covariance Determinant by FastMCD (Rousseeuw and Van Driessen 1999), following scikit-learn's EllipticEnvelope / MinCovDet.

The MCD estimate is the mean and covariance of the h rows whose covariance has the smallest determinant; h defaults to ceil((n + p + 1) / 2), so up to almost half the rows can be outliers without moving it. FastMCD searches by C-steps from random (p + 1)-subsets, as the paper does: 500 of them for n <= 500, the best 50 run to convergence; above that the nested scheme (at most 5 subsets of 300, pooled on 1500 rows, finished on the full data). scikit-learn starts from 30 random h-subsets instead, which reach the lowest determinant less often. The search is a heuristic either way: on Gaussian data many h-subsets have nearly the same determinant. The raw estimate is then made consistent at the normal model (covariance scaled by median(d^2) / chi2_p median) and reweighted (mean and covariance of the rows with corrected d^2 below the chi2_p 0.975 quantile), as MinCovDet does.

The score is the squared Mahalanobis distance under the reweighted estimate (sklearn's score_samples negated); the threshold is the (1 - contamination) quantile of the training distances. Contamination does not change the estimate.

A feature constant over the training rows is left out of the distance (what sklearn's pseudo-inverse does for it). If the covariance of the support is still singular -- collinear features, or h rows in a lower-dimensional subspace such as many duplicates -- Fit refuses and says so rather than regularising silently.

Implements: OutlierDetector

Example

envelope := EllipticEnvelope->New(0.05, 0.0, 42);
flags := envelope->FitPredict(X);
d2 := envelope->Score(X_test);    # squared Mahalanobis distances

Operations

ChiSquareQuantile # function

The chi-square quantile: the x with P(X <= x) = prob for dof degrees of freedom (scipy.stats.chi2(dof).ppf(prob)). The consistency correction uses the median and the reweighting the 0.975 quantile; a caller can use it to turn a squared Mahalanobis distance into a tail probability cut.

function : ChiSquareQuantile(dof:Int, prob:Float) ~ Float

Parameters

NameTypeDescription
dofIntdegrees of freedom (>= 1)
probFloatprobability in (0, 1)

Return

TypeDescription
Floatthe quantile, or 0.0 for a bad argument

DecisionFunction #

Score minus the threshold; positive means outlier.

method : public : DecisionFunction(X:Float[,]) ~ Float[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Floatvalues, or Nil when unfitted or the input is refused

Fit #

Fits the robust location and covariance.

method : public : Fit(X:Float[,]) ~ Bool

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Booltrue on success; false with GetError on bad input or a singular covariance

FitPredict #

Fits, then labels the training rows.

method : public : FitPredict(X:Float[,]) ~ Bool[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Boollabels (true = outlier), or Nil when the fit failed

GetCovariance #

The robust (reweighted) covariance, features x features; rows and columns of constant features are zero.

method : public : GetCovariance() ~ Float[,]

Return

TypeDescription
Floatcovariance, or Nil when unfitted

GetError #

Why the last call failed.

method : public : GetError() ~ String

Return

TypeDescription
Stringthe reason, or an empty string

GetLocation #

The robust (reweighted) location, one value per feature; a constant feature reports its constant.

method : public : GetLocation() ~ Float[]

Return

TypeDescription
Floatlocation, or Nil when unfitted

GetRawCovariance #

The raw MCD covariance, before the consistency correction and reweighting.

method : public : GetRawCovariance() ~ Float[,]

Return

TypeDescription
Floatcovariance, or Nil when unfitted

GetRawLocation #

The raw MCD location, before reweighting.

method : public : GetRawLocation() ~ Float[]

Return

TypeDescription
Floatlocation, or Nil when unfitted

GetRawSupport #

Which training rows formed the raw MCD support (the h rows).

method : public : GetRawSupport() ~ Bool[]

Return

TypeDescription
Boolmask, or Nil when unfitted

GetSensitivity #

The contamination.

method : public : GetSensitivity() ~ Float

Return

TypeDescription
Floatcontamination

GetSensitivityName #

The setting's name.

method : public : GetSensitivityName() ~ String

Return

TypeDescription
String"contamination"

GetSupport #

Which training rows formed the reweighted support.

method : public : GetSupport() ~ Bool[]

Return

TypeDescription
Boolmask, or Nil when unfitted

GetThreshold #

The distance threshold (sklearn's -offset_).

method : public : GetThreshold() ~ Float

Return

TypeDescription
Floatthreshold

GetTrainingScores #

The training rows' squared Mahalanobis distances.

method : public : GetTrainingScores() ~ Float[]

Return

TypeDescription
Floatdistances, or Nil when unfitted

IsFitted #

Whether the detector is fitted.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue when fitted

New # constructor

Constructor with scikit-learn's defaults: contamination 0.1, support fraction auto, seed 42.

New()

New # constructor

Constructor.

New(contamination:Float, support_fraction:Float, seed:Int)

Parameters

NameTypeDescription
contaminationFloatexpected outlier fraction in (0, 0.5]
support_fractionFloatshare of rows in the MCD support in (0, 1], or 0.0 for ceil((n + p + 1) / 2) rows
seedIntrandom seed

Predict #

Labels rows: true where the squared Mahalanobis distance exceeds the threshold.

method : public : Predict(X:Float[,]) ~ Bool[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Boollabels, or Nil when unfitted or the input is refused

Score #

Squared Mahalanobis distances under the robust estimate (sklearn's mahalanobis, the negation of score_samples).

method : public : Score(X:Float[,]) ~ Float[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Floatdistances, or Nil when unfitted or the input is refused

SetSensitivity #

Sets the contamination; a fitted detector keeps its estimate and re-derives the threshold from the training distances.

method : public : SetSensitivity(value:Float) ~ Bool

Parameters

NameTypeDescription
valueFloatcontamination in (0, 0.5]

Return

TypeDescription
Boolfalse when out of range