# `Nous.Eval.JudgeCalibration`
[🔗](https://github.com/nyo16/nous/blob/v0.21.1/lib/nous/eval/judge_calibration.ex#L1)

Measures an LLM judge (`Nous.Eval.Scorers.Judge`) against human labels.

    {:ok, cases} = Nous.Eval.JudgeCalibration.load("priv/eval/judge_cases.jsonl")

    {:ok, result} =
      Nous.Eval.JudgeCalibration.calibrate(
        [judge: "openai:gpt-4o-2024-08-06", rubric: "...", scale: :binary],
        cases
      )

    result.kappa        #=> 0.71
    result.false_pass   #=> %{estimate: 0.06, ci: {0.02, 0.17}, count: 3, n: 50}

`calibrate/3` replays stored agent outputs through the judge (no agent
runs) and compares its verdicts with the human labels on the **holdout**
split only; tune the rubric on the `tune` split and never look at holdout
results while doing so. `mix nous.eval.judge.calibrate` runs it from the
command line and stores the result in an eval store
(`c:Nous.Eval.Store.put_calibration/3`), keyed by the judge's
`Nous.Eval.Scorers.Judge.fingerprint_id/1`. A judge configured with
`require_calibration:` refuses to grade unless the stored result matches
its configuration and passes `check/3`.

## Cases (schema `nous.judge_case/1`)

A JSONL file, one case per line:

    {"id": "refund-17", "input": "Refund order 42", "output": "Refunded $20 for order 42.",
     "reference": "Refund of $20 issued for order 42.", "human_label": "pass",
     "annotator": "ana", "split": "holdout", "group": "refund-17",
     "provenance": "run 2026-10-30-main, sample 0"}

  * `id` - non-empty string, unique
  * `input` - the request: a string or a list of `{role, content}`
    messages (`system`, `user`, `assistant`)
  * `output` - the agent's answer: a string, or a JSON object for
    structured output
  * `reference` - optional reference answer (seen by a judge with
    `include: [:reference]`)
  * `human_label` - the human's verdict, a label of the judge's scale
    (`"pass"`, `"4"`, ...; an integer is read as its string). Human labels
    only: never a judge's own verdict
  * `annotator` - who labelled it
  * `split` - `"tune"` or `"holdout"`; all cases of a `group` must share it
  * `group` - cases that must stay in one split (paraphrases, outputs of
    the same request)
  * `provenance` - where the output came from (string or object)

Loading is strict like `Nous.Eval.Dataset`: an unknown key, a missing or
mistyped field, a duplicate id or a group spanning both splits is an
error, and nothing in the file becomes an atom. A line may carry
`"schema": "nous.judge_case/1"`.

## Metrics (holdout only)

  * `agreement` - share of cases where judge and human gave the same
    label, with a Wilson interval
  * `kappa` - Cohen's κ (`Nous.Eval.Stats.cohen_kappa/2`) for binary,
    ternary and choice scales; quadratic-weighted κ
    (`Nous.Eval.Stats.weighted_kappa/3`) for Likert scales. `nil` when
    undefined
  * `confusion` - counts by human label (rows) and judge label (columns),
    in the scale's label order
  * `false_pass` - among cases the human failed (label value below the
    judge's threshold), the share the judge passed, with a Wilson
    interval; `nil` without such cases
  * `error_rate` - share of cases where the judge returned a
    `Nous.Eval.ScorerError`; those cases are left out of the other
    metrics and listed in `errors`

# `calibrate`

```elixir
@spec calibrate(
  keyword() | {module(), keyword()} | Nous.Eval.Scorer.Spec.t(),
  [Nous.Eval.JudgeCalibration.Case.t()],
  keyword()
) :: {:ok, Nous.Eval.JudgeCalibration.Result.t()} | {:error, term()}
```

Replays the holdout cases through the judge and measures it against the
human labels.

`judge` is the judge's options (`[judge: "openai:...", scale: ...]`),
`{Nous.Eval.Scorers.Judge, opts}` or a `Nous.Eval.Scorer.Spec` for it.
`:require_calibration`, `:calibration` and `:gate` are ignored: they
decide whether a judge may grade, not what it answers.

## Options

  * `:concurrency` - judge calls in flight (default `4`)
  * `:timeout` - per case, in ms (default `120_000`); a case that runs
    over counts as a judge error

Returns `{:error, reason}` before any judge call when the judge options
are invalid, a human label is not a label of the judge's scale, the
judge includes evidence a case cannot provide (`:tool_calls`,
`:transcript`: judge cases carry no trace), or there are no holdout cases.

# `check`

```elixir
@spec check(
  Nous.Eval.JudgeCalibration.Result.t() | nil,
  String.t(),
  map() | keyword()
) ::
  :ok | {:error, :missing | :stale | {:below_threshold, String.t()}}
```

Checks a stored calibration against a judge configuration and the
`require_calibration` thresholds.

`requirements` holds `:min_kappa` (κ at least this) and
`:max_false_pass` (false-pass point estimate at most this); absent keys
are not checked. Fails closed: `nil` result → `{:error, :missing}`, a
result for another `fingerprint_id` → `{:error, :stale}`, an undefined κ
or no human-failed holdout cases while a threshold needs them →
`{:error, {:below_threshold, reason}}`.

# `format`

```elixir
@spec format(Nous.Eval.JudgeCalibration.Result.t()) :: String.t()
```

A plain-text report of `result`, as `mix nous.eval.judge.calibrate` prints it.

# `load`

```elixir
@spec load(Path.t()) ::
  {:ok, [Nous.Eval.JudgeCalibration.Case.t()]} | {:error, String.t()}
```

Loads a `nous.judge_case/1` JSONL file. Blank lines are skipped; errors
name the file and line.

# `new`

```elixir
@spec new([map()]) ::
  {:ok, [Nous.Eval.JudgeCalibration.Case.t()]} | {:error, String.t()}
```

Builds cases from string-keyed maps with the fields of the JSONL format,
validated the same way as `load/1`.

# `schema`

```elixir
@spec schema() :: String.t()
```

The schema id of the case format (`"nous.judge_case/1"`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
