Calibrator#

Calibrator can help you to normalize the score into a range of 0 to 1, where 1 represents the anomalies.

All calibrators can work in two ways:

  1. Global way (with parameter is_global = True): It can “remember” the distribution of all history scores with little memory usage.

  2. Rolling way (with parameter is_global = Flase): It only keeps the score within a rolling window, and uses them as references.

ZScore Calibrator#

It reports the scores that beyond k times (sigma) of standard deviation as anomalies.

from streamad.util import StreamGenerator, UnivariateDS, plot
from streamad.model import KNNDetector
from streamad.process import ZScoreCalibrator


ds = UnivariateDS()
stream = StreamGenerator(ds.data)
detector=KNNDetector()
calibrator = ZScoreCalibrator(sigma=3, extreme_sigma=5)

scores = []

for x in stream.iter_item():
    score = detector.fit_score(x)
    normalized_score = calibrator.normalize(score)
    scores.append(normalized_score)

data, label, date, features = ds.data, ds.label, ds.date, ds.features
plot(data=data,scores=scores,date=date,features=features,label=label)

TDigest Calibrator#

It reports the scores that beyond percentile_up and below percentile_down as anomalies.

from streamad.util import StreamGenerator, UnivariateDS, plot
from streamad.model import KNNDetector
from streamad.process import TDigestCalibrator


ds = UnivariateDS()
stream = StreamGenerator(ds.data)
detector = KNNDetector()
calibrator = TDigestCalibrator(percentile_up=99, percentile_down=0)

scores = []

for x in stream.iter_item():
    score = detector.fit_score(x)
    normalized_score = calibrator.normalize(score)
    scores.append(normalized_score)

data, label, date, features = ds.data, ds.label, ds.date, ds.features
plot(data=data,scores=scores,date=date,features=features,label=label)