MultivariateMuyGPS

class MuyGPyS.gp.multivariate_muygps.MultivariateMuyGPS(*model_args)[source]

Multivariate Local Kriging Gaussian Process.

Performs approximate GP inference by locally approximating an observation’s response using its nearest neighbors with a separate kernel allocated for each response dimension, implemented as individual MuyGPyS.gp.muygps.MuyGPS objects.

This class is similar in interface to MuyGPyS.gp.muygps.MuyGPS, but requires a list of hyperparameter dicts at initialization.

Example

>>> from MuyGPyS.gp import MultivariateMuyGPS as MMuyGPS
>>> k_kwargs1 = {
...     "noise": Parameter(1e-5),
...     "kernel": Matern(
...         smoothness=Parameter(0.67, (0.1, 2.5)),
...         deformation=Isotropy(
...             metric=l2,
...             length_scale=Parameter(0.2),
...         scale=AnalyticScale(),
...     ),
... }
>>> k_kwargs2 = {
...     "noise": Parameter(1e-5),
...     "kernel": Matern(
...         smoothness=Parameter(0.67, (0.1, 2.5)),
...         deformation=Isotropy(
...             metric=l2,
...             length_scale=Parameter(0.2),
...         scale=AnalyticScale(),
...     ),
... }
>>> k_args = [k_kwargs1, k_kwargs2]
>>> mmuygps = MMuyGPS(*k_args)

We can realize kernel tensors for each of the models contained within a MultivariateMuyGPS object by iterating over its models member. Once we have computed pairwise_diffs and crosswise_diffs tensors, it is straightforward to perform each of these realizations.

Example

>>> for model in MuyGPyS.models:
>>>     Kin = model.kernel(pairwise_diffs)
>>>     Kcross = model.kernel(crosswise_diffs)
>>>     # do something with Kin and Kcross...
Args
model_args:

Dictionaries defining each internal MuyGPyS.gp.muygps.MuyGPS instance.

fast_coefficients(pairwise_diffs_fast, train_nn_targets_fast)[source]

Produces coefficient matrix for the fast posterior mean given in Equation (8) of [dunton2022fast] for each response dimenion.

Fro each response dimension \(j\), given observation set \(X\) with responses \(Y\), noise prior set \(\varepsilon^{(j)}\), and kernel function \(Kin_{\theta^{(j)}}(\cdot, \cdot)\), computes the following for each observation element \(\mathbf{x}_i\) with nearest neighbors index set \(N^*_i\), containing i and the indices of the nn_count - 1 nearest neighbors of \(\mathbf{x}_i\):

\[C^{(j)}_i = \left ( Kin_{\theta^{(j)}}(X_{N_i}, X_{N_i}) + \varepsilon^{(j)}_{N_i} \right )^{-1} Y(X_{N_i})_{:, j}.\]
Parameters:
  • pairwise_diffs – A tensor of shape (train_count, nn_count, nn_count, feature_count) containing the (nn_count, nn_count, feature_count)-shaped pairwise nearest neighbor difference tensors corresponding to each of the batch elements.

  • batch_nn_targets – A tensor of shape (train_count, nn_count, response_count) listing the vector-valued responses for the nearest neighbors of each batch element.

Return type:

ndarray

Returns:

A tensor of shape (batch_count, nn_count, response_count) whose entries comprise the precomputed coefficients for fast posterior mean inference.

fast_posterior_mean(crosswise_diffs, coeffs_tensor)[source]

Performs fast posterior mean inference using provided cross-covariance and precomputed coefficient matrix for each response dimension.

Returns the predicted response across each response dimension in the form of a posterior mean for each element of the batch of observations, as computed in Equation (9) of [dunton2022fast]. For each response dimension \(j\), given the coefficients \(C^{(j)}\) created by fast_coefficients() and Equation (8) of [dunton2022fast], observation set \(X\), noise prior set \(\varepsilon^{(j)}\), and kernel function \(Kin_{\theta^{(j)}}(\cdot, \cdot)\), computes the following for each test point \(\mathbf{z}\) and index set \(N^*_i\) containing the union of the index \(i\) of the nearest neighbor \(\mathbf{x}_i\) of \(\mathbf{z}\) and the nn_count - 1 nearest neighbors of \(\mathbf{x}_i\):

\[\widehat{Y} \left ( \mathbf{z} \mid X \right )_j = \sigma^2 Kin_{\theta^{(j)}}(\mathbf{z}, X_{N^*_i}) C^{(j)}_i.\]
Parameters:
  • crosswise_diffs (ndarray) – A matrix of shape (batch_count, nn_count, feature_count) whose rows list the difference between each feature of each batch element element and its nearest neighbors.

  • coeffs_tensor (ndarray) – A tensor of shape (batch_count, nn_count, response_count) providing the precomputed coefficients.

Return type:

ndarray

Returns:

A matrix of shape (batch_count, response_count) whose rows are the predicted response for each of the given indices.

fixed()[source]

Checks whether all kernel and model parameters are fixed for each model, excluding \(\sigma^2\).

Return type:

bool

Returns:

Returns True if all parameters in all models are fixed, and False otherwise.

make_predict_tensors(batch_indices, batch_nn_indices, test_features, train_features, train_targets, **kwargs)[source]

Create the metric and target tensors for prediction using the model’s deformation.

@NOTE[mwp] uses the first model’s deformation, and expects all model deformations to agree in tensor shapes.

Creates the crosswise_tensor, pairwise_tensor and batch_nn_targets tensors required by posterior_mean() and posterior_variance().

Parameters:
  • batch_indices (ndarray) – A vector of integers of shape (batch_count,) identifying the training batch of observations to be approximated.

  • batch_nn_indices (ndarray) – A matrix of integers of shape (batch_count, nn_count) listing the nearest neighbor indices for all observations in the batch.

  • test_features (Optional[ndarray]) – The full floating point testing data matrix of shape (test_count, feature_count).

  • train_features (ndarray) – The full floating point training data matrix of shape (train_count, ...).

  • train_targets (ndarray) – A matrix of shape (train_count, ...) whose rows are vector-valued responses for each training element.

Return type:

Tuple[ndarray, ndarray, ndarray]

Returns:

  • crosswise_tensor – A tensor of shape (batch_count, nn_count, ...) whose second and subsequent dimensions list the metric comparison between each batch element element and its nearest neighbors.

  • pairwise_diffs – A tensor of shape (batch_count, nn_count, nn_count, ...) containing the (nn_count, nn_count, ...)-shaped pairwise nearest neighbor metrics tensors corresponding to each of the batch elements.

  • batch_nn_targets – Tensor of floats of shape (batch_count, nn_count, ...) containing the expected response for each nearest neighbor of each batch element.

make_train_tensors(batch_indices, batch_nn_indices, train_features, train_targets, **kwargs)[source]

Create the metric and target tensors needed for training.

@NOTE[mwp] uses the first model’s deformation, and expects all model deformations to agree in tensor shapes.

Similar to make_predict_tensors() but returns the additional batch_targets matrix, which is only defined for a batch of training data.

Parameters:
  • batch_indices (ndarray) – A vector of integers of shape (batch_count,) identifying the training batch of observations to be approximated.

  • batch_nn_indices (ndarray) – A matrix of integers of shape (batch_count, nn_count) listing the nearest neighbor indices for all observations in the batch.

  • train_features (ndarray) – The full floating point training data matrix of shape (train_count, ...).

  • train_targets (ndarray) – A matrix of shape (train_count, ...) whose rows are vector-valued responses for each training element.

Return type:

Tuple[ndarray, ndarray, ndarray, ndarray]

Returns:

  • crosswise_tensor – A tensor of shape (batch_count, nn_count, ...) whose second and subsequent dimensions list the metric comparison between each batch element element and its nearest neighbors.

  • pairwise_diffs – A tensor of shape (batch_count, nn_count, nn_count, ...) containing the (nn_count, nn_count, ...)-shaped pairwise nearest neighbor metrics tensors corresponding to each of the batch elements.

  • batch_targets – Matrix of floats of shape (batch_count, ...) whose rows give the expected response for each batch element.

  • batch_nn_targets – Tensor of floats of shape (batch_count, nn_count, ...) containing the expected response for each nearest neighbor of each batch element.

optimize_scale(pairwise_diffs, nn_targets)[source]

Optimize the value of the \(sigma^2\) scale parameter for each response dimension.

Uses the optimization method specified by the types of the scale parameters to optimize their value.

Parameters:
  • pairwise_diffs (ndarray) – A tensor of shape (batch_count, nn_count, nn_count, feature_count) containing the (nn_count, nn_count, feature_count)-shaped pairwise nearest neighbor difference tensors corresponding to each of the batch elements.

  • nn_targets (ndarray) – Tensor of floats of shape (batch_count, nn_count, response_count) containing the expected response for each nearest neighbor of each batch element.

Returns:

A reference to this model whose global scale parameter (and those of its submodels) has been optimized.

posterior_mean(pairwise_diffs, crosswise_diffs, batch_nn_targets)[source]

Performs simultaneous posterior mean inference on provided difference tensors and the target matrix.

Computes parallelized local solves of systems of linear equations using the kernel realizations, one for each internal model, of the last two dimensions of pairwise_diffs along with crosswise_diffs and batch_nn_targets to predict responses in terms of the posterior mean. Assumes that difference tensors pairwise_diffs and crosswise_diffs are already computed and given as arguments.

Returns the predicted response in the form of a posterior mean for each element of the batch of observations by solving a system of linear equations induced by each kernel functor, one per response dimension, in a generalization of Equation (3.4) of [muyskens2021muygps]. For each response dimension \(j\), given observation set \(X\) with responses \(Y\), noise prior set \(\varepsilon^{(j)}\), and kernel function \(Kin_{\theta^{(j)}}(\cdot, \cdot)\), computes the following for each prediction element \(\mathbf{z}_i\) with nearest neighbors index set \(N_i\):

\[\widehat{Y} (\mathbf{z}_i \mid X_{N_i})_j = \sigma^2_j Kin_{\theta^{(j)}} (\mathbf{z}_i, X_{N_i}) \left ( Kin_{\theta^{(j)}} (X_{N_i}, X_{N_i}) + \varepsilon^{(j)}_{N_i} \right )^{-1} Y(X_{N_i})_{:,j}.\]
Parameters:
  • pairwise_diffs (ndarray) – A tensor of shape (batch_count, nn_count, nn_count, feature_count) containing the (nn_count, nn_count, feature_count)-shaped pairwise nearest neighbor difference tensors corresponding to each of the batch elements.

  • crosswise_diffs (ndarray) – A matrix of shape (batch_count, nn_count, feature_count) whose rows list the difference between each feature of each batch element element and its nearest neighbors.

  • batch_nn_targets (ndarray) – A tensor of shape (batch_count, nn_count, response_count) listing the vector-valued responses for the nearest neighbors of each batch element.

Return type:

ndarray

Returns:

A matrix of shape (batch_count, response_count) whose rows are the predicted response for each of the given indices.

posterior_variance(pairwise_diffs, crosswise_diffs)[source]

Returns the posterior variance from the provided difference tensors.

Return the local posterior variances of each prediction, corresponding to the diagonal elements of a covariance matrix. For each response dimension, given observation set \(X\) with responses \(Y\), noise prior set \(\varepsilon^{(j)}\), and kernel function \(Kin_{\theta^{(j)}}(\cdot, \cdot)\), computes the following for each prediction element \(\mathbf{z}_i\) with nearest neighbors index set \(N_i\):

\[Var \left ( \widehat{Y} (\mathbf{z}_i \mid X_{N_i}) \right)_j = \sigma_j^2 \left ( Kin_{\theta^{(j)}} (\mathbf{z}_i, \mathbf{z}_i) - Kin_{\theta^{(j)}} (\mathbf{z}_i, X_{N_i}) \left ( Kin_{\theta^{(j)}} (X_{N_i}, X_{N_i} \right ) + \varepsilon^{(j)}_{N_i})^{-1} Kin_{\theta^{(j)}} (X_{N_i}, \mathbf{z}_i) \right ).\]
Parameters:
  • pairwise_diffs (ndarray) – A tensor of shape (batch_count, nn_count, nn_count, feature_count) containing the (nn_count, nn_count, feature_count)-shaped pairwise nearest neighbor difference tensors corresponding to each of the batch elements.

  • crosswise_diffs (ndarray) – A matrix of shape (batch_count, nn_count, feature_count) whose rows list the difference between each feature of each batch element element and its nearest neighbors.

Return type:

ndarray

Returns:

A vector of shape (batch_count, response_count) consisting of the diagonal elements of the posterior variance for each model.