Skip to content

Stack

src.geostat.kernel.Stack

Bases: Kernel

Stack kernel class for combining multiple Gaussian Process (GP) kernels additively.

The Stack class defines a kernel that combines multiple input kernels by stacking them together. This additive combination allows for capturing a more complex covariance structure by summing the contributions from each individual kernel.

Parameters:

  • parts (List[Kernel]) –

    A list of kernel objects to be combined additively.

Examples:

Creating and using a Stack kernel:

from geostat.kernel import Stack, SquaredExponential, Noise

# Create individual kernels
kernel1 = SquaredExponential(sill=1.0, range=2.0)
kernel2 = Noise(nugget=0.1)

# Combine kernels using the Stack class
stacked_kernel = Stack(parts=[kernel1, kernel2])

locs1 = np.array([[0.0], [1.0], [2.0]])
locs2 = np.array([[0.0], [1.0], [2.0]])
covariance_matrix = stacked_kernel({'locs1': locs1, 'locs2': locs2})

Adding another kernel to an existing Stack:

kernel3 = SquaredExponential(sill=0.5, range=1.0)
combined_stack = stacked_kernel + kernel3

Notes:

  • The call method computes the sum of all covariance matrices generated by the stacked kernels.
  • The vars method gathers parameters from all input kernels, making them accessible for optimization.
  • The Stack kernel is useful for building complex models where multiple covariance structures need to be combined additively, enabling richer and more flexible GP models.
Source code in src/geostat/kernel.py
class Stack(Kernel):
    """
    Stack kernel class for combining multiple Gaussian Process (GP) kernels additively.

    The `Stack` class defines a kernel that combines multiple input kernels by stacking them together.
    This additive combination allows for capturing a more complex covariance structure by summing the
    contributions from each individual kernel.

    Parameters:
        parts (List[Kernel]):
            A list of kernel objects to be combined additively.

    Examples:
        Creating and using a `Stack` kernel:

        ```python
        from geostat.kernel import Stack, SquaredExponential, Noise

        # Create individual kernels
        kernel1 = SquaredExponential(sill=1.0, range=2.0)
        kernel2 = Noise(nugget=0.1)

        # Combine kernels using the Stack class
        stacked_kernel = Stack(parts=[kernel1, kernel2])

        locs1 = np.array([[0.0], [1.0], [2.0]])
        locs2 = np.array([[0.0], [1.0], [2.0]])
        covariance_matrix = stacked_kernel({'locs1': locs1, 'locs2': locs2})
        ```

        Adding another kernel to an existing `Stack`:

        ```python
        kernel3 = SquaredExponential(sill=0.5, range=1.0)
        combined_stack = stacked_kernel + kernel3
        ```

    Examples: Notes:
        - The `call` method computes the sum of all covariance matrices generated by the stacked kernels.
        - The `vars` method gathers parameters from all input kernels, making them accessible for optimization.
        - The `Stack` kernel is useful for building complex models where multiple covariance structures need to be 
            combined additively, enabling richer and more flexible GP models.
    """

    def __init__(self, parts: List[Kernel]):
        self.parts = parts
        super().__init__({}, dict(locs1='locs1', locs2='locs2', parts=parts))

    def vars(self):
        return {k: p for part in self.parts for k, p in part.vars().items()}

    def __add__(self, other):
        if isinstance(other, Kernel):
            return Stack(self.parts + [other])

    def call(self, e):
        return tf.reduce_sum(e['parts'], axis=0)

    def report(self):
        return ' '.join(part.report(p) for part in self.parts)