-
Notifications
You must be signed in to change notification settings - Fork 56
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
Add expected improvement utility function #460
Merged
thomaspinder
merged 3 commits into
JaxGaussianProcesses:main
from
Thomas-Christie:expected-improvement
Jul 16, 2024
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
gpjax/decision_making/utility_functions/expected_improvement.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Copyright 2024 The JaxGaussianProcesses Contributors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
from dataclasses import dataclass | ||
from functools import partial | ||
|
||
from beartype.typing import Mapping | ||
import jax.numpy as jnp | ||
import tensorflow_probability.substrates.jax as tfp | ||
|
||
from gpjax.dataset import Dataset | ||
from gpjax.decision_making.utility_functions.base import ( | ||
AbstractSinglePointUtilityFunctionBuilder, | ||
SinglePointUtilityFunction, | ||
) | ||
from gpjax.decision_making.utils import ( | ||
OBJECTIVE, | ||
get_best_latent_observation_val, | ||
) | ||
from gpjax.gps import ConjugatePosterior | ||
from gpjax.typing import ( | ||
Array, | ||
Float, | ||
KeyArray, | ||
) | ||
|
||
|
||
@dataclass | ||
class ExpectedImprovement(AbstractSinglePointUtilityFunctionBuilder): | ||
""" | ||
Expected Improvement acquisition function as introduced by [Močkus, | ||
1974](https://link.springer.com/chapter/10.1007/3-540-07165-2_55). The "best" | ||
incumbent value is defined as the lowest posterior mean value evaluated at the the | ||
previously observed points. This enables the acquisition function to be utilised with noisy observations. | ||
""" | ||
|
||
def build_utility_function( | ||
self, | ||
posteriors: Mapping[str, ConjugatePosterior], | ||
datasets: Mapping[str, Dataset], | ||
key: KeyArray, | ||
) -> SinglePointUtilityFunction: | ||
r""" | ||
Build the Expected Improvement acquisition function. This computes the expected | ||
improvement over the "best" of the previously observed points, utilising the | ||
posterior distribution of the surrogate model. For posterior distribution | ||
$`f(\cdot)`$, and best incumbent value $`\eta`$, this is defined | ||
as: | ||
```math | ||
\alpha_{\text{EI}}(\mathbf{x}) = \mathbb{E}\left[\max(0, \eta - f(\mathbf{x}))\right] | ||
``` | ||
|
||
Args: | ||
posteriors (Mapping[str, ConjugatePosterior]): Dictionary of posteriors to | ||
used to form the utility function. One posteriors must correspond to the | ||
`OBJECTIVE` key, as we utilise the objective posterior to form the utility | ||
function. | ||
datasets (Mapping[str, Dataset]): Dictionary of datasets used to form the | ||
utility function. Keys in `datasets` should correspond to keys in | ||
`posteriors`. One of the datasets must correspond to the `OBJECTIVE` key. | ||
key (KeyArray): JAX PRNG key used for random number generation. | ||
|
||
Returns: | ||
SinglePointUtilityFunction: The Expected Improvement acquisition function to | ||
to be *maximised* in order to decide which point to query next. | ||
""" | ||
self.check_objective_present(posteriors, datasets) | ||
objective_posterior = posteriors[OBJECTIVE] | ||
objective_dataset = datasets[OBJECTIVE] | ||
|
||
if not isinstance(objective_posterior, ConjugatePosterior): | ||
raise ValueError( | ||
"Objective posterior must be a ConjugatePosterior to compute the Expected Improvement." | ||
) | ||
|
||
if ( | ||
objective_dataset.X is None | ||
or objective_dataset.n == 0 | ||
or objective_dataset.y is None | ||
): | ||
raise ValueError("Objective dataset must contain at least one item") | ||
|
||
eta = get_best_latent_observation_val(objective_posterior, objective_dataset) | ||
return partial( | ||
_expected_improvement, objective_posterior, objective_dataset, eta | ||
) | ||
|
||
|
||
def _expected_improvement( | ||
objective_posterior: ConjugatePosterior, | ||
objective_dataset: Dataset, | ||
eta: Float[Array, ""], | ||
x: Float[Array, "N D"], | ||
) -> Float[Array, "N 1"]: | ||
latent_dist = objective_posterior(x, objective_dataset) | ||
mean = latent_dist.mean() | ||
var = latent_dist.variance() | ||
normal = tfp.distributions.Normal(mean, jnp.sqrt(var)) | ||
return jnp.expand_dims( | ||
((eta - mean) * normal.cdf(eta) + var * normal.prob(eta)), -1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must this be float64? If the user is using float32 arrays (ill-advised, but possible), then this will cause mixed precision errors. Can we perhaps wrap in a
jnp.asarray()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have switched back to returning an array of integers, as these are samples from a Poisson distribution.