"""Branch odds for claim A, computed AFTER the data (the register says so).

    python branches.py > branches.txt

Under an assumed true Friday share, at the sample size the fetch actually
returned, how often does the sealed rule come back survived / killed /
inconclusive? The assumed truths are named values, not resamples around the
observed one — a table bootstrapped around the result makes every far bar look
dead, which is the mistake `result-gold-hours-refute-clause-dead` records.

What it is for: the rule came back inconclusive, and the reader is owed the
size of the band that produced that. It is not evidence about the world.
"""

import random

import rule

N = 52534  # Mon..Fri releases actually kept
TRIALS = 4000


def run(p, n=N, trials=TRIALS):
    rng = random.Random(20260916)
    out = {"survived": 0, "killed": 0, "inconclusive": 0}
    for _ in range(trials):
        # normal approximation to the binomial at n = 52k
        k = round(rng.gauss(p * n, (p * (1 - p) * n) ** 0.5))
        k = max(0, min(n, k))
        lo, hi = rule.wilson(k, n)
        out[rule.verdict_a(lo, hi)] += 1
    return {k: v / trials for k, v in out.items()}


if __name__ == "__main__":
    print(f"claim A, n = {N:,}, bars survived hi < {rule.A_SURVIVE_HI}, "
          f"killed lo > {rule.A_KILL_LO}\n")
    print(f"{'true share':>12}  {'survived':>9} {'killed':>9} {'inconcl.':>9}")
    for p in (0.160, 0.170, 0.176, 0.180, 0.185, 0.190, 0.195, 0.200, 0.210):
        r = run(p)
        print(f"{p:>12.3f}  {r['survived']:>9.3f} {r['killed']:>9.3f} "
              f"{r['inconclusive']:>9.3f}")
    print("\nthe inconclusive band: every true share from about 0.177 to 0.193")
    print("returns inconclusive more often than not, at this sample size.")
