-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHypTesting.py
105 lines (80 loc) · 3.75 KB
/
HypTesting.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
'''
Created on Nov 23, 2016
@author: zahran
'''
from MyEnums import *
import sys
class HypTesting:
def __init__(self, alpha, testsetCountAdjust, testsetCount):
self.sigLevel = alpha
self.testsetCountAdjust = testsetCountAdjust
self.testsetCount = testsetCount
self.type = None
def adjustSigLevel(self):
pass
def classify(self, keySortedPvalues, pValues):
pass
def classifyOne(self, rank, keySortedPvalues, pValues):
pass
class Bonferroni(HypTesting):
def __init__(self, alpha, testsetCountAdjust, testsetCount):
HypTesting.__init__(self, alpha, testsetCountAdjust, testsetCount)
self.adjustedSigLevel = None
self.type = HYP.BONFERRONI
def adjustSigLevel(self, actionsCount):
self.adjustedSigLevel = float(self.sigLevel) / float(actionsCount)
if(self.testsetCountAdjust):
self.adjustedSigLevel = self.adjustedSigLevel / float(self.testsetCount)
def classify(self, keySortedPvalues, pValues):
if(self.adjustedSigLevel == None):
self.adjustSigLevel(len(keySortedPvalues))
outlierVector = [DECISION.UNDECIDED]*len(keySortedPvalues)
for i in range(len(keySortedPvalues)):
if(pValues[keySortedPvalues[i]] <= self.adjustedSigLevel):
outlierVector[keySortedPvalues[i]] = DECISION.OUTLIER# rejecting H0 (i.e rejecting that the action is normal ==> outlier)
else:
outlierVector[keySortedPvalues[i]] = DECISION.NORMAL
return outlierVector
def classifyOne(self, rank, keySortedPvalues, pValues):
adjustedSigLevel = float(self.sigLevel) / float(len(pValues))
if(pValues[keySortedPvalues[rank]] <= adjustedSigLevel):
return DECISION.OUTLIER# rejecting H0 (i.e rejecting that the action is normal ==> outlier)
else:
return DECISION.NORMAL
class Holms(HypTesting):
def __init__(self, alpha, testsetCountAdjust, testsetCount):
HypTesting.__init__(self, alpha, testsetCountAdjust, testsetCount)
self.adjustedSigLevel = None
self.type = HYP.HOLMS
self.k = -1 #used when testsetCountAdjust = true
def adjustSigLevel(self, actionsCount, currentAction):
if(self.testsetCountAdjust == True):
self.adjustedSigLevel = float(self.sigLevel)/float(((actionsCount*self.testsetCount)-currentAction))
else:
self.adjustedSigLevel = float(self.sigLevel)/float((actionsCount-currentAction))
def classify(self, keySortedPvalues, pValues):
outlierVector = [DECISION.UNDECIDED]*len(keySortedPvalues)
k = sys.maxint
for i in range(len(keySortedPvalues)):
self.adjustSigLevel(len(keySortedPvalues), i)
if(pValues[keySortedPvalues[i]] > self.adjustedSigLevel):
k = i
break
for i in range(len(keySortedPvalues)):
if(i<k):
outlierVector[keySortedPvalues[i]] = DECISION.OUTLIER
else:
outlierVector[keySortedPvalues[i]] = DECISION.NORMAL
return outlierVector
def classifyOne(self, rank, keySortedPvalues, pValues):
if(self.k == -1):
self.k = sys.maxint
for i in range(len(keySortedPvalues)):
adjustedSigLevel = self.sigLevel / float( len(keySortedPvalues) - i)
if(pValues[keySortedPvalues[i]] > adjustedSigLevel):
self.k = i
break
if(rank < self.k):
return DECISION.OUTLIER
else:
return DECISION.NORMAL