Bases: Op
Kernel class representing a covariance function for Gaussian Processes (GPs).
The Kernel
class defines the structure of a GP's covariance function. It supports operations
such as addition and multiplication with other kernels, enabling the construction of more
complex kernels through combinations. The class also provides methods for computing the
covariance matrix between sets of locations.
Parameters:
-
fa
(dict or callable
)
–
A dictionary or callable representing the functional attributes of the kernel.
-
autoinputs
(dict
)
–
A dictionary specifying the automatic input mappings for the kernel. If 'offset',
'locs1', or 'locs2' keys are not present, they are added with default values.
Examples:
Creating and using a Kernel
object:
from geostat.kernel import Kernel
import numpy as np
# Construct kernel and call it on locations
locs1 = np.array([[0.0, 0.0], [1.0, 1.0]])
locs2 = np.array([[2.0, 2.0], [3.0, 3.0]])
kernel = Kernel(fa={'alpha': 1.0}, autoinputs={})
covariance_matrix = kernel({'locs1': locs1, 'locs2': locs2})
print(covariance_matrix) # Covariance matrix only has zero entries as no kernel function was given
# tf.Tensor(
# [[0. 0.]
# [0. 0.]], shape=(2, 2), dtype=float32)
Combining two kernels using addition and multiplication:
kernel1 = Kernel(fa={'alpha': 1.0}, autoinputs={})
kernel2 = Kernel(fa={'range': 0.5}, autoinputs={})
combined_kernel = kernel1 + kernel2 # Adding kernels
product_kernel = kernel1 * kernel2 # Multiplying kernels
Notes:
- The
__call__
method computes the covariance matrix between two sets of locations
(locs1
and locs2
) and ensures the result is correctly broadcasted to the appropriate shape.
- The
report
method provides a summary of the kernel's parameters and their values.
- This class serves as a base class for more specialized kernel functions in GP modeling.
Source code in src/geostat/kernel.py
| class Kernel(Op):
"""
Kernel class representing a covariance function for Gaussian Processes (GPs).
The `Kernel` class defines the structure of a GP's covariance function. It supports operations
such as addition and multiplication with other kernels, enabling the construction of more
complex kernels through combinations. The class also provides methods for computing the
covariance matrix between sets of locations.
Parameters:
fa (dict or callable):
A dictionary or callable representing the functional attributes of the kernel.
autoinputs (dict):
A dictionary specifying the automatic input mappings for the kernel. If 'offset',
'locs1', or 'locs2' keys are not present, they are added with default values.
Examples:
Creating and using a `Kernel` object:
```python
from geostat.kernel import Kernel
import numpy as np
# Construct kernel and call it on locations
locs1 = np.array([[0.0, 0.0], [1.0, 1.0]])
locs2 = np.array([[2.0, 2.0], [3.0, 3.0]])
kernel = Kernel(fa={'alpha': 1.0}, autoinputs={})
covariance_matrix = kernel({'locs1': locs1, 'locs2': locs2})
print(covariance_matrix) # Covariance matrix only has zero entries as no kernel function was given
# tf.Tensor(
# [[0. 0.]
# [0. 0.]], shape=(2, 2), dtype=float32)
```
Combining two kernels using addition and multiplication:
```python
kernel1 = Kernel(fa={'alpha': 1.0}, autoinputs={})
kernel2 = Kernel(fa={'range': 0.5}, autoinputs={})
combined_kernel = kernel1 + kernel2 # Adding kernels
product_kernel = kernel1 * kernel2 # Multiplying kernels
```
Examples: Notes:
- The `__call__` method computes the covariance matrix between two sets of locations
(`locs1` and `locs2`) and ensures the result is correctly broadcasted to the appropriate shape.
- The `report` method provides a summary of the kernel's parameters and their values.
- This class serves as a base class for more specialized kernel functions in GP modeling.
"""
def __init__(self, fa, autoinputs):
if 'offset' not in autoinputs: autoinputs['offset'] = 'offset'
if 'locs1' not in autoinputs: autoinputs['locs1'] = 'locs1'
if 'locs2' not in autoinputs: autoinputs['locs2'] = 'locs2'
super().__init__(fa, autoinputs)
def __add__(self, other):
if other is None:
return self
else:
return Stack([self]) + other
def __mul__(self, other):
return Product([self]) * other
def call(self, e):
"""
Returns covariance for locations.
Return values may be unbroadcasted.
"""
pass
def __call__(self, e):
"""
Returns covariance for locations.
Return values have correct shapes.
"""
C = self.call(e)
if C is None: C = 0.
n1 = tf.shape(e['locs1'])[0]
n2 = tf.shape(e['locs2'])[0]
C = tf.broadcast_to(C, [n1, n2])
return C
def report(self):
string = ', '.join('%s %4.2f' % (v.name, p[v.name]) for v in self.vars())
return '[' + string + ']'
|