From cb040b397b525588a93a0b231316807737df7be2 Mon Sep 17 00:00:00 2001 From: Alexandr Velikiy <39257464+VargBurz@users.noreply.github.com> Date: Tue, 30 Apr 2019 17:15:41 +0300 Subject: [PATCH] Exponential smoothing for anomaly detector #610 (#611) --- analytics/analytics/utils/common.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/analytics/analytics/utils/common.py b/analytics/analytics/utils/common.py index b69802c..76482e0 100644 --- a/analytics/analytics/utils/common.py +++ b/analytics/analytics/utils/common.py @@ -6,7 +6,7 @@ from scipy.signal import argrelextrema from scipy.stats import gaussian_kde from scipy.stats.stats import pearsonr import math -from typing import Union, List, Generator, Tuple +from typing import Optional, Union, List, Generator, Tuple import utils import logging from itertools import islice @@ -16,15 +16,23 @@ SHIFT_FACTOR = 0.05 CONFIDENCE_FACTOR = 0.5 SMOOTHING_FACTOR = 5 -def exponential_smoothing(series, alpha): - result = [series[0]] + +def exponential_smoothing(series: pd.Series, alpha: float, last_smoothed_value: Optional[float] = None) -> pd.Series: + if alpha < 0 or alpha > 1: + raise ValueError('Alpha must be within the boundaries: 0 <= alpha <= 1') + if len(series) < 2: + return series + if last_smoothed_value is None: + result = [series.values[0]] + else: + result = [float(last_smoothed_value)] if np.isnan(result): result = [0] for n in range(1, len(series)): if np.isnan(series[n]): series[n] = 0 result.append(alpha * series[n] + (1 - alpha) * result[n - 1]) - return result + return pd.Series(result, index = series.index) def segments_box(segments): max_time = 0