Skip to content

TrendPrior

src.geostat.kernel.TrendPrior

Bases: Kernel

TrendPrior class representing a kernel with a linear trend prior for Gaussian Processes (GPs).

The TrendPrior class defines a kernel that incorporates a linear trend in the covariance structure using a provided featurizer function. This kernel is particularly useful when the underlying process is expected to exhibit a trend that can be captured by the specified features.

Parameters:

  • featurizer (Callable) –

    A function that takes input locations and returns a feature matrix. This function defines the features used in the trend prior.

  • alpha (float or Variable) –

    The scaling factor (weight) applied to the trend prior.

Examples:

Defining a TrendPrior kernel with a custom featurizer:

import tensorflow as tf
import geostat
from geostat import Parameters
from geostat.kernel import TrendPrior

# Define a simple featurizer function
@geostat.featurizer()
def simple_featurizer(x, y):
    return x, 2*x, x**2

# Create parameters.
p = Parameters(alpha=0.5)

# Construct kernel and call it
locs1 = tf.constant([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]])
locs2 = tf.constant([[1.5, 2.0], [2.5, 3.0], [3.5, 4.0], [4.5, 5.0]])
trend_prior_kernel = TrendPrior(featurizer=simple_featurizer, alpha=p.alpha)
covariance_matrix = trend_prior_kernel({'locs1': locs1, 'locs2': locs2, 'alpha': p.alpha.value})
print(covariance_matrix)
# tf.Tensor(
# [[  4.875      9.375     14.875     21.375   ]
#  [ 12.        25.        42.        63.      ]
#  [ 21.375     46.874996  81.37499  124.875   ]], shape=(3, 4), dtype=float32)

Notes:

  • The call method computes the covariance matrix using the features generated by the featurizer function and scales it by the alpha parameter.
  • The vars method returns the parameter dictionary for alpha using the ppp function.
  • The TrendPrior kernel is typically used when the GP model needs to account for linear or polynomial trends in the data.
Source code in src/geostat/kernel.py
class TrendPrior(Kernel):
    """
    TrendPrior class representing a kernel with a linear trend prior for Gaussian Processes (GPs).

    The `TrendPrior` class defines a kernel that incorporates a linear trend in the covariance structure
    using a provided featurizer function. This kernel is particularly useful when the underlying process
    is expected to exhibit a trend that can be captured by the specified features.

    Parameters:
        featurizer (Callable):
            A function that takes input locations and returns a feature matrix. This function defines
            the features used in the trend prior.
        alpha (float or tf.Variable):
            The scaling factor (weight) applied to the trend prior.

    Examples:
        Defining a TrendPrior kernel with a custom featurizer:

        ```python
        import tensorflow as tf
        import geostat
        from geostat import Parameters
        from geostat.kernel import TrendPrior

        # Define a simple featurizer function
        @geostat.featurizer()
        def simple_featurizer(x, y):
            return x, 2*x, x**2

        # Create parameters.
        p = Parameters(alpha=0.5)

        # Construct kernel and call it
        locs1 = tf.constant([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]])
        locs2 = tf.constant([[1.5, 2.0], [2.5, 3.0], [3.5, 4.0], [4.5, 5.0]])
        trend_prior_kernel = TrendPrior(featurizer=simple_featurizer, alpha=p.alpha)
        covariance_matrix = trend_prior_kernel({'locs1': locs1, 'locs2': locs2, 'alpha': p.alpha.value})
        print(covariance_matrix)
        # tf.Tensor(
        # [[  4.875      9.375     14.875     21.375   ]
        #  [ 12.        25.        42.        63.      ]
        #  [ 21.375     46.874996  81.37499  124.875   ]], shape=(3, 4), dtype=float32)
        ```

    Examples: Notes:
        - The `call` method computes the covariance matrix using the features generated by the featurizer
            function and scales it by the `alpha` parameter.
        - The `vars` method returns the parameter dictionary for `alpha` using the `ppp` function.
        - The `TrendPrior` kernel is typically used when the GP model needs to account for linear or 
            polynomial trends in the data.
    """

    def __init__(self, featurizer, alpha):
        fa = dict(alpha=alpha)
        self.featurizer = featurizer
        super().__init__(fa, dict(locs1='locs1', locs2='locs2'))

    def vars(self):
        return ppp(self.fa['alpha'])

    def call(self, e):
        F1 = tf.cast(self.featurizer(e['locs1']), tf.float32)
        F2 = tf.cast(self.featurizer(e['locs2']), tf.float32)
        return e['alpha'] * tf.einsum('ba,ca->bc', F1, F2)