Halton Sequence Sampler for Hyperparameter Search
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement a Halton quasi-random generator from scratch, map its unit-cube points into hyperparameter bounds, and measure grid coverage against uniform random sampling. Fill in these six functions:
```python
def first_primes(d): ...
def radical_inverse(index, base): ...
def halton_sequence(n, d, skip=0): ...
def scale_to_bounds(unit_points, space): ...
def coverage_fraction(unit_points, bins): ...
def compare_coverage(n, d, bins, seed): ...
```
What each function must do:
- `first_primes(d)` returns the first `d` primes in ascending order.
- `radical_inverse(index, base)` mirrors the base digits of `index` about the radix point. An index of 0 returns `0.0`, and a negative index raises `ValueError`.
- `halton_sequence(n, d, skip=0)` returns an `(n, d)` array. Row `r` (0-based) uses index `r + 1 + skip`, and column `j` uses the `j`-th prime as its base.
- `scale_to_bounds(unit_points, space)` takes one spec per column, each of the form `{"low", "high", "log"}`. A linear column maps to `low + u*(high-low)`, a log column to `exp(log low + u*(log high - log low))`. If the column count differs from `len(space)`, raise `ValueError`.
- `coverage_fraction(unit_points, bins)` assigns each point to the cell `floor(u*bins)`, clipped to `bins-1` in every coordinate, and returns the number of occupied cells divided by `bins**d`.
- `compare_coverage(n, d, bins, seed)` returns `(halton_cov, random_cov)`: the coverage of `halton_sequence(n, d)` with `skip=0` and the coverage of `np.random.default_rng(seed).random((n, d))`.
Example:
```python
halton_sequence(4, 3)
# [[0.5, 1/3, 0.2],
# [0.25, 2/3, 0.4],
# [0.75, 1/9, 0.6],
# [0.125, 4/9, 0.8]]
coverage_fraction(halton_sequence(4, 2), bins=2) # 0.75 (3 of 4 cells)
scale_to_bounds([[0.5]], [{"low": 1e-4, "high": 1e-1, "log": True}]) # [[10**-2.5]]
```
```hint Reading the digits
Peel the digits off the index with `%` and `//`, lowest digit first, and give each one a place value that shrinks by a factor of the base every step.
```
```hint Counting cells
Turn each point into a tuple of integer cell indices and count the distinct tuples. Think about where a coordinate of exactly 1.0 would land without the clip.
```
### Constraints & Assumptions
- Only `numpy` is available. Do not use `scipy.stats.qmc`.
- Bounds of a log-scaled dimension are positive.
- The random baseline must be drawn exactly as `np.random.default_rng(seed).random((n, d))` so results are reproducible for a given seed.
### What a Strong Answer Covers
- A correct radical inverse in any base, including the index-0 and negative-index cases.
- The index offset `r + 1 + skip` and one distinct prime base per column, reproducing the example rows.
- Linear and log scaling with the column-count check.
- Coverage computed as distinct grid cells over `bins**d`, with the clip, and a seeded random baseline for the comparison.
- The cost of generation and coverage, and why Halton fills more cells than random draws at small budgets.
### Follow-up Questions
- Why does plain Halton degrade in high dimensions, and how would you fix it?
- How would you handle integer and categorical hyperparameters?
- Why does Halton beat random on coverage but not guarantee better tuning results?
Overview: Implement a Halton quasi-random sequence from scratch with NumPy, scale its points into linear and log-scaled hyperparameter bounds, and compare grid coverage against uniform random sampling. It tests radical inverses in prime bases, log-space scaling, and reasoning about space-filling designs for hyperparameter search.