Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:AISecurityLab Hackagent Stratified Sampling

From Leeroopedia
Knowledge Sources
Domains Evaluation, Judge_Panel, Statistics
Last Updated 2026-10-29 00:00 GMT

Overview

Calibration sampling technique that draws labelled examples in proportion to label distribution, guaranteeing both classes are present so judge accuracy metrics have both recall and precision to report.

Description

When measuring a judge panel against human-labelled samples before a campaign (audit), the calibration dataset is much larger than the audit needs. Drawing a random sub-sample risks producing an all-positive (or all-negative) set — especially when one class is rare. A judge measured on a single-class sample has no recall to report and no false positives to find, making the audit meaningless.

stratified_sample splits the labelled dataset by class, shuffles each group deterministically with a fixed seed, then draws positives in proportion to their share of the full dataset. It enforces that at least one positive and one negative are included whenever the sample size allows (size >= 2 and both classes are non-empty). The final drawn set is shuffled again so order does not leak the label.

Usage

Use this heuristic whenever you are auditing a judge panel against a labelled calibration dataset and the dataset is larger than the desired sample size. It is applied automatically by load_calibration (via CalibrationSpec.sample_size and CalibrationSpec.seed). Override the defaults when you need a larger audit, a different random seed, or the full dataset (sample_size: null).

The Insight (Rule of Thumb)

  • Action: Set evaluation.audit.dataset.sample_size to the number of labelled samples to audit on (default: 100). Set seed for repeatability (default: 42).
  • Value: sample_size=100, seed=42 by default. None uses the full dataset.
  • Trade-off: Larger samples give more reliable accuracy estimates but cost more judge calls (one per sample per judge). Smaller samples are faster but noisier. The stratification guarantees both classes are represented regardless of size.
  • Constraint: When size >= 2 and both classes exist, at least one positive and one negative are always included (positives = min(size-1, max(1, positives))).

Reasoning

A judge audit reports precision (true positives / called positives) and recall (true positives / actual positives). Both require at least some positives and some negatives in the sample. If a random draw happens to select only positives, recall is trivially 1.0 and precision is undefined; if only negatives, recall is 0.0 and there are no false positives to find. Stratified sampling eliminates this failure mode by construction.

The proportional draw preserves the class balance of the source dataset, so the audit's accuracy metrics reflect what the judge would see on the full dataset, not on a skewed subset. The deterministic seed makes the audit repeatable: the same CalibrationSpec always draws the same sample, so a judge's measured precision means the same thing next month.

Code Evidence

Stratified sampling logic from calibration.py:123-151:

def stratified_sample(
    samples: list[LabelledSample], size: Optional[int], seed: int
) -> list[LabelledSample]:
    """Draw ``size`` samples deterministically, keeping both labels present."""
    if size is None or size >= len(samples):
        return list(samples)
    if size <= 0:
        raise ValueError("sample size must be greater than zero")

    randomizer = random.Random(seed)
    by_label: dict[bool, list[LabelledSample]] = {
        True: [item for item in samples if item.label],
        False: [item for item in samples if not item.label],
    }
    for group in by_label.values():
        randomizer.shuffle(group)

    positives = round(size * len(by_label[True]) / len(samples))
    if size >= 2 and by_label[False] and by_label[True]:
        positives = min(size - 1, max(1, positives))
    drawn = by_label[True][:positives] + by_label[False][: size - positives]
    randomizer.shuffle(drawn)
    return drawn

Default calibration spec from calibration.py:59-67:

sample_size: Optional[int] = Field(
    default=100, ge=1,
    description="How many samples to draw. ``None`` uses the whole dataset.",
)
seed: int = Field(
    default=42,
    description="Random seed for drawing the sample, so audits are repeatable.",
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment