Metadata-Version: 2.4
Name: judgebias
Version: 0.1.2
Summary: Point it at your LLM judge + your judgments; get per-bias effect sizes with 95% CIs and concrete corrections.
Author: macamiri
License: MIT
Project-URL: Homepage, https://github.com/macamiri/judgebias
Keywords: llm,llm-as-judge,evaluation,bias,calibration,position-bias,verbosity-bias,self-preference
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Requires-Dist: pandas>=1.5
Provides-Extra: examples
Requires-Dist: datasets>=2.14; extra == "examples"
Provides-Extra: dev
Requires-Dist: pytest>=7.4; extra == "dev"
Dynamic: license-file

# judgebias

**Point `judgebias` at your LLM judge and your judgments — it returns, per bias, a measured effect size with a 95% confidence interval and a concrete correction.** No model to train, no benchmark to beat: just statistics over the decisions your judge already made.

LLM-as-judge is now standard for model selection, prompt A/B tests, and release gating. But judges have systematic habits — they favor the response shown **first**, the **longer** one, and answers from their **own** model family — and those habits silently bend the win-rates teams ship decisions on. `judgebias` measures how much each habit is bending *your* numbers.

```python
import judgebias as jb

# your judge: judge_fn(first, second) -> "first" | "second" | "tie"
swapped = jb.swap_and_judge(pairs, my_judge)     # runs each pair in both A/B orders
print(jb.position_bias(swapped["inconsistent"])) # -> effect + 95% CI + the fix
```

---

## Who this is for

Teams increasingly use one AI model to grade another model's answers — it is fast and cheap. But the grader has hidden habits: it tends to favor whichever answer it sees first, the longer answer, and answers written in its own style. These habits quietly bend the scores teams rely on to pick a model, compare prompts, or sign off a release — so the apparent winner may have just been longer, or first, not better. `judgebias` is for the engineers running those automated comparisons. Today they either trust the grader blindly or read research papers and adjust by hand. Point it at your own grader and your own results: it measures how strongly each habit is skewing your scores and gives a concrete, specific way to correct each one — no paper required.

---

## What it measures (v0.1)

| Bias | Question it answers | Effect (no-bias baseline) |
|---|---|---|
| **position** | Does the verdict change when you swap A/B? | order-inconsistency rate (0.0) |
| **position vs noise** | Is it the order, or is the judge just random? (judges each order twice) | first-slot share (0.5), with a rerun noise floor |
| **length** | Does the judge prefer the longer response? | P(picks longer) (0.5) |
| **self-preference** | Does the judge favor its own model beyond a human baseline? | judge − human win-rate for own model (0.0) |

