"""Original hypothetical Normal example: known sigma; unknown population mean.
Run with python3. No external packages, real dataset, or trade submission.
"""
from math import sqrt
from statistics import NormalDist

n, sigma, observed_mean = 25, 10.0, 50.0
z = NormalDist().inv_cdf(0.975)
data_precision = n / sigma**2
ci_half = z / sqrt(data_precision)
print("Known-sigma confidence interval:", observed_mean-ci_half, observed_mean+ci_half)
for prior_sd in (2.0, 10.0):
    prior_mean = 40.0
    prior_precision = 1/prior_sd**2
    variance = 1/(prior_precision+data_precision)
    center = variance*(prior_mean*prior_precision+observed_mean*data_precision)
    sd = sqrt(variance)
    lo, hi = center-z*sd, center+z*sd
    posterior = NormalDist(center, sd)
    print("Prior SD:", prior_sd, "posterior mean/variance:", center, variance)
    print("Central95% credible interval:", lo, hi)
    print("Posterior mass:", posterior.cdf(hi)-posterior.cdf(lo))

# Independent repeated-sampling question: fix true mu=50, use Prior A.
# Posterior center=(40+sample_mean)/2, half-width z*sqrt(2).
half = z*sqrt(2)
mean_lo, mean_hi = 2*(50-half)-40, 2*(50+half)-40
sampling_mean = NormalDist(50, sigma/sqrt(n))
coverage = sampling_mean.cdf(mean_hi)-sampling_mean.cdf(mean_lo)
print("Sample means whose Prior-A interval covers fixed mu50:", mean_lo, mean_hi)
print("Fixed-mu frequentist coverage of Prior-A credible interval:", coverage)
print("Posterior mass is not the same probability question as fixed-mu coverage.")
