stats.py 523 B

1234567891011121314151617
  1. """Statistical utilities for drill result analysis."""
  2. from __future__ import annotations
  3. import math
  4. def wilson_ci(passed: int, total: int, z: float = 1.96) -> tuple[float, float]:
  5. if total == 0:
  6. return (0.0, 0.0)
  7. if passed > total:
  8. passed = total
  9. p = passed / total
  10. denom = 1 + z**2 / total
  11. center = (p + z**2 / (2 * total)) / denom
  12. margin = (z / denom) * math.sqrt(p * (1 - p) / total + z**2 / (4 * total**2))
  13. return (max(0.0, center - margin), min(1.0, center + margin))