Files
OBLITERATUS/obliteratus/analysis/example_selection_report.py
Joseph MaglyandClaude Opus 5.5 bb9dff57e5 feat(analysis): example selection study against random and lexical baselines
Add optimization study O02: does choosing fewer, better examples
preserve targeting quality at lower cost?

- Arms at matched budgets over fit rows only: uniform stratified
  sampling (exact inclusion probabilities), lexical max-min diversity
  on hashed n-grams (no text kept), and representation arms (k-means
  representatives, k-center coreset, boundary hard negatives fitted on
  a disjoint half).
- Quality is held-out: a difference-of-means direction fitted on the
  selection is scored on select rows or a fixed group-disjoint holdout
  no arm sees. In-sample AUC is reported as optimistic and never used.
- Cost counts forward passes and activation bytes (representation arms
  pay for embedding the pool unless amortization is declared); host
  timings are reported but excluded from the verdict and digest.
- Coverage limits (every set, rare sets) flag arms that drop cases.
- The verdict is "no improvement" unless a representation arm beats
  both baselines beyond seed spread at equal or lower cost without a
  coverage flag. Locked test is refused; reports are reproducible.
- `obliteratus experiments select-examples` and a Python API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 11:06:17 -05:00

112 lines
5.1 KiB
Python

"""Seed statistics, the O02 recommendation rule and the versioned example-selection report.
The recommendation is ``"no improvement"`` unless a representation arm, with
no coverage violation at any assessable budget, beats BOTH baselines beyond
seed uncertainty at some budget (its lowest held-out AUC over seeds exceeds
the highest of each baseline at that budget) AND that point is not dominated
on the quality/cost frontier: no baseline point (any budget of either
baseline, or the full development pool) needs no more forward passes and has
a mean held-out AUC at least the representation arm's lowest seed. Only
independent held-out AUC enters the rule; in-sample AUC and host timings are
reported but never consulted.
"""
from __future__ import annotations
import hashlib
import json
import math
from pathlib import Path
REPORT_DIR = "example-selection"
BASELINES = ("baseline-uniform", "baseline-lexical")
RULE = ("improvement only if a representation arm without coverage violations has its minimum "
"held-out AUC over seeds above the maximum of BOTH baselines at a coverage-assessable "
"budget, and no baseline point (or the full development pool) with no more forward "
"passes reaches that minimum on mean held-out AUC; in-sample AUC and host timings are "
"never used")
def seed_stats(values):
"""Mean, sample SD and the [min, max] interval over seeds (the uncertainty used)."""
n = len(values)
mean = sum(values) / n
sd = math.sqrt(sum((v - mean) ** 2 for v in values) / (n - 1)) if n > 1 else 0.0
return {"mean": mean, "sd": sd, "min": min(values), "max": max(values), "n": n,
"interval": "[min, max] over seeds", "per_seed": list(values)}
def _ok(row):
return row is not None and row["status"] == "ok"
def recommend(arms, full_pool, *, seeds, min_seeds=3):
"""Apply the adoption rule to per-arm frontier rows; returns verdict and reasons."""
out = {"verdict": "no improvement", "rule": RULE, "winners": [], "reasons": [],
"multiplicity": "several representation variants are compared; a win by one is a "
"best-of-variants result and needs confirmation on new data"}
if seeds < min_seeds:
out["reasons"].append(f"{seeds} seed(s) < {min_seeds}: seed uncertainty not assessable")
return out
baselines = {family: name for name, arm in arms.items() for family in BASELINES
if arm["family"] == family}
missing = [family for family in BASELINES if family not in baselines]
if missing:
out["reasons"].append(f"baseline unavailable: {', '.join(missing)}")
return out
by_budget = {name: {row["budget"]: row for row in arms[name]["frontier"]}
for name in baselines.values()}
points = [(row["cost"]["forwards"], row["heldout_auc"]["mean"])
for name in baselines.values() for row in arms[name]["frontier"] if _ok(row)]
points.append((full_pool["cost"]["forwards"], full_pool["heldout_auc"]))
for name, arm in arms.items():
if arm["family"] != "representation":
continue
if arm["coverage_violation"]:
out["reasons"].append(f"{name}: coverage limit violated; cannot be recommended")
continue
won = False
for row in arm["frontier"]:
compared = [by_budget[b].get(row["budget"]) for b in by_budget]
if not (_ok(row) and row["coverage"]["limits_assessable"]
and all(_ok(c) for c in compared)):
continue
low = row["heldout_auc"]["min"]
if low <= max(c["heldout_auc"]["max"] for c in compared):
continue
if any(cost <= row["cost"]["forwards"] and mean >= low for cost, mean in points):
out["reasons"].append(f"{name} @ {row['budget']}: beats both baselines at equal "
"examples but is dominated on the quality/cost frontier")
continue
won = True
out["winners"].append({"arm": name, "budget": row["budget"], "heldout_auc_min": low})
if not won:
out["reasons"].append(f"{name}: no budget beats both baselines beyond seed "
"uncertainty without being dominated on cost")
if out["winners"]:
out["verdict"] = "improvement"
return out
def canonical(value):
return json.dumps(value, sort_keys=True, allow_nan=False, separators=(",", ":"))
def seal(report):
"""Add ``report_sha256`` over everything except host measurements."""
body = {k: v for k, v in report.items() if k not in {"host_measurements", "report_sha256"}}
report["report_sha256"] = hashlib.sha256(canonical(body).encode()).hexdigest()
return report
def write_report(report, out_dir):
"""Write ``<out_dir>/<site>.json`` (refuses NaN/inf)."""
target = Path(out_dir)
target.mkdir(parents=True, exist_ok=True)
path = target / f"{report['site']}.json"
data = json.dumps(report, indent=2, sort_keys=True, allow_nan=False)
tmp = path.with_suffix(".tmp")
tmp.write_text(data + "\n")
tmp.replace(path)
return path