"""Exact arithmetic for a bounded synthetic unequal-exposure example. Standard library only. Not a general inference package or empirical fleet study. """ from fractions import Fraction from hashlib import sha256 from math import comb from pathlib import Path import argparse import json def conditional_probability(exposure_a, exposure_b, null_ratio=Fraction(1)): for value in (exposure_a, exposure_b, null_ratio): if isinstance(value, bool) or not isinstance(value, (int, Fraction)) or value <= 0: raise ValueError("Use positive integer or rational exposures and null ratio") a = Fraction(exposure_a) * null_ratio return a / (a + Fraction(exposure_b)) def probability_ordered_p(count_a, total, probability): if (type(count_a) is not int or type(total) is not int or not 0 <= count_a <= total <= 100): raise ValueError("This teaching helper requires integer 0 <= count <= total <= 100") if isinstance(probability, bool) or not isinstance(probability, (int, Fraction)): raise ValueError("Use an exact integer or Fraction probability") probability = Fraction(probability) if not 0 <= probability <= 1: raise ValueError("Probability must be between zero and one") masses = [comb(total, k) * probability**k * (1 - probability)**(total - k) for k in range(total + 1)] observed = masses[count_a] # Exact rational ordering includes ties without a floating-point tolerance. return sum((mass for mass in masses if mass <= observed), Fraction(0)) def interval_convention_example(): probability = conditional_probability(3, 7) lower_tail = (1 - probability)**10 ordered = probability_ordered_p(0, 10, probability) central = min(Fraction(1), 2 * lower_tail) return { "events_a": 0, "events_b": 10, "drive_years_a": 3, "drive_years_b": 7, "null_rate_ratio": "1", "conditional_probability": str(probability), "probability_ordered_p_fraction": str(ordered), "probability_ordered_p_value": float(ordered), "central_two_tail_p_fraction": str(central), "central_two_tail_p_value": float(central), "lower_tail_fraction": str(lower_tail), "ordered_rejects_at_5_percent": ordered < Fraction(1, 20), "central_95_interval_contains_null": lower_tail >= Fraction(1, 40), "scope": "Exact tail-membership check; no interval endpoints or native R execution.", } def example(): p0 = conditional_probability(900, 100) correct = probability_ordered_p(9, 10, p0) wrong = probability_ordered_p(9, 10, Fraction(1, 2)) groups = [ {"group": "A", "events": 9, "drive_years": 900}, {"group": "B", "events": 1, "drive_years": 100}, ] for group in groups: group["events_per_100_drive_years"] = int( Fraction(group["events"] * 100, group["drive_years"])) return { "schema_version": 1, "interval_convention_example": interval_convention_example(), "evidence_type": "synthetic exact arithmetic", "created": "2026-12-01", "groups": groups, "estimated_rate_ratio": str(Fraction(9, 900) / Fraction(1, 100)), "null_rate_ratio": "1", "conditional_probability": str(p0), "correct_p_fraction": str(correct), "correct_p_value": float(correct), "wrong_equal_exposure_p_fraction": str(wrong), "wrong_equal_exposure_p_value": float(wrong), "method": "Conditional independent Poisson counts; two-sided binomial probability ordering", "limits": "Fixed positive exposures, independent Poisson counts, prespecified comparison; no fleet fit, equivalence claim or calibration validation.", "generator_sha256": sha256(Path(__file__).read_bytes()).hexdigest(), } if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--check", action="store_true") args = parser.parse_args() target = Path(__file__).with_name("results.json") generated = json.dumps(example(), indent=2) + "\n" if args.check: if target.read_text() != generated: raise SystemExit("Stale unequal-exposure results") print("Exact unequal-exposure results match") else: target.write_text(generated)