Covariance Functions#

This section shows different ways to use a supplied covariance function or define your own.

The cov_func_curry argument of the model class supports a one argument function or class type that returns a function k(x, y) \(\rightarrow\) float. In this case, the length scale of the covariance function will be computed automatically if not passed as an argument. Alternatively, one can set the cov_func argument with a function taking two arrays of cell states returning the similatity between each pair.

Pass a predefined covariance function class (Default behavior)#
from mellon import Matern52
cov_func_cury = Matern52
cov_func = Matern52(length_scale)
Write a function of one variable that returns a function k(x, y) \(\rightarrow\) float#
from mellon import distance    # distance computes the distance between each point in x
                                 # and each point in y.
def Matern52(ls=1.0):
    def k(x, y):
        r = distance(x, y) / ls
        similarity = (sqrt(5.0) * r + square(sqrt(5.0) * r)/3 + 1) * exp(-sqrt(5.0) * r)
        return similarity
    return k
Write a function of one variable that returns a function \(k(x, y) \rightarrow\) float and inherit from the Covariance base class#
from mellon import distance
from mellon import Covariance  # The Covariance base class __call__ method calls k.

class Matern52(Covariance):
    def __init__(self, ls=1.0):
        super().__init__()
        self.ls = ls

    def k(self, x, y):
        r = distance(x, y) / self.ls
        similarity = (sqrt(5.0) * r + square(sqrt(5.0) * r)/3 + 1) * exp(-sqrt(5.0) * r)
        return simiAlarity

The Covariance class upports adding, multiplying, and taking the covariance to a power with the +, *, and ** operators:

Combining two covariance functions.#
from mellon import Matern52, ExpQuad
cov_func = Matern52(length_scale)*.7 + ExpQuad(length_scale)*.3

Implemented Covariance Functions#

class mellon.cov.ExpQuad(ls=1.0)#

Bases: Covariance

k(x, y)#

\(e^{-\frac{||x-y||^2}{2 l^2}}\)

class mellon.cov.Exponential(ls=1.0)#

Bases: Covariance

k(x, y)#

\(e^{-\frac{||x-y||}{2l}}\)

class mellon.cov.Matern32(ls=1.0)#

Bases: Covariance

k(x, y)#

\((1 + \frac{\sqrt{3}||x-y||}{l}) \cdot e^{-\frac{\sqrt{3}||x-y||}{l}}\)

class mellon.cov.Matern52(ls=1.0)#

Bases: Covariance

k(x, y)#

\((1 + \frac{\sqrt{5}||x-y||}{l} + \frac{5||x-y||^2}{3l^2}) \cdot e^{-\frac{\sqrt{5}||x-y||}{l}}\)

class mellon.cov.RatQuad(alpha, ls=1.0)#

Bases: Covariance

k(x, y)#

\((1 + \frac{||x-y||^2}{2 \alpha l^2})^{-\alpha l}\)