-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmanifold_gp.py
390 lines (312 loc) · 13.4 KB
/
manifold_gp.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
import george
from george import kernels
from scipy.optimize import minimize
from matplotlib import pyplot as plt
import numpy as np
from idrtools import math
def _build_george_gp(coordinates, target_value_uncertainties, parameters):
"""Build a george Gaussian Process object and kernels."""
total_target_value_uncertainties = np.sqrt(
target_value_uncertainties**2
+ parameters[0]**2 * np.ones(len(coordinates))
)
ndim = coordinates.shape[-1]
use_dim = list(range(ndim))
kernel = parameters[1]**2 * kernels.Matern32Kernel(
[parameters[2]**2] * len(use_dim), ndim=ndim, axes=use_dim
)
gp = george.GP(kernel)
gp.compute(coordinates, total_target_value_uncertainties)
return gp
class ManifoldGaussianProcess():
"""Class to build and evaluate a Gaussian Process over a given manifold."""
def __init__(self, analysis, coordinates, target_values, target_value_uncertainties,
covariates=None, mask=None, parameters=None):
self.analysis = analysis
self.coordinates = coordinates
self.target_values = target_values
self.target_value_uncertainties = target_value_uncertainties
if mask is None:
self.mask = np.ones(len(target_values), dtype=bool)
else:
self.mask = mask
# If we have additional covariates, parse them into a nice form.
if covariates is None:
self.covariates = covariates
else:
self.covariates = np.atleast_2d(covariates)
# Estimate the fit parameters if none were given.
if parameters is None:
self.parameters = self._get_default_parameters()
else:
self.parameters = parameters
def _get_default_parameters(self):
"""Return a default set of parameters to use for fits."""
default_parameters = [
0.1, # intrinsic dispersion
0.2, # kernel amplitude
3., # kernel length scale
0., # zeropoint offset
]
# Slopes for each covariate
if self.covariates is not None:
default_parameters += [0.] * self.covariates.shape[0]
return default_parameters
@property
def parameter_names(self):
parameter_names = [
'intrinsic_dispersion',
'gp_kernel_amplitude',
'gp_length_scale',
'offset',
]
if self.covariates is not None:
for i in range(self.covariates.shape[0]):
parameter_names.append(f'covariate_slope_{i}')
return parameter_names
@property
def parameter_bounds(self):
parameter_bounds = [
(1e-5, None),
(1e-5, None),
(0.1, None),
(None, None),
]
if self.covariates is not None:
for i in range(self.covariates.shape[0]):
parameter_bounds.append((None, None))
return parameter_bounds
@property
def parameter_dict(self):
return dict(zip(self.parameter_names, self.parameters))
def _parse_parameters(self, parameters):
"""Parse the parameters list, and return the GP parameters, offset and
covariate slopes separately
"""
gp_parameters = parameters[:3]
offset = parameters[3]
covariate_slopes = parameters[4:]
return gp_parameters, offset, covariate_slopes
def negative_log_likelihood(self, parameters=None):
"""Calculate the negative log likelihood for this Gaussian Process"""
if parameters is None:
parameters = self.parameters
gp_parameters, offset, covariate_slopes = self._parse_parameters(parameters)
gp = _build_george_gp(
self.coordinates[self.mask],
self.target_value_uncertainties[self.mask],
gp_parameters
)
# Calculate the covariate model for the conditioning dataset.
model = offset
if len(covariate_slopes) > 0:
model = offset + self.covariates.T.dot(covariate_slopes)
condition_residuals = (self.target_values - model)[self.mask]
result = -gp.log_likelihood(condition_residuals)
return result
def fit(self, cov=True, verbosity=1, options={}, **kwargs):
"""Fit the parameters to the given dataset"""
initial_parameters = self.parameters
if cov:
# Need a very accurate minimum to be able to measure the covariance since we
# are using finite differences.
use_options = {
'ftol': 1e-14
}
else:
use_options = {}
use_options.update(options)
# For some reason BFGS has some convergence issues with default parameters.
# Using a small value for ftol fixes this.
result = minimize(
self.negative_log_likelihood,
initial_parameters,
bounds=self.parameter_bounds,
options=use_options,
**kwargs,
)
self.fit_result = result
self.parameters = result.x
if cov:
# Calculate the parameter covariance using a custom code that numerically
# estimates the Hessian using a finite difference method with adaptive step
# sizes.
param_cov = math.calculate_covariance_finite_difference(
self.negative_log_likelihood,
self.parameter_names,
self.parameters,
self.parameter_bounds,
verbose=verbosity >= 3,
allow_no_effect=True,
)
self.parameter_covariance = param_cov
self.parameter_uncertainties = np.sqrt(np.diag(param_cov))
else:
self.parameter_covariance = np.nan * np.ones((len(self.parameters),
len(self.parameters)))
self.parameter_uncertainties = np.nan * np.ones(len(self.parameters))
# Calculate the residuals for out-of-sample predictions.
predictions, prediction_uncertainties = self.predict_out_of_sample()
self.residuals = self.target_values - predictions
self.raw_residual_uncertainties = np.sqrt(
self.target_value_uncertainties**2
+ prediction_uncertainties**2
)
self.residual_uncertainties = np.sqrt(
self.raw_residual_uncertainties**2
+ self.parameter_dict['intrinsic_dispersion']**2
)
if verbosity >= 1:
print("GP magnitude residuals fit:")
print(f" Fit result: {result['message']}")
for parameter_name, value, uncertainty in \
zip(self.parameter_names, self.parameters,
self.parameter_uncertainties):
if cov:
print(f" {parameter_name:25s} {value:.3f} ± {uncertainty:.3f}")
else:
print(f" {parameter_name:25s} {value:.3f}")
# Calculate statistics
good_residuals = self.residuals[self.mask]
nmad = math.nmad(good_residuals)
std = np.std(good_residuals, ddof=1)
print(f" {'Fit NMAD':25s} {nmad:.3f} mag")
print(f" {'Fit std':25s} {std:.3f} mag")
def _calculate_covariate_model(self, prediction_covariates=None, parameters=None):
"""Calculate the model with a given set of covariates"""
if parameters is None:
parameters = self.parameters
gp_parameters, offset, covariate_slopes = self._parse_parameters(parameters)
# Add in zeropoint offset
model = offset
# Add in covariates.
if prediction_covariates is not None:
prediction_covariates = np.atleast_2d(prediction_covariates)
model += prediction_covariates.T.dot(covariate_slopes)
return model
def predict(self, prediction_coordinates, prediction_covariates=None,
parameters=None, mask=None, return_uncertainties=True):
"""Predict a Gaussian Process on the given data."""
if parameters is None:
parameters = self.parameters
if mask is None:
mask = np.ones(len(self.target_values), dtype=bool)
if self.mask is not None:
mask = mask & self.mask
gp_parameters, offset, covariate_slopes = self._parse_parameters(parameters)
gp = _build_george_gp(
self.coordinates[mask],
self.target_value_uncertainties[mask],
gp_parameters
)
# Calculate the covariate model for the conditioning dataset, and subtract it
# out to get the model residuals.
model = self._calculate_covariate_model(self.covariates, parameters)
condition_residuals = (self.target_values - model)[mask]
predictions = gp.predict(
condition_residuals,
np.atleast_2d(prediction_coordinates),
return_cov=False,
return_var=return_uncertainties,
)
if return_uncertainties:
predictions, prediction_variances = predictions
prediction_uncertainties = np.sqrt(prediction_variances)
# Add the covariate model back in to the predictions.
prediction_model = self._calculate_covariate_model(prediction_covariates,
parameters)
predictions += prediction_model
if return_uncertainties:
return predictions, prediction_uncertainties
else:
return predictions
def predict_out_of_sample(self):
"""Do out-of-sample Gaussian Process predictions.
For data that was used to condition the GP, we evaluate the GP prediction at
each location using all entries other than the specific data point at that
location. Predictions for the rest of the sample are done using the full
conditioning sample.
"""
predictions = np.zeros(len(self.target_values))
prediction_uncertainties = np.zeros(len(self.target_values))
# Do out-of-sample predictions for data in the conditioning sample.
locs = np.where(self.mask)[0]
for loc in locs:
oos_mask = np.zeros(len(self.target_values), dtype=bool)
oos_mask[loc] = True
if self.covariates is not None:
use_covariates = self.covariates[:, oos_mask]
else:
use_covariates = None
prediction, prediction_uncertainty = self.predict(
self.coordinates[oos_mask],
use_covariates,
mask=~oos_mask,
return_uncertainties=True,
)
predictions[oos_mask] = prediction
prediction_uncertainties[oos_mask] = prediction_uncertainty
if self.covariates is not None:
use_covariates = self.covariates[:, ~self.mask]
else:
use_covariates = None
other_predictions, other_prediction_uncertainties = self.predict(
self.coordinates[~self.mask],
use_covariates,
)
predictions[~self.mask] = other_predictions
prediction_uncertainties[~self.mask] = other_prediction_uncertainties
return predictions, prediction_uncertainties
def plot(self, axis_1=0, axis_2=1, axis_3=2, vmin=-0.3, vmax=0.3, num_points=200,
edgecolors='0.3', **kwargs):
"""Plot the GP predictions over the parameter space"""
# We want to only show the residuals, so we need to take out the covariates and
# zeropoint offset.
covariate_model = self._calculate_covariate_model(self.covariates)
residuals = self.target_values - covariate_model
# Set the median value of the residuals to 0 for the plot.
zeropoint = np.median(residuals)
residuals -= zeropoint
ax12, ax13, ax32 = self.analysis.scatter_combined(
residuals,
mask=self.mask,
label='Magnitude residuals',
axis_1=axis_1,
axis_2=axis_2,
axis_3=axis_3,
vmin=vmin,
vmax=vmax,
invert_colorbar=True,
edgecolors=edgecolors,
**kwargs
)
subplot_datas = [
(ax12, axis_1, axis_2),
(ax13, axis_1, axis_3),
(ax32, axis_3, axis_2),
]
for ax, axis_x, axis_y in subplot_datas:
min_x, max_x = ax.get_xlim()
min_y, max_y = ax.get_ylim()
plot_x, plot_y = np.meshgrid(
np.linspace(min_x, max_x, num_points),
np.linspace(min_y, max_y, num_points)
)
flat_plot_x = plot_x.flatten()
flat_plot_y = plot_y.flatten()
plot_coords = np.zeros((len(flat_plot_x), self.coordinates.shape[1]))
plot_coords[:, axis_x] = flat_plot_x
plot_coords[:, axis_y] = flat_plot_y
# Predict the GP residuals over the manifold without covariates.
predictions = self.predict(plot_coords, return_uncertainties=False)
predictions -= self.parameter_dict['offset']
predictions -= zeropoint
predictions = predictions.reshape(plot_x.shape)
ax.imshow(
predictions[::-1],
extent=(min_x, max_x, min_y, max_y),
cmap=plt.cm.coolwarm.reversed(),
vmin=vmin,
vmax=vmax,
aspect="auto",
)