-
Notifications
You must be signed in to change notification settings - Fork 127
/
gpy_quadrature_wrappers.py
718 lines (571 loc) · 30.4 KB
/
gpy_quadrature_wrappers.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
"""GPy wrappers for the quadrature package."""
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import warnings
from typing import List, Optional, Tuple, Union
import GPy
import numpy as np
from scipy.linalg import lapack
from ..quadrature.interfaces import (
IRBF,
IBaseGaussianProcess,
IBrownian,
IProductBrownian,
IProductMatern12,
IProductMatern32,
IProductMatern52,
)
from ..quadrature.kernels import (
QuadratureBrownianLebesgueMeasure,
QuadratureKernel,
QuadratureProductBrownian,
QuadratureProductBrownianLebesgueMeasure,
QuadratureProductMatern12LebesgueMeasure,
QuadratureProductMatern32LebesgueMeasure,
QuadratureProductMatern52LebesgueMeasure,
QuadratureRBFGaussianMeasure,
QuadratureRBFLebesgueMeasure,
)
from ..quadrature.measures import BoxDomain, GaussianMeasure, IntegrationMeasure, LebesgueMeasure
from ..quadrature.typing import BoundsType
class BaseGaussianProcessGPy(IBaseGaussianProcess):
"""Wrapper for GPy's :class:`GPRegression` as required for some EmuKit quadrature methods.
An instance of this class can be passed as :attr:`base_gp` to a :class:`WarpedBayesianQuadratureModel` object.
.. note::
GPy's :class:`GPRegression` cannot take ``None`` as initial values for X and Y. Thus, we initialize
them with some arbitrary values. These will be re-set in the :class:`WarpedBayesianQuadratureModel`.
:param kern: An EmuKit quadrature kernel.
:param gpy_model: A GPy GP regression model.
:param noise_free: If ``False``, the observation noise variance will be treated as a model parameter,
if ``True`` the noise is set to 1e-10, defaults to ``True``.
"""
def __init__(self, kern: QuadratureKernel, gpy_model: GPy.models.GPRegression, noise_free: bool = True):
super().__init__(kern=kern)
if noise_free:
gpy_model.Gaussian_noise.constrain_fixed(1.0e-10)
self.gpy_model = gpy_model
if isinstance(kern, QuadratureProductBrownian):
if kern.offset != 0:
raise ValueError(
"The wrapper BaseGaussianProcessGPy does not support EmuKit product Brownian "
"motion kernels with non-zero offset as these are not supported in GPy."
)
@property
def X(self) -> np.ndarray:
"""The data nodes."""
return self.gpy_model.X
@property
def Y(self) -> np.ndarray:
"""The data evaluations at the nodes."""
return self.gpy_model.Y
@property
def observation_noise_variance(self) -> float:
return self.gpy_model.Gaussian_noise[0]
def set_data(self, X: np.ndarray, Y: np.ndarray) -> None:
"""Sets training data in model.
:param X: New training features, shape (num_points, input_dim).
:param Y: New training outputs, shape (num_points, 1).
"""
self.gpy_model.set_XY(X, Y)
def predict(self, X_pred: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""Predictive mean and covariance at the locations X_pred.
:param X_pred: Points at which to predict, with shape (number of points, input_dim).
:return: Predictive mean, predictive variances shapes (num_points, 1) and (num_points, 1).
"""
return self.gpy_model.predict(X_pred, full_cov=False)
def predict_with_full_covariance(self, X_pred: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""Predictive mean and covariance at the locations X_pred.
:param X_pred: Points at which to predict, with shape (num_points, input_dim).
:return: Predictive mean, predictive full covariance shapes (num_points, 1) and (num_points, num_points).
"""
return self.gpy_model.predict(X_pred, full_cov=True)
def solve_linear(self, z: np.ndarray) -> np.ndarray:
lower_chol = self.gpy_model.posterior.woodbury_chol
return lapack.dtrtrs(lower_chol.T, (lapack.dtrtrs(lower_chol, z, lower=1)[0]), lower=0)[0]
def graminv_residual(self) -> np.ndarray:
return self.gpy_model.posterior.woodbury_vector
def optimize(self) -> None:
"""Optimize the hyperparameters of the GP."""
self.gpy_model.optimize()
class RBFGPy(IRBF):
r"""Wrapper of the GPy RBF kernel as required for some EmuKit quadrature methods.
.. math::
k(x, x') = \sigma^2 e^{-\frac{1}{2}\sum_{i=1}^{d}r_i^2},
where :math:`d` is the input dimensionality,
:math:`r_i = \frac{x_i-x_i'}{\lambda_i}` is the scaled vector difference of dimension :math:`i`,
:math:`\lambda_i` is the :math:`i` th element of the ``lengthscales`` property
and :math:`\sigma^2` is the ``variance`` property.
:param gpy_rbf: An RBF kernel from GPy.
"""
def __init__(self, gpy_rbf: GPy.kern.RBF):
self.gpy_rbf = gpy_rbf
@property
def lengthscales(self) -> np.ndarray:
if self.gpy_rbf.ARD:
return self.gpy_rbf.lengthscale.values
return np.full((self.gpy_rbf.input_dim,), self.gpy_rbf.lengthscale[0])
@property
def variance(self) -> float:
return self.gpy_rbf.variance.values[0]
def K(self, x1: np.ndarray, x2: np.ndarray) -> np.ndarray:
return self.gpy_rbf.K(x1, x2)
class ProductMatern12GPy(IProductMatern12):
r"""Wrapper of the GPy Exponential (a.k.a Matern12) product kernel as required for some
EmuKit quadrature methods.
The product kernel is of the form
:math:`k(x, x') = \sigma^2 \prod_{i=1}^d k_i(x, x')` where
.. math::
k_i(x, x') = e^{-r_i}.
:math:`d` is the input dimensionality,
:math:`r_i:=\frac{|x_i - x'_i|}{\lambda_i}`,
:math:`\sigma^2` is the ``variance`` property and :math:`\lambda_i` is the :math:`i` th element
of the ``lengthscales`` property.
:param gpy_matern: An Exponential (a.k.a. Matern12) product kernel from GPy. For :math:`d=1` this is equivalent
to an Exponential kernel. For :math:`d>1`, this is *not* a :math:`d`-dimensional
Exponential kernel but a product of :math:`d` 1-dimensional Exponential kernels with differing
active dimensions constructed as k1 * k2 * ... .
Make sure to unlink all variances except the variance of the first kernel k1 in the product
as the variance of k1 will be used to represent :math:`\sigma^2`. If you are unsure what
to do, use the :attr:`lengthscales` and :attr:`variance` parameter instead.
If :attr:`gpy_matern` is not given, the :attr:`lengthscales` argument is used.
:param lengthscales: If :attr:`gpy_matern` is not given, a product Matern12 kernel will be constructed with
the given lengthscales. The number of elements need to be equal to the dimensionality
:math:`d`. If :attr:`gpy_matern` is given, this input is disregarded.
:param variance: The variance of the product kernel. Only used if :attr:`gpy_matern` is not given. Defaults to 1.
"""
def __init__(
self,
gpy_matern: Optional[Union[GPy.kern.Exponential, GPy.kern.Prod]] = None,
lengthscales: Optional[np.ndarray] = None,
variance: Optional[float] = None,
):
if gpy_matern is None and lengthscales is None:
raise ValueError("Either lengthscales or a GPy product matern kernel must be given.")
# product kernel from parameters
if gpy_matern is None:
input_dim = len(lengthscales)
if input_dim < 1:
raise ValueError("'lengthscales' must contain at least 1 value.")
# default variance
if variance is None:
variance = 1.0
gpy_matern = GPy.kern.Exponential(
input_dim=1, active_dims=[0], lengthscale=lengthscales[0], variance=variance
)
for dim in range(1, input_dim):
k = GPy.kern.Exponential(input_dim=1, active_dims=[dim], lengthscale=lengthscales[dim])
k.unlink_parameter(k.variance)
gpy_matern = gpy_matern * k
self.gpy_matern = gpy_matern
@property
def lengthscales(self) -> np.ndarray:
if isinstance(self.gpy_matern, GPy.kern.Exponential):
return np.array([self.gpy_matern.lengthscale[0]])
lengthscales = []
for kern in self.gpy_matern.parameters:
lengthscales.append(kern.lengthscale[0])
return np.array(lengthscales)
@property
def variance(self) -> float:
if isinstance(self.gpy_matern, GPy.kern.Exponential):
return self.gpy_matern.variance[0]
return self.gpy_matern.parameters[0].variance[0]
def K(self, x1: np.ndarray, x2: np.ndarray) -> np.ndarray:
return self.gpy_matern.K(x1, x2)
def _K_from_prod(self, x1: np.ndarray, x2: np.ndarray, skip: List[int] = None) -> np.ndarray:
"""The kernel k(x1, x2) evaluated at x1 and x2 computed as product from the
individual 1d kernels.
:param x1: First argument of the kernel, shape (n_points N, input_dim)
:param x2: Second argument of the kernel, shape (n_points M, input_dim)
:param skip: Skip these dimensions if specified.
:returns: Kernel evaluated at x1, x2, shape (N, M).
"""
if skip is None:
skip = []
K = np.ones([x1.shape[0], x2.shape[0]])
for dim, kern in enumerate(self.gpy_matern.parameters):
if dim in skip:
continue
K *= kern.K(x1, x2)
# correct for missing variance
if 0 in skip:
K *= self.variance
return K
def dK_dx1(self, x1: np.ndarray, x2: np.ndarray) -> np.ndarray:
if isinstance(self.gpy_matern, GPy.kern.Exponential):
return self._dK_dx1_1d(x1[:, 0], x2[:, 0], self.gpy_matern.lengthscale[0])[None, :, :]
# product kernel
dK_dx1 = np.ones([x1.shape[1], x1.shape[0], x2.shape[0]])
for dim, kern in enumerate(self.gpy_matern.parameters):
prod_term = self._K_from_prod(x1, x2, skip=[dim]) # N x M
grad_term = self._dK_dx1_1d(x1[:, dim], x2[:, dim], kern.lengthscale[0]) # N x M
dK_dx1[dim, :, :] *= prod_term * grad_term
return dK_dx1
class ProductMatern32GPy(IProductMatern32):
r"""Wrapper of the GPy Matern32 product kernel as required for some EmuKit quadrature methods.
The product kernel is of the form
:math:`k(x, x') = \sigma^2 \prod_{i=1}^d k_i(x, x')` where
.. math::
k_i(x, x') = (1 + \sqrt{3}r_i ) e^{-\sqrt{3} r_i}.
:math:`d` is the input dimensionality,
:math:`r_i:=\frac{|x_i - x'_i|}{\lambda_i}`,
:math:`\sigma^2` is the ``variance`` property and :math:`\lambda_i` is the :math:`i` th element
of the ``lengthscales`` property.
:param gpy_matern: A Matern32 product kernel from GPy. For :math:`d=1` this is equivalent to a
Matern32 kernel. For :math:`d>1`, this is *not* a :math:`d`-dimensional
Matern32 kernel but a product of :math:`d` 1-dimensional Matern32 kernels with differing
active dimensions constructed as k1 * k2 * ... .
Make sure to unlink all variances except the variance of the first kernel k1 in the product
as the variance of k1 will be used to represent :math:`\sigma^2`. If you are unsure what
to do, use the :attr:`lengthscales` and :attr:`variance` parameter instead.
If :attr:`gpy_matern` is not given, the :attr:`lengthscales` argument is used.
:param lengthscales: If :attr:`gpy_matern` is not given, a product Matern32 kernel will be constructed with
the given lengthscales. The number of elements need to be equal to the dimensionality
:math:`d`. If :attr:`gpy_matern` is given, this input is disregarded.
:param variance: The variance of the product kernel. Only used if :attr:`gpy_matern` is not given. Defaults to 1.
"""
def __init__(
self,
gpy_matern: Optional[Union[GPy.kern.Matern32, GPy.kern.Prod]] = None,
lengthscales: Optional[np.ndarray] = None,
variance: Optional[float] = None,
):
if gpy_matern is None and lengthscales is None:
raise ValueError("Either lengthscales or a GPy product matern kernel must be given.")
# product kernel from parameters
if gpy_matern is None:
input_dim = len(lengthscales)
if input_dim < 1:
raise ValueError("'lengthscales' must contain at least 1 value.")
# default variance
if variance is None:
variance = 1.0
gpy_matern = GPy.kern.Matern32(input_dim=1, active_dims=[0], lengthscale=lengthscales[0], variance=variance)
for dim in range(1, input_dim):
k = GPy.kern.Matern32(input_dim=1, active_dims=[dim], lengthscale=lengthscales[dim])
k.unlink_parameter(k.variance)
gpy_matern = gpy_matern * k
self.gpy_matern = gpy_matern
@property
def lengthscales(self) -> np.ndarray:
if isinstance(self.gpy_matern, GPy.kern.Matern32):
return np.array([self.gpy_matern.lengthscale[0]])
lengthscales = []
for kern in self.gpy_matern.parameters:
lengthscales.append(kern.lengthscale[0])
return np.array(lengthscales)
@property
def variance(self) -> float:
if isinstance(self.gpy_matern, GPy.kern.Matern32):
return self.gpy_matern.variance[0]
return self.gpy_matern.parameters[0].variance[0]
def K(self, x1: np.ndarray, x2: np.ndarray) -> np.ndarray:
return self.gpy_matern.K(x1, x2)
def _K_from_prod(self, x1: np.ndarray, x2: np.ndarray, skip: List[int] = None) -> np.ndarray:
"""The kernel k(x1, x2) evaluated at x1 and x2 computed as product from the
individual 1d kernels.
:param x1: First argument of the kernel, shape (n_points N, input_dim)
:param x2: Second argument of the kernel, shape (n_points M, input_dim)
:param skip: Skip these dimensions if specified.
:returns: Kernel evaluated at x1, x2, shape (N, M).
"""
if skip is None:
skip = []
K = np.ones([x1.shape[0], x2.shape[0]])
for dim, kern in enumerate(self.gpy_matern.parameters):
if dim in skip:
continue
K *= kern.K(x1, x2)
# correct for missing variance
if 0 in skip:
K *= self.variance
return K
def dK_dx1(self, x1: np.ndarray, x2: np.ndarray) -> np.ndarray:
if isinstance(self.gpy_matern, GPy.kern.Matern32):
return self._dK_dx1_1d(x1[:, 0], x2[:, 0], self.gpy_matern.lengthscale[0])[None, :, :]
# product kernel
dK_dx1 = np.ones([x1.shape[1], x1.shape[0], x2.shape[0]])
for dim, kern in enumerate(self.gpy_matern.parameters):
prod_term = self._K_from_prod(x1, x2, skip=[dim]) # N x M
grad_term = self._dK_dx1_1d(x1[:, dim], x2[:, dim], kern.lengthscale[0]) # N x M
dK_dx1[dim, :, :] *= prod_term * grad_term
return dK_dx1
class ProductMatern52GPy(IProductMatern52):
r"""Wrapper of the GPy Matern52 product kernel as required for some EmuKit quadrature methods.
The product kernel is of the form
:math:`k(x, x') = \sigma^2 \prod_{i=1}^d k_i(x, x')` where
.. math::
k_i(x, x') = (1 + \sqrt{5} r_i + \frac{5}{3} r_i^2) \exp(- \sqrt{5} r_i).
:math:`d` is the input dimensionality,
:math:`r_i:=\frac{|x_i - x'_i|}{\lambda_i}`,
:math:`\sigma^2` is the ``variance`` property and :math:`\lambda_i` is the :math:`i` th element
of the ``lengthscales`` property.
:param gpy_matern: A Matern52 product kernel from GPy. For :math:`d=1` this is equivalent to a
Matern52 kernel. For :math:`d>1`, this is *not* a :math:`d`-dimensional
Matern52 kernel but a product of :math:`d` 1-dimensional Matern52 kernels with differing
active dimensions constructed as k1 * k2 * ... .
Make sure to unlink all variances except the variance of the first kernel k1 in the product
as the variance of k1 will be used to represent :math:`\sigma^2`. If you are unsure what
to do, use the :attr:`lengthscales` and :attr:`variance` parameter instead.
If :attr:`gpy_matern` is not given, the :attr:`lengthscales` argument is used.
:param lengthscales: If :attr:`gpy_matern` is not given, a product Matern52 kernel will be constructed with
the given lengthscales. The number of elements need to be equal to the dimensionality
:math:`d`. If :attr:`gpy_matern` is given, this input is disregarded.
:param variance: The variance of the product kernel. Only used if :attr:`gpy_matern` is not given. Defaults to 1.
"""
def __init__(
self,
gpy_matern: Optional[Union[GPy.kern.Matern52, GPy.kern.Prod]] = None,
lengthscales: Optional[np.ndarray] = None,
variance: Optional[float] = None,
):
if gpy_matern is None and lengthscales is None:
raise ValueError("Either lengthscales or a GPy product matern kernel must be given.")
# product kernel from parameters
if gpy_matern is None:
input_dim = len(lengthscales)
if input_dim < 1:
raise ValueError("'lengthscales' must contain at least 1 value.")
# default variance
if variance is None:
variance = 1.0
gpy_matern = GPy.kern.Matern52(input_dim=1, active_dims=[0], lengthscale=lengthscales[0], variance=variance)
for dim in range(1, input_dim):
k = GPy.kern.Matern52(input_dim=1, active_dims=[dim], lengthscale=lengthscales[dim])
k.unlink_parameter(k.variance)
gpy_matern = gpy_matern * k
self.gpy_matern = gpy_matern
@property
def lengthscales(self) -> np.ndarray:
if isinstance(self.gpy_matern, GPy.kern.Matern52):
return np.array([self.gpy_matern.lengthscale[0]])
lengthscales = []
for kern in self.gpy_matern.parameters:
lengthscales.append(kern.lengthscale[0])
return np.array(lengthscales)
@property
def variance(self) -> float:
if isinstance(self.gpy_matern, GPy.kern.Matern52):
return self.gpy_matern.variance[0]
return self.gpy_matern.parameters[0].variance[0]
def K(self, x1: np.ndarray, x2: np.ndarray) -> np.ndarray:
return self.gpy_matern.K(x1, x2)
def _K_from_prod(self, x1: np.ndarray, x2: np.ndarray, skip: List[int] = None) -> np.ndarray:
"""The kernel k(x1, x2) evaluated at x1 and x2 computed as product from the
individual 1d kernels.
:param x1: First argument of the kernel, shape (n_points N, input_dim)
:param x2: Second argument of the kernel, shape (n_points M, input_dim)
:param skip: Skip these dimensions if specified.
:returns: Kernel evaluated at x1, x2, shape (N, M).
"""
if skip is None:
skip = []
K = np.ones([x1.shape[0], x2.shape[0]])
for dim, kern in enumerate(self.gpy_matern.parameters):
if dim in skip:
continue
K *= kern.K(x1, x2)
# correct for missing variance
if 0 in skip:
K *= self.variance
return K
def dK_dx1(self, x1: np.ndarray, x2: np.ndarray) -> np.ndarray:
if isinstance(self.gpy_matern, GPy.kern.Matern52):
return self._dK_dx1_1d(x1[:, 0], x2[:, 0], self.gpy_matern.lengthscale[0])[None, :, :]
# product kernel
dK_dx1 = np.ones([x1.shape[1], x1.shape[0], x2.shape[0]])
for dim, kern in enumerate(self.gpy_matern.parameters):
prod_term = self._K_from_prod(x1, x2, skip=[dim]) # N x M
grad_term = self._dK_dx1_1d(x1[:, dim], x2[:, dim], kern.lengthscale[0]) # N x M
dK_dx1[dim, :, :] *= prod_term * grad_term
return dK_dx1
class BrownianGPy(IBrownian):
r"""Wrapper of the GPy Brownian motion kernel as required for some EmuKit quadrature methods.
.. math::
k(x, x') = \sigma^2 \operatorname{min}(x, x')\quad\text{with}\quad x, x' \geq 0,
where :math:`\sigma^2` is the ``variance`` property.
:param gpy_brownian: A Brownian motion kernel from GPy.
"""
def __init__(self, gpy_brownian: GPy.kern.Brownian):
self.gpy_brownian = gpy_brownian
@property
def variance(self) -> float:
return self.gpy_brownian.variance[0]
def K(self, x1: np.ndarray, x2: np.ndarray) -> np.ndarray:
return self.gpy_brownian.K(x1, x2)
class ProductBrownianGPy(IProductBrownian):
r"""Wrapper of the GPy Brownian product kernel as required for some EmuKit quadrature methods.
The product kernel is of the form
:math:`k(x, x') = \sigma^2 \prod_{i=1}^d k_i(x, x')` where
.. math::
k_i(x, x') = \operatorname{min}(x_i-c, x_i'-c)\quad\text{with}\quad x_i, x_i' \geq c,
:math:`d` is the input dimensionality,
:math:`\sigma^2` is the ``variance`` property
and :math:`c` is the ``offset`` property.
:param gpy_brownian: A Brownian product kernel from GPy. For :math:`d=1` this is equivalent to a
Brownian kernel. For :math:`d>1`, this is a product of :math:`d` 1-dimensional Brownian
kernels with differing active dimensions constructed as k1 * k2 * ... .
Make sure to unlink all variances except the variance of the first kernel k1 in the product
as the variance of k1 will be used to represent :math:`\sigma^2`. If you are unsure what
to do, use the :attr:`input_dim` and :attr:`variance` parameter instead.
If :attr:`gpy_brownian` is not given, the :attr:`variance` and :attr:`input_dim`
argument is used.
:param offset: The offset :math:`c` of the kernel. Defaults to 0.
:param variance: The variance of the product kernel. Only used if :attr:`gpy_brownian` is not given. Defaults to 1.
:param input_dim: The input dimension. Only used if :attr:`gpy_brownian` is not given.
"""
def __init__(
self,
gpy_brownian: Optional[Union[GPy.kern.Brownian, GPy.kern.Prod]] = None,
offset: float = 0.0,
variance: Optional[float] = None,
input_dim: Optional[int] = None,
):
if gpy_brownian is not None:
if input_dim is not None or variance is not None:
warnings.warn("gpy_brownian and variance is given. The variance will be ignore.")
else:
if input_dim is None or variance is None:
raise ValueError(
"Please provide a GPy product Brownian kernel or alternitvely the the variance and input_dim."
)
# default variance
if variance is None:
variance = 1.0
# product kernel from parameters
if gpy_brownian is None:
gpy_brownian = GPy.kern.Brownian(input_dim=1, active_dims=[0], variance=variance)
for dim in range(1, input_dim):
k = GPy.kern.Brownian(input_dim=1, active_dims=[dim])
k.unlink_parameter(k.variance)
gpy_brownian = gpy_brownian * k
self.gpy_brownian = gpy_brownian
self._offset = offset
@property
def variance(self) -> float:
if isinstance(self.gpy_brownian, GPy.kern.Brownian):
return self.gpy_brownian.variance[0]
return self.gpy_brownian.parameters[0].variance[0]
@property
def offset(self) -> float:
return self._offset
def K(self, x1: np.ndarray, x2: np.ndarray) -> np.ndarray:
return self.gpy_brownian.K(x1 - self._offset, x2 - self.offset)
def _K_from_prod(self, x1: np.ndarray, x2: np.ndarray, skip: List[int] = None) -> np.ndarray:
"""The kernel k(x1, x2) with offset=0 evaluated at x1 and x2 computed as product from the
individual 1d kernels.
:param x1: First argument of the kernel, shape (n_points N, input_dim)
:param x2: Second argument of the kernel, shape (n_points M, input_dim)
:param skip: Skip these dimensions if specified.
:returns: Kernel evaluated at x1, x2, shape (N, M).
"""
if skip is None:
skip = []
K = np.ones([x1.shape[0], x2.shape[0]])
for dim, kern in enumerate(self.gpy_brownian.parameters):
if dim in skip:
continue
K *= kern.K(x1, x2)
# correct for missing variance
if 0 in skip:
K *= self.variance
return K
def dK_dx1(self, x1: np.ndarray, x2: np.ndarray) -> np.ndarray:
if isinstance(self.gpy_brownian, GPy.kern.Brownian):
return self._dK_dx1_1d(x1[:, 0], x2[:, 0])[None, :, :]
# product kernel
x1 = x1 - self.offset
x2 = x2 - self.offset
dK_dx1 = np.ones([x1.shape[1], x1.shape[0], x2.shape[0]])
for dim, kern in enumerate(self.gpy_brownian.parameters):
prod_term = self._K_from_prod(x1, x2, skip=[dim]) # N x M
grad_term = self._dK_dx1_1d(x1[:, dim], x2[:, dim]) # N x M
dK_dx1[dim, :, :] *= prod_term * grad_term
return dK_dx1
def dKdiag_dx(self, x: np.ndarray) -> np.ndarray:
"""The gradient of the diagonal of the kernel (the variance) v(x):=k(x, x) evaluated at x.
:param x: The locations where the gradient is evaluated, shape (n_points, input_dim).
:return: The gradient of the diagonal of the kernel evaluated at x, shape (input_dim, n_points).
"""
if isinstance(self.gpy_brownian, GPy.kern.Brownian):
return self.variance * np.ones((x.shape[1], x.shape[0]))
x = x - self.offset
dKdiag_dx = np.ones((x.shape[1], x.shape[0]))
for dim, kern in enumerate(self.gpy_brownian.parameters):
prod_term = np.prod(x, axis=1) / x[:, dim] # N,
grad_term = 1.0
dKdiag_dx[dim, :] *= prod_term * grad_term
return self.variance * dKdiag_dx
# === convenience functions start here
def create_emukit_model_from_gpy_model(
gpy_model: GPy.models.GPRegression,
integral_bounds: Optional[BoundsType] = None,
measure: Optional[IntegrationMeasure] = None,
integral_name: str = "",
) -> BaseGaussianProcessGPy:
"""Wraps a GPy model and returns an EmuKit quadrature model.
:param gpy_model: A GPy Gaussian process regression model ``GPy.models.GPRegression``.
:param integral_bounds: List of d tuples, where d is the dimensionality of the integral and the tuples contain the
lower and upper bounds of the integral
i.e., [(lb_1, ub_1), (lb_2, ub_2), ..., (lb_d, ub_d)].
Only used if ``measure`` is not given in which case the unnormalized Lebesgue measure is used.
:param measure: An integration measure. Either ``measure`` or ``integral_bounds`` must be given.
If both ``integral_bounds`` and ``measure`` are given, ``integral_bounds`` is disregarded.
:param integral_name: The (variable) name(s) of the integral.
:return: An EmuKit GP model for quadrature with GPy backend.
"""
if (integral_bounds is None) and (measure is None):
raise ValueError("Either measure or integral bounds must be given.")
if (integral_bounds is not None) and (measure is not None):
warnings.warn("Both measure and integral bounds are given. Bounds are being ignored.")
if measure is None:
domain = BoxDomain(name="", bounds=integral_bounds)
measure = LebesgueMeasure(domain=domain)
def _check_is_gpy_product_kernel(k, k_type):
is_type = isinstance(gpy_model.kern, k_type)
if isinstance(k, GPy.kern.Prod):
all_type = all(isinstance(kern, k_type) for kern in k.parameters)
all_univariante = all(kern.input_dim == 1 for kern in k.parameters)
if all_type and all_univariante:
is_type = True
return is_type
# wrap standard kernel
# RBF
qkern_emukit = None
if isinstance(gpy_model.kern, GPy.kern.RBF):
skern_emukit = RBFGPy(gpy_model.kern)
if isinstance(measure, LebesgueMeasure):
qkern_emukit = QuadratureRBFLebesgueMeasure(skern_emukit, measure, integral_name)
elif isinstance(measure, GaussianMeasure):
qkern_emukit = QuadratureRBFGaussianMeasure(skern_emukit, measure, integral_name)
# Univariate Matern12 or ProductMatern12
elif _check_is_gpy_product_kernel(gpy_model.kern, GPy.kern.Exponential):
skern_emukit = ProductMatern12GPy(gpy_model.kern)
if isinstance(measure, LebesgueMeasure):
qkern_emukit = QuadratureProductMatern12LebesgueMeasure(skern_emukit, measure, integral_name)
# Univariate Matern32 or ProductMatern32
elif _check_is_gpy_product_kernel(gpy_model.kern, GPy.kern.Matern32):
skern_emukit = ProductMatern32GPy(gpy_model.kern)
if isinstance(measure, LebesgueMeasure):
qkern_emukit = QuadratureProductMatern32LebesgueMeasure(skern_emukit, measure, integral_name)
# Univariate Matern52 or ProductMatern52
elif _check_is_gpy_product_kernel(gpy_model.kern, GPy.kern.Matern52):
skern_emukit = ProductMatern52GPy(gpy_model.kern)
if isinstance(measure, LebesgueMeasure):
qkern_emukit = QuadratureProductMatern52LebesgueMeasure(skern_emukit, measure, integral_name)
# Brownian
elif isinstance(gpy_model.kern, GPy.kern.Brownian):
skern_emukit = BrownianGPy(gpy_model.kern)
if isinstance(measure, LebesgueMeasure):
qkern_emukit = QuadratureBrownianLebesgueMeasure(skern_emukit, measure, integral_name)
# ProductBrownian
elif _check_is_gpy_product_kernel(gpy_model.kern, GPy.kern.Brownian):
skern_emukit = ProductBrownianGPy(gpy_model.kern)
if isinstance(measure, LebesgueMeasure):
qkern_emukit = QuadratureProductBrownianLebesgueMeasure(skern_emukit, measure, integral_name)
else:
raise ValueError(f"There is no GPy wrapper for the provided GPy kernel ({gpy_model.kern.name}).")
if qkern_emukit is None:
raise ValueError(f"Kernel embedding not available for provided kernel-measure combination.")
# wrap the base-gp model
return BaseGaussianProcessGPy(kern=qkern_emukit, gpy_model=gpy_model)