-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fair_logit_estimator.py
55 lines (49 loc) · 2.63 KB
/
test_fair_logit_estimator.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
# pylint: disable=missing-docstring
# pylint: disable=no-self-use
# pylint: disable=invalid-name
import numpy as np
from fair_logit_estimator import _get_fairness_constraint
class TestFairnessConstraint:
def test_perfect_correlation(self):
sensitive_x = np.array([0, 1, 0, 0, 1, 0, 1, 0, 0, 1])
unsensitive_x = np.array([[x, 0] for x in sensitive_x])
w = np.array([1, 0])
constraint = _get_fairness_constraint(unsensitive_x, sensitive_x, 0)
constraint_fn = constraint['fun']
assert constraint_fn(w, unsensitive_x, sensitive_x, 0) == -1
def test_perfect_correlation_scaled(self):
sensitive_x = 100*np.array([0, 1, 0, 0, 1, 0, 1, 0, 0, 1])
unsensitive_x = np.array([[x, 0] for x in sensitive_x])
w = np.array([1, 0])
constraint = _get_fairness_constraint(unsensitive_x, sensitive_x, 0)
constraint_fn = constraint['fun']
assert np.isclose(constraint_fn(w, unsensitive_x, sensitive_x, 0), -1,
atol=1e-10, rtol=1e-10)
def test_perfect_anticorrelation(self):
sensitive_x = np.array([0, 1, 0, 0, 1, 0, 1, 0, 0, 1])
unsensitive_x = np.array([[x, 0] for x in sensitive_x])
w = np.array([-1, 0])
constraint = _get_fairness_constraint(unsensitive_x, sensitive_x, 0)
constraint_fn = constraint['fun']
assert constraint_fn(w, unsensitive_x, sensitive_x, 0) == -1
def test_zero_correlation(self):
num_samples = 100000
sensitive_x = np.array([np.random.random() for _ in range(num_samples)])
unsensitive_x = np.array([[np.random.random(), np.random.random()]
for _ in range(num_samples)])
w = np.array([1, 1])
constraint = _get_fairness_constraint(unsensitive_x, sensitive_x, 0)
constraint_fn = constraint['fun']
assert np.isclose(constraint_fn(w, unsensitive_x, sensitive_x, 0), 0, atol=0.01)
def test_zero_correlation_different_scales(self):
num_samples = 100000
sensitive_x = np.array([np.random.random() for _ in range(num_samples)])
unsensitive_x = np.array([[np.random.random(), np.random.random()]
for _ in range(num_samples)])
w = np.array([1, 1])
constraint = _get_fairness_constraint(unsensitive_x, sensitive_x, 0)
constraint_fn = constraint['fun']
small_x_correlation = constraint_fn(w, unsensitive_x, sensitive_x, 0)
large_x_correlation = constraint_fn(w, 100*unsensitive_x, 100*sensitive_x, 0)
assert np.isclose(small_x_correlation, large_x_correlation,
atol=1e-10, rtol=1e-10)