-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutility.py
139 lines (94 loc) · 3.6 KB
/
utility.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
#!/usr/bin/env python3
import numpy as np
import inference
import robust
# for parallel processing
from joblib import Parallel, delayed
import multiprocessing
class Utility:
"""
Base class to implement a utility function.
"""
def __init__(self, prior_samples, weights, simobj):
"""
prior_samples: samples from the prior distribution
weights: a weight for each prior sample
simobj: simulator object
"""
self.prior_samples = prior_samples
self.weights = weights
self.simobj = simobj
class MutualInformation(Utility):
def __init__(self, prior_samples, weights, simobj, evalmethod='lfire'):
"""
prior_samples: samples from the prior distribution
weights: a weight for each prior sample
simobj: simulator object
type: type of mutual information implementation
"""
super(MutualInformation, self).__init__(prior_samples, weights, simobj)
self.evalmethod = evalmethod
def _mean_eval(self, U):
"""
U: array of utilities
"""
return np.mean(U)
def _median_eval(self, U):
"""
U: array of utilities
"""
return np.median(U)
def _robust_eval(self, U):
"""
U: array of utilities
"""
return robust.MEstimator(U)
def compute(self, d, numsamp=10000, evaltype='robust', verbose=True):
"""
Compute mutual information given the evaluation method (default is 'lfire').
d: design variable
evaltype: type of Monte-Carlo evaluation; 'mean', 'median' or 'robust'
"""
# GPyOpt wraps the design point in a weird double array // hacky fix
d = d[0]
if verbose:
print('Design point: ', d)
if self.evalmethod=='lfire':
# Define LFIRE object
infobj = inference.LFIRE(d, self.prior_samples, self.weights, self.simobj)
# compute the LFIRE ratios for 'numsamp' prior samples, where some may be repeated; set to default 10000 for now.
utils, _ = infobj.ratios(numsamp=numsamp)
self.utils = np.array(utils)
# self.coefs = coefs
else:
raise NotImplementedError()
if evaltype=='mean':
mutualinfo = self._mean_eval(self.utils)
elif evaltype=='median':
mutualinfo = self._median_eval(self.utils)
elif evaltype=='robust':
mutualinfo = self._robust_eval(self.utils)
else:
raise NotImplementedError()
return mutualinfo
def compute_final(self, d_opt, y_obs, num_cores=1):
"""
Final likelihood-free inference for the optimal design.
d_opt: optimal design
"""
if self.evalmethod=='lfire':
# Define LFIRE object
infobj = inference.LFIRE(d_opt, self.prior_samples, self.weights, self.simobj)
# Take summary statistics of observed data
if len(y_obs.shape) > 2:
psi_obs = self.simobj.summary(y_obs)
else:
psi_obs = self.simobj.summary(y_obs.reshape(1, -1))
# Compute coefficients for each prior sample
tmp_bl = Parallel(n_jobs=int(num_cores))(delayed(infobj._logistic_regression)(p) for p in self.prior_samples)
self.b_obs = np.array(tmp_bl)
# Compute ratios for each coefficient
self.r_obs = np.array([np.exp(psi_obs.reshape(1, -1) @ b[1:] + b[0])[0][0] for b in self.b_obs])
return self.r_obs, self.b_obs
else:
raise NotImplementedError()