Implement a One-Dimensional TPE (Tree-structured Parzen Estimator) Suggestion Step

Quick Overview

Implement one step of a one-dimensional Tree-structured Parzen Estimator: split past trials by a loss quantile, fit Gaussian kernel density estimates to the good and bad groups, and return the sampled candidate with the best density ratio. It tests numerical stability, bandwidth choice and reproducible sampling.

Implement a One-Dimensional TPE (Tree-structured Parzen Estimator) Suggestion Step

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Given past trial `values` and `losses` (lower is better) on `[low, high]`, implement one Tree-structured Parzen Estimator (TPE) step. Split trials by quantile `gamma`, fit a Gaussian KDE `l(x)` to the good group and `g(x)` to the bad group, then return the sampled candidate maximizing `l(x)/g(x)`. ```python def split_trials(values, losses, gamma): ... def kde_bandwidth(centers, low, high): ... def kde_log_pdf(x, centers, bandwidth): ... def tpe_log_ratio(candidates, good, bad, low, high): ... def tpe_suggest(values, losses, low, high, gamma=0.25, n_candidates=24, seed=0): ... ``` Requirements: - `split_trials`: `n_good = ceil(gamma * n)` clipped to `[1, n - 1]`. Sort by loss with a stable ascending sort (ties: lower index first) and return both groups in that order. Raise `ValueError` if `n < 2`, the lengths differ, or `gamma` is not in `(0, 1]`. - `kde_bandwidth`: `max(1.06 * std(centers, ddof=0) * n**(-1/5), 0.01 * (high - low))`, where `n` is the number of centers. - `kde_log_pdf`: the log density of an equal-weight Gaussian mixture (one component per center, standard deviation `bandwidth`) at each point of `x`, returned as a 1D array. - `tpe_log_ratio`: `log l(x) - log g(x)`, each KDE using its own `kde_bandwidth`. - `tpe_suggest`: `rng = np.random.default_rng(seed)`, then `idx = rng.integers(0, n_good, size=n_candidates)`, then `z = rng.normal(size=n_candidates)`. Candidates are `clip(good[idx] + h_l * z, low, high)`, where `good` is in the sorted order from `split_trials` and `h_l` is its bandwidth. Return the candidate with the largest log ratio (first on ties) as a `float`. Example: ```python values = np.arange(10.0) losses = [5, 1, 3, 1, 9, 0.5, 7, 3, 8, 6] split_trials(values, losses, 0.25) # -> ([5., 1., 3.], [2., 7., 0., 9., 6., 8., 4.]) kde_bandwidth([4.0], 0.0, 10.0) # -> 0.1 (floor applies) ``` ```hint Rounding the split Check what `ceil(gamma * n)` returns in floating point when `gamma * n` should be a whole number, for example `gamma = 0.07` with `n = 100`. ``` ```hint Far from the data Evaluate a single Gaussian term at a point 40 bandwidths from its center, and work out what that does to a plain ratio of two densities before you decide how to compute the mixture. ``` ```hint Degenerate groups Consider a group with one trial, or with identical values. Work out what Silverman's rule gives there and what the floor protects against. ``` ### Constraints & Assumptions - `values` and `losses` are 1D sequences of the same length `n`, with `n >= 2` for valid input. - Values lie in `[low, high]` with `low < high`, and losses are finite. - `n_candidates >= 1`. Defaults are `gamma = 0.25`, `n_candidates = 24` and `seed = 0`. - The parameter is continuous and on a linear scale. ### What a Strong Answer Covers - The split: the count rule with its clipping, a stable tie order, the validation errors, and floating-point care when `gamma * n` is a whole number. - Silverman's bandwidth with the range floor, and why the floor matters for tiny or identical groups. - A numerically stable mixture log density, vectorized over candidates and centers, with the correct normalization. - The suggestion step: the RNG call order, sampling around the good centers with `h_l`, clipping, a first-index argmax and the return type. - Why maximizing `l(x)/g(x)` is the TPE acquisition rule, and the time and space complexity. ### Follow-up Questions - How would you extend this to multiple or categorical hyperparameters? - Why work in log space, and how would you handle log-uniform parameters? - What breaks with very few trials, and how do real implementations mitigate it?

Overview: Implement one step of a one-dimensional Tree-structured Parzen Estimator: split past trials by a loss quantile, fit Gaussian kernel density estimates to the good and bad groups, and return the sampled candidate with the best density ratio. It tests numerical stability, bandwidth choice and reproducible sampling.

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

Given past trial values and losses (lower is better) on [low, high], implement one Tree-structured Parzen Estimator (TPE) step. Split trials by quantile gamma, fit a Gaussian KDE l(x) to the good group and g(x) to the bad group, then return the sampled candidate maximizing l(x)/g(x).

def split_trials(values, losses, gamma): ...

def kde_bandwidth(centers, low, high): ...

def kde_log_pdf(x, centers, bandwidth): ...

def tpe_log_ratio(candidates, good, bad, low, high): ...

def tpe_suggest(values, losses, low, high, gamma=0.25, n_candidates=24, seed=0): ...

Requirements:

  • split_trials : n_good = ceil(gamma * n) clipped to [1, n - 1] . Sort by loss with a stable ascending sort (ties: lower index first) and return both groups in that order. Raise ValueError if n < 2 , the lengths differ, or gamma is not in (0, 1] .
  • kde_bandwidth : max(1.06 * std(centers, ddof=0) * n**(-1/5), 0.01 * (high - low)) , where n is the number of centers.
  • kde_log_pdf : the log density of an equal-weight Gaussian mixture (one component per center, standard deviation bandwidth ) at each point of x , returned as a 1D array.
  • tpe_log_ratio : log l(x) - log g(x) , each KDE using its own kde_bandwidth .
  • tpe_suggest : rng = np.random.default_rng(seed) , then idx = rng.integers(0, n_good, size=n_candidates) , then z = rng.normal(size=n_candidates) . Candidates are clip(good[idx] + h_l * z, low, high) , where good is in the sorted order from split_trials and h_l is its bandwidth. Return the candidate with the largest log ratio (first on ties) as a float .

Example:

values = np.arange(10.0)
losses = [5, 1, 3, 1, 9, 0.5, 7, 3, 8, 6]
split_trials(values, losses, 0.25)
# -> ([5., 1., 3.], [2., 7., 0., 9., 6., 8., 4.])

kde_bandwidth([4.0], 0.0, 10.0)   # -> 0.1 (floor applies)

Constraints & Assumptions

  • values and losses are 1D sequences of the same length n , with n >= 2 for valid input.
  • Values lie in [low, high] with low < high , and losses are finite.
  • n_candidates >= 1 . Defaults are gamma = 0.25 , n_candidates = 24 and seed = 0 .
  • The parameter is continuous and on a linear scale.

What a Strong Answer Covers Guidance

  • The split: the count rule with its clipping, a stable tie order, the validation errors, and floating-point care when gamma * n is a whole number.
  • Silverman's bandwidth with the range floor, and why the floor matters for tiny or identical groups.
  • A numerically stable mixture log density, vectorized over candidates and centers, with the correct normalization.
  • The suggestion step: the RNG call order, sampling around the good centers with h_l , clipping, a first-index argmax and the return type.
  • Why maximizing l(x)/g(x) is the TPE acquisition rule, and the time and space complexity.

Follow-up Questions Guidance

  • How would you extend this to multiple or categorical hyperparameters?
  • Why work in log space, and how would you handle log-uniform parameters?
  • What breaks with very few trials, and how do real implementations mitigate it?