def test_base(): num_categ = 8 clf = LatestWindowAnomalyDetector(alpha=0.00001) history = dummy_data(num_days=10, num_categories=num_categ, min_val=100, max_val=1000) new_day = dummy_data(num_days=1, num_categories=num_categ, min_val=100, max_val=1000) clf.fit(history) prediction = clf.predict(new_day) assert len(prediction) == num_categ for x in prediction.values: assert x in [-1, 0, 1] num_new_days = 30 many_days = dummy_data(num_days=num_new_days, num_categories=num_categ, min_val=100, max_val=1000) predictions = clf.predict(many_days) assert len(predictions) == num_categ * num_new_days for x in predictions.values: assert x in [-1, 0, 1]
def test_partial_fit(): num_categ = 8 clf = LastNWindowsAnomalyDetector( n_windows=4, weights=uniform_weighter(), alpha=0.001, ) history = dummy_data(num_days=10, num_categories=num_categ, min_val=1000, max_val=1200) recent_history = dummy_data(num_days=6, num_categories=num_categ, min_val=1000, max_val=1200) new_day = dummy_data(num_days=1, num_categories=num_categ, min_val=1000, max_val=1200) clf.fit(history) clf.partial_fit(recent_history) prediction = clf.predict(new_day) assert len(prediction) == num_categ for x in prediction.values: assert x in [-1, 0, 1]
def test_exp_comp_weighter(): num_categ = 8 n_windows = 4 clf = LastNWindowsAnomalyDetector( n_windows=n_windows, weights=exp_comp_weighter(n=n_windows, concave_factor=1), alpha=0.001, ) history = dummy_data(num_days=10, num_categories=num_categ, min_val=1000, max_val=1200) new_day = dummy_data(num_days=1, num_categories=num_categ, min_val=1000, max_val=1200) clf.fit(history) prediction = clf.predict(new_day) assert len(prediction) == num_categ for x in prediction.values: assert x in [-1, 0, 1] clf = LastNWindowsAnomalyDetector( n_windows=n_windows, weights=exp_comp_weighter(n=n_windows, concave_factor=3), alpha=0.001, ) clf.fit(history) prediction = clf.predict(new_day) assert len(prediction) == num_categ for x in prediction.values: assert x in [-1, 0, 1] clf = LastNWindowsAnomalyDetector( n_windows=n_windows, weights=exp_comp_weighter(n=n_windows, concave_factor=30), alpha=0.001, ) clf.fit(history) prediction = clf.predict(new_day) assert len(prediction) == num_categ for x in prediction.values: assert x in [-1, 0, 1] clf = LastNWindowsAnomalyDetector( n_windows=n_windows, weights=exp_comp_weighter(n=n_windows, concave_factor=None), alpha=0.001, ) clf.fit(history) prediction = clf.predict(new_day) assert len(prediction) == num_categ for x in prediction.values: assert x in [-1, 0, 1]
def test_non_def_ddof(): num_categ = 8 clf = LatestWindowAnomalyDetector(p_threshold=0.00001, power=-2, ddof=4) history = dummy_data( num_days=10, num_categories=num_categ, min_val=100, max_val=1000) new_day = dummy_data( num_days=1, num_categories=num_categ, min_val=100, max_val=1000) clf.fit(history) prediction = clf.predict(new_day) assert len(prediction) == num_categ for x in prediction.values: assert x in [-1, 0, 1]
def test_diff_categ(): num_categ_1 = 8 num_categ_2 = 7 clf = LatestWindowAnomalyDetector(p_threshold=0.00001) history = dummy_data( num_days=10, num_categories=num_categ_1, min_val=100, max_val=1000) new_day = dummy_data( num_days=1, num_categories=num_categ_2, min_val=100, max_val=1000) clf.fit(history) prediction = clf.predict(new_day) assert len(prediction) == max(num_categ_1, num_categ_2) for x in prediction.values: assert x in [-1, 0, 1]
def test_partial_fit(): num_categ = 8 clf = LatestWindowAnomalyDetector(p_threshold=0.00001) history = dummy_data( num_days=10, num_categories=num_categ, min_val=100, max_val=1000) recent_history = dummy_data( num_days=6, num_categories=num_categ, min_val=100, max_val=1000) new_day = dummy_data( num_days=1, num_categories=num_categ, min_val=100, max_val=1000) clf.fit(history) clf.partial_fit(recent_history) prediction = clf.predict(new_day) assert len(prediction) == num_categ for x in prediction.values: assert x in [-1, 0, 1]
def test_errors(): # bad p thresholds with pytest.raises(ValueError): LastNWindowsAnomalyDetector( n_windows=4, weights=uniform_weighter(), alpha=2, ) # bad p thresholds with pytest.raises(ValueError): LastNWindowsAnomalyDetector( n_windows=4, weights=uniform_weighter(), alpha=-1, ) clf = LastNWindowsAnomalyDetector( n_windows=4, weights=uniform_weighter(), alpha=0.001, ) new_day = dummy_data(num_days=1, num_categories=8, min_val=1000, max_val=1200) with pytest.raises(NotFittedError): clf.predict(new_day)
def test_weights_function(): num_categ = 8 clf = LastNWindowsAnomalyDetector( n_windows=6, weights=_weight_by_inverse_days_delta, alpha=0.001, ) history = dummy_data(num_days=10, num_categories=num_categ, min_val=1000, max_val=1200) new_day = dummy_data(num_days=1, num_categories=num_categ, min_val=1000, max_val=1200) clf.fit(history) prediction = clf.predict(new_day) assert len(prediction) == num_categ for x in prediction.values: assert x in [-1, 0, 1]
def test_errors(): # bad p thresholds with pytest.raises(ValueError): LatestWindowAnomalyDetector(p_threshold=2) # bad p thresholds with pytest.raises(ValueError): LatestWindowAnomalyDetector(p_threshold=-1) clf = LatestWindowAnomalyDetector(p_threshold=0.00001) new_day = dummy_data( num_days=1, num_categories=8, min_val=100, max_val=1000) with pytest.raises(NotFittedError): clf.predict(new_day)
def test_diff_categ(): num_categ_1 = 8 num_categ_2 = 7 clf = LastNWindowsAnomalyDetector( n_windows=4, weights=uniform_weighter(), alpha=0.001, ) history = dummy_data(num_days=10, num_categories=num_categ_1, min_val=1000, max_val=1200) new_day = dummy_data(num_days=1, num_categories=num_categ_2, min_val=1000, max_val=1200) clf.fit(history) prediction = clf.predict(new_day) assert len(prediction) == max(num_categ_1, num_categ_2) for x in prediction.values: assert x in [-1, 0, 1]