welch_ttest.py
This file
Path
scripts/welch_ttest.py
59 lines · 1.6 KiB · Python
Last changed by
Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
History of this fileTake it
1#!/usr/bin/env python2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "scipy",6# ]7# ///89"""This script performs Welch's t-test on a JSON export file with two10benchmark results to test for a difference in their population means."""1112import argparse13import math1415from scipy import stats1617from plot_utils import METRICS, add_metric_argument, load_results181920def main():21 parser = argparse.ArgumentParser(description=__doc__)22 parser.add_argument("file", help="JSON file with two benchmark results")23 add_metric_argument(parser)24 args = parser.parse_args()2526 metric, [results] = load_results(parser, [args.file], args.metric)2728 if len(results) != 2:29 parser.error("the input file must contain exactly two benchmarks")3031 a, b = (x.get("name", x["command"]) for x in results[:2])32 X, Y = ([m[metric]["value"] for m in x["measurements"]] for x in results[:2])33 if min(len(X), len(Y)) < 2:34 parser.error("Welch's t-test requires at least two measurements per benchmark")3536 print(f"Metric: {METRICS[metric].label}")37 print(f"Command 1: {a}")38 print(f"Command 2: {b}\n")3940 t, p = stats.ttest_ind(X, Y, equal_var=False)41 if not math.isfinite(p):42 parser.error("Welch's t-test is undefined for these measurements")43 th = 0.0544 dispose = p < th45 print(f"t = {t:.3}, p = {p:.3}")46 print()4748 if dispose:49 print(50 f"A statistically significant difference in mean value was detected (p < {th})."51 )52 else:53 print(54 f"No statistically significant difference in mean value was detected (p >= {th})."55 )565758if __name__ == "__main__":59 main()