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.
Example
envelope := EllipticEnvelope->New(0.05, 0.0, 42);
flags := envelope->FitPredict(X);
d2 := envelope->Score(X_test); # squared Mahalanobis distancesOperations
- New
- ChiSquareQuantile
- DecisionFunction
- Fit
- FitPredict
- GetCovariance
- GetError
- GetLocation
- GetRawCovariance
- GetRawLocation
- GetRawSupport
- GetSensitivity
- GetSensitivityName
- GetSupport
- GetThreshold
- GetTrainingScores
- IsFitted
- Predict
- Score
- SetSensitivity
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) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| dof | Int | degrees of freedom (>= 1) |
| prob | Float | probability in (0, 1) |
Return
| Type | Description |
|---|---|
| Float | the quantile, or 0.0 for a bad argument |
DecisionFunction #
Score minus the threshold; positive means outlier.
method : public : DecisionFunction(X:Float[,]) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Float | values, or Nil when unfitted or the input is refused |
Fit #
Fits the robust location and covariance.
method : public : Fit(X:Float[,]) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | true 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
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | labels (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
| Type | Description |
|---|---|
| Float | covariance, or Nil when unfitted |
GetError #
Why the last call failed.
method : public : GetError() ~ StringReturn
| Type | Description |
|---|---|
| String | the 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
| Type | Description |
|---|---|
| Float | location, or Nil when unfitted |
GetRawCovariance #
The raw MCD covariance, before the consistency correction and reweighting.
method : public : GetRawCovariance() ~ Float[,]Return
| Type | Description |
|---|---|
| Float | covariance, or Nil when unfitted |
GetRawLocation #
The raw MCD location, before reweighting.
method : public : GetRawLocation() ~ Float[]Return
| Type | Description |
|---|---|
| Float | location, or Nil when unfitted |
GetRawSupport #
Which training rows formed the raw MCD support (the h rows).
method : public : GetRawSupport() ~ Bool[]Return
| Type | Description |
|---|---|
| Bool | mask, or Nil when unfitted |
GetSensitivity #
The contamination.
method : public : GetSensitivity() ~ FloatReturn
| Type | Description |
|---|---|
| Float | contamination |
GetSensitivityName #
The setting's name.
method : public : GetSensitivityName() ~ StringReturn
| Type | Description |
|---|---|
| String | "contamination" |
GetSupport #
Which training rows formed the reweighted support.
method : public : GetSupport() ~ Bool[]Return
| Type | Description |
|---|---|
| Bool | mask, or Nil when unfitted |
GetThreshold #
The distance threshold (sklearn's -offset_).
method : public : GetThreshold() ~ FloatReturn
| Type | Description |
|---|---|
| Float | threshold |
GetTrainingScores #
The training rows' squared Mahalanobis distances.
method : public : GetTrainingScores() ~ Float[]Return
| Type | Description |
|---|---|
| Float | distances, or Nil when unfitted |
IsFitted #
Whether the detector is fitted.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true 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
| Name | Type | Description |
|---|---|---|
| contamination | Float | expected outlier fraction in (0, 0.5] |
| support_fraction | Float | share of rows in the MCD support in (0, 1], or 0.0 for ceil((n + p + 1) / 2) rows |
| seed | Int | random seed |
Predict #
Labels rows: true where the squared Mahalanobis distance exceeds the threshold.
method : public : Predict(X:Float[,]) ~ Bool[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | labels, or Nil when unfitted or the input is refused |