code wiki / (root) / sketch_thompson.nx

sketch_thompson.nx

buildroot/runtime/sketch_thompson.nx

9487 B292 linesdepth 4pulls 4 transitivereach 2 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

about

sketch_thompson.nx -- Thompson Sampling (Bernoulli-Beta bandit). Thompson 1933 / Russo-Van Roy 2018 modern treatment. Bayesian alternative to UCB1 (sketch_ucb1.nx): instead of a confidence- bound upper-bonus, sample from each arm's posterior and pick the argmax. Empirically tighter regret on stochastic Bernoulli arms; matches UCB asymptotic bounds. CORE OPERATION: For arm i with s_i successes, f_i failures: posterior ~ Beta(s_i + 1, f_i + 1) (uniform Beta(1,1) prior) Per decision: for each arm i: sample x_i ~ Beta(s_i+1, f_i+1) pull arg max x_i SAMPLING WITHOUT f64 (the technical chokepoint): Beta(α, β) for integer α, β equals the distribution of the α-th smallest of α + β − 1 uniforms in [0, 1]. (David-Nagaraja "Order Statistics", standard textbook result.) Exact, no log or special functions needed. Cost O(α + β) per sample. HYBRID FOR LARGE (s + f): - exact order statistic when s + f + 1 ≤ NX_THOMPSON_EXACT_MAX - Normal approximation when larger (cost O(1) instead of O(n)). Beta(α, β) ≈ N(μ, σ²) with μ = α/(α+β), σ² = μ(1−μ)/(α+β+1). Normal sampled via Irwin-Hall sum of 12 uniforms (CLT). Both paths produce Q14 fixed-point samples in [0, Q14] = [0, 1]. EXTENDS NISHI-SUBSTRATE BEYOND DATASKETCHES (DS is summarization- only; bandits live elsewhere). Composes against sketch_ucb1 + sketch_epsilon_greedy as the bandit-family triple. LOSSLESS-LANGUAGE DISCIPLINE: nx_thompson_query returns ApproxI64 with NX_ENV_REL_STDDEV = 1/sqrt(count_i + 1), matching posterior shrinkage as samples accumulate.

dependencies 2 imports · 2 importers

syscalls.nx sketch_types.nx sketch_thompson.nx sketch_thompson_test.nx sketch_thompson_vs_ucb1_bench.nx

imports: syscalls.nxsketch_types.nx

imported by: sketch_thompson_test.nxsketch_thompson_vs_ucb1_bench.nx

structs

51struct Thompson

consts

41const NX_THOMPSON_ARMS_MIN: i64 = 2
42const NX_THOMPSON_ARMS_MAX: i64 = 256
43const NX_THOMPSON_Q14: i64 = 16384
44const NX_THOMPSON_EXACT_MAX: i64 = 64 // cutoff for order-stat sampler
47const NX_THOMPSON_LCG_A: i64 = 1103515245
48const NX_THOMPSON_LCG_C: i64 = 12345
49const NX_THOMPSON_LCG_MOD: i64 = 0x7FFFFFFF

functions

62func nx_thompson_alloc(n_arms: i64, seed: i64) -> *Thompson
called by 2: mainmain
87func nx_thompson_rng_next(t: *Thompson) -> i64
94func nx_thompson_uniform_q14(t: *Thompson) -> i64
101func nx_thompson_isqrt(x: i64) -> i64
123func nx_thompson_sort_scratch(t: *Thompson, n: i64) -> i64
145func nx_thompson_sample_order_stat(t: *Thompson, alpha: i64, n_uniform: i64) -> i64
162func nx_thompson_clt_normal_q14(t: *Thompson) -> i64
176func nx_thompson_sample_q14(t: *Thompson, arm: i64) -> i64
205func nx_thompson_select(t: *Thompson) -> i64
called by 2: mainmain calls 1: nx_thompson_sample_q14
227func nx_thompson_update(t: *Thompson, arm: i64, reward: i64) -> i64
called by 2: mainmain
244func nx_thompson_successes_for(t: *Thompson, arm: i64) -> i64
called by 1: main
248func nx_thompson_failures_for(t: *Thompson, arm: i64) -> i64
called by 1: main
253func nx_thompson_posterior_mean_ppm(t: *Thompson, arm: i64) -> i64
261func nx_thompson_best_arm(t: *Thompson) -> i64
278func nx_thompson_query(t: *Thompson, arm: i64) -> *ApproxI64
290func nx_thompson_memory_bytes(t: *Thompson) -> i64
called by 1: main