A fair coin gives 50 heads on average with a count standard deviation of 5, so 60 heads sits exactly 2 standard deviations high (z = 2). The normal approximation puts that just under the 5% line, but the exact binomial tail is a little heavier, so the exact test does not reject at alpha = 0.05.
A coin was flipped n times and landed heads heads times. Decide whether the data are consistent with a fair coin (H0:p=0.5) using two tests side by side: the exact binomial test and the large-sample z-test.
Implement coin_fairness_test(heads, n, alpha=0.05) and return a dict with these keys:
exact_p: two-sided exact binomial p-value. With X∼Binomial(n,0.5),
pexact=min(1,2min(P(X≤heads),P(X≥heads)))
z: the one-sample proportion z-statistic z=0.25/np^−0.5 with p^=heads/n
z_p: its two-sided p-value 2(1−Φ(∣z∣)), where Φ is the standard normal CDF
reject: True when exact_p < alpha, else False
Notes
1≤n≤5000 and 0≤heads≤n. Compute the binomial tail exactly; it must not underflow to a wrong value for n in the thousands.