Download code/worked_headroom.py from ai4he/headroom-decision-procedure: direct link, hf CLI and curl.
- Browser
- Download file 7.37 kB
-
https://huggingface.co/datasets/ai4he/headroom-decision-procedure/resolve/main/code/worked_headroom.py
- Command line
-
hf download hf://datasets/ai4he/headroom-decision-procedure/code/worked_headroom.py
-
curl -L -o worked_headroom.py https://huggingface.co/datasets/ai4he/headroom-decision-procedure/resolve/main/code/worked_headroom.py
7.37 kB
| #!/usr/bin/env python3 | |
| """Deterministic synthetic checks for the headroom decision procedure. | |
| The records below are constructions, not observations. They make every paired | |
| cell auditable and exercise ordinary, near-ceiling, harmful, and no-headroom | |
| branches of the procedure described in the paper. | |
| """ | |
| from __future__ import annotations | |
| import csv | |
| import math | |
| import random | |
| import statistics | |
| from pathlib import Path | |
| SEED = 20260829 | |
| BOOTSTRAPS = 50_000 | |
| ALPHA = 0.05 | |
| MIN_BENEFIT = 0.02 | |
| HARM_TOLERANCE = 0.02 | |
| R_HEADROOM_FLOOR = 0.05 | |
| R_MAX_UNDEFINED = 0.05 | |
| # Counts are (both correct, protocol only correct, baseline only correct, | |
| # both wrong). The first case exactly instantiates the manuscript's arithmetic | |
| # example. The others are declared stress tests, not empirical cases. | |
| CASES = { | |
| "worked_92_to_95": (90, 5, 2, 3), | |
| "near_ceiling": (99, 1, 0, 0), | |
| "harmful_protocol": (81, 2, 17, 0), | |
| "no_headroom": (100, 0, 0, 0), | |
| } | |
| # Independent synthetic planning samples (baseline correct, n). These exercise | |
| # uncertainty-aware opportunity decisions before a protocol study is run. | |
| PLANNING_CASES = { | |
| "planning_92_of_100": (92, 100), | |
| "planning_99_of_100": (99, 100), | |
| "planning_100_of_100": (100, 100), | |
| } | |
| def percentile(values: list[float], probability: float) -> float: | |
| ordered = sorted(values) | |
| position = probability * (len(ordered) - 1) | |
| lower = math.floor(position) | |
| upper = math.ceil(position) | |
| if lower == upper: | |
| return ordered[lower] | |
| weight = position - lower | |
| return ordered[lower] * (1 - weight) + ordered[upper] * weight | |
| def exact_sign_p(protocol_only: int, baseline_only: int) -> float: | |
| discordant = protocol_only + baseline_only | |
| if discordant == 0: | |
| return 1.0 | |
| tail = sum(math.comb(discordant, k) for k in range(protocol_only, discordant + 1)) | |
| if protocol_only < discordant / 2: | |
| tail = sum(math.comb(discordant, k) for k in range(0, protocol_only + 1)) | |
| return min(1.0, 2 * tail / (2**discordant)) | |
| def material_verdict(delta_low: float, delta_high: float, headroom: float) -> tuple[str, str]: | |
| if headroom == 0: | |
| return "no-opportunity", "replace the outcome or use a non-ceiling battery" | |
| if MIN_BENEFIT > headroom: | |
| return "design-infeasible", "change the battery before collecting protocol data" | |
| if delta_low >= MIN_BENEFIT: | |
| return "benefit-supported", "protocol clears the minimum-benefit gate" | |
| if delta_high <= -HARM_TOLERANCE: | |
| return "harm-supported", "do not authorize the protocol on this decision rule" | |
| if delta_low >= -HARM_TOLERANCE and delta_high <= MIN_BENEFIT: | |
| return "practically-negligible", "do not adopt for accuracy benefit" | |
| return "unresolved", "withhold adoption and add independent items" | |
| def wilson_interval(successes: int, n: int, alpha: float = ALPHA) -> tuple[float, float]: | |
| z = statistics.NormalDist().inv_cdf(1 - alpha / 2) | |
| p = successes / n | |
| denominator = 1 + z * z / n | |
| center = (p + z * z / (2 * n)) / denominator | |
| half = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / denominator | |
| return max(0.0, center - half), min(1.0, center + half) | |
| def planning_opportunity(name: str, successes: int, n: int) -> dict[str, object]: | |
| a0_low, a0_high = wilson_interval(successes, n) | |
| headroom_low = 1 - a0_high | |
| headroom_high = 1 - a0_low | |
| if MIN_BENEFIT <= headroom_low: | |
| verdict = "opportunity-supported" | |
| action = "proceed to a protocol design sized for the declared benefit" | |
| elif MIN_BENEFIT > headroom_high: | |
| verdict = "design-infeasible" | |
| action = "change the outcome or battery before protocol collection" | |
| else: | |
| verdict = "opportunity-unresolved" | |
| action = "add independent planning units or change the battery" | |
| return { | |
| "case": name, | |
| "provenance": "independent synthetic planning sample", | |
| "baseline_correct": successes, | |
| "n_planning_items": n, | |
| "a0_hat": successes / n, | |
| "a0_wilson_95_low": a0_low, | |
| "a0_wilson_95_high": a0_high, | |
| "headroom_95_low": headroom_low, | |
| "headroom_95_high": headroom_high, | |
| "minimum_benefit": MIN_BENEFIT, | |
| "opportunity_verdict": verdict, | |
| "operational_action": action, | |
| } | |
| def analyze(name: str, counts: tuple[int, int, int, int]) -> dict[str, object]: | |
| both, protocol_only, baseline_only, neither = counts | |
| pairs = ([(1, 1)] * both + [(0, 1)] * protocol_only + | |
| [(1, 0)] * baseline_only + [(0, 0)] * neither) | |
| n = len(pairs) | |
| baseline = (both + baseline_only) / n | |
| protocol = (both + protocol_only) / n | |
| headroom = 1 - baseline | |
| delta = protocol - baseline | |
| ratio = delta / headroom if headroom > 0 else None | |
| rng = random.Random(f"{SEED}:{name}") | |
| delta_draws: list[float] = [] | |
| ratio_draws: list[float] = [] | |
| undefined = 0 | |
| for _ in range(BOOTSTRAPS): | |
| draw = [pairs[rng.randrange(n)] for _ in range(n)] | |
| a0 = sum(base for base, _ in draw) / n | |
| a1 = sum(prot for _, prot in draw) / n | |
| delta_draws.append(a1 - a0) | |
| if a0 == 1: | |
| undefined += 1 | |
| else: | |
| ratio_draws.append((a1 - a0) / (1 - a0)) | |
| delta_low = percentile(delta_draws, ALPHA / 2) | |
| delta_high = percentile(delta_draws, 1 - ALPHA / 2) | |
| undefined_rate = undefined / BOOTSTRAPS | |
| ratio_stable = (headroom >= R_HEADROOM_FLOOR and | |
| undefined_rate <= R_MAX_UNDEFINED and ratio is not None) | |
| verdict, action = material_verdict(delta_low, delta_high, headroom) | |
| return { | |
| "case": name, | |
| "provenance": "synthetic construction", | |
| "n_items": n, | |
| "both_correct": both, | |
| "protocol_only": protocol_only, | |
| "baseline_only": baseline_only, | |
| "both_wrong": neither, | |
| "a0": baseline, | |
| "a1": protocol, | |
| "headroom": headroom, | |
| "delta": delta, | |
| "delta_ci_low": delta_low, | |
| "delta_ci_high": delta_high, | |
| "remaining_error_reduction": ratio if ratio_stable else "suppressed", | |
| "r_ci_low": percentile(ratio_draws, ALPHA / 2) if ratio_stable else "", | |
| "r_ci_high": percentile(ratio_draws, 1 - ALPHA / 2) if ratio_stable else "", | |
| "r_undefined_bootstrap_fraction": undefined_rate, | |
| "exact_paired_sign_p": exact_sign_p(protocol_only, baseline_only), | |
| "verdict": verdict, | |
| "operational_action": action, | |
| } | |
| def main() -> None: | |
| rows = [analyze(name, counts) for name, counts in CASES.items()] | |
| output = Path(__file__).with_name("worked_headroom_results.csv") | |
| with output.open("w", newline="", encoding="utf-8") as handle: | |
| writer = csv.DictWriter(handle, fieldnames=list(rows[0])) | |
| writer.writeheader() | |
| writer.writerows(rows) | |
| for row in rows: | |
| print(row) | |
| planning_rows = [ | |
| planning_opportunity(name, successes, n) | |
| for name, (successes, n) in PLANNING_CASES.items() | |
| ] | |
| planning_output = Path(__file__).with_name("planning_headroom_results.csv") | |
| with planning_output.open("w", newline="", encoding="utf-8") as handle: | |
| writer = csv.DictWriter(handle, fieldnames=list(planning_rows[0])) | |
| writer.writeheader() | |
| writer.writerows(planning_rows) | |
| for row in planning_rows: | |
| print(row) | |
| if __name__ == "__main__": | |
| main() | |