Compare grid search and random search under an equal evaluation budget
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement three functions that maximise `objective(x)`, where `x` is a 1-D float array, over the box `bounds` (a list of `(low, high)` pairs, one per dimension) using at most `budget` evaluations:
- `grid_search_best(objective, bounds, budget)`
- `random_search_best(objective, bounds, budget, seed)`
- `compare_search_strategies(objective, bounds, budget, seeds)`
Both search functions return `(best_score, best_config)` as a float and an array.
The functions must follow these rules:
- `grid_search_best`: use the largest `k` with `k**d <= budget`. Each axis is `np.linspace(low, high, k)`, or the midpoint when `k == 1`. Evaluate the points in `itertools.product` order.
- `random_search_best`: draw all points in one call, `np.random.default_rng(seed).uniform(lows, highs, size=(budget, d))`, then evaluate the rows in order.
- Both search functions raise `ValueError` when `budget < 1`.
- Ties keep the first config found (strict `>`).
- `compare_search_strategies` runs the grid once and random search once per seed, and returns `{"grid_best": float, "random_best": ndarray (one per seed), "random_mean": float, "random_win_rate": float}`.
- `random_win_rate` is the fraction of seeds where the random best is strictly greater than `grid_best`.
Example (score rounded):
```python
grid_search_best(lambda x: -(x[0] - 0.3) ** 2, [(0, 1), (0, 1)], 9)
# k = 3, axes = [0, 0.5, 1]
# -> (-0.04, array([0.5, 0.0])) # first of the tied configs
```
```hint Grid size
Work out `k` for `budget = 64` and `d = 3` with a floating-point root and see what you get.
```
```hint Reading the comparison
Count how many distinct values each method tries along one axis when only that axis affects the objective.
```
### Constraints & Assumptions
- `bounds` has `d ≥ 1` dimensions, each with `low <= high`.
- `budget` is a whole number of objective evaluations. The grid uses exactly `k**d` of them, which can be fewer than `budget`; random search uses all of them.
- `seeds` is a non-empty list of integers.
### What a Strong Answer Covers
- An exact integer computation of `k`, the midpoint case and the product evaluation order.
- Reproducible random draws in a single call, and strict `>` tie handling in both searches.
- The summary dict, with the win rate counting only strict wins.
- Why random search tends to beat the grid when few hyperparameters matter, and when the grid can still win.
- The cost of each search and edge cases such as a budget below 1, unused grid budget and NaN objective values.
### Follow-up Questions
- Why does random search tend to win when only one hyperparameter matters?
- How would you handle log-scaled hyperparameters such as the learning rate?
- How would you make the comparison statistically meaningful?
Overview: Implement grid search and seeded random search over a box of hyperparameters with the same evaluation budget, then measure how often random search finds a better score. It tests exact grid sizing, reproducible sampling, tie handling and reasoning about when each strategy finds better configurations.