"""Synthetic fixed-horizon Bernoulli examples, not device observations.""" from fractions import Fraction from math import expm1, isfinite, log, sqrt from pathlib import Path import json import sys def binary_jackknife(outcomes): if not isinstance(outcomes, (list, tuple)) or len(outcomes) < 2: raise ValueError("At least two binary outcomes are required") if any(type(x) is not int or x not in (0, 1) for x in outcomes): raise ValueError("Outcomes must be integer zero or one") n = len(outcomes) events = sum(outcomes) estimates = [Fraction(events - x, n - 1) for x in outcomes] center = sum(estimates) / n variance = Fraction(n - 1, n) * sum((x - center) ** 2 for x in estimates) return center, variance, estimates def zero_event_upper(n, alpha=0.05): if type(n) is not int or n < 1: raise ValueError("n must be a positive integer") if isinstance(alpha, bool) or not isinstance(alpha, (int, float)): raise ValueError("alpha must be a number") if not isfinite(alpha) or not 0 < alpha < 1: raise ValueError("alpha must be strictly between zero and one") return -expm1(log(alpha) / n) def example(): rows = [] for events in (0, 1, 10): outcomes = [1] * events + [0] * (100 - events) mean, variance, estimates = binary_jackknife(outcomes) rows.append({ "n": len(outcomes), "events": events, "fraction_exact": str(mean), "variance_exact": str(variance), "standard_error": sqrt(float(variance)), "leave_one_out_exact": [str(x) for x in estimates], }) return { "schema_version": 1, "kind": "synthetic-method-example", "assumptions": [ "Independent Bernoulli units with a common event probability", "One completely observed binary endpoint per unit", "Common prespecified horizon and fixed sample size", "No censoring, missing endpoints, optional stopping or fleet measurements", "Not an exposure-based AFR estimator", ], "rows": rows, "zero_event_one_sided_95_upper": zero_event_upper(100), "zero_event_probability_at_p_0_01": float(Fraction(99, 100) ** 100), "reproduction": "Python 3.10+ standard library; no randomness, network or hardware", } if __name__ == "__main__": path = Path(__file__).with_name("results.json") expected = json.dumps(example(), indent=2) + "\n" if sys.argv[1:] == ["--write"]: path.write_text(expected, encoding="utf-8") elif sys.argv[1:] == ["--check"]: if path.read_text(encoding="utf-8") != expected: raise SystemExit("Stale jackknife results; run model.py --write") print("Jackknife generated results match") else: raise SystemExit("Usage: model.py --write|--check")