fast_posterior_mean
Resources and high-level API for a fast posterior mean inference workflow.
The MuyGPyS.examples subpackage is deprecated and will be streamlined or
removed in future versions.
make_fast_regressor() is a
high-level API for creating the necessary components for fast posterior mean
inference.
do_fast_posterior_mean() is a
high-level api for executing a simple, generic fast posterior medan workflow
given data.
It calls the maker APIs above and
fast_posterior_mean_any().
- MuyGPyS.examples.fast_posterior_mean.do_fast_posterior_mean(test_features, train_features, train_targets, nn_count=30, batch_count=200, loss_fn=<MuyGPyS.optimize.loss.LossFn object>, opt_fn=<MuyGPyS.optimize.chassis.OptimizeFn object>, k_kwargs={}, nn_kwargs={}, opt_kwargs={}, verbose=False)[source]
Convenience function initializing a model and performing fast posterior mean inference.
Expected parameters include keyword argument dicts specifying kernel parameters and nearest neighbor parameters. See the docstrings of the appropriate functions for specifics.
Example
>>> from MuyGPyS.testing.test_utils import _make_gaussian_data >>> from MuyGPyS.examples.fast_posterior_mean import do_fast_posterior_mean >>> from MuyGPyS.gp.deformation import F2, Isotropy >>> from MuyGPyS.gp.hyperparameter import Parameter >>> from MuyGPyS.gp.hyperparameter import AnalyticScale >>> from MuyGPyS.gp.kernels import RBF >>> from MuyGPyS.gp.noise import HomoscedasticNoise >>> from MuyGPyS.optimize import Bayes_optimize >>> from MuyGPyS.optimize.objective import mse_fn >>> train_features, train_responses = make_train() # stand-in function >>> test_features, test_responses = make_test() # stand-in function >>> nn_kwargs = {"nn_method": "exact", "algorithm": "ball_tree"} >>> k_kwargs = { ... "kernel": RBF( ... deformation=Isotropy( ... metric=F2, ... length_scale=Parameter(1.0, (1e-2, 1e2)) ... ) ... ), ... "noise": HomoscedasticNoise(1e-5), ... "scale": AnalyticScale(), ... } >>> ( ... muygps, nbrs_lookup, predictions, precomputed_coefficients_matrix ... ) = do_fast_posterior_mean( ... test_features, ... train_features, ... train_responses, ... nn_count=30, ... batch_count=200, ... loss_fn=lool_fn, ... opt_fn=Bayes_optimize, ... k_kwargs=k_kwargs, ... nn_kwargs=nn_kwargs, ... verbose=False, ... )
- Parameters:
test_features (
ndarray) – A matrix of shape(test_count, feature_count)whose rows consist of observation vectors of the test data.train_features (
ndarray) – A matrix of shape(train_count, feature_count)whose rows consist of observation vectors of the train data.train_targets (
ndarray) – A matrix of shape(train_count, response_count)whose rows consist of response vectors of the train data.nn_count (
int) – The number of nearest neighbors to employ.batch_count (
int) – The number of elements to sample batch for hyperparameter optimization.loss_fn (
LossFn) – The loss functor to use in hyperparameter optimization. Ignored if all of the parameters specified by argumentk_kwargsare fixed.opt_fn (
OptimizeFn) – The optimization functor to use in hyperparameter optimization. Ignored if all of the parameters specified by argumentk_kwargsare fixed.k_kwargs (
Dict) – Assume that the elements are keyword arguments to a MuyGPs model (seemake_regressor()).nn_kwargs (
Dict) – Parameters for the nearest neighbors wrapper. SeeMuyGPyS.neighbors.NN_Wrapperfor the supported methods and their parameters.opt_kwargs (
Dict) – Parameters for the wrapped optimizer. See the docs of the corresponding library for supported parameters.verbose (
bool) – IfTrue, print summary statistics.
- Return type:
Tuple[ndarray,NN_Wrapper,ndarray,ndarray,Dict]- Returns:
muygps – A (possibly trained) MuyGPs object.
nbrs_lookup – A data structure supporting nearest neighbor queries into
train_features.predictions – The predicted response associated with each test observation.
precomputed_coefficients_matrix – A matrix of shape
(train_count, nn_count)whose rows list the precomputed coefficients for each nearest neighbors set in the training data.timing – A dictionary containing timings for the training, precomputation, nearest neighbor computation, and prediction.
- MuyGPyS.examples.fast_posterior_mean.fast_posterior_mean_any(muygps, test_features, train_features, nbrs_lookup, train_targets)[source]
Convenience function performing fast posterior mean inference using a pre-trained model.
- Parameters:
muygps (
MuyGPS) – A (possibly trained) MuyGPS object.test_features (
ndarray) – A matrix of shape(test_count, feature_count)whose rows consist of observation vectors of the test data.train_features (
ndarray) – A matrix of shape(train_count, feature_count)whose rows consist of observation vectors of the train data.nbrs_lookup (
NN_Wrapper) – A data structure supporting nearest neighbor queries intotrain_features.train_targets (
ndarray) – A matrix of shape(train_count, response_count)whose rows consist of response vectors of the train data.
- Return type:
Tuple[ndarray,ndarray,Dict]- Returns:
posterior_mean – The predicted response associated with each test observation.
precomputed_coefficients_matrix – A matrix of shape
(train_count, nn_count)whose rows list the precomputed coefficients for each nearest neighbors set in the training data.timing – A dictionary containing timings for the training, precomputation, nearest neighbor computation, and prediction.
- MuyGPyS.examples.fast_posterior_mean.make_fast_regressor(muygps, nbrs_lookup, train_features, train_targets)[source]
Convenience function for creating precomputed coefficient matrix and neighbor lookup data structure.
- Parameters:
muygps (
MuyGPS) – A (possibly trained) MuyGPS object.nbrs_lookup (
NN_Wrapper) – A data structure supporting nearest neighbor queries intotrain_features.train_features (
ndarray) – A matrix of shape(train_count, feature_count)whose rows consist of observation vectors of the train data.train_targets (
ndarray) – A matrix of shape(train_count, response_count)whose rows consist of response vectors of the train data.
- Return type:
Tuple[ndarray,ndarray]- Returns:
precomputed_coefficients_matrix – A matrix of shape
(train_count, nn_count)whose rows list the precomputed coefficients for each nearest neighbors set in the training data.nn_indices – A numpy.ndarrray supporting nearest neighbor queries.