Flag anomalies in a latency series using a rolling baseline of the previous window values (not
including the current one): value i is an anomaly when the baseline is full, its population standard
deviation is > 0, and abs(x - mean) / std > threshold.
Anomalies are not added to the baseline (so one spike doesn't hide the next); the baseline is the
last window non-anomalous values. Return the anomalous indices. Must run in O(n) for large series.
Implement anomalies. Input: values: list[float], window: int, threshold: float. Returns: list[int].
import statistics
def anomalies(values: list[float], window: int, threshold: float) -> list[int]:
out = []
for i in range(window, len(values)):
base = values[i - window:i]
if abs(values[i] - statistics.mean(base)) > threshold * statistics.pstdev(base):
out.append(i)
return out
Run executes the public tests. Submit grades against public and hidden tests.
Flag anomalies in a latency series using a rolling baseline of the previous window values (not
including the current one): value i is an anomaly when the baseline is full, its population standard
deviation is > 0, and abs(x - mean) / std > threshold.
Anomalies are not added to the baseline (so one spike doesn't hide the next); the baseline is the
last window non-anomalous values. Return the anomalous indices. Must run in O(n) for large series.
Implement anomalies. Input: values: list[float], window: int, threshold: float. Returns: list[int].
import statistics
def anomalies(values: list[float], window: int, threshold: float) -> list[int]:
out = []
for i in range(window, len(values)):
base = values[i - window:i]
if abs(values[i] - statistics.mean(base)) > threshold * statistics.pstdev(base):
out.append(i)
return out
Run executes the public tests. Submit grades against public and hidden tests.