Each result is flagged `BIAS` only when its 95% CI excludes the no-bias baseline. The rerun control flags on Holm-adjusted exact tests instead ([below](#is-it-the-order-or-is-the-judge-just-random)).

---

## Real example: GPT-4 grading MT-Bench

This is the **actual, unedited output** of [`examples/mtbench_judge_bias.py`](examples/mtbench_judge_bias.py), run on [`lmsys/mt_bench_human_judgments`](https://huggingface.co/datasets/lmsys/mt_bench_human_judgments) — a dataset that ships a real GPT-4 judge's 2,400 pairwise verdicts plus 3,355 human verdicts over 6 models, so all three biases can be measured on a real judge with a human baseline, fully offline. The 2,400 verdicts come from 80 questions, so the CIs resample whole questions:

```
judgebias - GPT-4 judge on MT-Bench (lmsys/mt_bench_human_judgments)
====================================================================
[BIAS] position bias    order-inconsistency rate
        effect = +0.158   95% CI [+0.138, +0.180]   (no-bias = +0.00, n = 2400)
        fix: Evaluate every pair in BOTH A/B orders and average; treat order-flips as ties (the MT-Bench convention, arXiv:2306.05685).

[BIAS] length bias      P(judge picks the longer response)
        effect = +0.735   95% CI [+0.692, +0.775]   (no-bias = +0.50, n = 1792)
        fix: Length-control your win-rate: regress preference on response length and report the length-adjusted estimate (cf. length-controlled AlpacaEval, arXiv:2404.04475).

[BIAS] self-preference  judge win-rate for own model minus human baseline
        effect = +0.119   95% CI [+0.079, +0.158]   (no-bias = +0.00, n = 430)
        fix: Don't let a model grade its own family: use a different judge for self-comparisons, or subtract this judge-vs-human gap as a debias offset.

summary: 3/3 biases flagged at 95% CI -> position bias, length bias, self-preference

context / baselines:
  length bias, HUMAN judges:  effect = +0.681  95% CI [+0.642, +0.717]  n = 2562
  self-preference compared on n = 430 pairs where gpt-4 is a competitor (judged by both GPT-4 and humans)
  95% CIs resample whole questions (80 in the GPT-4 set).
  position bias here includes any rerun noise: MT-Bench has one verdict per order, so the
  noise floor cannot be estimated. For your own judge use swap_and_judge(..., rerun=True).
```

**Read that as:** on MT-Bench, GPT-4-as-judge flips its verdict on **15.8%** of pairs when you swap the order (that rate also counts any run-to-run noise in the judge, which this dataset cannot separate out); picks the **longer** answer **73.5%** of the time (humans: 68.1% — the judge is *more* length-biased than people); and prefers its **own** model **+11.9 points** more often than humans do. Every number has a CI and a fix.

*On self-preference specifically:* on the 430 MT-Bench pairs where `gpt-4` is one of the two answers, GPT-4-**as-judge** chose gpt-4's answer **11.9 percentage points** more often than the **human raters** did on those *same* pairs (95% CI [+7.9, +15.8]). Because the human rate is the baseline, that gap is the judge's *excess* preference for its own model — over and above the quality humans already credited it.

---

## Is it the order, or is the judge just random?

A judge running above temperature 0, or behind a provider that is not fully deterministic, can change its verdict when it sees the same pair twice in the same order. The order-inconsistency rate above counts those changes as position bias. A judge that flips a coin, with no order preference at all, scores about 0.5 on it.

`swap_and_judge(..., rerun=True)` judges every pair in both orders twice (AB, BA, AB′, BA′: 4 calls per pair, in a shuffled order), and `position_bias_vs_noise` reports three numbers:

| Result | What it measures | Value with no order effect |
|---|---|---|
| **first-slot share** | Of the decisive verdicts, the share that picked the response shown first. This is the size of the order effect. | 0.50 |
| **rerun flip rate** | How often the verdict changes when the same order is shown again. The noise floor, reported as a yardstick with no test of its own. | (reference) |
| **swap beyond rerun** | Swap flips minus rerun flips, paired by pair. Says whether order matters beyond noise. The gap is not the size of the effect; read that from the first-slot share. | 0.00 |

```python
rr = jb.swap_and_judge(pairs, my_judge, rerun=True, session="2026-10-25")   # 4 calls per pair
rr["question_id"] = question_ids                                             # CIs resample whole questions
print(jb.position_bias_vs_noise(rr, groups="question_id", judge_model="my-judge-v3",
                                temperature=0.7, date="2026-10-25", run_seed=42))

# reruns on a later day: judge once now, add the reruns tomorrow
day1 = jb.swap_and_judge(pairs, my_judge, session="day 1")
both = jb.add_reruns(day1, pairs, my_judge, session="day 2")
```

How it works:

- Each swap comparison and each rerun comparison pairs a first-pass call with a rerun call (AB with BA′ and BA with AB′; AB with AB′ and BA with BA′). With no order effect the paired difference is symmetric around zero, even if the judge drifts between sessions.
- The two tests (first-slot share against 0.5, swap flips against rerun flips) are exact two-sided sign tests over pairs, or over questions with `groups=`, and the report states that their p-values are Holm-adjusted. The tests stay valid without `groups=`; the CIs need it when pairs share a question.
- With the default `ties="as_flip"` (any change counts as a flip) the expected swap-minus-rerun gap is half the squared distance between the judge's verdict distributions in the two orders, so any order effect pushes it above zero. With `ties="not_flip"` (a change to or from a tie is ignored, the rule behind the `inconsistent` column) the gap can be zero or negative when only the tie rate depends on the order.
- The report records the judge model, temperature, date, call-order seed (`run_seed`), bootstrap seed and tie rule. It counts tie verdicts and gives the first-slot share twice: with ties dropped, and with a tie scored as half a win. Half-win scoring, like the MT-Bench convention of scoring an order-inconsistent pair as a tie, pulls results toward 0.5 and can hide an order effect. 
- If every rerun matches its first call, the report warns that the judge may be deterministic or cached, which makes the noise floor 0 by construction.

Rerun every pair. Rerunning only the pairs that flipped is a tempting shortcut, and it biases the noise floor in a direction that depends on the mix of pairs (the test suite shows it going each way). `position_bias_vs_noise` refuses a partial frame unless you pass `rerun_subset="random"`, and still warns when the rerun subset's first-pass flip rate differs from the rest (Fisher exact test). A subset picked on some other outcome is not detected.

On a local judge (`examples/rerun_control_local.py`: Ollama `qwen3:4b`, 100 MT-Bench turn-1 pairs across 80 questions, temperature 0.7), the actual output is:

```
100 pairs over 80 questions (dataset lmsys/mt_bench_human_judgments @ f7d2896d2cc5)
400 calls in 1821 s, median 3.64 s/call, unparseable verdicts: 0
judgebias - position bias vs judge noise (same-order rerun control)
===================================================================
run: judge=qwen3:4b  temperature=0.7  date=2026-10-26  seed=42  bootstrap_seed=42  ties=as_flip  pairs=100  clusters=80  calls/pair=4

[BIAS] first-slot share share of decisive verdicts that picked the response shown first
        effect = +0.665   95% CI [+0.617, +0.712]   (no-bias = +0.50, n = 397)
        p = 1.77e-07   Holm-adjusted p = 2.24e-07
        tie verdicts: 3 of 400 calls; with a tie scored as half a win the share is 0.664. Scoring ties as half a win, or order-inconsistent pairs as ties, pulls results toward 0.5 and can hide an order effect.
        fix: Judge every pair in both orders and combine the two verdicts; randomise the order in production. This share is the size of the order effect.

[info] rerun flip rate  share of same-order reruns whose verdict changed (the noise floor)
        effect = +0.130   95% CI [+0.084, +0.176]   (reference = +0.00, n = 100)
        reported as the yardstick for the swap flips below; no test is run on it.
        fix: This is judge randomness and says nothing about order. Average several calls per order, or lower the temperature if your provider honours it.

[BIAS] swap beyond rerun swap flip rate minus rerun flip rate (paired)
        effect = +0.270   95% CI [+0.184, +0.357]   (no-bias = +0.00, n = 100)
        p = 1.12e-07   Holm-adjusted p = 2.24e-07
        swap flip rate 0.400 vs rerun flip rate 0.130. Tests whether order matters beyond noise; this gap is not the size of the effect.
        fix: If flagged, the order effect is real. Read its size from the first-slot share.

note: the noise floor covers within-session variation only: no rerun carries a session label different from its first pass (unlabelled runs count as the same session). Variation across days or provider updates is not measured unless the sessions span them.
note: 2 tests (first-slot share vs 0.5; swap flips vs rerun flips): exact two-sided sign tests over 80 clusters, p-values Holm-adjusted.

summary: 2/2 tested effects flagged (Holm-adjusted p < 0.05) -> first-slot share, swap beyond rerun
```

The same 100 pairs at temperature 0 (`--temperature 0`) are a control for the noise floor only. Every rerun matched its first call, so the report gives a noise floor of 0 and warns that it was never measured. That the judge repeated itself at temperature 0 is an observation about this local Ollama model, and it cannot be assumed for other models or hosted APIs [5]. The first-slot share was 0.645 (95% CI [0.591, 0.696]) and the swap flip rate 0.370. This CI overlaps the one at temperature 0.7, so the run gives no evidence that a lower temperature changes the order effect.

Why this matters: Cui and Alexander ran the same data analysis ten times for each of 48 model, prompt and temperature settings, and report that "even when temperature is zero it cannot be assumed that the model will provide the same results" ([5], §2.5). A judge is an LLM call as well, so one verdict per order cannot tell order bias from noise.

---

## Install

```bash
pip install judgebias
pip install "judgebias[examples]"   # adds the real-data MT-Bench demo
python -m judgebias.demo            # run the demo without cloning (or: judgebias-demo)
```

> The first demo run downloads `lmsys/mt_bench_human_judgments` (a few MB) once and caches it under `~/.cache/huggingface` — expect ~5–10s cold, near-instant after.

---

## Quickstart — bring your own judge

`judgebias` is judge-agnostic: it never calls a model itself. You supply your judge as a function (GPT-4o, Claude, Gemini, a fine-tune — anything), or you supply judgments you already have.

```python
import judgebias as jb

# --- position bias: judgebias drives YOUR judge in both orders ---
# my_judge(first, second) -> "first" | "second" | "tie"
pairs = [(qid, resp_a, resp_b), ...]
swapped = jb.swap_and_judge(pairs, my_judge)
print(jb.position_bias(swapped["inconsistent"]))

# --- length bias: over judgments you already have ---
# choice in {"a", "b", "tie"}; len_a/len_b are response lengths (chars, tokens — your call)
print(jb.length_bias(len_a, len_b, choice))

# --- self-preference: your judge vs a human (or reference) baseline on the SAME pairs ---
# 1 if the judge's own model was chosen on that pair, else 0
print(jb.self_preference(judge_chose_own, human_chose_own))

# --- or let judgebias run everything a judgments frame supports ---
report = jb.diagnose(df, choice_col="choice",
                     len_a_col="len_a", len_b_col="len_b",
                     inconsistent_col="inconsistent")
print(report)
```

---

## How the numbers are computed (architecture)

- **Confidence intervals are a nonparametric percentile bootstrap** (`n_boot=2000`, `seed=42`). We use bootstrap rather than a closed-form/exact test because it applies *uniformly* to all three effect types — including the **paired** judge-vs-human delta in `self_preference`, where a textbook proportion interval doesn't fit — and it runs in well under a second at these sample sizes. Everything is seeded, so reported numbers are reproducible.
- **Effects are reported on interpretable scales with an explicit no-bias baseline**, and a result is only flagged `BIAS` when its CI excludes that baseline — so the tool never cries wolf on a noisy estimate.
- **The core is pure `numpy`/`pandas`.** No deep-learning dependency; the bias math is statistics, not ML.
- Clustered intervals. Every estimator takes `groups=` (for example a question id); the bootstrap then resamples whole clusters, because judgments on the same question are not independent. The MT-Bench demo clusters by question.
- The rerun control uses exact sign tests (no normal approximation) with a Holm correction across its two tests; see [above](#is-it-the-order-or-is-the-judge-just-random).
- **Position bias** is the order-inconsistency rate. For your own judge, `swap_and_judge` generates it by running both orders. In the MT-Bench demo it is read from the dataset's `tie (inconsistent)` label — MT-Bench already ran both orders and recorded the flips.

---

## Why this exists (vs the alternatives)

| Tool | What it does | Why it isn't this |
|---|---|---|
| **length-controlled AlpacaEval** (arXiv:2404.04475) | Corrects **length** bias | One bias, inside *its own* benchmark — not your judge, not your data |
| **BiasScope** (ICLR 2026) | *Discovers* novel biases by attacking a judge on benchmark sets | Research code (not pip-installable); benchmark-driven discovery, not a point-at-your-own-judgments diagnostic |
| **CALM** (arXiv:2410.02736) | A 12-bias taxonomy + quantification study | Paper + dataset; no installable package |
| eval harnesses (lm-eval-harness, inspect-ai, Braintrust) | Run judge-based scoring | Treat the judge as a trusted oracle — no per-bias diagnostic |

**Why a bring-your-own-judge *workflow* is the contribution.** The individual estimators are not new: MT-Bench (arXiv:2306.05685) measured order sensitivity, length-controlled AlpacaEval (arXiv:2404.04475) corrects verbosity, and CALM (arXiv:2410.02736) catalogued twelve judge biases. What did *not* exist is an installed, judge-agnostic tool you point at **your own judge and your own judgments** to get per-bias effect sizes, confidence intervals, and concrete corrections in a single workflow. CALM ships as a paper plus a dataset; AlpacaEval corrects one bias inside its own benchmark; BiasScope is research code that auto-discovers biases on fixed benchmarks. `judgebias` packages the measurement-to-correction loop for the judge you actually run — the product is the workflow, not the paper.

---

## When NOT to use this

- **You don't control the judge.** Fixed leaderboards (Chatbot Arena, MMLU) — there is no judge of yours to debias.
- **Your judgment set is small (< ~400 pairs).** The bootstrap CIs get too wide to act on — collect more judgments first. (For reference, the self-preference example here runs on n = 430 and its CI is already ±0.035.)
- **You need causal debiasing, not measurement.** `judgebias` *diagnoses* biases and recommends corrections; it does not re-train or causally intervene on your judge.
- **Single-model, pointwise** evaluation with no pairwise comparison — position and self-preference don't apply (length still can).
- You need **conformal prediction intervals** for scores (use MAPIE/crepes), or you want to **discover unknown** biases rather than measure known ones (use BiasScope).

## Limitations (honest)

- **Length and self-preference are correlational.** A judge picking the longer answer 73.5% of the time may partly reflect that longer answers are genuinely better; the recommended fix (length-controlled win-rate) is the right next step, and self-preference is reported *relative to a human baseline* precisely to net out genuine quality.
- **The MT-Bench demo is one judge (GPT-4) over six 2023-era models.** It demonstrates the tool on a real judge; it is not a claim about every judge. Point the tool at *your* setup for numbers that apply to you.
- **`swap_and_judge` calls your judge twice per pair** (both orders) — budget for 2× judge calls when measuring position bias on your own judge. With `rerun=True` it is 4 calls per pair.
- A judge at temperature 0, or one behind a cache, gives a rerun noise floor of 0 by construction. The report says so when every rerun matches its first call.
- The noise floor covers variation within one session. Changes across days or provider updates are measured only if you rerun on a later day and label the sessions (`add_reruns(..., session=...)`).
- A first-slot share near 0.5 does not rule out an order effect on hard pairs. A judge can favour the first slot on some questions and the second slot on others, and the two cancel in the share.
- The exact sign tests are conservative at small samples. They never exceed the 5% level, but the binomial steps keep the true type-I error between 0.012 (11 discordant units) and 0.049 (17) for 10 to 60 units, 0.032 on average (exact enumeration: `judgebias.calibration.sign_test_type1_table`). Below about 60 units expect less power than a 5% test would have.
- Clustered bootstrap coverage (simulation, `JUDGEBIAS_RUN_COVERAGE=1 pytest tests/test_calibration.py`): the nominal 95% CI contained the true first-slot share in 94.2% of 1000 simulated datasets at 20 clusters, 93.8% at 40 and 95.6% at 80 (Monte Carlo SE about 0.7 points; 5 pairs of 4 calls per cluster, true share 0.6, cluster-level share drawn from Beta(4.8, 3.2)). The largest shortfall below 95%, 1.2 points at 40 clusters, is 1.6 Monte Carlo SE.
- v0.1 covers position, length, and self-preference. Formatting and sycophancy biases are planned (they need a controlled-perturbation harness).

## Reproduce the demo

```bash
pip install -e ".[examples]"
python -m judgebias.demo                   # prints the block shown above; seeded, deterministic
python examples/mtbench_judge_bias.py      # identical output, the from-a-clone equivalent
pytest -q                                  # 46 offline tests (planted biases, the rerun control, the demo entry point)
JUDGEBIAS_RUN_COVERAGE=1 pytest tests/test_calibration.py -q   # opt-in: CI coverage simulation
```

**Second example — your own judge on Chatbot Arena.** [`examples/chatbot_arena_demo.py`](examples/chatbot_arena_demo.py) runs the same diagnostics on `lmsys/chatbot_arena_conversations` with a judge you supply; it needs HuggingFace access (accept the dataset terms + a token) and an LLM judge API key, so it is not run in CI.

---

## Notes

Built with assistance from AI coding agents; the architecture, methodology, statistics, and analysis are the author's.

Grounding: judgebias's three measurements trace to published work — position bias and the swap-and-average correction to [1], length-controlled win-rates to [2], and the bias taxonomy to [3] (see [References](#references)). Demo data: `lmsys/mt_bench_human_judgments` (CC-BY-4.0).

## References

[1] Zheng et al. (2023). *Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena*. NeurIPS 2023 Datasets and Benchmarks Track. [arXiv:2306.05685](https://arxiv.org/abs/2306.05685)

[2] Dubois et al. (2024). *Length-Controlled AlpacaEval: A Simple Way to Debias Automatic Evaluators*. COLM 2024. [arXiv:2404.04475](https://arxiv.org/abs/2404.04475)

[3] Ye et al. (2024). *Justice or Prejudice? Quantifying Biases in LLM-as-a-Judge*. arXiv preprint. [arXiv:2410.02736](https://arxiv.org/abs/2410.02736)

[4] Lai et al. (2026). *BiasScope: Towards Automated Detection of Bias in LLM-as-a-Judge Evaluation*. ICLR 2026. [arXiv:2602.09383](https://arxiv.org/abs/2602.09383)

[5] Cui and Alexander (2026). *Same Prompt, Different Outcomes: Evaluating the Reproducibility of Data Analysis by LLMs*. arXiv preprint. [arXiv:2602.14349](https://arxiv.org/abs/2602.14349)

[6] Holm (1979). *A Simple Sequentially Rejective Multiple Test Procedure*. Scandinavian Journal of Statistics 6, 65–70.

## License

MIT — see [LICENSE](LICENSE).
