-
Notifications
You must be signed in to change notification settings - Fork 2
/
truedistribution.py
476 lines (394 loc) · 16.2 KB
/
truedistribution.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
466
467
468
469
470
471
472
473
474
475
476
"""
This module contains a class representing a true underlying distribution.
"""
import os
import numpy as np
from scipy.interpolate import interp1d
from scipy.stats.distributions import truncnorm
from sklearn.model_selection import train_test_split
import utils
from data import get_data
# -------------------------------------------------------------------------
# region (Abstract) Base Distribution
# -------------------------------------------------------------------------
class BaseDistribution:
"""A generative model of the underlying ground truth distribution."""
def __init__(self, config):
"""
Initialize the true distribution.
Args:
config: The configuration dictionary.
"""
self.config = config
self.feature_dim = None
self.is_1d = False
self._test_set = None
def sample_features(self, n):
"""
Sample feature variables.
Args:
n: Number of examples.
Returns:
2-tuple of np.ndarrays:
[0]: x, an iid sample of features from the true distribution
[1]: s, an iid sample of protected from the true distribution
"""
raise NotImplementedError("Subclass must override `sample_features`.")
def sample_labels(self, x, s, yproba=False):
"""
Sample labels given feature variables.
Args:
x: Feature examples.
s: Protected attributes.
yproba: Whether to return the probabilities of the binary labels.
Returns:
An iid sample of labels from the true distribution given the
features x and protected attribute s (np.ndarray).
"""
raise NotImplementedError("Subclass must override `sample_labels`.")
def sample_all(self, n, yproba=False):
"""
Draw a full sample of features and labels.
Args:
n: The number of exmaples to draw.
yproba: Whether to return the probabilities of the binary labels.
Returns:
x, s, y, (yproba): np.ndarrays of features, binary labels, and the
corresponding probabilities for the labels. The latter only if
`yproba=True`.
"""
x, s = self.sample_features(n=n)
if yproba:
y, yproba = self.sample_labels(x, s, yproba=yproba)
return x, y, s, yproba
else:
y = self.sample_labels(x, s, yproba=yproba)
return x, y, s
def get_test(self, n=1000000):
"""Get a test set for this distribution.
A testset of the given size is sampled and stored. If the test set
function is called multiple times, with the same `n`, the cached data
is returned. If `n` changes, a new test set is sampled and returned.
Args:
n: The number of examples to use for the test set.
Returns:
3-tuple of np.ndarrays
[0]: x, the features
[1]: y, the labels
[2]: s, the protected attributes
"""
if self._test_set is None or self._test_set[0].shape[0] != n:
self._test_set = self.sample_all(n)
return self._test_set
# endregion
# -------------------------------------------------------------------------
# region Dummy Custom Synthetic 1D Distribution for Proof of Concept
# -------------------------------------------------------------------------
class DummyDistribution1D(BaseDistribution):
"""A simple generative model of the true distribution."""
def __init__(self, config):
"""
Initialize the true distribution.
Args:
config: The configuration dictionary.
"""
super().__init__(config)
self.type = "custom1d"
self.theta = np.array(config["theta"])
self.feature_dim = len(self.theta)
if "split_support" not in config["custom_tweaks"]:
self.threshold = self._threshold
self.is_1d = True
def sample_features(self, n, **kwargs):
"""
Draw examples only for the features of the true distribution.
Args:
n: The number of examples to draw.
Returns:
x: np.ndarray with the features of dimension (n, k), where k is
either 1 or 2 depending on whether a constant is added
"""
if self.config["protected_fraction"] is not None:
s = (
np.random.rand(n, 1) < self.config["protected_fraction"]
).astype(int)
x = 3.5 * np.random.randn(n, 1) + 3 * (0.5 - s)
else:
s = np.full(n, np.nan)
x = 3.5 * np.random.randn(n, 1)
if self.config["protected_as_feature"]:
x = np.concatenate((x, s.reshape(-1, 1)), axis=1)
if self.config["add_constant"]:
x = np.hstack([np.ones([n, 1]), x])
return x, s.ravel()
def sample_labels(self, x, s, yproba=False):
"""
Draw examples of labels for given features.
Args:
x: Given features (usually obtained by calling `sample_features`).
s: Sensitive attribute.
yproba: Whether to return the probabilities of the binary labels.
Returns:
y: np.ndarray of binary (0/1) labels (if `yproba=False`)
y, yproba: np.ndarrays of binary (0/1) labels as well as the
original probabilities of the labels (if `yproba=False`)
"""
yprob = utils.sigmoid(x.dot(self.theta))
if "bump_left" in self.config["custom_tweaks"]:
yprob += np.exp(-(x[:, 1] + 6) ** 2 * 2) * 0.5
yprob = np.maximum(np.minimum(yprob, 1), 0)
if "bump_right" in self.config["custom_tweaks"]:
yprob -= np.exp(-(x[:, 1] - 5) ** 2 * 0.8) * 0.35
yprob = np.maximum(np.minimum(yprob, 1), 0)
if "split_support" in self.config["custom_tweaks"]:
yprob = 0.8 * utils.sigmoid(0.6 * (x[:, 1] + 3)) * utils.sigmoid(
-5 * (x[:, 1] - 3)
) + utils.sigmoid(x[:, 1] - 5)
y = np.random.binomial(1, yprob)
if yproba:
return y, yprob
return y
def _threshold(self, cost):
"""The threshold for this policy."""
if len(self.theta) == 1:
return 0.0
if len(self.theta) == 2:
return utils.get_threshold(self.theta, cost)
else:
raise RuntimeError("Scalar threshold exists only for 1D.")
# endregion
# -------------------------------------------------------------------------
# region Fico Score Based Distribution
# -------------------------------------------------------------------------
class InverseCDF(BaseDistribution):
"""
A simple generative model of FICO scores estimated from real data.
This is currently unused in the experiments as it did not yield any
interesting insights beyond the 1D synthetic examples that we looked at.
"""
def __init__(self, config, group_weights=None):
"""
Initialize the true distribution.
Args:
config: The configuration dictionary.
group_weights: How to weight the possible groups in the mixture.
Dictionary with keys in 'black', 'white', 'asian', 'hispanic'.
"""
super().__init__(config)
self.type = "inv_cdf"
self.feature_dim = 2 if self.config["add_constant"] else 1
self.thresh = None
self.inv_cdfs = None
self.pdfs = None
self.prob_bnds = None
self.score_bnds = None
self.is_1d = True
if group_weights is None:
self.weights = {"white": 0.0, "black": 1.0}
else:
self.weights = group_weights
if sum(self.weights.values()) != 1:
raise ValueError("Weights must sum to 1.")
self._set_inverse_cdf()
def _set_inverse_cdf(self):
"""Compute the inverse CDF from data."""
data_dir = os.path.abspath(self.config["path"])
data_dir = os.path.join(data_dir, self.config["type"])
self.inv_cdfs = {}
self.pdfs = {}
lo_prob, hi_prob = 0, 1000
lo_score, hi_score = 0, 1000
for group in self.weights:
data_path = os.path.join(data_dir, "marginals_" + group + ".npz")
data = np.load(data_path)
score, proba, cdf = data["fico"], data["proba"], data["cdf"]
# score = (score - 300.0) / 550.0
self.inv_cdfs[group] = interp1d(
cdf, score, kind="linear", assume_sorted=False
)
self.pdfs[group] = interp1d(
score, proba, kind="linear", assume_sorted=False
)
lo_prob = max(lo_prob, min(proba))
hi_prob = min(hi_prob, max(proba))
lo_score = max(lo_score, min(score))
hi_score = min(hi_score, max(score))
self.prob_bnds = (1.001 * lo_prob, 0.999 * hi_prob)
self.score_bnds = (lo_score, hi_score)
def set_threshold_get_cost(self, threshold):
"""Find the cost corresponding to a given score threshold."""
self.thresh = float(threshold)
scores = np.linspace(*self.score_bnds, 300)
return utils.find_x_for_y(self.pdf(scores), scores, threshold)
def inv_cdf(self, prob):
"""Get the inverse CDF value."""
x = np.zeros_like(prob, dtype="float64")
for group, w in self.weights.items():
x += self.weights[group] * self.inv_cdfs[group](prob)
return x
def pdf(self, score):
"""Get the probability of repayment."""
x = np.zeros_like(score, dtype="float64")
for group, w in self.weights.items():
x += self.weights[group] * self.pdfs[group](score)
return x
def sample_features(self, n, **kwargs):
"""
Draw examples only for the features of the true distribution.
Args:
n: The number of examples to draw.
Returns:
x: np.ndarray with the features of dimension (n, 2)
"""
# Assume there are only two groups
if len(self.weights.keys()) != 2:
raise RuntimeError("Can only sample for two groups.")
group_names = sorted(list(self.weights))
s = np.random.binomial(1, self.weights[group_names[0]], (n, 1)).astype(
int
)
x = np.zeros_like(s, dtype="float64")
for group in group_names:
probs = (
np.random.rand(n, 1) * np.diff(self.prob_bnds)[0]
+ self.prob_bnds[0]
)
x += s * self.inv_cdfs[group](probs)
s = 1 - s
if self.config["add_constant"]:
x = np.hstack([np.ones([n, 1]), x])
return x, s
def sample_labels(self, x, s, yproba=False):
"""
Draw examples of labels for given features.
Args:
x: Given features (usually obtained by calling `sample_features`).
s: Sensitive attribute.
yproba: Whether to return the probabilities of the binary labels.
Returns:
y: np.ndarray of binary (0/1) labels (if `yproba=False`)
y, yproba: np.ndarrays of binary (0/1) labels as well as the
original probabilities of the labels (if `yproba=False`)
"""
if x.shape[1] == 2:
yprob = self.pdf(x[:, 1])
else:
yprob = self.pdf(x)
y = np.random.binomial(1, yprob)
if yproba:
return y, yprob
return y
def threshold(self, cost):
"""The threshold for this policy."""
assert self.thresh is not None, "Need to set threshold first"
return self.thresh
# endregion
# -------------------------------------------------------------------------
# region A score based distribution that is uncalibrated
# -------------------------------------------------------------------------
class UncalibratedScore(BaseDistribution):
"""An distribution modelling an uncalibrated score."""
def __init__(self, config):
super().__init__(config)
self.feature_dim = 2 if self.config["add_constant"] else 1
self.type = "uncalibratedscore"
params = config["uncalibrated_params"]
self.bound = params["bound"]
self.width = params["width"]
self.height = params["height"]
self.shift = params["shift"]
self.thresh = None
self.is_1d = True
def set_threshold_get_cost(self, threshold):
"""Find the cost corresponding to a given score threshold."""
self.thresh = float(threshold)
x = np.linspace(-self.bound, self.bound, 300)
return utils.find_x_for_y(self.pdf(x), x, threshold)
def pdf(self, x):
"""Get the probability of repayment."""
num = (
np.tan(x)
+ np.tan(self.bound)
+ self.height
* np.exp(-self.width * (x - self.bound - self.shift) ** 4)
)
den = 2 * np.tan(self.bound) + self.height
return num / den
def sample_features(self, n, **kwargs):
if self.config["protected_fraction"] is not None:
s = (
np.random.rand(n, 1) < self.config["protected_fraction"]
).astype(int)
shifts = s - 0.5
x = truncnorm.rvs(
-self.bound + shifts, self.bound + shifts, loc=-shifts
).reshape(-1, 1)
else:
s = np.full(n, np.nan)
x = truncnorm.rvs(-self.bound, self.bound, size=n).reshape(-1, 1)
if self.config["protected_as_feature"]:
x = np.concatenate((x, s.reshape(-1, 1)), axis=1)
if self.config["add_constant"]:
x = np.hstack([np.ones([n, 1]), x])
return x, s.ravel()
def sample_labels(self, x, s, yproba=False):
if x.shape[1] == 2:
yprob = self.pdf(x[:, 1])
else:
yprob = self.pdf(x)
y = np.random.binomial(1, yprob)
if yproba:
return y, yprob
return y
def threshold(self, cost):
"""The threshold for this policy."""
assert self.thresh is not None, "Need to set threshold first"
return self.thresh
# endregion
# -------------------------------------------------------------------------
# region Real data resampling distribution
# -------------------------------------------------------------------------
class ResamplingDistribution(BaseDistribution):
"""Resample from a finite dataset."""
def __init__(self, config):
super().__init__(config)
self.type = "resampling"
self.feature_dim = None
self._load_data()
def _load_data(self):
"""Load the specified dataset."""
test_size = self.config["test_size"]
x, y, s = get_data(self.config)
x, xte, y, yte, s, ste = train_test_split(x, y, s, test_size=test_size)
self.x, self.y, self.s = x, y, s
self.xtest, self.ytest, self.stest = xte, yte, ste
self.n_examples = self.x.shape[0]
self.allindices = np.arange(self.n_examples)
self.n_group = {}
self.indices_group = {}
if self.config["add_constant"]:
self.x = np.hstack([np.ones([self.n_examples, 1]), self.x])
self.xtest = np.hstack(
[np.ones([self.xtest.shape[0], 1]), self.xtest]
)
self.feature_dim = self.x.shape[1]
def sample_features(self, n, **kwargs):
raise NotImplementedError("Only use `sample_all` for Resampling.")
def sample_labels(self, x, **kwargs):
raise NotImplementedError("Only use `sample_all` for Resampling.")
def sample_all(self, n, yproba=False):
assert not yproba, "Cannot compute probabilities for real data."
n = min(self.n_examples, n)
indices = np.random.choice(self.allindices, n, replace=True)
return self.x[indices], self.y[indices], self.s[indices]
def sample_by_group(self, n, group=None):
group = int(group)
if group not in self.indices_group:
self.indices_group[group] = self.allindices[self.s == group]
self.n_group[group] = len(self.indices_group[group])
n = min(self.n_group[group], n)
indices = np.random.choice(self.indices_group[group], n, replace=True)
return self.x[indices], self.y[indices]
def get_test(self, n=None):
return self.xtest, self.ytest, self.stest
# endregion