-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathstdev_test.py
More file actions
157 lines (127 loc) · 4.5 KB
/
Copy pathstdev_test.py
File metadata and controls
157 lines (127 loc) · 4.5 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging
import math
import random
import time
import unittest
from parameterized import parameterized
from apache_beam.ml.anomaly.univariate.stdev import IncLandmarkStdevTracker
from apache_beam.ml.anomaly.univariate.stdev import IncSlidingStdevTracker
from apache_beam.ml.anomaly.univariate.stdev import SimpleSlidingStdevTracker
class LandmarkStdevTest(unittest.TestCase):
def test_without_nan(self):
t = IncLandmarkStdevTracker()
self.assertTrue(math.isnan(t.get()))
t.push(1)
self.assertTrue(math.isnan(t.get()))
t.push(2)
self.assertEqual(t.get(), 0.7071067811865476)
t.push(3)
self.assertEqual(t.get(), 1.0)
t.push(10)
self.assertEqual(t.get(), 4.08248290463863)
def test_with_nan(self):
t = IncLandmarkStdevTracker()
t.push(float('nan'))
self.assertTrue(math.isnan(t.get())) # NaN is ignored
t.push(float('nan'))
self.assertTrue(math.isnan(t.get())) # NaN is ignored
t.push(2)
self.assertTrue(math.isnan(t.get()))
t.push(1)
self.assertEqual(t.get(), 0.7071067811865476)
t.push(3)
self.assertEqual(t.get(), 1.0)
# flush the only number out
t.push(float('nan'))
self.assertEqual(t.get(), 1.0)
t.push(10)
self.assertEqual(t.get(), 4.08248290463863)
def test_accuracy_fuzz(self):
seed = int(time.time())
rng = random.Random(seed)
print("Random seed: %d" % seed)
for _ in range(10):
numbers = []
for _ in range(5000):
numbers.append(rng.randint(0, 1000))
t1 = IncLandmarkStdevTracker()
t2 = SimpleSlidingStdevTracker(len(numbers))
for v in numbers:
t1.push(v)
t2.push(v)
self.assertTrue((math.isnan(t1.get()) and math.isnan(t2.get())) or
abs(t1.get() - t2.get()) < 1e-9)
class SlidingStdevTest(unittest.TestCase):
@parameterized.expand([SimpleSlidingStdevTracker, IncSlidingStdevTracker])
def test_without_nan(self, tracker):
t = tracker(3)
self.assertTrue(math.isnan(t.get()))
t.push(2)
self.assertTrue(math.isnan(t.get()))
t.push(1)
self.assertEqual(t.get(), 0.7071067811865476)
t.push(3)
self.assertEqual(t.get(), 1.0)
t.push(10)
self.assertEqual(t.get(), 4.725815626252609)
@parameterized.expand([SimpleSlidingStdevTracker, IncSlidingStdevTracker])
def test_stdev_with_nan(self, tracker):
t = tracker(3)
t.push(float('nan'))
self.assertTrue(math.isnan(t.get())) # NaN is ignored
t.push(float('nan'))
self.assertTrue(math.isnan(t.get())) # NaN is ignored
t.push(1)
self.assertTrue(math.isnan(t.get()))
t.push(2)
self.assertEqual(t.get(), 0.7071067811865476)
t.push(3)
self.assertEqual(t.get(), 1.0)
# flush the only number out
t.push(float('nan'))
self.assertEqual(t.get(), 0.7071067811865476)
t.push(float('nan'))
self.assertTrue(math.isnan(t.get()))
t.push(float('nan'))
self.assertTrue(math.isnan(t.get()))
if tracker is IncSlidingStdevTracker:
self.assertEqual(t._m2, 0)
self.assertEqual(t._mean, 0)
t.push(4)
self.assertTrue(math.isnan(t.get()))
t.push(5)
self.assertEqual(t.get(), 0.7071067811865476)
def test_accuracy_fuzz(self):
seed = int(time.time())
rng = random.Random(seed)
print("Random seed: %d" % seed)
for _ in range(10):
numbers = []
for _ in range(5000):
numbers.append(rng.randint(0, 1000))
t1 = IncSlidingStdevTracker(100)
t2 = SimpleSlidingStdevTracker(100)
for v in numbers:
t1.push(v)
t2.push(v)
self.assertTrue((math.isnan(t1.get()) and math.isnan(t2.get())) or
abs(t1.get() - t2.get()) < 1e-9)
if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
unittest.main()