-
Notifications
You must be signed in to change notification settings - Fork 39
/
distribution_statistics.py
424 lines (357 loc) · 16.6 KB
/
distribution_statistics.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
# Copyright (c) 2020 ING Bank N.V.
#
# 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 itertools
import warnings
import numpy as np
import pandas as pd
from tqdm import tqdm
from probatus.binning import AgglomerativeBucketer, QuantileBucketer, SimpleBucketer
from probatus.stat_tests import ad, es, ks, psi, sw
from probatus.utils.arrayfuncs import check_numeric_dtypes
class DistributionStatistics:
"""
Wrapper that applies a statistical test to compare two distributions.
Details on the available tests can be found [here](/probatus/api/stat_tests.html#available-tests).
For some tests, default data binning strategies are also provided.
Example:
```python
import numpy as np
import pandas as pd
from probatus.stat_tests import DistributionStatistics
d1 = np.histogram(np.random.normal(size=1000), 10)[0]
d2 = np.histogram(np.random.normal(size=1000), 10)[0]
myTest = DistributionStatistics('KS', bin_count=10)
test_statistic, p_value = myTest.compute(d1, d2, verbose=True)
```
"""
binning_strategy_dict = {
"simplebucketer": SimpleBucketer,
"agglomerativebucketer": AgglomerativeBucketer,
"quantilebucketer": QuantileBucketer,
None: None,
}
statistical_test_dict = {
"ES": {
"func": es,
"name": "Epps-Singleton",
"default_binning": None,
},
"KS": {
"func": ks,
"name": "Kolmogorov-Smirnov",
"default_binning": None,
},
"AD": {
"func": ad,
"name": "Anderson-Darling TS",
"default_binning": None,
},
"SW": {
"func": sw,
"name": "Shapiro-Wilk based difference",
"default_binning": None,
},
"PSI": {
"func": psi,
"name": "Population Stability Index",
"default_binning": "quantilebucketer",
},
}
def __init__(self, statistical_test, binning_strategy="default", bin_count=10):
"""
Initializes the class.
Args:
statistical_test (str): Statistical test to apply. Available tests:
- `'ES'`: Epps-Singleton
- `'KS'`: Kolmogorov-Smirnov
- `'PSI'`: Population Stability Index
- `'SW'`: Shapiro-Wilk
- `'AD'`: Anderson-Darling
Details on the available tests can be found [here](/probatus/api/stat_tests.html#available-tests)
binning_strategy (string, optional):
Binning strategy to apply, binning strategies implemented:
- `'simplebucketer'`: equally spaced bins,
- `'agglomerativebucketer'`: binning by applying the Scikit-learn implementation of Agglomerative
Clustering,
- `'quantilebucketer'`: bins with equal number of elements,
- `'default'`: applies a default binning for a given stats_test. For all tests apart from PSI, no
binning (None) is used. For PSI by default quantilebucketer is used,
- `None`: no binning is applied. The test is computed based on original distribution.
bin_count (int, optional): In case binning_strategy is not None, specify the number of bins to be used by
the binning strategy. By default 10 bins are used.
"""
self.statistical_test = statistical_test.upper()
self.binning_strategy = binning_strategy
self.bin_count = bin_count
self.fitted = False
# Initialize the statistical test
if self.statistical_test not in self.statistical_test_dict:
raise NotImplementedError(f"The statistical test should be one of {self.statistical_test_dict.keys()}")
else:
self.statistical_test_name = self.statistical_test_dict[self.statistical_test]["name"]
self._statistical_test_function = self.statistical_test_dict[self.statistical_test]["func"]
# Initialize the binning strategy
if self.binning_strategy:
self.binning_strategy = self.binning_strategy.lower()
if self.binning_strategy == "default":
self.binning_strategy = self.statistical_test_dict[self.statistical_test]["default_binning"]
if self.binning_strategy not in self.binning_strategy_dict:
raise NotImplementedError(
f"The binning strategy should be one of {list(self.binning_strategy_dict.keys())}"
)
else:
binner = self.binning_strategy_dict[self.binning_strategy]
if binner is not None:
self.binner = binner(bin_count=self.bin_count)
def __repr__(self):
"""
String representation.
"""
repr_ = f"DistributionStatistics object\n\tstatistical_test: {self.statistical_test}"
if self.binning_strategy:
repr_ += f"\n\tbinning_strategy: {self.binning_strategy}\n\tbin_count: {self.bin_count}"
else:
repr_ += "\n\tNo binning applied"
if self.fitted:
repr_ += f"\nResults\n\tvalue {self.statistical_test}-statistic: {self.statistic}"
if hasattr(self, "p_value"):
repr_ += f"\n\tp-value: {self.p_value}"
return repr_
def compute(self, d1, d2, verbose=False):
"""
Apply the statistical test and compute statistic value and p-value.
Args:
d1 (np.array or pandas.DataFrame):
distribution 1.
d2 (np.array or pandas.DataFrame):
distribution 2.
verbose (bool, optional):
Flag indicating whether prints should be shown.
Returns:
float: Statistic value
float: p_value. For PSI test, only the statistic value is returned
"""
check_numeric_dtypes(d1)
check_numeric_dtypes(d2)
# Bin the data
if self.binning_strategy:
self.binner.fit(d1)
d1_preprocessed = self.binner.compute(d1)
d2_preprocessed = self.binner.compute(d2)
else:
d1_preprocessed, d2_preprocessed = d1, d2
# Perform the statistical test
res = self._statistical_test_function(d1_preprocessed, d2_preprocessed, verbose=verbose)
self.fitted = True
# Check form of results and return
if type(res) == tuple:
self.statistic, self.p_value = res
return self.statistic, self.p_value
else:
self.statistic = res
return self.statistic
class AutoDist:
"""Apply stat tests and binning strategies.
Class to automatically apply all implemented statistical distribution tests and binning strategies
to (a selection of) features in two dataframes.
Details on the available tests can be found [here](/probatus/api/stat_tests.html#available-tests).
Example:
```python
import numpy as np
import pandas as pd
from probatus.stat_tests import AutoDist
df1 = pd.DataFrame(np.random.normal(size=(1000, 2)), columns=['feat_0', 'feat_1'])
df2 = pd.DataFrame(np.random.normal(size=(1000, 2)), columns=['feat_0', 'feat_1'])
myAutoDist = AutoDist(statistical_tests=["KS", "PSI"], binning_strategies='simplebucketer', bin_count=10)
myAutoDist.compute(df1, df2, column_names=df1.columns)
```
<img src="../img/autodist.png" width="700" />
"""
def __init__(self, statistical_tests="all", binning_strategies="default", bin_count=10):
"""
Initializes the class.
Args:
statistical_tests (str or list of str, optional): Test or list of tests to apply.
Set to `'all'` to apply all the available test. Available tests:
- `'ES'`: Epps-Singleton
- `'KS'`: Kolmogorov-Smirnov
- `'PSI'`: Population Stability Index
- `'SW'`: Shapiro-Wilk
- `'AD'`: Anderson-Darling
Details on the available tests can be found [here](/probatus/api/stat_tests.html#available-tests).
binning_strategies (str, optional): Binning strategies to apply for each test, either list of tests names,
'all' or 'default'. Binning strategies that can be chosen:
- `'SimpleBucketer'`: equally spaced bins,
- `'AgglomerativeBucketer'`: binning by applying the Scikit-learn implementation of Agglomerative
Clustering,
- `'QuantileBucketer'`: bins with equal number of elements,
- `None`: no binning is applied. Note that not all statistical tests will be performed since some of
them require binning strategies.
- `'default'`: applies a default binning for a given stats_test. For all tests apart from PSI, no
binning (None) is used. For PSI by default quantilebucketer is used.
- `'all'`: each binning strategy is used for each statistical test
bin_count (integer, None or list of integers, optional):
bin_count value(s) to be used, note that None can only be used when no bucketing strategy is applied.
"""
self.fitted = False
# Initialize statistical tests to be performed
if statistical_tests == "all":
self.statistical_tests = list(DistributionStatistics.statistical_test_dict.keys())
elif isinstance(statistical_tests, str):
self.statistical_tests = [statistical_tests]
else:
self.statistical_tests = statistical_tests
# Initialize binning strategies to be used
if binning_strategies == "all":
self.binning_strategies = list(DistributionStatistics.binning_strategy_dict.keys())
elif isinstance(binning_strategies, str):
self.binning_strategies = [binning_strategies]
elif binning_strategies is None:
self.binning_strategies = [None]
else:
self.binning_strategies = binning_strategies
if not isinstance(bin_count, list):
self.bin_count = [bin_count]
else:
self.bin_count = bin_count
def __repr__(self):
"""
String representation.
"""
repr_ = "AutoDist object"
if not self.fitted:
repr_ += "\n\tAutoDist not fitted"
if self.fitted:
repr_ += "\n\tAutoDist fitted"
repr_ += f"\n\tstatistical_tests: {self.statistical_tests}"
repr_ += f"\n\tbinning_strategies: {self.binning_strategies}"
repr_ += f"\n\tbin_count: {self.bin_count}"
return repr_
def compute(
self,
df1,
df2,
column_names=None,
return_failed_tests=True,
suppress_warnings=False,
):
"""
Fit the AutoDist object to data; i.e. apply the statistical tests and binning strategies.
Args:
df1 (pandas.DataFrame):
DataFrame 1 for distribution comparison with DataFrame 2.
df2 (pandas.DataFrame):
DataFrame 2 for distribution comparison with DataFrame 1.
column_names (list of str, optional):
list of columns in df1 and df2 that should be compared. If None, all column names will be compared.
return_failed_tests (bool, optional):
remove tests in result that did not succeed.
suppress_warnings (bool, optional):
whether to suppress warnings during the fit process.
Returns:
pandas.DataFrame: DataFrame with results of the performed statistical tests and binning strategies.
"""
if column_names is None:
column_names = df1.columns.to_list()
if len(set(column_names) - set(df2.columns)):
raise Exception("column_names was set to None but columns in provided dataframes are different")
# Check if all columns in column_names are in df1 and df2
elif len(set(column_names) - set(df1.columns)) or len(set(column_names) - set(df2.columns)):
raise Exception("Not all columns in `column_names` are in the provided dataframes")
# Calculate statistics and p-values for all combinations
result_all = []
for col in column_names:
# Issue a warning if missing values are present in one of the two columns. These observations are removed
# in the calculations.
if np.sum(df1[col].isna()) + np.sum(df2[col].isna()):
warnings.warn(f"Missing values in column {col} have been removed")
# Remove the missing values.
feature_df1 = df1[col].dropna()
feature_df2 = df2[col].dropna()
for stat_test, bin_strat, bins in tqdm(
list(
itertools.product(
self.statistical_tests,
self.binning_strategies,
self.bin_count,
)
)
):
if self.binning_strategies == ["default"]:
bin_strat = DistributionStatistics.statistical_test_dict[stat_test]["default_binning"]
dist = DistributionStatistics(
statistical_test=stat_test,
binning_strategy=bin_strat,
bin_count=bins,
)
try:
if suppress_warnings:
warnings.filterwarnings("ignore")
_ = dist.compute(feature_df1, feature_df2)
if suppress_warnings:
warnings.filterwarnings("default")
statistic = dist.statistic
p_value = dist.p_value
except Exception:
statistic, p_value = "an error occurred", None
pass
# Append result to results list
result_ = {
"column": col,
"statistical_test": stat_test,
"binning_strategy": bin_strat,
"bin_count": bins,
"statistic": statistic,
"p_value": p_value,
}
result_all.append(result_)
result_all = pd.DataFrame(result_all)
if not return_failed_tests:
result_all = result_all[result_all["statistic"] != "an error occurred"]
self.fitted = True
self._result = result_all[
[
"column",
"statistical_test",
"binning_strategy",
"bin_count",
"statistic",
"p_value",
]
]
self._result["bin_count"] = self._result["bin_count"].astype(int)
self._result.loc[self._result["binning_strategy"].isnull(), "bin_count"] = 0
self._result.loc[self._result["binning_strategy"].isnull(), "binning_strategy"] = "no_bucketing"
# Remove duplicates that appear if multiple bin numbers are passed, and binning strategy None
self._result = self._result.drop_duplicates(
subset=["column", "statistical_test", "binning_strategy", "bin_count"],
keep="first",
)
# create pivot table as final output
self.result = pd.pivot_table(
self._result,
values=["statistic", "p_value"],
index="column",
columns=["statistical_test", "binning_strategy", "bin_count"],
aggfunc="sum",
)
# flatten multi-index
self.result.columns = ["_".join([str(x) for x in line]) for line in self.result.columns.values]
self.result.reset_index(inplace=True)
return self.result