Minimize Black-Box Unimodal Function Without Derivatives
Heaps, Sorting & SearchModel answerLast updated
Find the minimum of a convex/unimodal function over continuous and discrete intervals using zero-order optimization.
Suppose you are given access to a black-box evaluation function $F(x)$ that returns a numerical value for any real scalar $x$. You are also given a search boundary $[a, b]$.
Assume that $F$ is strictly unimodal (or strictly convex) over $[a, b]$, meaning it has a unique global minimum within this interval, strictly decreases to the left of the minimum, and strictly increases to the right. You have no access to derivatives, gradients, or analytical expressions; your only primitive is evaluating $F(x)$ at specific points.
- Continuous Domain: Design an algorithm to estimate the optimal minimizer $x^$ such that $|x - x^| \le \epsilon$, where $\epsilon > 0$ is a given tolerance. Explain how to minimize the total number of calls to $F(x)$.
- Integer Domain: Adapt your approach for an integer domain $x \in {a, a+1, \dots, b}$ to find the exact integer minimizer.
- Complexity & Edge Cases: Detail the time complexity, evaluation counts, and numerical stability limits for both cases.
Hints
Hint 1
If you pick two interior points m1 < m2 in the interval, how do F(m1) and F(m2) help you eliminate one portion of the search space?
Hint 2
Compare Ternary Search (which drops 1/3 of the interval per iteration using 2 evaluations) with Golden-Section Search. How can Golden-Section Search reduce the number of new evaluations per step to 1?
Hint 3
For the integer case, can you reformulate the problem into finding the boundary where the discrete slope F(k+1) - F(k) changes sign?
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Key Concepts & Approach
Because derivatives are unavailable, we rely on zero-order optimization (value comparisons). The core property of a strictly unimodal function $F(x)$ on $[a, b]$ is:
- If $x_1 < x_2$ and $F(x_1) < F(x_2)$, the global minimum $x^*$ cannot lie in $(x_2, b]$. We can narrow the search interval to $[a, x_2]$.
- If $x_1 < x_2$ and $F(x_1) > F(x_2)$, the global minimum $x^*$ cannot lie in $[a, x_1)$. We can narrow the search interval to $[x_1, b]$.
Part 1: Continuous Domain Search
1. Ternary Search (Baseline)
Divide $[a, b]$ into three equal segments by picking $m_1 = a + \frac{b-a}{3}$ and $m_2 = b - \frac{b-a}{3}$.
- Evaluate $F(m_1)$ and $F(m_2)$.
- If $F(m_1) < F(m_2)$, drop $[m_2, b]$. New interval is $[a, m_2]$.
- Else, drop $[a, m_1]$. New interval is $[m_1, b]$.
Drawback: Each iteration reduces the interval size by a factor of $\frac{2}{3}$, but requires 2 new evaluations per step.
2. Golden-Section Search (Optimal Evaluation Efficiency)
To minimize function calls (which are often computationally expensive simulations or loss evaluations), we choose probe points such that one probe point from the current iteration can be reused in the next iteration.
Let the golden ratio be $\phi = \frac{1 + \sqrt{5}}{2} \approx 1.6180339887$, and $\rho = \frac{1}{\phi} \approx 0.6180339887$.
We place our interior points at: $$c = b - \rho(b - a)$$ $$d = a + \rho(b - a)$$
When we contract the interval to $[a, d]$ (because $F(c) < F(d)$), the old interior point $c$ becomes the new $d'$ in the shrunk interval, requiring only one new function call to evaluate the new $c'$.
Initial Interval:
a ------------ c ------------ d ------------ b
|--- (b-a)/φ ---|
If F(c) < F(d), new interval is [a, d]:
a ------ c' -- c --------------------------- d (old)
|------ new interval --------|
(Old 'c' is reused as the new 'd')
Part 2: Integer Domain Search
On an integer domain $x \in [a, b]$, $F(x)$ forms a discrete unimodal sequence. The discrete difference $\Delta F(x) = F(x+1) - F(x)$ is monotonically increasing.
We can perform a standard Binary Search on the sign of the slope:
- Compute $\text{mid} = \lfloor \frac{lo + hi}{2} \rfloor$.
- Compare $F(\text{mid})$ with $F(\text{mid} + 1)$:
- If $F(\text{mid}) < F(\text{mid} + 1)$, the sequence is rising after $\text{mid}$. The minimum must be $\le \text{mid}$. Set $hi = \text{mid}$.
- If $F(\text{mid}) \ge F(\text{mid} + 1)$, the sequence is strictly non-increasing up to $\text{mid} + 1$. Set $lo = \text{mid} + 1$.
- Stop when $lo == hi$.
This requires at most $2 \lceil \log_2(b - a + 1) \rceil$ function evaluations (or $\lceil \log_2(b - a + 1) \rceil + 1$ if values are cached).
Complexity Analysis
Let $N = \frac{b - a}{\epsilon}$ be the relative precision target.
| Algorithm | Interval Shrink Factor | New Calls / Iteration | Total Function Evaluations | Space |
|---|---|---|---|---|
| Ternary Search | $\frac{2}{3} \approx 0.667$ | 2 | $2 \cdot \log_{1.5}(N) \approx 3.42 \log_2(N)$ | $O(1)$ |
| Golden-Section Search | $\frac{1}{\phi} \approx 0.618$ | 1 (after initialization) | $\log_{\phi}(N) + 1 \approx 1.44 \log_2(N)$ | $O(1)$ |
| Integer Binary Search | $\frac{1}{2} = 0.500$ | 1 (with caching) | $\log_2(b - a + 1) + 1$ | $O(1)$ |
Python Implementation
import math
from typing import Callable, Tuple
# Golden ratio constant
INV_PHI = (math.sqrt(5) - 1) / 2 # 1 / phi ≈ 0.6180339887
def golden_section_search(
f: Callable[[float], float],
a: float,
b: float,
eps: float = 1e-7,
max_iter: int = 200,
) -> Tuple[float, float]:
"""Finds an approximate minimizer x* of a unimodal function f in [a, b].
Guarantees |x* - true_x*| <= eps.
Returns (x_opt, f(x_opt)).
"""
if a > b:
a, b = b, a
c = b - INV_PHI * (b - a)
d = a + INV_PHI * (b - a)
fc, fd = f(c), f(d)
for _ in range(max_iter):
if (b - a) <= eps:
break
if fc < fd:
# Drop (d, b] -> new upper bound is d
b, d, fd = d, c, fc
c = b - INV_PHI * (b - a)
fc = f(c)
else:
# Drop [a, c) -> new lower bound is c
a, c, fc = c, d, fd
d = a + INV_PHI * (b - a)
fd = f(d)
x_opt = (a + b) / 2
return x_opt, f(x_opt)
def integer_binary_search(
f: Callable[[int], float],
lo: int,
hi: int,
) -> Tuple[int, float]:
"""Finds the exact integer minimizer of a discrete unimodal function f in [lo, hi].
"""
if lo > hi:
lo, hi = hi, lo
cache = {}
def get_f(x: int) -> float:
if x not in cache:
cache[x] = f(x)
return cache[x]
while lo < hi:
mid = (lo + hi) // 2
if get_f(mid) < get_f(mid + 1):
hi = mid
else:
lo = mid + 1
return lo, get_f(lo)
Edge Cases & Practical Engineering Considerations
- Precision in $x$ vs. Precision in $F(x)$:
- Compressing the interval guarantees $|x - x^*| \le \epsilon$.
- If the function is flat around the minimum (e.g., $F(x) = (x - 2)^4$), $|F(x) - F(x^)|$ will be much smaller than $\epsilon$. However, if $F$ has steep slopes ($L$-Lipschitz continuous), $|F(x) - F(x^)| \le L \cdot \epsilon$.
- Floating Point Precision:
- Setting $\epsilon$ below standard machine precision (
1e-16for double precision) can cause infinite loops if $(b - a) \le \epsilon$ is never met due to roundoff. Limit iterations withmax_iter.
- Setting $\epsilon$ below standard machine precision (
- Noisy Functions:
- If evaluation of $F(x)$ contains stochastic noise (e.g., measuring click-through rate in A/B testing), simple comparison methods can fail. In such scenarios, Bayesian Optimization or averaging multiple evaluations per point is preferred.