"""Separate recall and false-positive regressions by physical validation group. Diagnostic/future protocol helper. Does not rewrite an experiment's frozen choice. """ import math def component_regressions(rows, reference, candidates): if not rows or any(r["phase"] != "development" for r in rows): raise ValueError("Development rows only") selected = {reference, *candidates} by_arm = {a: {} for a in selected} for row in rows: if row["arm"] not in selected: continue group = row["held_out"] if group in row["fitted_groups"] or group in by_arm[row["arm"]]: raise ValueError("Leaked or duplicate physical group") if not row["completed"]: raise ValueError("Incomplete fit") by_arm[row["arm"]][group] = row["metrics"] groups = set(by_arm[reference]) if not groups or any(set(by_arm[a]) != groups for a in candidates): raise ValueError("Unmatched physical groups") regressions = [] for arm in candidates: for group in sorted(groups): for metric, direction in [("recall", 1), ("fpr", -1)]: before = by_arm[reference][group][metric] after = by_arm[arm][group][metric] if before is None and after is None: continue if ( before is None or after is None or not all(math.isfinite(x) and 0 <= x <= 1 for x in [before, after]) ): raise ValueError("Inconsistent metric support") if direction * (after - before) < -1e-12: regressions.append( dict( arm=arm, group=group, metric=metric, before=before, after=after, delta=after - before, ) ) return dict( reference=reference, candidates=list(candidates), regressions=regressions, all_components_non_regressing=not regressions, )