"""Exact conditional bootstrap distributions for a synthetic four-drive example.""" import argparse from collections import Counter from fractions import Fraction from hashlib import sha256 from itertools import product import json from pathlib import Path GROUPS = [[0, 0], [1, 1]] MAX_DRAWS = 100000 def distribution(groups, unit): if unit not in ("device", "rack"): raise ValueError("unit must be device or rack") if not groups or any(not group for group in groups): raise ValueError("nonempty groups required") if any(type(x) is not int or x not in (0, 1) for g in groups for x in g): raise ValueError("outcomes must be integer zero or one") units = [list(g) for g in groups] if unit == "rack" else [[x] for g in groups for x in g] n = len(units) if n ** n > MAX_DRAWS: raise ValueError("exact enumeration limit exceeded") counts = Counter() sizes = Counter() for indexes in product(range(n), repeat=n): sample = [x for i in indexes for x in units[i]] counts[Fraction(sum(sample), len(sample))] += 1 sizes[len(sample)] += 1 total = n ** n mean = sum(value * count for value, count in counts.items()) / total # These are ALL equiprobable draws, not a Monte Carlo sample of replicates. variance = sum((value - mean) ** 2 * count for value, count in counts.items()) / total return { "unit": unit, "draws": total, "mean": str(mean), "variance": str(variance), "standard_deviation": float(variance) ** 0.5, "distribution": [ {"proportion": str(value), "count": count, "probability": str(Fraction(count, total))} for value, count in sorted(counts.items()) ], "sample_sizes": [{"devices": size, "count": count} for size, count in sorted(sizes.items())], } def result(): unequal = [[0, 0], [1]] return { "schema_version": 1, "kind": "synthetic exact enumeration, not empirical fleet inference", "model_sha256": sha256(Path(__file__).read_bytes()).hexdigest(), "groups": GROUPS, "observation": "One binary failure-by-horizon outcome per drive, complete common follow-up, no censoring.", "statistic": "Failed drives divided by all selected drives; dimensionless, not AFR.", "rows": [distribution(GROUPS, unit) for unit in ("device", "rack")], "unequal_example": { "groups": unequal, "observed_proportion": str(Fraction(sum(map(sum, unequal)), sum(map(len, unequal)))), "mean_rack_proportion": str(sum(Fraction(sum(g), len(g)) for g in unequal) / len(unequal)), "bootstrap": distribution(unequal, "rack"), "interpretation": "A pooled ratio's bootstrap mean need not equal the observed ratio or the equally weighted rack mean.", }, "limits": [ "Conditional bootstrap spread is not the known sampling variance of a real fleet.", "Two clusters do not justify a reliable empirical confidence interval.", "The factor of two is specific to this equal-size perfectly concordant example.", "More resamples do not add independent racks or repair selection bias.", ], } if __name__ == "__main__": parser = argparse.ArgumentParser() mode = parser.add_mutually_exclusive_group() mode.add_argument("--write", action="store_true") mode.add_argument("--check", action="store_true") args = parser.parse_args() path = Path(__file__).with_name("results.json") encoded = json.dumps(result(), indent=2) + "\n" if args.write: path.write_text(encoded) elif args.check: if path.read_text() != encoded: raise SystemExit("results.json differs from exact enumeration") print("Exact bootstrap result and model hash verified") else: print(encoded, end="")