MeanVarPosteriorSampledEstimates

class axtreme.sampling.base.MeanVarPosteriorSampledEstimates(*args, **kwargs)

Bases: Protocol

This is a protocol for classes that computes estimates using a PosteriorSampler.

Some posterior samplers require that mean and variance of the estimates calculated by sampling from it need to be calculated in a special way. This protocol allows for these special methods to be easier to be used on the outside of the class that inherits from this protocol. One example of a case where this is useful is when using a posterior sampler in a QoIEstimator. For more information check issue #132.

Example of usage:

class MyClass(MeanVarPosteriorSampledEstimates):
    def __init__(self, posterior_sampler: PosteriorSampler):
        self.posterior_sampler = posterior_sampler

    def f(self, x: torch.Tensor) -> torch.Tensor:
        # Some functions that takes the samples and returns a tensor
        # The dimension representing the posterior_sampling should be preserved.
        ...

    def foo(self) -> torch.Tensor:
        #  Get a model and points x to evaluate posterior
        x: torch.Tensor = ...
        model: botorch.models.Model = ...

        # Calculating posterior
        posterior = model.posterior(x)

        # Sampling from the posterior
        samples = self.posterior_sampler(posterior)

        return self.f(samples)


estimator = MyClass(posterior_sampler)
result = estimator.foo()

# Using the mean and var methods from MeanVarPosteriorSampledEstimates
# This will use the mean and var methods from the posterior_sampler if they are available.
mean = estimator.mean(result)
var = estimator.var(result)
__init__(*args, **kwargs)

Methods

__init__(*args, **kwargs)

mean(x)

Function that computes the mean of the estimates produced by using self.posterior_sampler.

var(x)

Function that computes the variance of the estimates produced by using self.posterior_sampler.

Attributes

mean(x: Tensor) Tensor

Function that computes the mean of the estimates produced by using self.posterior_sampler.

The dimension representing the posterior_sampling should be preserved from the sampling using self.posterior_sampler to sample a posterior until using this method.

For many applications this method will just be using a default implementation that computes the mean. E.g using torch.mean(x) like in this implementation.

However, in some special cases, it might be useful to provide a custom implementation to give a more accurate estimate. For instance if one uses UTSampler to sample the posterior the mean should be estimated as a weighted sum of the estimates.

This function uses the mean method of the posterior_sampler if it is available, otherwise it uses the default implementation.

Parameters:

x – A tensor of the estimates with shape (num_posterior_samples,).

Returns:

The mean of the the estimates. Should be a scalar with shape ().

Return type:

torch.Tensor

var(x: Tensor) Tensor

Function that computes the variance of the estimates produced by using self.posterior_sampler.

The dimension representing the posterior_sampling should be preserved from the sampling using self.posterior_sampler to sample a posterior until using this method.

For many applications this method will just be using a default implementation that computes the variance. E.g using torch.var(x) like in this implementation.

However, in some special cases, it might be useful to provide a custom implementation to give a more accurate estimate. For instance if one uses UTSampler to sample the posterior the variance should be estimated in a special way.

This function uses the variance method of the posterior_sampler if it is available, otherwise it uses the default implementation.

Parameters:

x – A tensor of the estimates with shape (num_posterior_samples,).

Returns:

The variance of the the estimates. Should be a scalar with shape ().

Return type:

torch.Tensor

posterior_sampler: PosteriorSampler