Skip to content
Myr

welch_ttest.py

This file

Path

scripts/welch_ttest.py

53 lines · 1.5 KiB · Python

Last changed by

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

History of this file

Take 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_results1819parser = argparse.ArgumentParser(description=__doc__)20parser.add_argument("file", help="JSON file with two benchmark results")21add_metric_argument(parser)22args = parser.parse_args()2324metric, [results] = load_results(parser, [args.file], args.metric)2526if len(results) != 2:27    parser.error("the input file must contain exactly two benchmarks")2829a, b = (x.get("name", x["command"]) for x in results[:2])30X, Y = ([m[metric]["value"] for m in x["measurements"]] for x in results[:2])31if min(len(X), len(Y)) < 2:32    parser.error("Welch's t-test requires at least two measurements per benchmark")3334print(f"Metric: {METRICS[metric].label}")35print(f"Command 1: {a}")36print(f"Command 2: {b}\n")3738t, p = stats.ttest_ind(X, Y, equal_var=False)39if not math.isfinite(p):40    parser.error("Welch's t-test is undefined for these measurements")41th = 0.0542dispose = p < th43print(f"t = {t:.3}, p = {p:.3}")44print()4546if dispose:47    print(48        f"A statistically significant difference in mean value was detected (p < {th})."49    )50else:51    print(52        f"No statistically significant difference in mean value was detected (p >= {th})."53    )