"""Synthetic arithmetic examples, not device measurements or population estimates.""" import argparse import json from pathlib import Path from statistics import median, stdev def summarize(values): if not isinstance(values, list) or len(values) < 2: raise ValueError("At least two finite nonnegative latencies are required") if any(type(x) not in (int, float) or not (x == 0 or 1e-100 <= x <= 1e100) for x in values): raise ValueError("Latencies must be zero or within [1e-100, 1e100]; this is a bounded example helper") center = median(values) mean = sum(values) / len(values) sd = stdev(values) return { "count": len(values), "mean": mean, "median": center, "raw_mad": median([abs(x - center) for x in values]), "sample_sd": sd, "cv": None if mean == 0 else sd / mean, "maximum": max(values), } def results(): rows = [ ("constant", [1, 1, 1, 1, 1], "ms"), ("long-tail", [1, 1, 1, 1, 100], "ms"), ("base", [1, 2, 3], "ms"), ("added-delay", [101, 102, 103], "ms"), ("unit-conversion", [1000, 2000, 3000], "us"), ] return { "kind": "synthetic-arithmetic", "conventions": "Raw unscaled MAD; even medians average two central values; sample SD uses n-1; CV is a ratio, not percent.", "limits": "Fixed completed observations only; no sampling inference, timeout correction or device benchmark.", "rows": [dict(id=name, values=values, unit=unit, **summarize(values)) for name, values, unit in rows], } if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--check", action="store_true") args = parser.parse_args() output = json.dumps(results(), indent=2, allow_nan=False) + "\n" target = Path(__file__).with_name("results.json") if args.check: if target.read_text() != output: raise SystemExit("Generated results differ; run model.py") print("Synthetic arithmetic matches results.json") else: target.write_text(output)