botorch.sampling

Monte-Carlo Samplers

Sampler modules to be used with MC-evaluated acquisition functions.

class botorch.sampling.samplers.MCSampler(batch_range=0, - 2)[source]

Bases: torch.nn.modules.module.Module, abc.ABC

Abstract base class for Samplers.

Subclasses must implement the _construct_base_samples method.

resample

If True, re-draw samples in each forward evaluation - this results in stochastic acquisition functions (and thus should not be used with deterministic optimization algorithms).

collapse_batch_dims

If True, collapse the t-batch dimensions of the produced samples to size 1. This is useful for preventing sampling variance across t-batches.

Example

This method is usually not called directly, but via the sampler’s __call__ method: >>> posterior = model.posterior(test_X) >>> samples = sampler(posterior)

Abstract base class for Samplers.

Parameters

batch_range (Tuple[int, int]) – The range of t-batch dimensions in the event_shape used by collapse_batch_dims. The t-batch dims are batch_range[0]:batch_range[1]. By default, this is (0, -2), for the case where the non-batch dimensions are -2 (q) and -1 (d) and all dims in the front are t-batch dims.

property batch_range

The t-batch range.

Return type

Tuple[int, int]

forward(posterior)[source]

Draws MC samples from the posterior.

Parameters

posterior (Posterior) – The Posterior to sample from.

Return type

Tensor

Returns

The samples drawn from the posterior.

property sample_shape

The shape of a single sample

Return type

Size

training: bool
class botorch.sampling.samplers.IIDNormalSampler(num_samples, resample=False, seed=None, collapse_batch_dims=True, batch_range=0, - 2)[source]

Bases: botorch.sampling.samplers.MCSampler

Sampler for MC base samples using iid N(0,1) samples.

Example

>>> sampler = IIDNormalSampler(1000, seed=1234)
>>> posterior = model.posterior(test_X)
>>> samples = sampler(posterior)

Sampler for MC base samples using iid N(0,1) samples.

Parameters
  • num_samples (int) – The number of samples to use.

  • resample (bool) – If True, re-draw samples in each forward evaluation - this results in stochastic acquisition functions (and thus should not be used with deterministic optimization algorithms).

  • seed (Optional[int]) – The seed for the RNG. If omitted, use a random seed.

  • collapse_batch_dims (bool) – If True, collapse the t-batch dimensions to size 1. This is useful for preventing sampling variance across t-batches.

  • batch_range (Tuple[int, int]) – The range of t-batch dimensions in the event_shape used by collapse_batch_dims. The t-batch dims are batch_range[0]:batch_range[1]. By default, this is (0, -2), for the case where the non-batch dimensions are -2 (q) and -1 (d) and all dims in the front are t-batch dims.

training: bool
class botorch.sampling.samplers.SobolQMCNormalSampler(num_samples, resample=False, seed=None, collapse_batch_dims=True, batch_range=0, - 2)[source]

Bases: botorch.sampling.samplers.MCSampler

Sampler for quasi-MC base samples using Sobol sequences.

Example

>>> sampler = SobolQMCNormalSampler(1000, seed=1234)
>>> posterior = model.posterior(test_X)
>>> samples = sampler(posterior)

Sampler for quasi-MC base samples using Sobol sequences.

Parameters
  • num_samples (int) – The number of samples to use.

  • resample (bool) – If True, re-draw samples in each forward evaluation - this results in stochastic acquisition functions (and thus should not be used with deterministic optimization algorithms).

  • seed (Optional[int]) – The seed for the RNG. If omitted, use a random seed.

  • collapse_batch_dims (bool) – If True, collapse the t-batch dimensions to size 1. This is useful for preventing sampling variance across t-batches.

  • batch_range (Tuple[int, int]) – The range of t-batch dimensions in the event_shape used by collapse_batch_dims. The t-batch dims are batch_range[0]:batch_range[1]. By default, this is (0, -2), for the case where the non-batch dimensions are -2 (q) and -1 (d) and all dims in the front are t-batch dims.

training: bool

Pairwise Monte-Carlo Samplers

class botorch.sampling.pairwise_samplers.PairwiseMCSampler(max_num_comparisons=None, seed=None)[source]

Bases: botorch.sampling.samplers.MCSampler

Abstract class for Pairwise MC Sampler.

This sampler will sample pairwise comparisons. It is to be used together with PairwiseGP and BoTorch acquisition functions (e.g., qKnowledgeGradient)

Parameters
  • max_num_comparisons (Optional[int]) – Max number of comparisons drawn within samples. If None, use all possible pairwise comparisons

  • seed (Optional[int]) – The seed for np.random.seed. If omitted, use a random seed. May be overwritten by sibling classes or subclasses.

