forked from pockerman/hidden_markov_modeling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypothesis_testing.py
executable file
·285 lines (213 loc) · 8.7 KB
/
hypothesis_testing.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
Hypothesis testing utilities
"""
from collections import defaultdict
import numpy as np
import scipy.stats as st
from preprocess_utils import compute_statistic
from helpers import WindowState
from helpers import INFO
def zscore_statistic(data, null, **kwargs):
statistic = compute_statistic(data=data,
statistics = null.name)
# compute the variance
var = compute_statistic(data=data, statistics = "var")
score = (statistic - null.value)/(np.sqrt(var))
if "alternative" in kwargs:
direction = kwargs["alternative"].direction
if direction == ">" or direction == ">=":
prob = 1.0 - st.norm.cdf(score)
return prob, statistic
elif direction == "<" or direction == "<=":
prob = st.norm.cdf(score)
return prob, statistic
else:
prob = 2.0*(1.0 - st.norm.cdf(np.abs(score)))
return prob, statistic
else:
# assume two-sided by default
prob = 2.0*(1.0 - st.norm.cdf(np.fabs(score)))
return prob, statistic
class Hypothesis(object):
def __init__(self, name, direction, value):
self._name = name
self._direction = direction
self._value = value
self._description = name + direction +str(value)
self._accepted=False
@property
def value(self):
return self._value
@value.setter
def value(self, val):
self._value = val
@property
def description(self):
return self._description
@property
def direction(self):
return self._direction
@property
def accepted(self):
return self._accepted
@accepted.setter
def accepted(self, val):
self._accepted = val
@property
def name(self):
return self._name
class LessThan(Hypothesis):
def __init__(self, parameter_name, value):
super(LessThan, self).__init__(name=parameter_name,
direction="<",
value=value)
class LessOrEqualThan(Hypothesis):
def __init__(self, parameter_name, value):
super(LessOrEqualThan, self).__init__(name=parameter_name,
direction="<=",
value=value)
class GreaterThan(Hypothesis):
def __init__(self, parameter_name, value):
super(GreaterThan, self).__init__(name=parameter_name,
direction=">",
value=value)
class GreaterOrEqualThan(Hypothesis):
def __init__(self, parameter_name, value):
super(GreaterOrEqualThan, self).__init__(name=parameter_name,
direction=">=",
value=value)
class Equal(Hypothesis):
def __init__(self, parameter_name, value):
super(Equal, self).__init__(name=parameter_name,
direction="=",
value=value)
class NotEqual(Hypothesis):
def __init__(self, parameter_name, value):
super(NotEqual, self).__init__(name=parameter_name,
direction="!=",
value=value)
class HypothesisTest(object):
def __init__(self, null, alternative,
alpha, data, statistic_calculator):
self._alpha = alpha
self._null = null
self._alternative = alternative
self._data = data
self._statistic_calculator = statistic_calculator
self._p_value = None
def test(self):
self._p_value, statistic = \
self._statistic_calculator(data=self._data,
null = self._null,
alternative = self._alternative)
print("{0} Hypothesis test: ".format(INFO))
if self._p_value < self._alpha :
print("\tH0: " + self._null.description +
"vs" + "H1: " + self._alternative.description)
print("\trejected H0 with a="+str(self._alpha) +
" p-value="+str(self._p_value))
print("\tstatistic computed: " +str(statistic))
self._null.accepted=False
self._alternative.accepted=True
else:
print("\tH0: " + self._null.description +
"vs" + "H1: " + self._alternative.description)
print("\tcannot reject H0 with a="+str(self._alpha) +
" p-value="+str(self._p_value))
print("\tstatistic computed: " +str(statistic))
self._null.accepted=True
self._alternative.accepted=False
class SignificanceTestLabeler(object):
def __init__(self, clusters, windows):
self._clusters = clusters
self._windows = windows
def label(self, test_config):
"""
Label the given clusters
"""
print("{0} Labeling clusters...".format(INFO))
cluster_data = defaultdict(list)
for cluster in self._clusters:
print("{0} Testing cluster {1}".format(INFO, cluster.cidx))
print("{0} Testing FULL DELETE".format(INFO))
windows = [window.get_window(wtype="wga_w")
for window in self._windows]
cluster_data = cluster.get_data_from_windows(windows=windows)
# get the cluster statistics
cluster_stats = cluster.get_statistics(windows=self._windows,
statistic="all",
window_type="wga_w")
h0 = \
Equal(parameter_name=test_config["statistic_parameter"],
value=0.0)
ha = \
GreaterThan(parameter_name=test_config["statistic_parameter"],
value=0.0)
test = HypothesisTest(null=h0,
alternative=ha,
alpha=test_config["significance"],
data=cluster_data,
statistic_calculator=zscore_statistic)
test.test()
if h0.accepted:
print("{0} Cluster {1} is labeled as DELETE".format(INFO,cluster.cidx))
cluster.state = WindowState.DELETE
else:
print("{0} Testing ONE COPY DELETE".format(INFO))
h0 = \
Equal(parameter_name=test_config["statistic_parameter"],
value=10.0)
ha = \
NotEqual(parameter_name=test_config["statistic_parameter"],
value=10.0)
test = HypothesisTest(null=h0,
alternative=ha,
alpha=test_config["significance"],
data=cluster_data[cluster.cidx],
statistic_calculator=zscore_statistic)
test.test()
if h0.accepted:
print("{0} Cluster {1} is labeled as ONE COPY DELETE".format(INFO, cluster.cidx))
cluster.state = WindowState.ONE_COPY_DELETE
else:
print("{0} Testing NORMAL".format(INFO))
h0 = \
Equal(parameter_name=test_config["statistic_parameter"],
value=20.0)
ha = \
NotEqual(parameter_name=test_config["statistic_parameter"],
value=20.0)
test = HypothesisTest(null=h0,
alternative=ha,
alpha=test_config["significance"],
data=cluster_data[cluster.cidx],
statistic_calculator=zscore_statistic)
test.test()
if h0.accepted:
print("{0} Cluster {1} is labeled as NORMAL".format(INFO, cluster.cidx))
cluster.state = WindowState.NORMAL
else:
print("{0} Testing for DUPLICATION".format(INFO))
h0 = \
GreaterThan(parameter_name=test_config["statistic_parameter"],
value=20.0)
ha = \
LessThan(parameter_name=test_config["statistic_parameter"],
value=20.0)
test = HypothesisTest(null=h0,
alternative=ha,
alpha=test_config["significance"],
data=cluster_data[cluster.cidx],
statistic_calculator=zscore_statistic)
test.test()
if h0.accepted:
print("{0} Cluster {1} is labeled as DUPLICATION".format(INFO, cluster.cidx))
cluster.state = WindowState.INSERT
else:
# if we reach here then this means that statistically
# mu is not 10 is not 20 is not 0 and it is
# less than 20 we classify this cluster as TUF
print("{0} Cluster {1} is labeled as TUF".format(INFO, cluster.cidx))
cluster.state = WindowState.TUF
print("{0} Done...".format(INFO))
return self._clusters