-
Notifications
You must be signed in to change notification settings - Fork 141
/
test_few_shot_predict.py
109 lines (87 loc) · 2.7 KB
/
test_few_shot_predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""Check model predicting with short series
"""
import numpy as np
import pandas as pd
from adtk.detector import (
AutoregressionAD,
LevelShiftAD,
PersistAD,
VolatilityShiftAD,
)
s = pd.Series(
np.sin(np.arange(100)),
index=pd.date_range(start="2017-1-1", periods=100, freq="D"),
)
def test_persist_ad():
model = PersistAD(window=1)
s_train = s.copy().iloc[:-10]
model.fit(s_train)
s_test = s.copy().iloc[-2:]
s_test.iloc[-1] = 10
pd.testing.assert_series_equal(
model.predict(s_test), pd.Series([np.nan, 1.0], index=s_test.index)
)
s_test = s.copy().iloc[-1:]
s_test.iloc[-1] = 10
pd.testing.assert_series_equal(
model.predict(s_test), pd.Series([np.nan], index=s_test.index)
)
model = PersistAD(window=5)
s_train = s.copy().iloc[:-10]
model.fit(s_train)
s_test = s.copy().iloc[-5:]
s_test.iloc[-1] = 10
pd.testing.assert_series_equal(
model.predict(s_test), pd.Series([np.nan] * 5, index=s_test.index)
)
s_test = s.copy().iloc[-6:]
s_test.iloc[-1] = 10
pd.testing.assert_series_equal(
model.predict(s_test),
pd.Series([np.nan] * 5 + [1.0], index=s_test.index),
)
def test_level_shift_ad():
model = LevelShiftAD(window=3)
s_train = s.copy().iloc[:-10]
model.fit(s_train)
s_test = s.copy().iloc[-5:]
s_test.iloc[-3:] = 10
pd.testing.assert_series_equal(
model.predict(s_test), pd.Series([np.nan] * 5, index=s_test.index)
)
s_test = s.copy().iloc[-6:]
s_test.iloc[-3:] = 10
pd.testing.assert_series_equal(
model.predict(s_test),
pd.Series([np.nan] * 3 + [1.0] + [np.nan] * 2, index=s_test.index),
)
def test_volatility_shift_ad():
model = VolatilityShiftAD(window=3)
s_train = s.copy().iloc[:-10]
model.fit(s_train)
s_test = s.copy().iloc[-5:]
s_test.iloc[-3:] *= 10
pd.testing.assert_series_equal(
model.predict(s_test), pd.Series([np.nan] * 5, index=s_test.index)
)
s_test = s.copy().iloc[-6:]
s_test.iloc[-3:] *= 10
pd.testing.assert_series_equal(
model.predict(s_test),
pd.Series([np.nan] * 3 + [1.0] + [np.nan] * 2, index=s_test.index),
)
def test_autoregression_ad():
model = AutoregressionAD(n_steps=3, step_size=7)
s_train = s.copy().iloc[:-10]
model.fit(s_train)
s_test = s.copy().iloc[-21:]
s_test.iloc[-1:] = 10
pd.testing.assert_series_equal(
model.predict(s_test), pd.Series([np.nan] * 21, index=s_test.index)
)
s_test = s.copy().iloc[-22:]
s_test.iloc[-1:] = 10
pd.testing.assert_series_equal(
model.predict(s_test),
pd.Series([np.nan] * 21 + [1.0], index=s_test.index),
)