Latin Hypercube Sampler for Hyperparameter Search

Quick Overview

Implement a seeded Latin hypercube sampler in NumPy that places exactly one point in each equal-width stratum of every hyperparameter range. It tests the stratum-plus-jitter construction, reproducible random draw order, careful input validation, and how stratified designs compare with plain random search.

Latin Hypercube Sampler for Hyperparameter Search

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `latin_hypercube(n_samples, bounds, seed=0)` returning an `(n_samples, n_dims)` NumPy array of trial points, where `bounds` is a sequence of `(low, high)` pairs. Each dimension must place exactly one point in each of the `n_samples` equal-width strata of its range. Requirements: - Use `rng = np.random.default_rng(seed)`. For each dimension `j` in order, draw `perm = rng.permutation(n)`, then `u = rng.uniform(size=n)`. This draw order is fixed so output can be compared exactly for a given seed. - Unit value: `unit[:, j] = (perm + u) / n`. Output: `low + unit * (high - low)`. - The same `seed` gives identical output. - Any bound with `low >= high`, or any non-finite bound, raises `ValueError`. - An `n_samples` that is not a positive integer, or `bounds` that do not have shape `(n_dims, 2)`, raises `ValueError`. Example: ```python x = latin_hypercube(7, [(0.0, 1.0), (-3.0, 3.0), (10.0, 20.0)], seed=1) x.shape # (7, 3) # per column: floor((x - low) / (high - low) * 7) is a permutation of 0..6 latin_hypercube(5, [(2.0, 1.0)]) # raises ValueError ``` ```hint Validate before drawing Convert `bounds` to a float array once and check its shape, finiteness and ordering before touching the generator. Remember that `True` is an `int` in Python and that every comparison with `nan` is false. ``` ### What a Strong Answer Covers - The fixed draw order (permutation, then uniform, per dimension) from one generator, so a seed reproduces the output exactly. - Why `(perm + u) / n` puts exactly one point in each stratum of every dimension, and why the values stay inside the bounds. - Complete input validation: a positive integer `n_samples`, an `(n_dims, 2)` shape, finite bounds and `low < high`. - Time and memory cost, and how the result differs from plain uniform random sampling. ### Follow-up Questions - How does a Latin hypercube sample compare with uniform random sampling and with a Halton sequence for a small tuning budget? - How would you use this function to sample a learning rate on a log scale? - The pairing of strata across dimensions is random, so two columns can come out strongly correlated. How would you detect and reduce that?

Overview: Implement a seeded Latin hypercube sampler in NumPy that places exactly one point in each equal-width stratum of every hyperparameter range. It tests the stratum-plus-jitter construction, reproducible random draw order, careful input validation, and how stratified designs compare with plain random search.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement latin_hypercube(n_samples, bounds, seed=0) returning an (n_samples, n_dims) NumPy array of trial points, where bounds is a sequence of (low, high) pairs. Each dimension must place exactly one point in each of the n_samples equal-width strata of its range.

Requirements:

  • Use rng = np.random.default_rng(seed) . For each dimension j in order, draw perm = rng.permutation(n) , then u = rng.uniform(size=n) . This draw order is fixed so output can be compared exactly for a given seed.
  • Unit value: unit[:, j] = (perm + u) / n . Output: low + unit * (high - low) .
  • The same seed gives identical output.
  • Any bound with low >= high , or any non-finite bound, raises ValueError .
  • An n_samples that is not a positive integer, or bounds that do not have shape (n_dims, 2) , raises ValueError .

Example:

x = latin_hypercube(7, [(0.0, 1.0), (-3.0, 3.0), (10.0, 20.0)], seed=1)
x.shape  # (7, 3)
# per column: floor((x - low) / (high - low) * 7) is a permutation of 0..6

latin_hypercube(5, [(2.0, 1.0)])  # raises ValueError

What a Strong Answer Covers Guidance

  • The fixed draw order (permutation, then uniform, per dimension) from one generator, so a seed reproduces the output exactly.
  • Why (perm + u) / n puts exactly one point in each stratum of every dimension, and why the values stay inside the bounds.
  • Complete input validation: a positive integer n_samples , an (n_dims, 2) shape, finite bounds and low < high .
  • Time and memory cost, and how the result differs from plain uniform random sampling.

Follow-up Questions Guidance

  • How does a Latin hypercube sample compare with uniform random sampling and with a Halton sequence for a small tuning budget?
  • How would you use this function to sample a learning rate on a log scale?
  • The pairing of strata across dimensions is random, so two columns can come out strongly correlated. How would you detect and reduce that?