Search, Reflection, and Test-Time Compute › Code Implementation
Language AI HandbookAll chapters The simulation makes the distribution of candidate answers explicit. It draws 400 problems with varying per-draw correctness probabilities, scores their candidates under several verifier settings, and counts the draws used by an early-stopping policy.
Setup
We need a small task suite. Each problem has a latent per-sample success probability, drawn from a heterogeneous distribution so that difficulty varies across problems, and a true answer drawn from ten possibilities.
PythonIn [8]Expand
import matplotlib.pyplot as plt
import numpy as np
rng = np.random.default_rng(20240117)
N_PROBLEMS = 400
N_CHOICES = 10
N_MAX = 32
# Latent difficulty: probability that a single independent sample is correct.
P_MIN, P_MAX = 0.05, 0.85
p_correct = rng.uniform(P_MIN, P_MAX, size=N_PROBLEMS)
true_answer = rng.integers(0, N_CHOICES, size=N_PROBLEMS)The generator draws an answer for one problem: with probability it emits the true answer, otherwise it emits one of the other nine uniformly at random. This is the simplest error model that still exhibits the mode structure we discussed.
PythonIn [9]Expand
def draw_samples(problem, n):
"""Draw n i.i.d. answers for one problem."""
p = p_correct[problem]
truth = true_answer[problem]
hit = rng.random(n) < p
wrong = (truth + rng.integers(1, N_CHOICES, size=n)) % N_CHOICES
return np.where(hit, truth, wrong), hitWe draw the maximum budget once per problem and reuse its prefixes at smaller budgets. At any fixed length, the prefix has the same distribution as a fresh draw of that size. Sharing candidates pairs the comparisons across budgets.
PythonIn [10]Expand
answers = np.zeros((N_PROBLEMS, N_MAX), dtype=int)
is_correct = np.zeros((N_PROBLEMS, N_MAX), dtype=bool)
for i in range(N_PROBLEMS):
a, c = draw_samples(i, N_MAX)
answers[i] = a
is_correct[i] = cConsoleOut [11]Expand
Problems in the suite: 400 Mean latent per-sample accuracy: 0.455 Empirical single-sample accuracy: 0.470
In this run, the model probabilities average 0.455 and the first draw succeeds on 0.470 of the problems.
Selectors and a dial-able verifier
Three selectors cover the range we care about: an oracle-free plurality vote, a verifier-guided best-of-, and (implicitly) random selection when the verifier has no signal.
PythonIn [12]Expand
def plurality_vote(block):
"""Plurality vote over the answers in each row of `block`."""
out = np.empty(block.shape[0], dtype=int)
for i, row in enumerate(block):
out[i] = np.argmax(np.bincount(row, minlength=N_CHOICES))
return out
def best_of_n(block_answers, block_scores):
"""Pick the highest-scoring candidate in each row."""
idx = np.argmax(block_scores, axis=1)
return np.take_along_axis(block_answers, idx[:, None], axis=1).ravel()The tie rule chooses the lowest label among tied leaders. With uniformly sampled true labels and symmetric distractors, this has the same aggregate expected accuracy as choosing uniformly among the leaders. It still favors lower predicted labels in ties and can change the observed accuracy of a particular run.
The simulator draws independent unit-variance normal scores around separation for correct candidates and around zero for incorrect ones. The difference between one correct and one incorrect score has variance 2 and mean separation. The chance that it is positive is , where is the standard normal cumulative distribution function. The probabilities at separations 0, 1, and 3 are about 0.50, 0.76, and 0.98.
PythonIn [13]Expand
def score_with_verifier(block_correct, separation):
return rng.normal(loc=separation * block_correct.astype(float), scale=1.0)Measuring coverage versus selection
The sweep records five curves: coverage, plurality voting, and best-of- with each of three verifier settings.
PythonIn [14]Expand
sample_counts = [1, 2, 4, 8, 16, 32]
separations = {"uninformative": 0.0, "weak": 1.0, "strong": 3.0}
# One verifier noise draw per quality level, reused as prefixes across N.
verifier_scores = {
k: score_with_verifier(is_correct, s) for k, s in separations.items()
}
coverage_curve = []
vote_curve = []
bon_curves = {k: [] for k in separations}
for n in sample_counts:
block_answers = answers[:, :n]
block_correct = is_correct[:, :n]
coverage_curve.append(block_correct.any(axis=1).mean())
vote_curve.append((plurality_vote(block_answers) == true_answer).mean())
for name in separations:
picked = best_of_n(block_answers, verifier_scores[name][:, :n])
bon_curves[name].append((picked == true_answer).mean())ConsoleOut [15]Expand
N coverage plurality rand-ver weak-ver strong-ver 1 0.470 0.470 0.470 0.470 0.470 2 0.682 0.510 0.490 0.585 0.677 4 0.815 0.620 0.470 0.655 0.805 8 0.920 0.703 0.460 0.685 0.920 16 0.973 0.792 0.443 0.752 0.963 32 0.995 0.807 0.477 0.790 0.983
Coverage rises from 0.470 at one draw to 0.995 at 32. At 32, plurality voting gives 0.807 and the strong verifier reaches 0.983. The random verifier fluctuates near the one-draw baseline. In this run the weak verifier scores below plurality at budgets of 8 and above.
VisualizationOut [16]Expand

