Nous.Eval.Compare (nous v0.21.1)

Copy Markdown View Source

Compares a candidate eval run against a baseline run of the same dataset and decides whether the candidate regressed.

{:ok, comparison} = Nous.Eval.Compare.compare(baseline, candidate)
comparison.verdict
#=> :pass | :regression | :error

The result is a %Nous.Eval.Compare{} (atom keys, atoms for verdicts and labels); to_map/1 turns it into JSON-native data for storage and Nous.Eval.Reporter.

Matching cases

Cases are matched by id. Runs of different datasets (:dataset_hash) are refused with {:error, {:dataset_changed, %{baseline: hash, candidate: hash}}} unless allow_dataset_change: true. Either way only the ids present in both runs are compared; ids only in the candidate are listed as added, ids only in the baseline as removed. A matched case is comparable when it has at least one scored (:pass/:fail) sample in both runs.

Per-case labels (descriptive, never a gate)

With p the share of a case's scored samples that passed and "majority pass" meaning p > 0.5:

  • :fixed - not a majority pass in the baseline, a majority pass in the candidate
  • :broken - the reverse
  • :flaky - neither, and some samples passed while others failed (0 < p < 1) in either run; only possible with k > 1
  • :unchanged - neither, and every scored sample agreed in both runs

A matched case without scored samples on one side has label nil. error_changed is set, separately from the label, when the case has :error samples in one run and none in the other. With k samples per case no single case can be significant at realistic k (3-of-3 vs 0-of-3 has Fisher p = 0.1), so labels are for reading failures, not for gating.

Gate rule

The gate options (:gate, a map or keyword list) are:

  • :metrics - the declared family of gate metrics, default [:pass_rate] (see Nous.Eval.Compare.Metric: :pass_rate, :case_score, {:scorer, name})
  • :margin - tolerated drop, 0.0 <= margin < 1, default 0.0
  • :alpha - one-sided level, 0 < alpha < 0.5, default 0.05
  • :max_error_rate - highest tolerated share of :error samples in the candidate, default 0.0

For each gate metric, over the n cases where it is defined in both runs, let Δ be the candidate's mean per-case value minus the baseline's. B paired cluster bootstrap replicates Δ*₁…Δ*_B are drawn with the case as the cluster (Nous.Eval.Stats.Bootstrap.paired_cluster/2, seeded). The one-sided test of

  • H0: Δ ≥ −margin (the candidate is no worse than the margin) against
  • H1: Δ < −margin (the candidate is worse by more than the margin)

