forked from nikikilbertus/fair-decisions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policies.py
251 lines (197 loc) · 7.62 KB
/
policies.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
"""
This module contains different policies for making decisions.
"""
from copy import deepcopy
import numpy as np
import featuremaps
import truedistribution
import utils
# -------------------------------------------------------------------------
# region (Abstract) Base Policy
# -------------------------------------------------------------------------
class BasePolicy:
"""Base class for policies."""
def __init__(self, init=None, cost=None, featuremap=None, config=None):
"""Initialize a policy.
Args:
init: Indicator how to initialize the policy.
cost: The cost factor of the utility.
featuremap: A featuremap to apply to the inputs (not needed if init
is an instance of a BasePolicy.
config: Configuration dictionary.
"""
self.theta = None
self.cost = cost
self.fm = featuremap
self.config = deepcopy(config)
self.init = init
if self.fm is None:
self.fm = featuremaps.FeatureMapIdentity()
if init is not None:
if isinstance(self.init, np.ndarray):
self.theta = self.init.copy()
elif isinstance(self.init, BasePolicy):
self.theta = self.init.theta.copy()
self.cost = self.init.cost
self.fm = self.init.fm
self.config = deepcopy(self.init.config)
else:
self._init_theta()
def _init_theta(self):
"""Initialize theta."""
if self.init == "normal":
self.theta = np.random.randn(self.fm.n_components)
elif self.init == "uniform":
self.theta = np.random.rand(self.fm.n_components)
elif self.init in "zeros":
self.theta = np.zeros(self.fm.n_components)
else:
raise RuntimeError(f"Unknown initialization {self.init}.")
def set_theta(self, theta):
"""
Set the weight vector theta.
Args:
theta: np.ndarray, the weight vector theta
"""
self.theta = theta
self.fm.n_components = len(theta)
def sample(self, x):
"""
Sample decisions for given inputs.
Args:
x: The inputs for which to sample (binary) decisions (np.ndarray).
Returns:
d: A binary (0/1) vector np.ndarray of length x.shape[0]
"""
raise NotImplementedError("Subclass must override sample(x).")
def copy(self):
"""Create and return a copy."""
raise NotImplementedError("Subclass must override copy().")
# endregion
# -------------------------------------------------------------------------
# region Stochastic Logistic Policy (with variations)
# -------------------------------------------------------------------------
class LogisticPolicy(BasePolicy):
"""A policy based on generalized logistic regression."""
def __init__(self, init, cost=None, featuremap=None, config=None):
"""Initialize a logistic policy."""
super().__init__(init, cost, featuremap, config)
self.keep_positive = self.config["keep_positive"]
self.type = "semi_logistic" if self.keep_positive else "logistic"
def sample(self, x):
"""
Sample decisions for given inputs.
Args:
x: The inputs for which to sample (binary) decisions (np.ndarray).
Returns:
d: A binary (0/1) vector np.ndarray of length x.shape[0]
"""
if self.theta is None:
self.fm.fit(x)
self._init_theta()
yprob = utils.sigmoid(np.matmul(self.fm(x), self.theta))
d = np.round(yprob)
explore = np.ones(len(yprob)).astype(bool)
if self.keep_positive:
explore &= yprob < 0.5
d[explore] = np.random.binomial(1, yprob[explore])
return d.astype(float)
def copy(self):
return LogisticPolicy(self)
# endregion
# -------------------------------------------------------------------------
# region Deterministic Logistic Policy
# -------------------------------------------------------------------------
class DeterministicThreshold(BasePolicy):
"""A deterministic threshold policy."""
def __init__(self, init, cost, featuremap=None):
"""Initialize a logistic policy."""
super().__init__(init, cost, featuremap)
self.type = "deterministic_threshold"
def sample(self, x):
"""
Compute decisions for given inputs.
Args:
x: The inputs for which to sample (binary) decisions (np.ndarray).
Returns:
d: A binary (0/1) vector np.ndarray of length x.shape[0]
"""
if self.theta is None:
self.fm.fit(x)
self._init_theta()
return (
utils.sigmoid(np.matmul(self.fm(x), self.theta)) > self.cost
).astype(float)
def set_rule(self, func):
"""Override the sample function."""
self.sample = func
self.theta = None
def set_threshold(self, thresh):
"""Override the sample function by a threshold."""
self.sample = lambda x: (x[:, 1] > thresh).astype(float)
self.theta = None
def copy(self):
return DeterministicThreshold(self, self.cost)
# endregion
# -------------------------------------------------------------------------
# region Bernoulli (fully randomized) Policy
# -------------------------------------------------------------------------
class Bernoulli(BasePolicy):
"""A fully randomized fair coin flip policy."""
def __init__(self):
"""Initialize a logistic policy."""
super().__init__()
self.type = "bernoulli"
self.theta = None
def sample(self, x):
"""
Compute decisions for given inputs.
Args:
x: The inputs for which to sample (binary) decisions (np.ndarray).
Returns:
d: A binary (0/1) vector np.ndarray of length x.shape[0]
"""
return np.random.randint(0, 2, x.shape[0])
def copy(self):
return self
# endregion
# -------------------------------------------------------------------------
# region Static helper functions and global variables
# -------------------------------------------------------------------------
def get_optimal_policy(opt, cost, featuremap=None):
"""
Compute the optimal policy either from a threshold, a weight vector,
or examples.
This is a bit dirty. Basically we need to figure out whether we can compute
the optimal deterministic policy for the given distribution analytically.
Therefore we do a whole lot of checks for the distribution to figure this
out. Could be done better.
Args:
opt: Can be a single float (threshold) an np.ndarray of 2 elements
(given theta) or a tuple (x,y) with datapoints x, y.
cost: The cost factor in the utility.
featuremap: The featuremap to be used.
Returns:
The optimal `DeterministicThreshold` policy.
"""
# if we can find the optimal one it is going to be deterministic
pi = DeterministicThreshold("zeros", cost, featuremap)
# threshold
if isinstance(opt, float):
pi.set_threshold(opt)
elif isinstance(opt, truedistribution.BaseDistribution):
if hasattr(opt, "threshold"):
pi = get_optimal_policy(opt.threshold(cost), cost, featuremap=None)
else:
if opt.is_1d:
pi.set_rule(
lambda x: (
opt.sample_labels(x, None, yproba=True)[1] > cost
).astype(float)
)
else:
pi = None
else:
pi = None
return pi
# endregion