Core Models API¶
NullModel ¶
Bases: BaseModel
Null model that serves as a baseline for anomaly detection.
This model never learns from data and always returns a score of 0.0, indicating no anomalies are detected. It's useful as a baseline for comparing other anomaly detection models.
Examples:
from aberrant.model import NullModel
model = NullModel()
score = model.score_one({"feature1": 1.0, "feature2": 2.0})
model.learn_one({"feature1": 1.0, "feature2": 2.0})
assert score == 0.0
Initialize the null model.
learn_one ¶
Update the model with a single data point.
This method does nothing as the null model never learns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
Feature dictionary with string keys and float values. |
required |
score_one ¶
Compute anomaly score for a single data point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
Feature dictionary with string keys and float values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Always returns 0.0 (no anomaly detected). |
RandomModel ¶
Bases: BaseModel
Random model that generates random anomaly scores.
This model never learns from data and returns uniformly distributed random scores between 0 and 1. It serves as a random baseline for comparing other anomaly detection models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
Random seed for reproducibility. |
1
|
Examples:
from aberrant.model import RandomModel
model = RandomModel(seed=42)
score = model.score_one({"feature1": 1.0, "feature2": 2.0})
model.learn_one({"feature1": 1.0, "feature2": 2.0})
assert 0.0 <= score < 1.0
Initialize the random model with a specified seed.
learn_one ¶
Update the model with a single data point.
This method does nothing as the random model never learns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
Feature dictionary with string keys and float values. |
required |
score_one ¶
Compute a random anomaly score for a single data point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
Feature dictionary with string keys and float values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Random float between 0.0 and 1.0. |
ThresholdModel ¶
ThresholdModel(ceiling: float | dict[str, float] | None = None, floor: float | dict[str, float] | None = None)
Bases: BaseModel
Threshold model that detects anomalies based on boundary violations.
This model never learns from data and returns a binary anomaly score based on whether feature values exceed specified thresholds. It supports both one-sided (ceiling or floor only) and two-sided (corridor) detection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ceiling
|
float | dict[str, float] | None
|
Upper threshold(s). Can be a scalar (applies to all features) or a dict mapping feature names to their upper thresholds. |
None
|
floor
|
float | dict[str, float] | None
|
Lower threshold(s). Can be a scalar (applies to all features) or a dict mapping feature names to their lower thresholds. |
None
|
Note
At least one of ceiling or floor must be provided.
Examples:
from aberrant.model import ThresholdModel
ceiling_only = ThresholdModel(ceiling=10.0)
assert ceiling_only.score_one({"temp": 15.0}) == 1.0
assert ceiling_only.score_one({"temp": 5.0}) == 0.0
corridor = ThresholdModel(ceiling=100.0, floor=0.0)
assert corridor.score_one({"temp": 50.0}) == 0.0
assert corridor.score_one({"temp": -5.0}) == 1.0
per_feature = ThresholdModel(
ceiling={"temp": 100.0, "pressure": 50.0},
floor={"temp": 0.0, "pressure": 10.0},
)
assert per_feature.score_one({"temp": 50.0, "pressure": 30.0}) == 0.0
assert per_feature.score_one({"temp": 150.0, "pressure": 30.0}) == 1.0
Initialize the threshold model.
learn_one ¶
Update the model with a single data point.
This method does nothing as the threshold model never learns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
Feature dictionary with string keys and float values. |
required |
score_one ¶
Compute anomaly score for a single data point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
Feature dictionary with string keys and float values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
1.0 if any feature violates its threshold(s), 0.0 otherwise. |
QuantileThreshold ¶
Bases: BaseModel
Adaptive threshold model based on score distribution quantiles.
This model maintains a sliding window of anomaly scores and computes an adaptive threshold based on a specified quantile. Points scoring at or above the threshold are classified as anomalies.
Unlike static thresholds, QuantileThreshold adapts to the actual distribution of scores observed during streaming.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quantile
|
float
|
Quantile level for the threshold. Values closer to 1.0 result in higher thresholds (fewer detections). Default is 0.95. |
0.95
|
window_size
|
int
|
Number of scores to keep for quantile computation. Default is 1000. |
1000
|
score_key
|
str
|
Name of the score feature in the input dictionary. Default is "score". |
'score'
|
Examples:
from aberrant.model import QuantileThreshold
threshold = QuantileThreshold(quantile=0.8, window_size=10)
for score in range(10):
threshold.learn_one({"score": float(score)})
assert threshold.score_one({"score": 10.0}) == 1.0
Note
The model expects input dictionaries with a score key (default "score").
score_one returns:
- 1.0 if the score is greater than or equal to the threshold (anomaly)
- max(score/threshold, 0.0) if below a positive threshold (in [0, 1))
- 0.0 if below a zero or negative threshold
- 0.0 during warmup (insufficient data for threshold)
threshold
property
¶
Current adaptive threshold.
Returns None if insufficient data has been collected.
learn_one ¶
Update the threshold estimate with a new score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
Dictionary containing the score. Must have the score_key. |
required |
score_one ¶
Evaluate a score against the adaptive threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
Dictionary containing the score. Must have the score_key. |
required |
Returns:
| Type | Description |
|---|---|
float
|
|
float
|
|
float
|
|
float
|
|