-
Notifications
You must be signed in to change notification settings - Fork 267
/
truth_tables.py
229 lines (179 loc) · 5.97 KB
/
truth_tables.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
from sys import stdout
from csv import DictReader, DictWriter
class PeekyReader:
def __init__(self, reader):
self.peeked = None
self.reader = reader
def peek(self):
if self.peeked is None:
self.peeked = next(self.reader)
return self.peeked
def __iter__(self):
return self
def __next__(self):
if self.peeked is not None:
ret = self.peeked
self.peeked = None
return ret
try:
return next(self.reader)
except StopIteration:
self.peeked = None
raise StopIteration
class Person:
def __init__(self, reader):
self.__rows = []
self.__idx = reader.peek()['id']
try:
while reader.peek()['id'] == self.__idx:
self.__rows.append(next(reader))
except StopIteration:
pass
@property
def lifetime(self):
memo = 0
for it in self.__rows:
memo += int(it['end']) - int(it['start'])
return memo
@property
def recidivist(self):
return (self.__rows[0]['is_recid'] == "1" and
self.lifetime <= 730)
@property
def violent_recidivist(self):
return (self.__rows[0]['is_violent_recid'] == "1" and
self.lifetime <= 730)
@property
def low(self):
return self.__rows[0]['score_text'] == "Low"
@property
def high(self):
return not self.low
@property
def low_med(self):
return self.low or self.score == "Medium"
@property
def true_high(self):
return self.score == "High"
@property
def vlow(self):
return self.__rows[0]['v_score_text'] == "Low"
@property
def vhigh(self):
return not self.vlow
@property
def vlow_med(self):
return self.vlow or self.vscore == "Medium"
@property
def vtrue_high(self):
return self.vscore == "High"
@property
def score(self):
return self.__rows[0]['score_text']
@property
def vscore(self):
return self.__rows[0]['v_score_text']
@property
def race(self):
return self.__rows[0]['race']
@property
def valid(self):
return (self.__rows[0]['is_recid'] != "-1" and
(self.recidivist and self.lifetime <= 730) or
self.lifetime > 730)
@property
def compas_felony(self):
return 'F' in self.__rows[0]['c_charge_degree']
@property
def score_valid(self):
return self.score in ["Low", "Medium", "High"]
@property
def vscore_valid(self):
return self.vscore in ["Low", "Medium", "High"]
@property
def rows(self):
return self.__rows
def count(fn, data):
return len(list(filter(fn, list(data))))
def t(tn, fp, fn, tp):
surv = tn + fp
recid = tp + fn
print(" \tLow\tHigh")
print("Survived \t%i\t%i\t%.2f" % (tn, fp, surv / (surv + recid)))
print("Recidivated\t%i\t%i\t%.2f" % (fn, tp, recid / (surv + recid)))
print("Total: %.2f" % (surv + recid))
print("False positive rate: %.2f" % (fp / surv * 100))
print("False negative rate: %.2f" % (fn / recid * 100))
spec = tn / (tn + fp)
sens = tp / (tp + fn)
ppv = tp / (tp + fp)
npv = tn / (tn + fn)
prev = recid / (surv + recid)
print("Specificity: %.2f" % spec)
print("Sensitivity: %.2f" % sens)
print("Prevalence: %.2f" % prev)
print("PPV: %.2f" % ppv)
print("NPV: %.2f" % npv)
print("LR+: %.2f" % (sens / (1 - spec)))
print("LR-: %.2f" % ((1-sens) / spec))
def table(recid, surv, prefix=''):
tn = count(lambda i: getattr(i, prefix + 'low'), surv)
fp = count(lambda i: getattr(i, prefix + 'high'), surv)
fn = count(lambda i: getattr(i, prefix + 'low'), recid)
tp = count(lambda i: getattr(i, prefix + 'high'), recid)
t(tn, fp, fn, tp)
def hightable(recid, surv, prefix=''):
tn = count(lambda i: getattr(i, prefix + 'low_med'), surv)
fp = count(lambda i: getattr(i, prefix + 'true_high'), surv)
fn = count(lambda i: getattr(i, prefix + 'low_med'), recid)
tp = count(lambda i: getattr(i, prefix + 'true_high'), recid)
t(tn, fp, fn, tp)
def vtable(recid, surv):
table(recid, surv, prefix='v')
def vhightable(recid, surv):
hightable(recid, surv, prefix='v')
def is_race(race):
return lambda x: x.race == race
def write_two_year_file(f, pop, test, headers):
headers = list(headers)
headers.append('two_year_recid')
with open(f, 'w') as o:
writer = DictWriter(o, fieldnames=headers)
writer.writeheader()
for person in pop:
row = person.rows[0]
if getattr(person, test):
row['two_year_recid'] = 1
else:
row['two_year_recid'] = 0
if person.compas_felony:
row['c_charge_degree'] = 'F'
else:
row['c_charge_degree'] = 'M'
writer.writerow(row)
stdout.write('.')
def create_two_year_files():
people = []
headers = []
with open("./cox-parsed.csv") as f:
reader = PeekyReader(DictReader(f))
try:
while True:
p = Person(reader)
if p.valid:
people.append(p)
except StopIteration:
pass
headers = reader.reader.fieldnames
pop = list(filter(lambda i: (i.recidivist and i.lifetime <= 730) or
i.lifetime > 730,
filter(lambda x: x.score_valid, people)))
vpop = list(filter(lambda i: (i.violent_recidivist and i.lifetime <= 730) or
i.lifetime > 730,
filter(lambda x: x.vscore_valid, people)))
write_two_year_file("./compas-scores-two-years.csv", pop,
'recidivist', headers)
write_two_year_file("./compas-scores-two-years-violent.csv", vpop,
'violent_recidivist', headers)
if __name__ == "__main__":
create_two_year_files()