Ahmed Doghri Logo Image
Ahmed Doghri

riskbandit

The reward-greedy policy does exactly what you asked and exactly what you were afraid it would do. riskbandit builds the guardrail that stops it, dropping violations from 0.733 to 0.007.

riskbandit, a conformal risk-controlled contextual bandit

Greedy Looks Smart Until the Bill Arrives

The reward-greedy policy does exactly what you told it to do and exactly what you were afraid it would do. It finds the reward, then blasts straight through the cost budget without a second thought, because nobody told it a budget existed.

riskbandit makes that tradeoff impossible to ignore. The synthetic logged data has contexts, actions, rewards, and costs. The benchmark trains separate reward and cost models, then asks the only question that matters: which policy actually survives a budget.

Conformal Bounds as a Gate, Not a Suggestion

The conformal policy calibrates cost residuals on held-out logged data. At decision time it only considers actions whose upper cost estimate fits inside the budget, then picks the best reward among the survivors. That makes safety a selection rule baked into the policy, not a paragraph in the README nobody enforces. If every risky action gets blocked, it falls back to the safe action instead of shrugging.

The Number

Reward-greedy averages 0.774 reward while violating the budget on 73.3% of decisions, which is what "optimize for reward, don't ask questions" looks like in practice. The conformal policy gives up some reward, landing at 0.572, and drops the violation rate to 0.7%. The safe action alone gets 0.442 reward with zero violations, the floor everything else is measured against.

The Guardrail's Own Math Was Off by One

The whole pitch is that safety becomes a selection rule with a statistical guarantee behind it, calibrated for 90% coverage. So I checked whether the upper cost bound the policy gates on actually covers 90% of fresh data it never saw during calibration. It didn't.

_quantile picked the ceil(q·n)-th order statistic of the calibration residuals. Split conformal's coverage guarantee needs the ceil((n+1)·q)-th, one index further out. The k-th order statistic of n residuals covers a fresh point with probability k/(n+1), so the shortcut falls short by about q/(n+1). On the 75-, 90-, and 135-point per-action slices this benchmark calibrates on, that predicts about 89.4% coverage instead of 90%.

Measured, not derived: across 20 tuning seeds, each with fresh train, calibration, and test splits, the original formula lands at 89.2% coverage against a 90% target. A disjoint 15-seed holdout, evaluated once, confirms it at 88.8%. Adding the missing +1 brings both to 90.1%. The original policy is untouched, so the headline reward and violation numbers still reproduce exactly, and the fix ships as an explicit opt-in. 14 tests pass on GitHub Actions across Python 3.9 through 3.13.

It's a small number, and that's the point. A guarantee that's quietly 89% isn't a guarantee, and nobody downstream of the policy will ever see the difference until the one decision that mattered lands in the missing point.

Try It

git clone https://github.com/ahmeddoghri/riskbandit && cd riskbandit
pip install -e .
python -m riskbandit.benchmark  # greedy violates 73.3%, conformal 0.7%
python -m riskbandit.eval_v2    # does 90% coverage mean 90%? 89.2% → 90.1%

Tools Used

Python
Contextual Bandits
Conformal Prediction
Safe RL
Offline Evaluation
Split Conformal Prediction
pytest