Implement probability of improvement and UCB acquisition functions for Bayesian optimization
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
A Bayesian optimization loop has a surrogate model that predicts a mean `mu` and a standard deviation `sigma` for each candidate point. Implement two acquisition functions and a selector with these signatures:
```python
def probability_of_improvement(mu, sigma, best, xi=0.0, maximize=True): ...
def upper_confidence_bound(mu, sigma, kappa=2.0, maximize=True): ...
def select_next_candidate(mu, sigma, best, acquisition="pi", xi=0.0, kappa=2.0, maximize=True): ...
```
- `probability_of_improvement` returns $\Phi(\text{imp} / \sigma)$, where $\Phi$ is the standard normal CDF, with `imp = mu - best - xi` when maximizing and `imp = best - mu - xi` when minimizing. Where `sigma == 0`, PI is 1.0 if `imp > 0` (strictly), else 0.0.
- `upper_confidence_bound` returns `mu + kappa*sigma` when maximizing and `kappa*sigma - mu` when minimizing.
- Both return a float `np.ndarray` where higher is better.
- `select_next_candidate` returns the index of the highest score under the chosen acquisition (`"pi"` or `"ucb"`). Ties resolve to the lowest index, and the result is a Python `int`.
- Mismatched shapes of `mu` and `sigma`, or any negative `sigma`, raise `ValueError`. An unknown `acquisition` also raises `ValueError`.
For example:
```python
mu, sigma, best = [1.0, 0.8], [0.01, 1.0], 0.9
probability_of_improvement(mu, sigma, best) # [~1.0, 0.4602]
upper_confidence_bound(mu, sigma, kappa=2.0) # [1.02, 2.8]
select_next_candidate(mu, sigma, best, acquisition="pi") # 0
select_next_candidate(mu, sigma, best, acquisition="ucb") # 1
```
```hint Zero variance
Look at what `imp / sigma` gives when `sigma` is 0, and at what the normal CDF approaches as `sigma` shrinks with `imp` held fixed.
```
```hint Input handling
Convert and validate the inputs in one shared helper, so a scalar and a one-element list behave the same and the checks run before any arithmetic.
```
### Constraints & Assumptions
- Inputs are lists or arrays; a scalar is treated as a 1-D array of length 1.
- All values are finite.
### What a Strong Answer Covers
- The improvement in both optimization directions, including how `xi` enters, and the minimizing UCB score as a negated lower confidence bound.
- The `sigma == 0` case handled without dividing by zero, following the strict step rule.
- Input coercion and validation (shapes, negative `sigma`, unknown acquisition) that raise `ValueError`.
- Argmax selection with ties going to the lowest index, returned as a Python `int`, and why PI and UCB can choose different candidates.
### Follow-up Questions
- How would you add Expected Improvement, and why might it beat PI?
- How should `kappa` or `xi` be chosen or scheduled over iterations?
- How would you pick a batch of q candidates instead of one?
Overview: Implement probability of improvement and upper confidence bound acquisition functions for Bayesian optimization, then pick the next candidate by argmax. It tests both optimization directions, the zero-variance case, input validation and why the two acquisitions can choose different points.