#!/usr/bin/env python3
"""Percentile-bootstrap 95% interval for the headline ratio of Experiment 1:
median same-tenant wait before the fix / median after it (30 runs each).
10,000 resamples of each sample with replacement, random seed 1. Standard library only.
Also prints the worst-case bound (slowest after-fix run vs fastest before-fix run)."""
import json, os, random, statistics as st
RES = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "results")
def same(v):
    return [json.loads(l)["same_tenant_ms"] for l in open(os.path.join(RES, f"exp1_lockscope_{v}.jsonl")) if l.strip()]
b, a = same("before"), same("after")
random.seed(1)
rs = sorted(st.median(random.choices(b, k=len(b))) / st.median(random.choices(a, k=len(a))) for _ in range(10000))
print(json.dumps({"ratio_of_medians": st.median(b) / st.median(a), "boot95_lo": rs[249], "boot95_hi": rs[9749],
                  "worst_case_min_before_over_max_after": min(b) / max(a), "n_before": len(b), "n_after": len(a)}, indent=2))