forward(posterior)[source]

Draws MC samples from the posterior and make comparisons

Parameters

posterior (Posterior) – The Posterior to sample from. The returned samples are expected to have output dimension of 1.

Return type

Tensor

Returns

Posterior sample pairwise comparisons.

training: bool
class botorch.sampling.pairwise_samplers.PairwiseIIDNormalSampler(num_samples, resample=False, seed=None, collapse_batch_dims=True, max_num_comparisons=None)[source]

Bases: botorch.sampling.pairwise_samplers.PairwiseMCSampler, botorch.sampling.samplers.IIDNormalSampler

Abstract class for Pairwise MC Sampler.

This sampler will sample pairwise comparisons. It is to be used together with PairwiseGP and BoTorch acquisition functions (e.g., qKnowledgeGradient)

Parameters
  • max_num_comparisons (Optional[int]) – Max number of comparisons drawn within samples. If None, use all possible pairwise comparisons

  • seed (Optional[int]) – The seed for np.random.seed. If omitted, use a random seed. May be overwritten by sibling classes or subclasses.

training: bool
class botorch.sampling.pairwise_samplers.PairwiseSobolQMCNormalSampler(num_samples, resample=False, seed=None, collapse_batch_dims=True, max_num_comparisons=None)[source]

Bases: botorch.sampling.pairwise_samplers.PairwiseMCSampler, botorch.sampling.samplers.SobolQMCNormalSampler

Abstract class for Pairwise MC Sampler.

This sampler will sample pairwise comparisons. It is to be used together with PairwiseGP and BoTorch acquisition functions (e.g., qKnowledgeGradient)

Parameters
  • max_num_comparisons (Optional[int]) – Max number of comparisons drawn within samples. If None, use all possible pairwise comparisons

  • seed (Optional[int]) – The seed for np.random.seed. If omitted, use a random seed. May be overwritten by sibling classes or subclasses.

training: bool

QMC Base Functionality

Quasi Monte-Carlo sampling from Normal distributions.

References:

Pages2018numprob(1,2)

G. Pages. Numerical Probability: An Introduction with Applications to Finance. Universitext. Springer International Publishing, 2018.

class botorch.sampling.qmc.NormalQMCEngine(d, seed=None, inv_transform=False)[source]

Bases: object

Engine for qMC sampling from a Multivariate Normal N(0, I_d).

By default, this implementation uses Box-Muller transformed Sobol samples following pg. 123 in [Pages2018numprob]. To use the inverse transform instead, set inv_transform=True.

Example

>>> engine = NormalQMCEngine(3)
>>> samples = engine.draw(10)

Engine for drawing qMC samples from a multivariate normal N(0, I_d).

Parameters
  • d (int) – The dimension of the samples.

  • seed (Optional[int]) – The seed with which to seed the random number generator of the underlying SobolEngine.

  • inv_transform (bool) – If True, use inverse transform instead of Box-Muller.

draw(n=1, out=None, dtype=torch.float32)[source]

Draw n qMC samples from the standard Normal.

Parameters
  • n (int) – The number of samples to draw.

  • out (Optional[Tensor]) – An option output tensor. If provided, draws are put into this tensor, and the function returns None.

  • dtype (dtype) – The desired torch data type (ignored if out is provided).

Return type

Optional[Tensor]

Returns

A n x d tensor of samples if out=None and None otherwise.

class botorch.sampling.qmc.MultivariateNormalQMCEngine(mean, cov, seed=None, inv_transform=False)[source]

Bases: object

Engine for qMC sampling from a multivariate Normal N(mu, Sigma).

By default, this implementation uses Box-Muller transformed Sobol samples following pg. 123 in [Pages2018numprob]. To use the inverse transform instead, set inv_transform=True.

Example

>>> mean = torch.tensor([1.0, 2.0])
>>> cov = torch.tensor([[1.0, 0.25], [0.25, 2.0]])
>>> engine = MultivariateNormalQMCEngine(mean, cov)
>>> samples = engine.draw(10)

Engine for qMC sampling from a multivariate Normal N(mu, Sigma).

Parameters
  • mean (Tensor) – The mean vector.

  • cov (Tensor) – The covariance matrix.

  • seed (Optional[int]) – The seed with which to seed the random number generator of the underlying SobolEngine.

  • inv_transform (bool) – If True, use inverse transform instead of Box-Muller.

draw(n=1, out=None)[source]

Draw n qMC samples from the multivariate Normal.

Parameters
  • n (int) – The number of samples to draw.

  • out (Optional[Tensor]) – An option output tensor. If provided, draws are put into this tensor, and the function returns None.

Return type

Optional[Tensor]

Returns

A n x d tensor of samples if out=None and None otherwise.