kernels

Hyperparameters and kernel functors

Defines kernel functors (inheriting KernelFn) that transform crosswise difference tensors into cross-covariance matrices and pairwise difference matrices into covariance or kernel tensors.

See the following example to initialize an MuyGPyS.gp.kernels.Matern object. Other kernel functors are similar, but require different hyperparameters.

Example

>>> from MuyGPyS.gp.kernels import Matern
>>> kern = Matern(
...     smoothness=Parameter("log_sample", (0.1, 2.5)),
...     deformation=Isotropy(
...         metric=l2,
...         length_scale=Parameter(1.0),
...     ),
... )

One uses a previously computed pairwise_diffs tensor (see MuyGPyS.gp.tensor.pairwise_tensor()) to compute a kernel tensor whose second two dimensions contain square kernel matrices. Similarly, one uses a previously computed crosswise_diffs matrix (see MuyGPyS.gp.tensor.crosswise_diffs()) to compute a cross-covariance matrix. See the following example, which assumes that you have already constructed the difference numpy.ndarrays and the kernel kern as shown above.

Example

>>> K = kern(pairwise_diffs)
>>> Kcross = kern(crosswise_diffs)
class MuyGPyS.gp.kernels.kernel_fn.KernelFn(deformation)[source]

Bases: object

A kernel functor.

Base class for kernel functors that include a hyperparameter Dict and a call mechanism.

Parameters:

kwargs – Ignored (by this base class) keyword arguments.

__call__(diffs, **kwargs)[source]

Call self as a function.

Return type:

ndarray

get_opt_params()[source]

Report lists of unfixed hyperparameter names, values, and bounds.

Return type:

Tuple[List[str], List[float], List[Tuple[float, float]]]

Returns:

names:

A list of unfixed hyperparameter names.

params:

A list of unfixed hyperparameter values.

bounds:

A list of unfixed hyperparameter bound tuples.

set_params(**kwargs)[source]

Reset hyperparameters using hyperparameter dict(s).

Parameters:

kwargs – Hyperparameter kwargs.

Return type:

None

class MuyGPyS.gp.kernels.rbf.RBF(deformation=<MuyGPyS.gp.deformation.isotropy.Isotropy object>, _backend_fn=<function _rbf_fn>)[source]

The radial basis function (RBF) or squared-exponential kernel.

The RBF kernel includes a parameterized scaled distance function \(d_\ell(\cdot, \cdot)\).

The kernel is defined by

\[K(x_i, x_j) = \exp\left(- d_\ell(x_i, x_j)\right).\]

Typically, \(d(\cdot,\cdot)\) is the squared Euclidean distance or second frequency moment of the difference of the operands.

Parameters:

deformation (DeformationFn) – The deformation functor to be used. Includes length_scale hyperparameter information via the MuyGPyS.gp.deformation module

__call__(diffs, **kwargs)[source]

Compute RBF kernel(s) from a difference tensor.

Parameters:

diffs (ndarray) – A tensor of pairwise diffs of shape (data_count, nn_count, nn_count, feature_count) or (data_count, nn_count, feature_count). In the four dimensional case, it is assumed that the diagonals dists diffs[i, j, j, :] == 0.

Return type:

ndarray

Returns:

A cross-covariance matrix of shape (data_count, nn_count) or a tensor of shape (data_count, nn_count, nn_count) whose last two dimensions are kernel matrices.

get_opt_fn()[source]

Return a kernel function with fixed parameters set.

Assumes that optimization parameter literals will be passed as keyword arguments.

Return type:

Callable

Returns:

A function implementing the kernel where all fixed parameters are set. The function expects keyword arguments corresponding to current hyperparameter values for unfixed parameters.

class MuyGPyS.gp.kernels.matern.Matern(smoothness=<MuyGPyS.gp.hyperparameter.scalar.Parameter object>, deformation=<MuyGPyS.gp.deformation.isotropy.Isotropy object>, **_backend_fns)[source]

The Matérn kernel.

The Màtern kernel includes a parameterized deformation model \(d_\ell(\cdot, \cdot)\) and an additional smoothness parameter \(\nu>0\). \(\nu\) is proportional to the smoothness of the resulting function. As \(\nu\rightarrow\infty\), the kernel becomes equivalent to the RBF kernel. When \(\nu = 1/2\), the Matérn kernel is identical to the absolute exponential kernel. Important intermediate values are \(\nu=1.5\) (once differentiable functions) and \(\nu=2.5\) (twice differentiable functions).

The kernel is defined by

\[k(x_i, x_j) = \frac{1}{\Gamma(\nu)2^{\nu-1}}\Bigg( \frac{\sqrt{2\nu}}{l} d_\ell(x_i , x_j ) \Bigg)^\nu K_\nu\Bigg( \frac{\sqrt{2\nu}}{l} d(x_i , x_j )\Bigg),\]

where \(K_{\nu}(\cdot)\) is a modified Bessel function and \(\Gamma(\cdot)\) is the gamma function. Typically, \(d(\cdot,\cdot)\) is the Euclidean distance or \(\ell_2\) norm of the difference of the operands.

Parameters:
  • smoothness (Parameter) – A parameter determining the differentiability of the function distribution.

  • deformation (DeformationFn) – The deformation functor to be used. Includes length_scale hyperparameter information via the MuyGPyS.gp.deformation module.

__call__(diffs, **kwargs)[source]

Compute Matern kernels from distance tensor.

Takes inspiration from scikit-learn’s implementation.

Parameters:

diffs – A tensor of pairwise differences of shape (data_count, nn_count, nn_count, feature_count). It is assumed that the vectors along the diagonals diffs[i, j, j, :] == 0.

Returns:

A cross-covariance matrix of shape (data_count, nn_count) or a tensor of shape (data_count, nn_count, nn_count) whose last two dimensions are kernel matrices.

get_opt_fn()[source]

Return a kernel function with fixed parameters set.

Assumes that optimization parameter literals will be passed as keyword arguments.

Return type:

Callable

Returns:

A function implementing the kernel where all fixed parameters are set. The function expects keyword arguments corresponding to current hyperparameter values for unfixed parameters.

get_opt_params()[source]

Report lists of unfixed hyperparameter names, values, and bounds.

Return type:

Tuple[List[str], List[float], List[Tuple[float, float]]]

Returns:

names:

A list of unfixed hyperparameter names.

params:

A list of unfixed hyperparameter values.

bounds:

A list of unfixed hyperparameter bound tuples.