Skip to content
Myr

welch_ttest.py

This file

Path

scripts/welch_ttest.py

48 lines · 1.1 KiB · Python

Last changed by

Correct the interpretation of Welch's t-test results (#943)David Peterbefore adoptionf6d5931

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 mean runtimes."""1112import argparse13import json14import sys1516from scipy import stats1718parser = argparse.ArgumentParser(description=__doc__)19parser.add_argument("file", help="JSON file with two benchmark results")20args = parser.parse_args()2122with open(args.file) as f:23    results = json.load(f)["results"]2425if len(results) != 2:26    print("The input file has to contain exactly two benchmarks")27    sys.exit(1)2829a, b = (x["command"] for x in results[:2])30X, Y = (x["times"] for x in results[:2])3132print(f"Command 1: {a}")33print(f"Command 2: {b}\n")3435t, p = stats.ttest_ind(X, Y, equal_var=False)36th = 0.0537dispose = p < th38print(f"t = {t:.3}, p = {p:.3}")39print()4041if dispose:42    print(43        f"A statistically significant difference in mean runtime was detected (p < {th})."44    )45else:46    print(47        f"No statistically significant difference in mean runtime was detected (p >= {th})."48    )