"""Compare the CLT estimate with the exact upper tail for exponential waits."""
from math import erfc, exp, factorial, sqrt

sample_size = 100
mean_wait = 2.0
alert_threshold = 2.4

# For an exponential wait the standard deviation equals the mean.
standard_error = mean_wait / sqrt(sample_size)
z = (alert_threshold - mean_wait) / standard_error
normal_tail = erfc(z / sqrt(2)) / 2

# In a Poisson process, the first 100 waits sum to at least 240 seconds
# when fewer than 100 events arrive by that time (ties have probability zero).
expected_events = sample_size * alert_threshold / mean_wait
exact_tail = exp(-expected_events) * sum(
    expected_events ** k / factorial(k) for k in range(sample_size)
)

print(f"Standard error: {standard_error:.2f} s")
print(f"Standardized distance: {z:.2f}")
print(f"Normal approximation: {normal_tail:.2%}")
print(f"Exact exponential-model tail: {exact_tail:.2%}")
