"""Exact zero-failure selection probabilities, not empirical drive results.""" import argparse from decimal import Decimal, localcontext from fractions import Fraction import hashlib import json from pathlib import Path def validate(p, n, length): if not isinstance(p, Fraction) or not 0 <= p <= 1: raise ValueError("p must be a Fraction in [0, 1]") if type(n) is not int or n < 0: raise ValueError("n must be a nonnegative integer") if type(length) is not int or length < 1: raise ValueError("length must be a positive integer") def first_run(p, n, length): """Integer weights of live trailing-zero states; absorb each first hit.""" validate(p, n, length) a, b = p.numerator, p.denominator live = [1] + [0] * (min(length, n + 1) - 1) stopped = 0 denominator = 1 hits = [] for step in range(1, n + 1): next_live = [sum(live) * a] + [mass * (b - a) for mass in live[:-1]] hit = live[-1] * (b - a) if length <= n else 0 stopped = stopped * b + hit denominator *= b live = next_live if sum(live) + stopped != denominator: raise ArithmeticError("probability mass not conserved") hits.append(Fraction(hit, denominator)) return {"probability": Fraction(stopped, denominator), "first_hit": hits} def fixed_prefix(p, n, length): validate(p, n, length) return (1 - p) ** length if n >= length else Fraction(0) def disjoint_blocks(p, n, length): validate(p, n, length) return 1 - (1 - (1 - p) ** length) ** (n // length) def encode(value): with localcontext() as context: context.prec = 40 decimal = Decimal(value.numerator) / Decimal(value.denominator) return {"numerator": str(value.numerator), "denominator": str(value.denominator), "decimal": str(decimal)} def calculate(): p = Fraction(1, 100) n, length = 1000, 300 prefix = fixed_prefix(p, n, length) disjoint = disjoint_blocks(p, n, length) rolling = first_run(p, n, length)["probability"] policies = [ ("fixed_prefix", prefix, [[1, 300]], "Only the first 300 outcomes can qualify."), ("three_disjoint_blocks", disjoint, [[1, 300], [301, 600], [601, 900]], "Qualify if any prespecified block has zero failures; last 100 outcomes unused."), ("rolling_zero_window", rolling, None, "After every outcome from 300 to 1000, qualify if the last 300 have zero failures."), ] return { "schema_version": 1, "kind": "exact_synthetic_probability_calculation", "failure_probability": encode(p), "null_hypothesis": "Common endpoint failure probability p >= 0.01.", "false_conclusion": "Common endpoint failure probability p < 0.01.", "endpoint_outcome_budget": n, "required_consecutive_nonfailures": length, "nominal_single_window_alpha": encode(Fraction(1, 20)), "assumptions": [ "One binary first-failure outcome at a common completed device-age horizon per device.", "Independent identically distributed Bernoulli outcomes in a fixed outcome-independent order.", "No firmware, population, workload or endpoint-horizon change between windows.", "All historical outcomes remain in the audit record; only the selection rule changes.", "Stop at first qualifying prefix, block or rolling window, otherwise do not qualify.", ], "scope": [ "These are probabilities of an erroneous procedure-level conclusion, not drive AFR.", "No sampled fleet, physical test, Monte Carlo simulation or empirical confidence interval.", "Overlapping windows are dependent; no independent-window shortcut is used.", "A real corrective intervention changes the scientific question and needs a new design.", ], "rows": [ {"id": name, "policy": policy, "fixed_windows_inclusive": windows, "false_qualification_probability": encode(value), "display_percent": f"{100 * float(value):.6f}%"} for name, value, windows, policy in policies ], "generator_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), } def main(): parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group(required=True) group.add_argument("--write", type=Path) group.add_argument("--verify", type=Path) args = parser.parse_args() result = calculate() if args.verify: if json.loads(args.verify.read_text()) != result: raise SystemExit("FAIL: retained results differ from exact regeneration") print("PASS: all reset-window result fields replay exactly") else: args.write.write_text(json.dumps(result, indent=2) + "\n") for row in result["rows"]: print(row["id"], row["display_percent"]) if __name__ == "__main__": main()