Files
hyperfine/scripts/welch_ttest.py
T

53 lines
1.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2024-08-28 16:52:02 +02:00
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "scipy",
# ]
# ///
2019-10-07 14:57:42 +09:00
2019-10-07 18:44:55 +02:00
"""This script performs Welch's t-test on a JSON export file with two
benchmark results to test for a difference in their population means."""
2019-10-07 18:44:55 +02:00
2019-10-07 14:57:42 +09:00
import argparse
import math
2024-08-28 17:01:09 +02:00
2019-10-07 14:57:42 +09:00
from scipy import stats
from plot_utils import METRICS, add_metric_argument, load_results
2019-10-07 18:44:55 +02:00
parser = argparse.ArgumentParser(description=__doc__)
2019-10-07 18:36:00 +02:00
parser.add_argument("file", help="JSON file with two benchmark results")
add_metric_argument(parser)
2019-10-07 14:57:42 +09:00
args = parser.parse_args()
metric, [results] = load_results(parser, [args.file], args.metric)
2019-10-07 14:57:42 +09:00
2019-10-07 18:36:00 +02:00
if len(results) != 2:
parser.error("the input file must contain exactly two benchmarks")
2019-10-07 18:36:00 +02:00
2026-10-05 18:11:10 +02:00
a, b = (x.get("name", x["command"]) for x in results[:2])
X, Y = ([m[metric]["value"] for m in x["measurements"]] for x in results[:2])
if min(len(X), len(Y)) < 2:
parser.error("Welch's t-test requires at least two measurements per benchmark")
2019-10-07 14:57:42 +09:00
print(f"Metric: {METRICS[metric].label}")
2024-08-28 16:41:58 +02:00
print(f"Command 1: {a}")
print(f"Command 2: {b}\n")
2019-10-07 14:57:42 +09:00
t, p = stats.ttest_ind(X, Y, equal_var=False)
if not math.isfinite(p):
parser.error("Welch's t-test is undefined for these measurements")
2019-10-07 14:57:42 +09:00
th = 0.05
dispose = p < th
2024-08-28 16:41:58 +02:00
print(f"t = {t:.3}, p = {p:.3}")
2019-10-07 18:58:07 +02:00
print()
2019-10-07 14:57:42 +09:00
if dispose:
print(
f"A statistically significant difference in mean value was detected (p < {th})."
)
2019-10-07 14:57:42 +09:00
else:
print(
f"No statistically significant difference in mean value was detected (p >= {th})."
)