"""Exact waiting-time worksheet for complete, synthetic inspection intervals. Python standard library only. Inputs are positive integer day lengths between successful verifications of the SAME block and failure mode. No censored edge intervals, scan-job durations, device measurements or stochastic simulation. """ import argparse from datetime import datetime, timezone from fractions import Fraction import hashlib import json from pathlib import Path def intervals_checked(intervals): if not isinstance(intervals, (list, tuple)) or not intervals: raise ValueError("provide a nonempty list or tuple of integer day lengths") if any(type(value) is not int or value <= 0 for value in intervals): raise ValueError("intervals must be positive integer day lengths") return tuple(intervals) def mean_wait(intervals): gaps = intervals_checked(intervals) return Fraction(sum(gap * gap for gap in gaps), 2 * sum(gaps)) def tail_probability(intervals, threshold): gaps = intervals_checked(intervals) if type(threshold) not in (int, Fraction) or threshold < 0: raise ValueError("threshold must be a nonnegative integer or Fraction of a day") return sum((max(Fraction(gap) - threshold, 0) for gap in gaps), Fraction(0)) / sum(gaps) def encode(value): value = Fraction(value) return {"numerator": value.numerator, "denominator": value.denominator, "decimal": float(value)} def summarize(name, intervals): gaps = intervals_checked(intervals) count = len(gaps) total = sum(gaps) mean = Fraction(total, count) # Population variance of this finite list, not an unbiased sample estimator. variance = sum(((gap - mean) ** 2 for gap in gaps), Fraction(0)) / count return { "name": name, "intervals_days": list(gaps), "interval_count": count, "window_days": total, "mean_interval_days": encode(mean), "population_variance_days_squared": encode(variance), "mean_wait_days": encode(mean_wait(gaps)), "max_wait_supremum_days": max(gaps), "probability_wait_exceeds_14_days": encode(tail_probability(gaps, 14)), } def result(): return { "schema_version": 1, "evidence_type": "exact-arithmetic-on-synthetic-schedules", "unit_of_analysis": "one fixed block, one complete 56-day inspection window", "assumptions": [ "A hypothetical onset time is uniform over the entire window.", "Each endpoint verifies the same block and detects the chosen persistent fault.", "No intervening reads, overwrites, repairs or health-triggered scheduling.", "All intervals are complete; no unobserved interval at either edge.", ], "limitations": [ "Not measured MTTD, a device benchmark, a trace replay or a failure-rate estimate.", "No inference about repair, data loss or optimal operational scrub frequency.", "No confidence intervals: these are exact constructed inputs, not fleet samples.", ], "formula_mean_wait": "sum(gap_days**2) / (2 * sum(gap_days))", "formula_tail": "sum(max(gap_days - threshold_days, 0)) / sum(gap_days)", "generator_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "sources": [{ "title": "Gallager, Discrete Stochastic Processes, chapter 4, example 4.4.1 and equation 4.15", "url": "https://ocw.mit.edu/courses/6-262-discrete-stochastic-processes-spring-2011/931ffa0940899c27f34b71ad64fd2bb0_MIT6_262S11_chap04.pdf", "accessed": "2026-10-17", "role": "renewal-theory background; finite examples and code are Hesela's", }], "scenarios": [ summarize("Regular", [14, 14, 14, 14]), summarize("Alternating", [7, 21, 7, 21]), summarize("One long gap", [7, 7, 7, 35]), ], } def main(): parser = argparse.ArgumentParser(description=__doc__) action = parser.add_mutually_exclusive_group() action.add_argument("--write", action="store_true", help="generate results.json") action.add_argument("--check", action="store_true", help="compare all reproducible fields") args = parser.parse_args() expected = result() path = Path(__file__).with_name("results.json") if args.check: stored = json.loads(path.read_text()) stamp = stored.pop("generated_at") if datetime.fromisoformat(stamp).utcoffset() is None: raise SystemExit("generated_at must be timezone aware") if stored != expected: raise SystemExit("results.json differs from the generator") print("Exact worksheet and generator hash verified") else: expected["generated_at"] = datetime.now(timezone.utc).isoformat() serialized = json.dumps(expected, indent=2) + "\n" if args.write: path.write_text(serialized) else: print(serialized, end="") if __name__ == "__main__": main()