StatWhy: Formal Verification Tool for Statistical Hypothesis Testing Programs

Yusuke Kawamoto, Kentaro Kobayashi, Kohei Suenaga

TL;DR

StatWhy is a formal verification tool designed to ensure that statistical hypothesis tests are used correctly within software. By leveraging Belief Hoare Logic and the Why3 platform, it forces programmers to explicitly annotate their statistical assumptions and automatically detects common pitfalls like p-hacking and violated distribution requirements.

What Problem Does This Paper Address?

Statistical methods are the backbone of modern science, yet they are notoriously easy to misuse. Issues like p-hacking (manipulating analyses to find a significant result) and ignoring the underlying assumptions of a test (e.g., assuming a population is normally distributed when it isn't) have fueled a global "reproducibility crisis."

While natural language guidelines exist to prevent these errors, there has been a lack of automated tools to check if a program implementing these tests is actually following the rules. Most verification tools focus on memory safety or basic functional correctness; they don't understand the "epistemic" nature of statistics—where a result isn't a mathematical certainty but a "belief" based on data and assumptions.

Methods

The authors introduce StatWhy, which treats statistical procedures as formal programs that can be verified. Its architecture relies on several key components:

  • Belief Hoare Logic (BHL): Unlike standard Hoare logic (which uses preconditions and postconditions to prove absolute truths), BHL is designed to reason about statistical beliefs. It tracks what a researcher "knows" after performing a test with a specific p-value.
  • Why3 & Cameleer: StatWhy is built as an extension of Cameleer, a tool that translates OCaml code into WhyML (the intermediate language for the Why3 verification platform).
  • Formal Annotations: Programmers use the Gospel specification language to annotate their OCaml code. They must define requires clauses (e.g., "the input data is sampled from a normal distribution") and ensures clauses (e.g., "the resulting p-value represents a specific hypothesis").
  • Test History Tracking: The tool maintains a record of all hypothesis tests executed. This is crucial for detecting p-hacking, as it allows the tool to verify if multiple comparisons were properly accounted for (e.g., using a Bonferroni correction).

Datasets

As a formal verification paper, the authors do not use a traditional training dataset. Instead, they evaluate StatWhy using a suite of synthetic OCaml programs that implement common statistical scenarios:

  • One-sample and two-sample t-tests.
  • Multiple comparison procedures (Tukey’s HSD, Dunnett’s, Williams’, Steel-Dwass, and Steel’s tests).
  • "Incorrect" programs designed to fail verification, such as those performing p-hacking or omitting normality assumptions.

Key Insights and Ideas

  • Forcing the "Aha!" Moment: A core philosophy of StatWhy is that some things cannot be proven mathematically (like whether an unknown population is truly "normal"). By requiring these as formal annotations, the tool forces the programmer to consciously acknowledge and externally justify their assumptions.
  • Procedural Integrity: The tool doesn't just check the final p-value; it checks the procedure used to get there. If you run two tests but only report the "best" one, StatWhy’s internal "test history" tracker will flag the discrepancy.
  • Bridging Logic and Stats: The paper successfully maps abstract statistical concepts (like the null hypothesis) into a logic-based framework that off-the-shelf SMT solvers (like Z3 and CVC5) can understand and verify.

Key Results

  • Error Detection: StatWhy successfully identified missing requirements in t-test implementations and flagged programs attempting p-value hacking.
  • Multiple Comparisons: The tool correctly verified programs using the Bonferroni correction, ensuring that the combined p-value for disjunctive hypotheses was calculated accurately.
  • Performance: The verification process is efficient enough for real-world use. For practical numbers of hypotheses (up to 10), verification times remained under 10 seconds. Even complex multiple-comparison tests with 7 groups (21 combinations) were verified in roughly 16 seconds on a modern MacBook Pro.

Limitations and Future Work

  • External Verification of Assumptions: StatWhy can check if you stated an assumption, but it cannot prove that the assumption is true in the real world (e.g., it can't prove your physical data source is actually Gaussian). The user must still perform empirical checks.
  • Language Support: The current implementation is limited to OCaml. The authors express interest in expanding to more common data science languages, specifically Python.
  • Scope of Statistics: Currently, the tool focuses heavily on hypothesis testing. Future versions aim to include power analysis and interval estimation, providing a more comprehensive suite for statistical verification.
  • Implementation Trust: The tool assumes the underlying library implementation of the statistical test (e.g., the actual math of the t-test) is correct. Verifying the low-level math of these libraries is noted as a separate, future goal.

Summary updated

BibTeX
@inproceedings{kawamoto2025statwhy,
  author = {Yusuke Kawamoto and Kentaro Kobayashi and Kohei Suenaga},
  title = {{StatWhy: Formal Verification Tool for Statistical Hypothesis Testing Programs}},
  booktitle = {CAV},
  year = {2025},
  doi = {10.1007/978-3-031-98679-6\_10},
}
Figure