# /// script
# requires-python = ">=3.10"
# ///
# Copyright (c) 2026 Aayush Sharma · SPDX-License-Identifier: MIT
# https://thedataengineering.com/lab-code/LICENSE.txt
"""Base-rate simulation of alerting.

One million checks with an incident probability of 10^-3, and a detector with
recall 1.0 and false-positive rates of 5%, 1%, 0.1% and 0.01%: how many pages,
and what share of them are real (precision)?
"""
import argparse, json, random

p = argparse.ArgumentParser(description=__doc__.splitlines()[0])
p.add_argument("--checks", type=int, default=1_000_000, help="Checks to simulate.")
p.add_argument("--p-incident", type=float, default=1e-3, help="Incident probability per check.")
p.add_argument("--seed", type=int, default=0, help="Random seed.")
p.add_argument("--json", help="Also write the results to this JSON file.")
args = p.parse_args()
random.seed(args.seed)

rows = []
for fpr in (0.05, 0.01, 0.001, 0.0001):
    tp = fp = 0
    for _ in range(args.checks):
        if random.random() < args.p_incident: tp += 1
        elif random.random() < fpr: fp += 1
    rows.append({"fpr_pct": fpr * 100, "pages": tp + fp, "real": tp, "precision_pct": tp / (tp + fp) * 100,
                 "formula_pct": args.p_incident / (args.p_incident + (1 - args.p_incident) * fpr) * 100})
for r in rows: print(f"fpr={r['fpr_pct']:<6g}% pages={r['pages']:>6}  real={r['real']:>5}  precision={r['precision_pct']:.1f}%  (formula {r['formula_pct']:.1f}%)")

if args.json:
    with open(args.json, "w") as f:
        json.dump({"tables": {"detectors": rows}}, f)
