Skip to content
Myr

welch_ttest.py

This file

Path

scripts/welch_ttest.py

38 lines · 1014 bytes · Python

Last changed by

Auto-format Python scriptssharkdpbefore adoption8fff864

History of this file

Take it

scripts: add welch's t testYasuaki Uechibefore adoption5a3fc3c

lines 1–2why, one step back
1#!/usr/bin/python
2

Add help textsharkdpbefore adoptionc06f50d

lines 3–6why, one step back
3"""This script performs Welch's t-test on a JSON export file with two
4benchmark results to test whether or not the two distributions are5the same."""6

scripts: add welch's t testYasuaki Uechibefore adoption5a3fc3c

lines 7–8why, one step back
7import argparse
8import json

Add error messagesharkdpbefore adoption071d5c6

line 9why, one step back
9import sys

scripts: add welch's t testYasuaki Uechibefore adoption5a3fc3c

lines 10–11why, one step back
10from scipy import stats
11

Add help textsharkdpbefore adoptionc06f50d

line 12why, one step back
12parser = argparse.ArgumentParser(description=__doc__)

Add error messagesharkdpbefore adoption071d5c6

line 13why, one step back
13parser.add_argument("file", help="JSON file with two benchmark results")

scripts: add welch's t testYasuaki Uechibefore adoption5a3fc3c

lines 14–17why, one step back
14args = parser.parse_args()
1516with open(args.file) as f:17 results = json.load(f)["results"]

Add error messagesharkdpbefore adoption071d5c6

lines 18–21why, one step back
18
19if len(results) != 2:20 print("The input file has to contain exactly two benchmarks")21 sys.exit(1)

scripts: add welch's t testYasuaki Uechibefore adoption5a3fc3c

lines 22–31why, one step back
22
23a, b = [x["command"] for x in results[:2]]24X, Y = [x["times"] for x in results[:2]]2526print("Command 1: {}".format(a))27print("Command 2: {}\n".format(b))2829t, p = stats.ttest_ind(X, Y, equal_var=False)30th = 0.0531dispose = p < th

Output formattingsharkdpbefore adoption94d458c

lines 32–34why, one step back
32print("t = {:.3}, p = {:.3}".format(t, p))
33print()34

scripts: add welch's t testYasuaki Uechibefore adoption5a3fc3c

line 35why, one step back
35if dispose:

Auto-format Python scriptssharkdpbefore adoption8fff864

line 36why, one step back
36 print("There is a difference between the two benchmarks (p < {}).".format(th))

scripts: add welch's t testYasuaki Uechibefore adoption5a3fc3c

line 37why, one step back
37else:

Output formattingsharkdpbefore adoption94d458c

line 38why, one step back
38 print("The two benchmarks are almost the same (p >= {}).".format(th))