-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
cov.py
1158 lines (938 loc) · 37.5 KB
/
cov.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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2023 The PyMC Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numbers
import warnings
from collections import Counter
from functools import reduce
from operator import add, mul
from typing import Any, Callable, List, Optional, Sequence, Union
import numpy as np
import pytensor.tensor as pt
from pytensor.graph.basic import Variable
from pytensor.tensor.sharedvar import TensorSharedVariable
from pytensor.tensor.var import TensorConstant, TensorVariable
__all__ = [
"Constant",
"WhiteNoise",
"ExpQuad",
"RatQuad",
"Exponential",
"Matern52",
"Matern32",
"Matern12",
"Linear",
"Polynomial",
"Cosine",
"Periodic",
"WarpedInput",
"WrappedPeriodic",
"Gibbs",
"Coregion",
"ScaledCov",
"Kron",
]
TensorLike = Union[np.ndarray, TensorVariable]
IntSequence = Union[np.ndarray, Sequence[int]]
class BaseCovariance:
"""
Base class for kernels/covariance functions.
"""
def __call__(
self,
X: TensorLike,
Xs: Optional[TensorLike] = None,
diag: bool = False,
) -> TensorVariable:
r"""
Evaluate the kernel/covariance function.
Parameters
----------
X: The training inputs to the kernel.
Xs: The optional prediction set of inputs the kernel.
If Xs is None, Xs = X.
diag: bool
Return only the diagonal of the covariance function.
Default is False.
"""
if diag:
return self.diag(X)
else:
return self.full(X, Xs)
def diag(self, X: TensorLike) -> TensorVariable:
raise NotImplementedError
def full(self, X: TensorLike, Xs: Optional[TensorLike] = None) -> TensorVariable:
raise NotImplementedError
def __add__(self, other) -> "Add":
# If it's a scalar, cast as Constant covariance. This allows validation for power spectral
# density calc.
if isinstance(other, numbers.Real):
other = Constant(c=other)
return Add([self, other])
def __mul__(self, other) -> "Prod":
return Prod([self, other])
def __radd__(self, other) -> "Add":
return self.__add__(other)
def __rmul__(self, other) -> "Prod":
return self.__mul__(other)
def __pow__(self, other) -> "Exponentiated":
other = pt.as_tensor_variable(other).squeeze()
if not other.ndim == 0:
raise ValueError("A covariance function can only be exponentiated by a scalar value")
if not isinstance(self, Covariance):
raise TypeError(
"Can only exponentiate covariance functions which inherit from `Covariance`"
)
return Exponentiated(self, other)
def __array_wrap__(self, result):
"""
Required to allow radd/rmul by numpy arrays.
"""
result = np.squeeze(result)
if len(result.shape) <= 1:
result = result.reshape(1, 1)
elif len(result.shape) > 2:
raise ValueError(
f"cannot combine a covariance function with array of shape {result.shape}"
)
r, c = result.shape
A = np.zeros((r, c))
for i in range(r):
for j in range(c):
r = result[i, j]._factor_list[1]
if isinstance(r, Constant):
# Counteract the elemwise Add edgecase
r = r.c
A[i, j] = r
if isinstance(result[0][0], Add):
return result[0][0]._factor_list[0] + A
elif isinstance(result[0][0], Prod):
return result[0][0]._factor_list[0] * A
else:
raise TypeError(
f"Unknown Covariance combination type {result[0][0]}. "
"Known types are `Add` or `Prod`."
)
@staticmethod
def _alloc(X, *shape: int) -> TensorVariable:
return pt.alloc(X, *shape) # type: ignore
class Covariance(BaseCovariance):
"""
Base class for kernels/covariance functions with input_dim and active_dims, which excludes
kernels like `Constant` and `WhiteNoise`.
Parameters
----------
input_dim: integer
The number of input dimensions, or columns of X (or Xs)
the kernel will operate on.
active_dims: List of integers
Indicate which dimension or column of X the covariance
function operates on.
"""
def __init__(self, input_dim: int, active_dims: Optional[IntSequence] = None):
self.input_dim = input_dim
if active_dims is None:
self.active_dims = np.arange(input_dim)
else:
self.active_dims = np.asarray(active_dims, int)
if max(self.active_dims) > self.input_dim:
raise ValueError("Values in `active_dims` can't be larger than `input_dim`.")
@property
def n_dims(self) -> int:
"""The dimensionality of the input, as taken from the
`active_dims`.
"""
# Evaluate lazily in-case this changes.
return len(self.active_dims)
def _slice(self, X, Xs=None):
xdims = X.shape[-1]
if isinstance(xdims, Variable):
# Circular dependency
from pymc.pytensorf import constant_fold
[xdims] = constant_fold([xdims])
if self.input_dim != xdims:
warnings.warn(
f"Only {self.input_dim} column(s) out of {xdims} are"
" being used to compute the covariance function. If this"
" is not intended, increase 'input_dim' parameter to"
" the number of columns to use. Ignore otherwise.",
UserWarning,
)
X = pt.as_tensor_variable(X[:, self.active_dims])
if Xs is not None:
Xs = pt.as_tensor_variable(Xs[:, self.active_dims])
return X, Xs
class Combination(Covariance):
def __init__(self, factor_list: Sequence):
"""Use constituent factors to get input_dim and active_dims for the Combination covariance."""
# Check if all input_dim are the same in factor_list
input_dims = {factor.input_dim for factor in factor_list if isinstance(factor, Covariance)}
if len(input_dims) != 1:
raise ValueError("All covariances must have the same `input_dim`.")
input_dim = input_dims.pop()
# Union all active_dims sets in factor_list for the combination covariance
active_dims = np.sort(
np.asarray(
list(
set.union(
*[
set(factor.active_dims)
for factor in factor_list
if isinstance(factor, Covariance)
]
)
),
dtype=int,
)
)
super().__init__(input_dim=input_dim, active_dims=active_dims)
# Set up combination kernel, flatten out factor_list so that
self._factor_list: List[Any] = []
for factor in factor_list:
if isinstance(factor, self.__class__):
self._factor_list.extend(factor._factor_list)
else:
self._factor_list.append(factor)
def _merge_factors_cov(self, X, Xs=None, diag=False):
"""Called to evaluate either all the sums or all the
products of kernels that are possible to evaluate.
"""
factor_list = []
for factor in self._factor_list:
# make sure diag=True is handled properly
if isinstance(factor, BaseCovariance):
factor_list.append(factor(X, Xs, diag))
elif isinstance(factor, np.ndarray):
if np.ndim(factor) == 2 and diag:
factor_list.append(np.diag(factor))
else:
factor_list.append(factor)
elif isinstance(
factor,
(
TensorConstant,
TensorVariable,
TensorSharedVariable,
),
):
if factor.ndim == 2 and diag:
factor_list.append(pt.diag(factor))
else:
factor_list.append(factor)
else:
factor_list.append(factor)
return factor_list
def _merge_factors_psd(self, omega):
"""Called to evaluatate spectral densities of combination kernels when possible.
Implements
a more restricted set of rules than `_merge_factors_cov` -- just additivity of stationary
covariances with defined power spectral densities and multiplication by scalars. Also, the
active_dims for all covariances in the sum must be the same.
"""
factor_list = []
for factor in self._factor_list:
if isinstance(factor, Covariance):
# Allow merging covariances for psd only if active_dims are the same
if set(self.active_dims) != set(factor.active_dims):
raise ValueError(
"For power spectral density calculations `active_dims` must be the same "
"for all covariances in the sum."
)
# If it's a covariance try to calculate the psd
try:
factor_list.append(factor.power_spectral_density(omega))
except (AttributeError, NotImplementedError) as e:
if isinstance(factor, Stationary):
raise NotImplementedError(
f"No power spectral density method has been implemented for {factor}."
) from e
else:
raise ValueError(
"Power spectral densities, `.power_spectral_density(omega)`, can only "
f"be calculated for `Stationary` covariance functions. {factor} is "
"non-stationary."
) from e
else:
# Otherwise defer the reduction to later
factor_list.append(factor)
return factor_list
class Add(Combination):
def __call__(
self,
X: TensorLike,
Xs: Optional[TensorLike] = None,
diag: bool = False,
) -> TensorVariable:
return reduce(add, self._merge_factors_cov(X, Xs, diag))
def power_spectral_density(self, omega: TensorLike) -> TensorVariable:
return reduce(add, self._merge_factors_psd(omega))
class Prod(Combination):
def __call__(
self,
X: TensorLike,
Xs: Optional[TensorLike] = None,
diag: bool = False,
) -> TensorVariable:
return reduce(mul, self._merge_factors_cov(X, Xs, diag))
def power_spectral_density(self, omega: TensorLike) -> TensorVariable:
check = Counter([isinstance(factor, Covariance) for factor in self._factor_list])
if check.get(True, 0) >= 2:
raise NotImplementedError(
"The power spectral density of products of covariance "
"functions is not implemented."
)
return reduce(mul, self._merge_factors_psd(omega))
class Exponentiated(Covariance):
def __init__(self, kernel: Covariance, power):
self.kernel = kernel
self.power = power
super().__init__(input_dim=self.kernel.input_dim, active_dims=self.kernel.active_dims)
def __call__(
self, X: TensorLike, Xs: Optional[TensorLike] = None, diag: bool = False
) -> TensorVariable:
return self.kernel(X, Xs, diag=diag) ** self.power
class Kron(Covariance):
r"""Form a covariance object that is the kronecker product of other covariances.
In contrast to standard multiplication, where each covariance is given the
same inputs X and Xs, kronecker product covariances first split the inputs
into their respective spaces (inferred from the input_dim of each object)
before forming their product. Kronecker covariances have a larger
input dimension than any of its factors since the inputs are the
concatenated columns of its components.
Factors must be covariances or their combinations, arrays will not work.
Generally utilized by the `gp.MarginalKron` and gp.LatentKron`
implementations.
"""
def __init__(self, factor_list: Sequence[Covariance]):
self.input_dims = [factor.input_dim for factor in factor_list]
input_dim = sum(self.input_dims)
super().__init__(input_dim=input_dim)
self._factor_list = factor_list
def _split(self, X, Xs):
indices = np.cumsum(self.input_dims)
X_split = np.hsplit(X, indices)
if Xs is not None:
Xs_split = np.hsplit(Xs, indices)
else:
Xs_split = [None] * len(X_split)
return X_split, Xs_split
def __call__(
self, X: TensorLike, Xs: Optional[TensorLike] = None, diag: bool = False
) -> TensorVariable:
X_split, Xs_split = self._split(X, Xs)
covs = [cov(x, xs, diag) for cov, x, xs in zip(self._factor_list, X_split, Xs_split)]
return reduce(mul, covs)
class Constant(BaseCovariance):
r"""
Constant valued covariance function.
.. math::
k(x, x') = c
"""
def __init__(self, c):
self.c = c
def diag(self, X: TensorLike) -> TensorVariable:
return self._alloc(self.c, X.shape[0])
def full(self, X: TensorLike, Xs: Optional[TensorLike] = None) -> TensorVariable:
if Xs is None:
return self._alloc(self.c, X.shape[0], X.shape[0])
else:
return self._alloc(self.c, X.shape[0], Xs.shape[0])
class WhiteNoise(BaseCovariance):
r"""
White noise covariance function.
.. math::
k(x, x') = \sigma^2 \mathrm{I}
"""
def __init__(self, sigma):
self.sigma = sigma
def diag(self, X: TensorLike) -> TensorVariable:
return self._alloc(pt.square(self.sigma), X.shape[0])
def full(self, X: TensorLike, Xs: Optional[TensorLike] = None) -> TensorVariable:
if Xs is None:
return pt.diag(self.diag(X))
else:
return self._alloc(0.0, X.shape[0], Xs.shape[0])
class Circular(Covariance):
R"""
Circular Kernel.
.. math::
k_g(x, y) = W_\pi(\operatorname{dist}_{\mathit{geo}}(x, y)),
with
.. math::
W_c(t) = \left(1 + \tau \frac{t}{c}\right)\left(1-\frac{t}{c}\right)^\tau_+
where :math:`c` is maximum value for :math:`t` and :math:`\tau\ge 4`.
:math:`\tau` controls for correlation strength, larger :math:`\tau` leads to less smooth functions
See [1]_ for more explanations and use cases.
Parameters
----------
period : scalar
defines the circular interval :math:`[0, \mathit{bound})`
tau : scalar
:math:`\tau\ge 4` defines correlation strength, the larger,
the smaller correlation is. Minimum value is :math:`4`
References
----------
.. [1] Espéran Padonou, O Roustant, "Polar Gaussian Processes for Predicting on Circular Domains"
https://hal.archives-ouvertes.fr/hal-01119942v1/document
"""
def __init__(
self,
input_dim: int,
period,
tau=4,
active_dims: Optional[IntSequence] = None,
):
super().__init__(input_dim, active_dims)
self.c = pt.as_tensor_variable(period / 2)
self.tau = tau
def dist(self, X, Xs):
if Xs is None:
Xs = pt.transpose(X)
else:
Xs = pt.transpose(Xs)
return pt.abs((X - Xs + self.c) % (self.c * 2) - self.c)
def weinland(self, t):
return (1 + self.tau * t / self.c) * pt.clip(1 - t / self.c, 0, np.inf) ** self.tau
def full(self, X: TensorLike, Xs: Optional[TensorLike] = None) -> TensorVariable:
X, Xs = self._slice(X, Xs)
return self.weinland(self.dist(X, Xs))
def diag(self, X: TensorLike) -> TensorVariable:
return self._alloc(1.0, X.shape[0])
class Stationary(Covariance):
r"""
Base class for stationary kernels/covariance functions.
Parameters
----------
ls: Lengthscale. If input_dim > 1, a list or array of scalars or PyMC random
variables. If input_dim == 1, a scalar or PyMC random variable.
ls_inv: Inverse lengthscale. 1 / ls. One of ls or ls_inv must be provided.
"""
def __init__(
self,
input_dim: int,
ls=None,
ls_inv=None,
active_dims: Optional[IntSequence] = None,
):
super().__init__(input_dim, active_dims)
if (ls is None and ls_inv is None) or (ls is not None and ls_inv is not None):
raise ValueError("Only one of 'ls' or 'ls_inv' must be provided")
elif ls_inv is not None:
if isinstance(ls_inv, (list, tuple)):
ls = 1.0 / np.asarray(ls_inv)
else:
ls = 1.0 / ls_inv
self.ls = pt.as_tensor_variable(ls)
def square_dist(self, X, Xs):
X = pt.mul(X, 1.0 / self.ls)
X2 = pt.sum(pt.square(X), 1)
if Xs is None:
sqd = -2.0 * pt.dot(X, pt.transpose(X)) + (
pt.reshape(X2, (-1, 1)) + pt.reshape(X2, (1, -1))
)
else:
Xs = pt.mul(Xs, 1.0 / self.ls)
Xs2 = pt.sum(pt.square(Xs), 1)
sqd = -2.0 * pt.dot(X, pt.transpose(Xs)) + (
pt.reshape(X2, (-1, 1)) + pt.reshape(Xs2, (1, -1))
)
return pt.clip(sqd, 0.0, np.inf)
def euclidean_dist(self, X, Xs):
r2 = self.square_dist(X, Xs)
return self._sqrt(r2)
def _sqrt(self, r2):
return pt.sqrt(r2 + 1e-12)
def diag(self, X: TensorLike) -> TensorVariable:
return self._alloc(1.0, X.shape[0])
def full(self, X: TensorLike, Xs: Optional[TensorLike] = None) -> TensorVariable:
X, Xs = self._slice(X, Xs)
r2 = self.square_dist(X, Xs)
return self.full_from_distance(r2, squared=True)
def full_from_distance(self, dist: TensorLike, squared: bool = False) -> TensorVariable:
raise NotImplementedError
def power_spectral_density(self, omega: TensorLike) -> TensorVariable:
raise NotImplementedError
class ExpQuad(Stationary):
r"""
The Exponentiated Quadratic kernel. Also referred to as the Squared
Exponential, or Radial Basis Function kernel.
.. math::
k(x, x') = \mathrm{exp}\left[ -\frac{(x - x')^2}{2 \ell^2} \right]
"""
def full_from_distance(self, dist: TensorLike, squared: bool = False) -> TensorVariable:
r2 = dist if squared else dist**2
return pt.exp(-0.5 * r2)
def power_spectral_density(self, omega: TensorLike) -> TensorVariable:
r"""
The power spectral density for the ExpQuad kernel is:
.. math::
S(\boldsymbol\omega) =
(\sqrt(2 \pi)^D \prod_{i}^{D}\ell_i
\exp\left( -\frac{1}{2} \sum_{i}^{D}\ell_i^2 \omega_i^{2} \right)
"""
ls = pt.ones(self.n_dims) * self.ls
c = pt.power(pt.sqrt(2.0 * np.pi), self.n_dims)
exp = pt.exp(-0.5 * pt.dot(pt.square(omega), pt.square(ls)))
return c * pt.prod(ls) * exp
class RatQuad(Stationary):
r"""
The Rational Quadratic kernel.
.. math::
k(x, x') = \left(1 + \frac{(x - x')^2}{2\alpha\ell^2} \right)^{-\alpha}
"""
def __init__(
self,
input_dim: int,
alpha,
ls=None,
ls_inv=None,
active_dims: Optional[IntSequence] = None,
):
super().__init__(input_dim, ls, ls_inv, active_dims)
self.alpha = alpha
def full_from_distance(self, dist: TensorLike, squared: bool = False) -> TensorVariable:
r2 = dist if squared else dist**2
return pt.power(
(1.0 + 0.5 * r2 * (1.0 / self.alpha)),
-1.0 * self.alpha,
)
class Matern52(Stationary):
r"""
The Matern kernel with nu = 5/2.
.. math::
k(x, x') = \left(1 + \frac{\sqrt{5(x - x')^2}}{\ell} +
\frac{5(x-x')^2}{3\ell^2}\right)
\mathrm{exp}\left[ - \frac{\sqrt{5(x - x')^2}}{\ell} \right]
"""
def full_from_distance(self, dist: TensorLike, squared: bool = False) -> TensorVariable:
r = self._sqrt(dist) if squared else dist
return (1.0 + np.sqrt(5.0) * r + 5.0 / 3.0 * pt.square(r)) * pt.exp(-1.0 * np.sqrt(5.0) * r)
def power_spectral_density(self, omega: TensorLike) -> TensorVariable:
r"""
The power spectral density for the Matern52 kernel is:
.. math::
S(\boldsymbol\omega) =
\frac{2^D \pi^{\frac{D}{2}} \Gamma(\frac{D+5}{2}) 5^{5/2}}
{\frac{3}{4}\sqrt{\pi}}
\prod_{i=1}^{D}\ell_{i}
\left(5 + \sum_{i=1}^{D}\ell_{i}^2 \boldsymbol\omega_{i}^{2}\right)^{-\frac{D+5}{2}}
"""
ls = pt.ones(self.n_dims) * self.ls
D52 = (self.n_dims + 5) / 2
num = (
pt.power(2, self.n_dims)
* pt.power(np.pi, self.n_dims / 2)
* pt.gamma(D52)
* pt.power(5, 5 / 2)
)
den = 0.75 * pt.sqrt(np.pi)
pow = pt.power(5.0 + pt.dot(pt.square(omega), pt.square(ls)), -1 * D52)
return (num / den) * pt.prod(ls) * pow
class Matern32(Stationary):
r"""
The Matern kernel with nu = 3/2.
.. math::
k(x, x') = \left(1 + \frac{\sqrt{3(x - x')^2}}{\ell}\right)
\mathrm{exp}\left[ - \frac{\sqrt{3(x - x')^2}}{\ell} \right]
"""
def full_from_distance(self, dist: TensorLike, squared: bool = False) -> TensorVariable:
r = self._sqrt(dist) if squared else dist
return (1.0 + np.sqrt(3.0) * r) * pt.exp(-np.sqrt(3.0) * r)
def power_spectral_density(self, omega: TensorLike) -> TensorVariable:
r"""
The power spectral density for the Matern32 kernel is:
.. math::
S(\boldsymbol\omega) =
\frac{2^D \pi^{D/2} \Gamma\left(\frac{D+3}{2}\right) 3^{3/2}}
{\frac{1}{2}\sqrt{\pi}}
\prod_{i=1}^{D}\ell_{i}
\left(3 + \sum_{i=1}^{D}\ell_{i}^2 \boldsymbol\omega_{i}^{2}\right)^{-\frac{D+3}{2}}
"""
ls = pt.ones(self.n_dims) * self.ls
D32 = (self.n_dims + 3) / 2
num = (
pt.power(2, self.n_dims)
* pt.power(np.pi, self.n_dims / 2)
* pt.gamma(D32)
* pt.power(3, 3 / 2)
)
den = 0.5 * pt.sqrt(np.pi)
pow = pt.power(3.0 + pt.dot(pt.square(omega), pt.square(ls)), -1 * D32)
return (num / den) * pt.prod(ls) * pow
class Matern12(Stationary):
r"""
The Matern kernel with nu = 1/2
.. math::
k(x, x') = \mathrm{exp}\left[ -\frac{(x - x')^2}{\ell} \right]
"""
def full_from_distance(self, dist: TensorLike, squared: bool = False) -> TensorVariable:
r = self._sqrt(dist) if squared else dist
return pt.exp(-r)
class Exponential(Stationary):
r"""
The Exponential kernel.
.. math::
k(x, x') = \mathrm{exp}\left[ -\frac{||x - x'||}{2\ell} \right]
"""
def full_from_distance(self, dist: TensorLike, squared: bool = False) -> TensorVariable:
r = self._sqrt(dist) if squared else dist
return pt.exp(-0.5 * r)
class Cosine(Stationary):
r"""
The Cosine kernel.
.. math::
k(x, x') = \mathrm{cos}\left( 2 \pi \frac{||x - x'||}{ \ell^2} \right)
"""
def full_from_distance(self, dist: TensorLike, squared: bool = False) -> TensorVariable:
r = self._sqrt(dist) if squared else dist
return pt.cos(2.0 * np.pi * r)
class Periodic(Stationary):
r"""
The Periodic kernel.
.. math::
k(x, x') = \mathrm{exp}\left( -\frac{\mathrm{sin}^2(\pi |x-x'| \frac{1}{T})}{2\ell^2} \right)
Notes
-----
Note that the scaling factor for this kernel is different compared to the more common
definition (see [1]_). Here, 0.5 is in the exponent instead of the more common value, 2.
Divide the length-scale by 2 when initializing the kernel to recover the standard definition.
References
----------
.. [1] David Duvenaud, "The Kernel Cookbook"
https://www.cs.toronto.edu/~duvenaud/cookbook/
"""
def __init__(
self,
input_dim: int,
period,
ls=None,
ls_inv=None,
active_dims: Optional[IntSequence] = None,
):
super().__init__(input_dim, ls, ls_inv, active_dims)
self.period = period
def full(self, X: TensorLike, Xs: Optional[TensorLike] = None) -> TensorVariable:
X, Xs = self._slice(X, Xs)
if Xs is None:
Xs = X
f1 = pt.expand_dims(X, axis=(0,))
f2 = pt.expand_dims(Xs, axis=(1,))
r = np.pi * (f1 - f2) / self.period
r2 = pt.sum(pt.square(pt.sin(r) / self.ls), 2)
return self.full_from_distance(r2, squared=True)
def full_from_distance(self, dist: TensorLike, squared: bool = False) -> TensorVariable:
# NOTE: This is the same as the ExpQuad as we assume the periodicity
# has already been accounted for in the distance
r2 = dist if squared else dist**2
return pt.exp(-0.5 * r2)
class Linear(Covariance):
r"""
The Linear kernel.
.. math::
k(x, x') = (x - c)(x' - c)
"""
def __init__(self, input_dim: int, c, active_dims: Optional[IntSequence] = None):
super().__init__(input_dim, active_dims)
self.c = c
def _common(self, X, Xs=None):
X, Xs = self._slice(X, Xs)
Xc = pt.sub(X, self.c)
return X, Xc, Xs
def full(self, X: TensorLike, Xs: Optional[TensorLike] = None) -> TensorVariable:
X, Xc, Xs = self._common(X, Xs)
if Xs is None:
return pt.dot(Xc, pt.transpose(Xc))
else:
Xsc = pt.sub(Xs, self.c)
return pt.dot(Xc, pt.transpose(Xsc))
def diag(self, X: TensorLike) -> TensorVariable:
X, Xc, _ = self._common(X, None)
return pt.sum(pt.square(Xc), 1)
class Polynomial(Linear):
r"""
The Polynomial kernel.
.. math::
k(x, x') = [(x - c)(x' - c) + \mathrm{offset}]^{d}
"""
def __init__(self, input_dim: int, c, d, offset, active_dims: Optional[IntSequence] = None):
super().__init__(input_dim, c, active_dims)
self.d = d
self.offset = offset
def full(self, X: TensorLike, Xs: Optional[TensorLike] = None) -> TensorVariable:
linear = super().full(X, Xs)
return pt.power(linear + self.offset, self.d)
def diag(self, X: TensorLike) -> TensorVariable:
linear = super().diag(X)
return pt.power(linear + self.offset, self.d)
class WarpedInput(Covariance):
r"""
Warp the inputs of any kernel using an arbitrary function
defined using PyTensor.
.. math::
k(x, x') = k(w(x), w(x'))
Parameters
----------
cov_func: Covariance
warp_func: callable
PyTensor function of X and additional optional arguments.
args: optional, tuple or list of scalars or PyMC variables
Additional inputs (besides X or Xs) to warp_func.
"""
def __init__(
self,
input_dim: int,
cov_func: Covariance,
warp_func: Callable,
args=None,
active_dims: Optional[IntSequence] = None,
):
super().__init__(input_dim, active_dims)
if not callable(warp_func):
raise TypeError("warp_func must be callable")
if not isinstance(cov_func, Covariance):
raise TypeError("Must be or inherit from the Covariance class")
self.w = handle_args(warp_func)
self.args = args
self.cov_func = cov_func
def full(self, X: TensorLike, Xs: Optional[TensorLike] = None) -> TensorVariable:
X, Xs = self._slice(X, Xs)
if Xs is None:
return self.cov_func(self.w(X, self.args), Xs)
else:
return self.cov_func(self.w(X, self.args), self.w(Xs, self.args))
def diag(self, X: TensorLike) -> TensorVariable:
X, _ = self._slice(X, None)
return self.cov_func(self.w(X, self.args), diag=True)
class WrappedPeriodic(Covariance):
r"""
The `WrappedPeriodic` kernel constructs periodic kernels from any `Stationary` kernel.
This is done by warping the input with the function
.. math::
\mathbf{u}(x) = \left(
\mathrm{sin} \left( \frac{2\pi x}{T} \right),
\mathrm{cos} \left( \frac{2\pi x}{T} \right)
\right)
The original derivation as applied to the squared exponential kernel can
be found in [1] or referenced in Chapter 4, page 92 of [2].
Notes
-----
Note that we drop the resulting scaling by 4 found in the original derivation
so that the interpretation of the length scales is consistent between the
underlying kernel and the periodic version.
Examples
--------
In order to construct a kernel equivalent to the `Periodic` kernel you
can do the following (though using `Periodic` will likely be a bit faster):
.. code:: python
exp_quad = pm.gp.cov.ExpQuad(1, ls=0.5)
cov = pm.gp.cov.WrappedPeriodic(exp_quad, period=5)
References
----------
.. [1] MacKay, D. J. C. (1998). Introduction to Gaussian Processes.
In Bishop, C. M., editor, Neural Networks and Machine Learning. Springer-Verlag
.. [2] Rasmussen, C. E., & Williams, C. K. I. (2006). Gaussian processes for machine learning. MIT Press.
http://gaussianprocess.org/gpml/chapters/
Parameters
----------
cov_func: Stationary
Base kernel or covariance function
period: Period
"""
def __init__(self, cov_func: Stationary, period):
super().__init__(cov_func.input_dim, cov_func.active_dims)
if not isinstance(cov_func, Stationary):
raise TypeError("Must inherit from the Stationary class")
self.cov_func = cov_func
self.period = period
def full(self, X: TensorLike, Xs: Optional[TensorLike] = None) -> TensorVariable:
X, Xs = self._slice(X, Xs)
if Xs is None:
Xs = X
f1 = pt.expand_dims(X, axis=(0,))
f2 = pt.expand_dims(Xs, axis=(1,))
r = np.pi * (f1 - f2) / self.period
r2 = pt.sum(pt.square(pt.sin(r) / self.cov_func.ls), 2)
return self.cov_func.full_from_distance(r2, squared=True)
def diag(self, X: TensorLike) -> TensorVariable:
return self._alloc(1.0, X.shape[0])
class Gibbs(Covariance):
r"""
The Gibbs kernel. Use an arbitrary lengthscale function defined
using PyTensor. Only tested in one dimension.
.. math::
k(x, x') = \sqrt{\frac{2\ell(x)\ell(x')}{\ell^2(x) + \ell^2(x')}}
\mathrm{exp}\left[ -\frac{(x - x')^2}
{\ell^2(x) + \ell^2(x')} \right]
Parameters
----------
lengthscale_func: callable
PyTensor function of X and additional optional arguments.
args: optional, tuple or list of scalars or PyMC variables
Additional inputs (besides X or Xs) to lengthscale_func.
"""
def __init__(
self,
input_dim: int,
lengthscale_func: Callable,
args=None,
active_dims: Optional[IntSequence] = None,
):
super().__init__(input_dim, active_dims)
if active_dims is not None:
if len(active_dims) > 1:
raise NotImplementedError(("Higher dimensional inputs ", "are untested"))
else:
if input_dim != 1:
raise NotImplementedError(("Higher dimensional inputs ", "are untested"))
if not callable(lengthscale_func):
raise TypeError("lengthscale_func must be callable")
self.lfunc = handle_args(lengthscale_func)
self.args = args
def square_dist(self, X, Xs=None):
X2 = pt.sum(pt.square(X), 1)
if Xs is None:
sqd = -2.0 * pt.dot(X, pt.transpose(X)) + (
pt.reshape(X2, (-1, 1)) + pt.reshape(X2, (1, -1))