Implement Expected Improvement with exploration and numerical stability
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `expected_improvement(mu, sigma, best, xi=0.01, maximize=True)`, which returns a float array of Expected Improvement (EI) values for the Gaussian posterior means `mu` and standard deviations `sigma`. Then implement `propose_next(candidates, mu, sigma, best, xi=0.01, maximize=True)`, which returns `(point, index)` for the candidate with the largest EI.
The functions must follow these rules:
- Improvement: `imp = mu - best - xi` when maximizing, `imp = best - mu - xi` when minimizing.
- For `sigma > 1e-12`: `EI = imp * Φ(z) + sigma * φ(z)` with `z = imp / sigma` (use `scipy.stats.norm`).
- For `sigma <= 1e-12`: `EI = max(imp, 0)`. The output must be finite, with no division warnings leaking NaN or inf.
- Clip all EI values to `>= 0`. The output shape matches the broadcast of `mu` and `sigma`.
- `propose_next` returns a copy of `candidates[idx]` and a Python `int` index. Ties go to the lowest index.
Examples (values rounded to five decimals):
```python
expected_improvement([1.0, 0.0, -1.0], [1.0, 1.0, 2.0], best=0.0, xi=0.0)
# -> [1.08332, 0.39894, 0.39559]
expected_improvement([2.0, 0.5, 1.1], [0.0, 0.0, 0.0], best=1.0, xi=0.1)
# -> [0.9, 0.0, 0.0]
cands = [[0.], [1.], [2.]]
propose_next(cands, [1.0, -0.5, 0.4], [0.2]*3, best=0.0, xi=0.0, maximize=False)
# -> (array([1.]), 1)
```
```hint Zero variance
Work out what the closed form tends to as `sigma` shrinks to 0, and remember that `np.where` evaluates both of its branches on every element.
```
```hint Ties and return types
Check which index `np.argmax` returns when several entries share the maximum, and what type that index has.
```
### Constraints & Assumptions
- `mu` and `sigma` are finite, broadcastable, and `sigma >= 0`.
- `candidates` has shape `(n, d)`, aligned row-wise with `mu` and `sigma` of length `n ≥ 1`.
- NumPy and `scipy.stats.norm` are available.
### What a Strong Answer Covers
- The improvement in both directions, with `xi` handled the same way when maximizing and minimizing.
- A zero-variance guard placed before the division, so no NaN, inf or runtime warning appears, even in the branch that gets discarded.
- Why `max(imp, 0)` is the right value when `sigma` is 0, and why a final clip at 0 is still needed.
- Broadcasting of `mu` and `sigma` and the exact return contract: a float array, a copied candidate row, a Python `int` index and lowest-index tie breaking.
- How `xi` shifts the balance between exploration and exploitation, and the cost of the computation.
### Follow-up Questions
- EI underflows to exactly 0 far from the incumbent. How would you keep a gradient signal for the optimizer that maximizes the acquisition?
- How should `xi` be chosen or scheduled over a run?
- How would you propose a batch of q points instead of one?
Overview: Implement the Expected Improvement acquisition function for Bayesian optimization with an exploration margin and a zero-variance guard, then pick the next candidate to evaluate. It tests the closed-form EI formula, vectorised NumPy code, numerical stability and tie handling.