Skip to content

Add Posterior Standard Deviation acquisition function #2060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions botorch/acquisition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
LogNoisyExpectedImprovement,
NoisyExpectedImprovement,
PosteriorMean,
PosteriorStandardDeviation,
ProbabilityOfImprovement,
qAnalyticProbabilityOfImprovement,
UpperConfidenceBound,
Expand Down Expand Up @@ -91,6 +92,7 @@
"PairwiseBayesianActiveLearningByDisagreement",
"PairwiseMCPosteriorVariance",
"PosteriorMean",
"PosteriorStandardDeviation",
"PriorGuidedAcquisitionFunction",
"ProbabilityOfImprovement",
"ProximalAcquisitionFunction",
Expand Down
52 changes: 52 additions & 0 deletions botorch/acquisition/analytic.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,58 @@ def forward(self, X: Tensor) -> Tensor:
return self._mean_and_sigma(X, compute_sigma=False)[0] @ self.weights


class PosteriorStandardDeviation(AnalyticAcquisitionFunction):
r"""Single-outcome Posterior Standard Deviation.

An acquisition function for pure exploration.
Only supports the case of q=1. Requires the model's posterior to have
`mean` and `variance` properties. The model must be either single-outcome
or combined with a `posterior_transform` to produce a single-output posterior.

Example:
>>> model = SingleTaskGP(train_X, train_Y)
>>> PSTD = PosteriorMean(model)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be PosteriorStandardDeviation right? I'll put in a quick fix.

>>> std = PSTD(test_X)
"""

def __init__(
self,
model: Model,
posterior_transform: Optional[PosteriorTransform] = None,
maximize: bool = True,
) -> None:
r"""Single-outcome Posterior Mean.

Args:
model: A fitted single-outcome GP model (must be in batch mode if
candidate sets X will be)
posterior_transform: A PosteriorTransform. If using a multi-output model,
a PosteriorTransform that transforms the multi-output posterior into a
single-output posterior is required.
maximize: If True, consider the problem a maximization problem. Note
that if `maximize=False`, the posterior standard deviation is negated.
As a consequence,
`optimize_acqf(PosteriorStandardDeviation(gp, maximize=False))`
actually returns -1 * minimum of the posterior standard deviation.
"""
super().__init__(model=model, posterior_transform=posterior_transform)
self.maximize = maximize

@t_batch_mode_transform(expected_q=1)
def forward(self, X: Tensor) -> Tensor:
r"""Evaluate the posterior standard deviation on the candidate set X.

Args:
X: A `(b1 x ... bk) x 1 x d`-dim batched tensor of `d`-dim design points.

Returns:
A `(b1 x ... bk)`-dim tensor of Posterior Mean values at the
given design points `X`.
"""
_, std = self._mean_and_sigma(X)
return std if self.maximize else -std


# --------------- Helper functions for analytic acquisition functions. ---------------


Expand Down
43 changes: 43 additions & 0 deletions test/acquisition/test_analytic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
LogProbabilityOfImprovement,
NoisyExpectedImprovement,
PosteriorMean,
PosteriorStandardDeviation,
ProbabilityOfImprovement,
ScalarizedPosteriorMean,
UpperConfidenceBound,
Expand Down Expand Up @@ -292,6 +293,48 @@ def test_posterior_mean_batch(self):
PosteriorMean(model=mm2)


class TestPosteriorStandardDeviation(BotorchTestCase):
def test_posterior_stddev(self):
for dtype in (torch.float, torch.double):
mean = torch.rand(3, 1, device=self.device, dtype=dtype)
std = torch.rand_like(mean)
mm = MockModel(MockPosterior(mean=mean, variance=std.square()))

acqf = PosteriorStandardDeviation(model=mm)
X = torch.rand(3, 1, 2, device=self.device, dtype=dtype)
pm = acqf(X)
self.assertTrue(torch.equal(pm, std.view(-1)))

acqf = PosteriorStandardDeviation(model=mm, maximize=False)
X = torch.rand(3, 1, 2, device=self.device, dtype=dtype)
pm = acqf(X)
self.assertTrue(torch.equal(pm, -std.view(-1)))

# check for proper error if multi-output model
mean2 = torch.rand(1, 2, device=self.device, dtype=dtype)
std2 = torch.rand_like(mean2)
mm2 = MockModel(MockPosterior(mean=mean2, variance=std2.square()))
with self.assertRaises(UnsupportedError):
PosteriorStandardDeviation(model=mm2)

def test_posterior_stddev_batch(self):
for dtype in (torch.float, torch.double):
mean = torch.rand(3, 1, 1, device=self.device, dtype=dtype)
std = torch.rand_like(mean)
mm = MockModel(MockPosterior(mean=mean, variance=std.square()))
acqf = PosteriorStandardDeviation(model=mm)
X = torch.empty(3, 1, 1, device=self.device, dtype=dtype)
pm = acqf(X)
self.assertTrue(torch.equal(pm, std.view(-1)))
# check for proper error if multi-output model
mean2 = torch.rand(3, 1, 2, device=self.device, dtype=dtype)
std2 = torch.rand_like(mean2)
mm2 = MockModel(MockPosterior(mean=mean2, variance=std2.square()))
msg = "Must specify a posterior transform when using a multi-output model."
with self.assertRaisesRegex(UnsupportedError, msg):
PosteriorStandardDeviation(model=mm2)


class TestProbabilityOfImprovement(BotorchTestCase):
def test_probability_of_improvement(self):
for dtype in (torch.float, torch.double):
Expand Down