glearn.Covariance.get_matrix#

Covariance.get_matrix(sigma=None, sigma0=None, scale=None, derivative=[])#

Compute the covariance matrix or its derivatives for a given set of hyperparameters.

Parameters:
sigmafloat, default=None

The hyperparameter \(\sigma\) of the covariance model where \(\sigma^2\) represents the variance of the correlated errors of the model. \(\sigma\) should be positive and cannot be None.

sigma0float, default=None

The hyperparameter \(\varsigma\) of the covariance model where \(\varsigma^2\) represents the variance of the input noise to of the model. \(\sigma\) should be positive and cannot be None.

scalefloat or array_like[float], default=None

The scale hyperparameters \(\boldsymbol{\alpha} = (\alpha_1, \dots, \alpha_d)\) in scales the distance between data points in \(\mathbb{R}^d\). If an array of the size \(d\) is given, each \(\alpha_i\) scales the distance in the \(i\)-th dimension. If a scalar value \(\alpha\) is given, all dimensions are scaled isometrically. \(\boldsymbol{\alpha}\) cannot be None.

derivativelist, default=[]

Specifies a list of derivatives of covariance matrix with respect to the hyperparameters \(\boldsymbol{\alpha} = (\alpha_1, \dots, \alpha_d)\). A list of the size \(q\) with the components [i, j, ..., k] corresponds to take the derivative

\[\left. \frac{\partial^q}{\partial \alpha_{i+1} \partial \alpha_{j+1} \dots \partial \alpha_{k+1}} \boldsymbol{\Sigma}(\boldsymbol{\alpha} \vert \sigma^2, \varsigma^2) \right|_{\boldsymbol{\alpha}}.\]

Note

The derivative with respect to each hyperparameter \(\alpha_i\) can be at most of the order two, \(\partial^2 / \partial \alpha_i^2\). That is, each index in the derivative list can appear at most twice. For instance derivative=[1, 1] (second order derivative with respect to \(\alpha_{2}\)) is a valid input argument, how ever derivative=[1, 1, 1] (third order derivative) is an invalid input.

Returns:
Snumpy.ndarray

An array of the size \(n \times \times n\) where \(n\) is the size of the matrix.

Notes

This function returns

\[\left. \frac{\partial^q}{\partial \alpha_{i} \partial \alpha_{j} \dots \partial \alpha_{k}} \boldsymbol{\Sigma}(\boldsymbol{\alpha} \vert \sigma, \varsigma) \right|_{\boldsymbol{\alpha}},\]

where the covariance matrix \(\boldsymbol{\Sigma}\) is defined by

\[\boldsymbol{\Sigma}(\boldsymbol{\alpha}, \sigma, \varsigma) = \sigma^2 \mathbf{K}(\boldsymbol{\alpha}) + \varsigma^2 \mathbf{I}.\]

In the above, \(\mathbf{I}\) is the identity matrix and \(\mathbf{K}\) is the correlation matrix that depends on a set of scale hyperparameters \(\boldsymbol{\alpha}=(\alpha_1, \dots, \alpha_d)\).

Derivatives:

Note that the indices in list derivative=[i, j, ..., k] are zero-indexed, meaning that the index i corresponds to take derivative with respect to the hyperparameter \(\alpha_{i+1}\). For instance:

  • [] corresponds to no derivative.

  • [0] corresponds to \(\partial / \partial \alpha_1\) and [1] corresponds to \(\partial / \partial \alpha_2\).

  • [0, 2] corresponds to \(\partial^2 / \partial \alpha_1 \partial \alpha_3\).

  • [0, 0] corresponds to \(\partial^2 / \partial \alpha_1^2\).

  • [0, 2, 2, 4] corresponds to \(\partial^4 / \partial \alpha_1 \partial \alpha_{3}^2 \partial \alpha_5\).

Output Matrix:

The covariance matrix \(\boldsymbol{\Sigma}\) or its derivatives is symmetric, i.e., \(\partial \Sigma_{ij} = \partial \Sigma_{ji}\). Also, the covariance matrix is positive-definite, however, its derivatives are not necessarily positive-definite. In addition, the diagonal elements of the derivatives of the covariance matrix are zero, i.e., \(\partial \Sigma_{ii} = 0\).

Examples

Basic Usage:

Create a sample dataset with four points in \(d=2\) dimensional space. Then, compute the covariance matrix \(\boldsymbol{\Sigma} (\boldsymbol{\alpha}, \sigma, \varsigma)\) for \(\boldsymbol{\alpha} = (1, 2)\), \(\sigma=2\), and \(\varsigma=3\).

>>> # Generate a set of points
>>> from glearn.sample_data import generate_points
>>> x = generate_points(num_points=4, dimension=2)

>>> # Create a covariance object
>>> from glearn import Covariance
>>> cov = Covariance(x)

>>> # Compute covariance matrix
>>> C = cov.get_matrix(sigma=2.0, sigma0=3.0, scale=[1.0, 2.0])
array([[13.        ,  3.61643745,  3.51285267,  3.47045163],
       [ 3.61643745, 13.        ,  3.32078482,  3.14804532],
       [ 3.51285267,  3.32078482, 13.        ,  3.53448631],
       [ 3.47045163,  3.14804532,  3.53448631, 13.        ]])

Taking Derivatives:

Compute the second mixed derivative

\[\frac{\partial^2}{\partial \alpha_1 \partial \alpha_2} \boldsymbol{\Sigma}(\alpha_1, \alpha_2 \vert \sigma, \varsigma).\]
>>> # Compute second mixed derivative
>>> C = cov.get_matrix(sigma=2.0, sigma0=3.0, scale=[1.0, 2.0],
...    derivative=[0, 1])
array([[0.         0.04101073 0.01703885 0.0667311 ]
       [0.04101073 0.         0.02500619 0.11654524]
       [0.01703885 0.02500619 0.         0.00307613]
       [0.0667311  0.11654524 0.00307613 0.        ]])

Note that as mentioned in the above notes, its diagonal elements are zero.