-
Notifications
You must be signed in to change notification settings - Fork 35
/
hist_comparer.py
465 lines (404 loc) · 17.4 KB
/
hist_comparer.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# Copyright (c) 2020 ING Wholesale Banking Advanced Analytics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import numpy as np
import pandas as pd
from scipy.stats import norm, pearsonr
from ...analysis.apply_func import ApplyFunc
from ...analysis.functions import (
expand_norm_hist_mean_cov,
expanding_hist,
hist_sum,
normalized_hist_mean_cov,
relative_chi_squared,
roll_norm_hist_mean_cov,
rolling_hist,
)
from ...analysis.hist_numpy import (
check_similar_hists,
get_consistent_numpy_1dhists,
get_consistent_numpy_2dgrids,
get_consistent_numpy_entries,
)
from ...base import Pipeline
from ...hist.histogram import HistogramContainer
from ...stats.numpy import googl_test, ks_prob, ks_test, uu_chi2
def hist_compare(row, hist_name1="", hist_name2="", max_res_bound=7.0):
"""Function to compare two histograms
Apply statistical tests to compare two input histograms, such as:
Chi2, KS, Pearson, max probability difference.
For categorical histograms, also check for unknown labels.
:param pd.Series row: row to apply compare function to
:param str hist_name1: name of histogram one to compare
:param str hist_name2: name of histogram two to compare
:param float max_res_bound: count number of normalized residuals with (absolute) value greater than X.
Default is 7.0.
:return: pandas Series with popular comparison metrics.
"""
x = pd.Series()
x["ks"] = np.nan
x["ks_zscore"] = np.nan
x["ks_pvalue"] = np.nan
x["pearson"] = np.nan
x["chi2"] = np.nan
x["chi2_norm"] = np.nan
x["chi2_zscore"] = np.nan
x["chi2_pvalue"] = np.nan
x["chi2_max_residual"] = np.nan
x["chi2_spike_count"] = np.nan
x["max_prob_diff"] = np.nan
unknown_labels = np.nan
x["unknown_labels"] = unknown_labels
# basic name checks
cols = row.index.to_list()
if len(hist_name1) == 0 or len(hist_name2) == 0 and len(cols) == 2:
hist_name1 = cols[0]
hist_name2 = cols[1]
if not all([name in cols for name in [hist_name1, hist_name2]]):
raise RuntimeError("Need to provide two histogram column names.")
# basic histogram checks
hc1 = row[hist_name1]
hc2 = row[hist_name2]
if not all([isinstance(hc, HistogramContainer) for hc in [hc1, hc2]]):
return x
if not check_similar_hists([hc1, hc2]):
return x
# compare
is_num = hc1.is_num
if hc1.n_dim == 1:
if is_num:
numpy_1dhists = get_consistent_numpy_1dhists([hc1, hc2])
entries_list = [nphist[0] for nphist in numpy_1dhists]
# KS-test only properly defined for (ordered) 1D interval variables
ks_testscore = ks_test(*entries_list)
x["ks"] = ks_testscore
ks_pvalue = ks_prob(ks_testscore)
x["ks_pvalue"] = ks_pvalue
x["ks_zscore"] = -norm.ppf(ks_pvalue)
else: # categorical
entries_list = get_consistent_numpy_entries([hc1, hc2])
# check consistency of bin_labels
labels1 = hc1.hist.bin_labels()
labels2 = hc2.hist.bin_labels()
subset = set(labels1) <= set(labels2)
unknown_labels = int(not subset)
elif hc1.n_dim == 2:
numpy_2dgrids = get_consistent_numpy_2dgrids([hc1, hc2])
entries_list = [entry.flatten() for entry in numpy_2dgrids]
# calculate pearson coefficient
pearson, pvalue = (np.nan, np.nan)
if len(entries_list[0]) >= 2:
same0 = all(entries_list[0] == entries_list[0][0])
same1 = all(entries_list[1] == entries_list[1][0])
if not same0 and not same1:
# this avoids std==0, and thereby avoid runtime warnings
pearson, pvalue = pearsonr(*entries_list)
chi2, chi2_norm, zscore, pvalue, res = uu_chi2(*entries_list)
x["pearson"] = pearson
x["chi2"] = chi2
x["chi2_norm"] = chi2_norm
x["chi2_zscore"] = zscore
x["chi2_pvalue"] = pvalue
x["chi2_max_residual"] = max(list(map(abs, res)))
x["chi2_spike_count"] = sum(abs(r) > max_res_bound for r in res)
x["max_prob_diff"] = googl_test(*entries_list)
x["unknown_labels"] = unknown_labels
return x
class HistComparer(Pipeline):
"""Base pipeline to compare histogram to previous rolling histograms"""
def __init__(
self,
func_hist_collector,
read_key,
store_key,
assign_to_key=None,
hist_col="histogram",
suffix="comp",
max_res_bound=7.0,
*args,
**kwargs,
):
"""Initialize an instance of RollingHistComparer.
:param func_hist_collector: histogram collection function
:param str read_key: key of input data to read from data store
:param str store_key: key of output data to store in data store
:param str assign_to_key: key of the input data to assign function applied-output to. (optional)
:param str hist_col: column/key in input df/dict that contains the histogram. default is 'histogram'
:param str suffix: column/key of rolling histogram. default is 'roll' -> column = 'histogram_roll'
:param float max_res_bound: count number of normalized residuals with (absolute) value greater than X.
Default is 7.0.
:param args: (tuple, optional): residual args passed on to func_mean and func_std
:param kwargs: (dict, optional): residual kwargs passed on to func_mean and func_std
"""
super().__init__(modules=[])
if assign_to_key is None:
assign_to_key = read_key
# make reference histogram(s)
hist_collector = ApplyFunc(apply_to_key=read_key, assign_to_key=assign_to_key)
hist_collector.add_apply_func(
func=func_hist_collector, entire=True, suffix=suffix, *args, **kwargs
)
# do histogram comparison
hist_comparer = ApplyFunc(
apply_to_key=assign_to_key,
assign_to_key=store_key,
apply_funcs=[
dict(
func=hist_compare,
hist_name1=hist_col,
hist_name2=hist_col + "_" + suffix,
prefix=suffix,
axis=1,
max_res_bound=max_res_bound,
)
],
)
self.modules = [hist_collector, hist_comparer]
class RollingHistComparer(HistComparer):
"""Compare histogram to previous rolling histograms"""
def __init__(
self,
read_key,
store_key,
window,
shift=1,
hist_col="histogram",
suffix="roll",
max_res_bound=7.0,
):
"""Initialize an instance of RollingHistComparer.
:param str read_key: key of input data to read from data store
:param str store_key: key of output data to store in data store
:param int window: size of rolling window
:param int shift: shift of rolling window. default is 1.
:param str hist_col: column/key in input df/dict that contains the histogram. default is 'histogram'
:param str suffix: column/key of rolling histogram. default is 'roll' -> column = 'histogram_roll'
:param float max_res_bound: count number of normalized residuals with (absolute) value greater than X.
Default is 7.0.
"""
kws = {"window": window, "shift": shift, "hist_name": hist_col}
super().__init__(
rolling_hist,
read_key,
store_key,
read_key,
hist_col,
suffix,
max_res_bound,
**kws,
)
self.read_key = read_key
self.window = window
def transform(self, datastore):
self.logger.info(
f'Comparing "{self.read_key}" with rolling sum of {self.window} previous histogram(s).'
)
return super().transform(datastore)
class PreviousHistComparer(RollingHistComparer):
"""Compare histogram to previous histograms"""
def __init__(
self,
read_key,
store_key,
hist_col="histogram",
suffix="prev1",
max_res_bound=7.0,
):
"""Initialize an instance of PreviousHistComparer.
:param str read_key: key of input data to read from data store
:param str store_key: key of output data to store in data store
:param str hist_col: column/key in input df/dict that contains the histogram. default is 'histogram'
:param str suffix: column/key of rolling histogram. default is 'prev' -> column = 'histogram_prev'
:param float max_res_bound: count number of normalized residuals with (absolute) value greater than X.
Default is 7.0.
"""
super().__init__(read_key, store_key, 1, 1, hist_col, suffix, max_res_bound)
class ExpandingHistComparer(HistComparer):
"""Compare histogram to previous expanding histograms"""
def __init__(
self,
read_key,
store_key,
shift=1,
hist_col="histogram",
suffix="expanding",
max_res_bound=7.0,
):
"""Initialize an instance of ExpandingHistComparer.
:param str read_key: key of input data to read from data store
:param str store_key: key of output data to store in data store
:param int shift: shift of rolling window. default is 1.
:param str hist_col: column/key in input df/dict that contains the histogram. default is 'histogram'
:param str suffix: column/key of rolling histogram. default is 'expanding' -> column = 'histogram_expanding'
:param float max_res_bound: count number of normalized residuals with (absolute) value greater than X.
Default is 7.0.
"""
kws = {"shift": shift, "hist_name": hist_col}
super().__init__(
expanding_hist,
read_key,
store_key,
read_key,
hist_col,
suffix,
max_res_bound,
**kws,
)
self.read_key = read_key
def transform(self, datastore):
self.logger.info(
f'Comparing "{self.read_key}" with expanding sum of past histograms.'
)
return super().transform(datastore)
class ReferenceHistComparer(HistComparer):
"""Compare histogram to reference histograms"""
def __init__(
self,
reference_key,
assign_to_key,
store_key,
hist_col="histogram",
suffix="ref",
max_res_bound=7.0,
):
"""Initialize an instance of ReferenceHistComparer.
:param str reference_key: key of input data to read from data store
:param str assign_to_key: key of input data to read from data store
:param str store_key: key of output data to store in data store
:param str hist_col: column/key in input df/dict that contains the histogram. default is 'histogram'
:param str suffix: column/key of rolling histogram. default is 'ref' -> column = 'histogram_ref'
:param float max_res_bound: count number of normalized residuals with (absolute) value greater than X.
Default is 7.0.
"""
kws = {"metrics": [hist_col]}
super().__init__(
hist_sum,
reference_key,
store_key,
assign_to_key,
hist_col,
suffix,
max_res_bound,
**kws,
)
self.reference_key = reference_key
self.assign_to_key = assign_to_key
def transform(self, datastore):
self.logger.info(
f'Comparing "{self.assign_to_key}" with reference "{self.reference_key}"'
)
return super().transform(datastore)
class NormHistComparer(Pipeline):
"""Base pipeline to compare histogram to normalized histograms"""
def __init__(
self,
func_hist_collector,
read_key,
store_key,
assign_to_key=None,
hist_col="histogram",
*args,
**kwargs,
):
"""Initialize an instance of NormHistComparer.
:param func_hist_collector: histogram collection function
:param str read_key: key of input data to read from data store
:param str store_key: key of output data to store in data store
:param str assign_to_key: key of the input data to assign function applied-output to. (optional)
:param str hist_col: column/key in input df/dict that contains the histogram. default is 'histogram'
:param args: (tuple, optional): residual args passed on to func_hist_collector
:param kwargs: (dict, optional): residual kwargs passed on to func_hist_collector
"""
super().__init__(modules=[])
if assign_to_key is None:
assign_to_key = read_key
# make reference histogram(s)
hist_collector = ApplyFunc(apply_to_key=read_key, assign_to_key=assign_to_key)
hist_collector.add_apply_func(
func=func_hist_collector, hist_name=hist_col, suffix="", *args, **kwargs
)
# do histogram comparison
hist_comparer = ApplyFunc(
apply_to_key=assign_to_key,
assign_to_key=store_key,
apply_funcs=[
dict(func=relative_chi_squared, hist_name=hist_col, suffix="", axis=1)
],
)
self.modules = [hist_collector, hist_comparer]
class RollingNormHistComparer(NormHistComparer):
"""Compare histogram to previous rolling normalized histograms"""
def __init__(self, read_key, store_key, window, shift=1, hist_col="histogram"):
"""Initialize an instance of RollingNormHistComparer.
:param str read_key: key of input data to read from data store
:param str store_key: key of output data to store in data store
:param int window: size of rolling window
:param int shift: shift of rolling window. default is 1.
:param str hist_col: column/key in input df/dict that contains the histogram. default is 'histogram'
"""
if window < 2:
raise ValueError("Need window size of 2 or greater.")
kws = {"window": window, "shift": shift, "entire": True}
super().__init__(
roll_norm_hist_mean_cov, read_key, store_key, read_key, hist_col, **kws
)
self.read_key = read_key
self.window = window
def transform(self, datastore):
self.logger.info(
f'Comparing "{self.read_key}" with relative mean of {self.window} previous histogram(s).'
)
return super().transform(datastore)
class ExpandingNormHistComparer(NormHistComparer):
"""Compare histogram to previous expanding normalized histograms"""
def __init__(self, read_key, store_key, shift=1, hist_col="histogram"):
"""Initialize an instance of ExpandingNormHistComparer.
:param str read_key: key of input data to read from data store
:param str store_key: key of output data to store in data store
:param int shift: shift of rolling window. default is 1.
:param str hist_col: column/key in input df/dict that contains the histogram. default is 'histogram'
"""
kws = {"shift": shift, "entire": True}
super().__init__(
expand_norm_hist_mean_cov, read_key, store_key, read_key, hist_col, **kws
)
self.read_key = read_key
def transform(self, datastore):
self.logger.info(
f'Comparing "{self.read_key}" with normalized mean of expanding past histograms.'
)
return super().transform(datastore)
class ReferenceNormHistComparer(NormHistComparer):
"""Compare histogram to reference normalized histograms"""
def __init__(self, reference_key, assign_to_key, store_key, hist_col="histogram"):
"""Initialize an instance of ReferenceNormHistComparer.
:param str reference_key: key of input data to read from data store
:param str assign_to_key: key of input data to read from data store
:param str store_key: key of output data to store in data store
:param str hist_col: column/key in input df/dict that contains the histogram. default is 'histogram'
"""
super().__init__(
normalized_hist_mean_cov, reference_key, store_key, assign_to_key, hist_col
)
self.reference_key = reference_key
self.assign_to_key = assign_to_key
def transform(self, datastore):
self.logger.info(
f'Comparing "{self.assign_to_key}" with normalized reference "{self.reference_key}"'
)
return super().transform(datastore)