rohitg00/ai-engineering-from-scratch · error · PairingError

candidate and baseline lengths disagree

Error message

candidate and baseline lengths disagree

What it means

paired_t_test guard: the candidate and baseline score lists have different lengths, but a paired test requires one candidate score per baseline score.

Solutions

  1. Run both arms over the same set of seeds so lists align
  2. Drop unmatched entries (or intersect by seed) before calling the test
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/53-result-evaluator/code/main.py:156 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/8e4b66f734abaefa. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/53-result-evaluator/code/main.py:156

    bt = math.exp(log_bt)
    if x < (a + 1.0) / (a + b + 2.0):
        return bt * _lentz_betacf(a, b, x) / a
    return 1.0 - bt * _lentz_betacf(b, a, 1.0 - x) / b


def two_sided_t_p_value(t_stat: float, df: int) -> float:
    """Two sided p value for a t statistic with df degrees of freedom."""
    if df <= 0:
        return 1.0
    x = df / (df + t_stat * t_stat)
    p = regularised_incomplete_beta(df / 2.0, 0.5, x)
    return max(0.0, min(1.0, p))


def paired_t_test(candidate: list[float], baseline: list[float]) -> tuple[float, float | None, int]:
    """Return (mean_diff, p_value, n). p_value is None when n < 2."""
    if len(candidate) != len(baseline):
        raise PairingError("candidate and baseline lengths disagree")
    n = len(candidate)
    if n < 2:
        if n == 0:
            return 0.0, None, 0
        return float(candidate[0] - baseline[0]), None, n
    diffs = np.array(candidate) - np.array(baseline)
    mean_diff = float(diffs.mean())
    var_diff = float(diffs.var(ddof=1))
    if var_diff == 0.0:
        return mean_diff, (0.0 if mean_diff != 0.0 else 1.0), n
    t_stat = mean_diff / math.sqrt(var_diff / n)
    p = two_sided_t_p_value(t_stat, n - 1)
    return mean_diff, p, n


def _pair_by_seed(
    candidates: list[ExperimentResultLike],
    baselines: list[ExperimentResultLike],

View on GitHub (pinned to 39ea8a1c6d)