Skip to content

Instantly share code, notes, and snippets.

@dceoy
Last active October 12, 2018 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dceoy/e0b527ef4b7c8e51f32c670afaef527b to your computer and use it in GitHub Desktop.
Save dceoy/e0b527ef4b7c8e51f32c670afaef527b to your computer and use it in GitHub Desktop.
[Python] Hotelling's T-squared anomaly detection
#!/usr/bin/env python
import numpy as np
from scipy import stats
class HotellingsT2(object):
def __init__(self, alpha):
self.anomaly_score_threshold = stats.chi2.ppf(q=(1 - alpha), df=1)
def is_anomaly_1dim(self, new_x, normal_x):
anomaly_score = (new_x - np.mean(normal_x)) ** 2 / np.var(normal_x)
return anomaly_score > self.anomaly_score_threshold
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment