"""Exact illustrative arithmetic, not a reproduction of ATC 2017.""" from datetime import datetime, timezone from fractions import Fraction as F from hashlib import sha256 import json from pathlib import Path import sys def probability(value): result = F(str(value)) if not 0 <= result <= 1: raise ValueError("Probability must be between zero and one") return result def operating_point(prevalence, recall, fpr, acceleration=2): p, r, f = map(probability, (prevalence, recall, fpr)) k = F(str(acceleration)) if k < 1: raise ValueError("Acceleration must be at least one") tp, fp = p * r, (1 - p) * f q = tp + fp return { "alert_fraction": q, "precision": tp / q if q else None, "true_positive_fraction": tp, "false_positive_fraction": fp, "scan_volume_ratio": 1 + (k - 1) * q, } def maximum_fpr(prevalence, recall, budget): p, r, q = map(probability, (prevalence, recall, budget)) if p == 1: raise ValueError("FPR has no negative-case denominator at prevalence one") if p * r > q: return None return min(F(1), (q - p * r) / (1 - p)) def result(): scenarios = [ ("1% prevalence; 2% FPR", F(1, 100), F(4, 5), F(1, 50)), ("1% prevalence; 10% FPR", F(1, 100), F(4, 5), F(1, 10)), ("0.1% prevalence; 2% FPR", F(1, 1000), F(4, 5), F(1, 50)), ("1% prevalence; 2% total budget", F(1, 100), F(4, 5), maximum_fpr(F(1, 100), F(4, 5), F(1, 50))), ] rows = [] for label, p, r, f in scenarios: values = operating_point(p, r, f) rows.append({ "label": label, "prevalence_percent": float(100 * p), "recall_percent": float(100 * r), "fpr_percent": float(100 * f), "alert_percent": float(100 * values["alert_fraction"]), "precision_percent": float(100 * values["precision"]), "extra_scan_percent": float(100 * (values["scan_volume_ratio"] - 1)), "true_positive_weeks_per_million": int(1000000 * values["true_positive_fraction"]), "false_positive_weeks_per_million": int(1000000 * values["false_positive_fraction"]), "exact": {key: str(value) if value is not None else None for key, value in values.items()}, }) return { "schemaVersion": 1, "kind": "illustrative-arithmetic", "sourceSha256": sha256(Path(__file__).read_bytes()).hexdigest(), "method": "Exact rational arithmetic; no fitting, random sampling or empirical confidence intervals.", "assumptions": [ "All prevalence and classifier rates in scenarios are hypothetical, not inferred from the paper.", "Equal-duration drive-weeks; one alert enables acceleration for that entire interval.", "Constant baseline scan rate per drive and acceleration factor two; scan-volume weighting is normalized per drive.", "No foreground latency, energy, repair, error-location or data-loss model is included.", "No independence assumption is needed for the marginal alert identity; burstiness and shared resource limits remain unmodeled.", ], "formula": "q = recall * prevalence + fpr * (1 - prevalence); volume ratio = 1 + (acceleration - 1) * q", "scenarios": rows, "mttd_ratio_conversions": [ {"baseline_over_adaptive": float(g), "time_reduction_percent": float(100 * (1 - 1 / g))} for g in (F(17, 10), F(18, 10)) ], "two_percent_of_52_weeks": float(F(2, 100) * 52), } if __name__ == "__main__": path = Path(__file__).with_name("results.json") payload = result() if sys.argv[1:] == ["--check"]: previous = json.loads(path.read_text()) previous.pop("generatedAt") if previous != payload: raise SystemExit("Generated result or source hash is stale") print("Exact arithmetic and source hash match") elif not sys.argv[1:]: payload["generatedAt"] = datetime.now(timezone.utc).isoformat() path.write_text(json.dumps(payload, indent=2) + "\n") print(path) else: raise SystemExit("Usage: python3 model.py [--check]")