has the bootstrap p-value p = (1 + #{b : Δ*_b ≥ −margin}) / (B + 1). The p-values of the declared metrics are Holm-adjusted together (Nous.Eval.Stats.Hypothesis.holm/2); a metric regresses when its adjusted p-value is <= alpha. For a single metric this is the percentile rule "the upper one-sided 1 − α bootstrap bound of Δ is below −margin" (reported as :upper_bound), up to the +1 correction.

The verdict is, in this order:

  1. :error when the candidate's error rate exceeds :max_error_rate, no case is comparable, either run is :aborted, or a gate metric cannot be tested (defined for fewer than two cases): operational failures fail closed and never read as a pass
  2. :regression when any gate metric regresses
  3. :pass otherwise
:reasons lists why (`%{reason: :error_rate:no_comparable_cases
:run_aborted:metric_untestable:regression, ...}`).

A :pass means no drop larger than the margin was detected. Each gate metric carries :mde, the drop this comparison could detect with 80% power (Nous.Eval.Stats.Power.mde/3 with the variance of the per-case differences, floored at the binomial noise m̃(1 − m̃)(1/k_A + 1/k_B) of the smoothed metric mean m̃ so identical runs still report a finite MDE); no significant change is not the same as no change. Percentile bootstrap intervals undercover below about 100 cases (Bowyer et al. 2025), flagged by small_n?: true.

Secondary statistics

  • :mcnemar - exact McNemar on per-case majority pass (b = fixed, c = broken cases), reported, not gated
  • :breakdowns - exploratory per-tag (pass rate) and per-scorer (score pass rate) deltas with two-sided bootstrap p-values min(1, 2·min(1 + #{Δ* ≤ 0}, 1 + #{Δ* ≥ 0}) / (B + 1)), Benjamini–Hochberg adjusted across all breakdowns as one family (significant? at alpha). Tags come from the :cases option.
  • :resources - cost, latency and token deltas from both runs' Nous.Eval.Summary: :cost_total, :cost_mean, :duration_ms_total, :duration_ms_mean, :duration_ms_p95, :tokens_total and :tokens_mean (per sample), each %{baseline, candidate, delta, relative} with relative = delta / baseline (nil when the baseline is zero or unknown)
  • :fingerprint_changes - the top-level Nous.Eval.Fingerprint keys (except "id") whose values differ
  • :bootstrap - seed, algorithm, resamples, streams, confidence: everything needed to reproduce the intervals

Summary

Functions

Compares candidate against baseline.

The comparison as JSON-native data: string keys, strings for atoms, [lower, upper] lists for intervals and metric names in their string form ("scorer:contains"). Includes both runs' summaries; never traces.

Types

ci()

@type ci() :: {float(), float()}

label()

@type label() :: :fixed | :broken | :flaky | :unchanged

t()

@type t() :: %Nous.Eval.Compare{
  baseline: %{
    id: String.t(),
    status: Nous.Eval.Run.status(),
    summary: Nous.Eval.Summary.t()
  },
  bootstrap: map(),
  breakdowns: [map()],
  candidate: %{
    id: String.t(),
    status: Nous.Eval.Run.status(),
    summary: Nous.Eval.Summary.t()
  },
  cases: [map()],
  comparable_cases: non_neg_integer(),
  counts: %{required(atom()) => non_neg_integer()},
  dataset: %{
    changed?: boolean(),
    baseline_hash: String.t() | nil,
    candidate_hash: String.t() | nil,
    added: [String.t()],
    removed: [String.t()]
  },
  fingerprint_changes: [%{key: String.t(), baseline: term(), candidate: term()}],
  gate: %{
    metrics: [Nous.Eval.Compare.Metric.t()],
    margin: float(),
    alpha: float(),
    max_error_rate: float()
  },
  mcnemar: map() | nil,
  metrics: [map()],
  reasons: [map()],
  resources: map(),
  verdict: verdict()
}

verdict()

@type verdict() :: :pass | :regression | :error

Functions

compare(baseline, candidate, opts \\ [])

@spec compare(Nous.Eval.Run.t(), Nous.Eval.Run.t(), keyword()) ::
  {:ok, t()} | {:error, term()}

Compares candidate against baseline.

Options:

  • :gate - see "Gate rule" in the moduledoc
  • :allow_dataset_change - compare runs of different datasets on their common case ids, default false
  • :seed - bootstrap seed (integer); default the candidate run's seed, else a fixed constant
  • :resamples - bootstrap replicates, default 10_000
  • :streams / :max_concurrency - bootstrap random streams (default 8) and how many run at once (default 1); results depend on :streams, never on :max_concurrency
  • :confidence - two-sided level of the reported intervals, default 0.95
  • :cases - the dataset's cases ([Nous.Eval.Case.t()]) or a map of case id to tags, for the per-tag breakdown

Returns {:error, {:dataset_changed, %{baseline: h, candidate: h}}}, {:error, {:invalid_option, key}} or {:error, {:unknown_option, key}} before computing anything.

to_map(c)

@spec to_map(t()) :: map()

The comparison as JSON-native data: string keys, strings for atoms, [lower, upper] lists for intervals and metric names in their string form ("scorer:contains"). Includes both runs' summaries; never traces.