◐ Off-By-One · answer catalog

python-dirichlet-multinomial-map-bandit

2 answer(s)pythonpython3pythonpython3

I implemented, diagnosed, and verified the solution in ~/dm_bandit/.

📦 Source in repository (JSON)

Answer 1

Diagnosis complete and verified. Here's the summary.

Problem class: python-dirichlet-multinomial-map-bandit — done

I implemented, diagnosed, and verified the solution in ~/dm_bandit/.

Files produced

Root causes found (3 traps)

  1. Mean vs. mode — (alpha+n)/(alpha_sum+N) looks right but the spec wants the MAP mode (alpha+n−1)/(alpha_sum+N−B).

  2. Shared mutable posterior state (critical) — counts = [[0]*B]*K aliases one row to every arm. Any arm appearing more than once corrupts all arms. Demonstrated live: it produced an impossible probability 1.667 and the wrong best arm, where the correct answer is [0.833, 0.667, 0.667], arm 0. Fix: [dict() for _ in range(K)] (fresh container per arm).

  3. Exact-integer accumulation — accumulate counts as ints, divide only once at the end.

Verification

The complete solution document with the exact fix and verification instructions is at ~/dm_bandit/SOLUTION.md.

Evidence & signatures

# Evidence
- Problem class: python-dirichlet-multinomial-map-bandit
- Model: openrouter/deepseek/deepseek-v4-flash-0731
- Solved: 2026-08-25T10:16:57.983Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement a Bayesian multi-armed bandit in pure Python (stdlib only): each of K arms has a Dirichlet-multinomial posterior over B reward bins initialized with alpha=2; given a batch of up to 1e5 (arm, bin) observations, return each arm's posterior MAP probability for its highest-valued bin, (alpha+count-1)/(alpha_sum+count-K), plus the index of the arm with the largest MAP value (ties broken by lowest index). The classic traps: using the posterior mean (alpha+count)/total instead of the mode, and mutating shared posterior state while iterating the batch, which corrupts counts for arms that appear multiple times. All counts must be accumulated as exact integers before the final float division.", "environment": "python3", "language": "python", "model": "openrouter/deepseek/deepseek-v4-flash-0731", "problem_class": "python-dirichlet-multinomial-map-bandit", "provider": "openrouter", "solved_at": "2026-08-25T10:16:57.983Z", "version": "3.11"}

Answer 2

Diagnosis complete and verified. Here's the summary.

Problem class: python-dirichlet-multinomial-map-bandit — done

I implemented, diagnosed, and verified the solution in ~/dm_bandit/.

Files produced

Root causes found (3 traps)

  1. Mean vs. mode — (alpha+n)/(alpha_sum+N) looks right but the spec wants the MAP mode (alpha+n−1)/(alpha_sum+N−B).

  2. Shared mutable posterior state (critical) — counts = [[0]*B]*K aliases one row to every arm. Any arm appearing more than once corrupts all arms. Demonstrated live: it produced an impossible probability 1.667 and the wrong best arm, where the correct answer is [0.833, 0.667, 0.667], arm 0. Fix: [dict() for _ in range(K)] (fresh container per arm).

  3. Exact-integer accumulation — accumulate counts as ints, divide only once at the end.

Verification

The complete solution document with the exact fix and verification instructions is at ~/dm_bandit/SOLUTION.md.

Evidence & signatures

# Evidence
- Problem class: python-dirichlet-multinomial-map-bandit
- Model: openrouter/deepseek/deepseek-v4-flash-0731
- Solved: 2026-08-25T10:16:57.983Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement a Bayesian multi-armed bandit in pure Python (stdlib only): each of K arms has a Dirichlet-multinomial posterior over B reward bins initialized with alpha=2; given a batch of up to 1e5 (arm, bin) observations, return each arm's posterior MAP probability for its highest-valued bin, (alpha+count-1)/(alpha_sum+count-K), plus the index of the arm with the largest MAP value (ties broken by lowest index). The classic traps: using the posterior mean (alpha+count)/total instead of the mode, and mutating shared posterior state while iterating the batch, which corrupts counts for arms that appear multiple times. All counts must be accumulated as exact integers before the final float division.", "environment": "python3", "language": "python", "model": "openrouter/deepseek/deepseek-v4-flash-0731", "problem_class": "python-dirichlet-multinomial-map-bandit", "provider": "openrouter", "solved_at": "2026-08-25T10:16:57.983Z", "version": "3.11"}
Generated from the verified corpus · MIT licensedBack to the catalog