forked from uhh-lt/sensegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignificance.py
59 lines (45 loc) · 2.05 KB
/
significance.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
from scipy.stats import binom
from pandas import read_csv
import numpy as np
import argparse
def mcnemar_midp(b, c):
"""
Compute McNemar's test using the "mid-p" variant suggested by:
M.W. Fagerland, S. Lydersen, P. Laake. 2013. The McNemar test for
binary matched-pairs data: Mid-p and asymptotic are better than exact
conditional. BMC Medical Research Methodology 13: 91.
`b` is the number of observations correctly labeled by the first---but
not the second---system; `c` is the number of observations correctly
labeled by the second---but not the first---system.
"""
n = b + c
x = min(b, c)
dist = binom(n, .5)
p = 2. * dist.cdf(x)
midp = p - dist.pmf(x)
chi = float(abs(b - c)**2)/n
print("b = ", b)
print("c = ", c)
print("Exact p = ", p)
print("Mid p = ", midp)
print("Chi = ", chi)
def run(set1, set2):
r1 = read_csv(set1, sep='\t', encoding='utf8',
dtype={'predict_sense_ids': np.str, 'gold_sense_ids': np.str, 'context_id': np.str},
doublequote=False, quotechar="\u0000" )
r2 = read_csv(set2, sep='\t', encoding='utf8',
dtype={'predict_sense_ids': np.str, 'gold_sense_ids': np.str, 'context_id': np.str},
doublequote=False, quotechar="\u0000" )
s1 = r1["correct"].values.tolist()
s2 = r2["correct"].values.tolist()
b = sum([x and not y for (x,y) in zip(s1,s2)])
c = sum([not x and y for (x,y) in zip(s1,s2)])
mcnemar_midp(b, c)
def main():
parser = argparse.ArgumentParser(description='Compute statistical significance of predicted label sets')
parser.add_argument('set1', help='A path to the first evaluated dataset. Format: "context_id<TAB>target<TAB>target_pos<TAB>target_position<TAB>gold_sense_ids<TAB>predict_sense_ids<TAB>golden_related<TAB>predict_related<TAB>context<TAB>smth<TAB>correct')
parser.add_argument("set2", help="A path to the second evaluated dataset")
args = parser.parse_args()
run(args.set1, args.set2)
if __name__ == '__main__':
main()