Detect#

milia.anomaly flags the days or windows that are not normal, at a false-alarm rate that you set.

Point, contextual and collective anomalies.

Three kinds of anomaly: one reading that departs from its day, an ordinary value at an unusual time, and ordinary members whose sum is unusual.#

from milia import available
from milia.data import profiles, synthetic
from milia.features.window import vector

population = synthetic.generate(meters=30, days=90, steps=48, rate=0.06)
days = profiles.tensor(population.pivot)[0]  # one meter: days by steps
planted = population.anomalies[0].astype(bool)
features = [vector(day) for day in days]
available("detector")
['controllimit',
 'copod',
 'ecod',
 'hbos',
 'iforest',
 'kde',
 'knn',
 'lof',
 'mahalanobis',
 'ocsvm',
 'reconstruction']

Fit on normal days, calibrate on held-out normal days#

An ensemble rescales the scores of several detectors to a common range and combines them. The threshold is the split-conformal quantile of the scores of held-out normal days, so that, for exchangeable data, the expected share of normal days that are flagged is at most alpha.

A level set of the density of normal days.

Detection as a level set: the threshold encloses the region that holds 90% of normal days, and a normal day outside it is a false alarm.#

Five rules for the threshold, each at a target false-alarm rate of 10%.

Five rules for the threshold at a target rate of 10%. Only the conformal threshold reaches it, because it is read from held-out normal scores.#

import numpy as np

from milia.anomaly.ensemble import Ensemble

normal = np.flatnonzero(~planted)
detector = Ensemble().fit([features[i] for i in normal[:50]])
detector.calibrate([features[i] for i in normal[50:]], alpha=0.1)
flags = np.asarray(detector.flag(features))
np.flatnonzero(flags), np.flatnonzero(planted)
(array([ 9, 12, 26, 43, 56, 64, 81]), array([ 9, 12, 26, 43, 64, 84]))

Scores, p-values and a threshold per group#

The score of each day is available through score_samples. conformal.pvalues turns scores into conformal p-values, and conformal.mondrian gives each group its own threshold, so that a group with a different normal range is not judged by the range of another.

One threshold against a threshold for each group.

One threshold does not hold the false-alarm rate in each group, because the groups have different normal ranges. A threshold for each group does.#

from milia.anomaly import conformal

scores = detector.score_samples(features)
held = detector.score_samples([features[i] for i in normal[50:]])
pvalues = conformal.pvalues(held, scores)
selected = conformal.select(pvalues, alpha=0.1)
int(selected.sum())
50

Change points#

When the question is when did behaviour change, changepoint.cusum finds the points where a series drifts away from its baseline, and pelt and binseg segment it (they need the changepoint extra).

The difference between an anomaly and a change of normal behaviour.

An anomaly is a departure from normal, and a change is a new normal. CUSUM accumulates small departures, so it finds a change that no single day reveals.#

from milia.anomaly import changepoint

level = days.mean(axis=1)
level[60:] += 0.3 * level.std()
changepoint.cusum(level, baseline=40)[:5]
array([], dtype=int64)

Limits#

The guarantee is about normal days that look like the calibration days. A threshold calibrated in summer is not valid in winter, and a planted anomaly in a test is easier than a real one. Calibrate on recent, representative normal data.