Random search trial budget: trials needed to hit the top q with probability p
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Random search draws configurations independently. If a fraction `q` of the search space counts as good, how many trials do you need so that at least one lands in it with probability `p`?
Implement three functions:
- `success_probability(q, n_trials)` returns `1 - (1 - q) ** n_trials` as a float.
- `n_trials_needed(q, p)` returns the minimal integer `n` with `1 - (1 - q) ** n >= p`.
- `simulate_hit_rate(q, n_trials, n_repeats, seed)` estimates the hit rate of random search on a uniform objective.
Examples:
```python
n_trials_needed(0.05, 0.95) # 59
n_trials_needed(0.5, 0.75) # 2 (exact boundary counts as success)
n_trials_needed(0.2, 0.0) # 0
n_trials_needed(1.0, 0.99) # 1
success_probability(0.5, 2) # 0.75
```
```hint Exact boundaries
Run your closed form on `n_trials_needed(0.5, 0.75)` and think about what floating-point logs can do near an integer. How will you confirm that the count you return is the smallest one that works?
```
### Constraints & Assumptions
- `q` must lie in `(0, 1]` and `p` in `[0, 1)`, else raise `ValueError`. A negative `n_trials` in `success_probability` or `n_repeats <= 0` also raises `ValueError`.
- Simulation: create `rng = np.random.default_rng(seed)` and draw `rng.random((n_repeats, n_trials))` in one call. A repeat is a hit if any of its scores is `>= 1 - q`. Return the mean hit fraction as a float.
- If `n_trials <= 0`, `simulate_hit_rate` returns `0.0`.
### What a Strong Answer Covers
- Justifies the success formula from independent trials that each miss with probability `1 - q`.
- Inverts it with logarithms and handles `p == 0` and `q == 1` before taking the log of zero.
- Guarantees the returned count is minimal at exact floating-point boundaries.
- Follows the simulation contract exactly (one seeded draw, the `>= 1 - q` hit rule, a float result) and validates inputs as specified.
- Knows how far the simulated rate can drift from the formula for a given `n_repeats`.
### Follow-up Questions
- Why is the classic answer "about 60 trials" independent of the number of hyperparameters?
- Why might the closed-form log formula be off by one?
Overview: Compute how many random search trials are needed to land in the top fraction of a search space with a target probability, and check the formula by simulation. It tests the independent-trials probability model, inverting it with logarithms, floating-point boundary cases and seeded simulation.