Coverage grows most over the early part of the sweep. The strong verifier stays close to coverage throughout this run. The gap between coverage and the weak verifier’s selected accuracy is widest at and narrows at the two larger budgets shown.
Adaptive stopping versus equal-total and fixed-cap baselines
All policies use each problem's existing sample pool. The adaptive policy stops after four unanimous responses and uses sixteen otherwise. The equal-total baseline uses the adaptive policy's observed aggregate count, then shuffles assignments of the two adjacent integer budgets without looking at any individual stopping decision. The fixed-cap baseline uses sixteen draws on every problem.
PythonIn [17]Expand
N0 = 4
N1 = 12
initial_answers = answers[:, :N0]
stopped = np.all(initial_answers == initial_answers[:, [0]], axis=1)
wrong_stop = stopped & (initial_answers[:, 0] != true_answer)
adaptive_budgets = np.where(stopped, N0, N0 + N1)
total_budget = int(adaptive_budgets.sum())
lower_budget, round_up_count = divmod(total_budget, N_PROBLEMS)
mean_matched_budgets = np.full(N_PROBLEMS, lower_budget)
order = np.random.default_rng(17).permutation(N_PROBLEMS)
mean_matched_budgets[order[:round_up_count]] += 1
assert mean_matched_budgets.sum() == adaptive_budgets.sum()
cap_matched_budgets = np.full(N_PROBLEMS, N0 + N1)
def prefix_accuracy(budgets):
selected = [
np.argmax(np.bincount(row[:budget], minlength=N_CHOICES))
for row, budget in zip(answers, budgets, strict=True)
]
return np.mean(np.asarray(selected) == true_answer)
adaptive_acc = prefix_accuracy(adaptive_budgets)
fixed_acc = prefix_accuracy(mean_matched_budgets)
fixed_worst_acc = prefix_accuracy(cap_matched_budgets)
power = N0 + 1
correct_mass = P_MAX**power - P_MIN**power
wrong_mass = ((1 - P_MIN) ** power - (1 - P_MAX) ** power) / (
N_CHOICES - 1
) ** (N0 - 1)
expected_stop_rate = (correct_mass + wrong_mass) / (power * (P_MAX - P_MIN))ConsoleOut [18]Expand
Adaptive: 0.792 accuracy, 14.38 samples per problem Mean-matched non-adaptive: 0.775 accuracy, 14.38 samples per problem Cap-matched fixed: 0.792 accuracy, 16.00 samples per problem Early stop: 0.135; wrong early stops: 0 Expected early-stop rate under the simulation distribution: 0.111
The adaptive and mean-matched policies both use 5,752 draws. In this run the adaptive policy reaches 0.792 accuracy versus 0.775 for the mean-matched policy. The cap-matched policy also reaches 0.792 while using 6,400 draws. These are observed results from one shared set of pools and one shuffle of the non-adaptive budgets.
The early-stop rate is 0.135 in the seeded suite. The expected rate is 0.111 when averaging over the simulator's distribution of . On a problem with given , the chance of four unanimous draws is . The second term is positive for even though this run has zero observed wrong early stops.
VisualizationOut [19]Expand

Verifier accuracy at a fixed sample budget
Next, hold the candidate pool and its budget fixed and vary verifier separation.
PythonIn [20]Expand
quality_levels = [0.0, 0.5, 1.0, 1.5, 2.0, 3.0]
N_SWEEP = 16
sweep_answers = answers[:, :N_SWEEP]
sweep_correct = is_correct[:, :N_SWEEP]
sweep_noise = rng.standard_normal(sweep_correct.shape)
sweep_bon = []
for q in quality_levels:
picked = best_of_n(sweep_answers, sweep_noise + q * sweep_correct)
sweep_bon.append((picked == true_answer).mean())
vote_at_n = (plurality_vote(sweep_answers) == true_answer).mean()ConsoleOut [21]Expand
Plurality vote at N=16: 0.792 verifier separation 0.0: best-of-16 accuracy 0.460 <- below voting verifier separation 0.5: best-of-16 accuracy 0.613 <- below voting verifier separation 1.0: best-of-16 accuracy 0.748 <- below voting verifier separation 1.5: best-of-16 accuracy 0.848 verifier separation 2.0: best-of-16 accuracy 0.917 verifier separation 3.0: best-of-16 accuracy 0.963
At zero separation, the expected accuracy matches that of one draw. The observed result is 0.460, and the first-draw result is 0.470. At separation 1, best-of-16 scores 0.748, below the plurality result of 0.792. At 1.5 it reaches 0.848. The sweep holds and every candidate's noise fixed, so only the correct-candidate bonus changes. This is an accuracy comparison. A cost comparison would also count the verifier calls.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!