Skip to content

Instantly share code, notes, and snippets.

@wkschwartz
Created April 24, 2013 21:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wkschwartz/5455925 to your computer and use it in GitHub Desktop.
Save wkschwartz/5455925 to your computer and use it in GitHub Desktop.
Timing/profiling tests on different branches of https://github.com/wkschwartz/algorithms2
from math import sqrt
def ttest(x1, x2):
"Student's t-test for unpaired, equal variance, equal size samples."
if len(x1) != len(x2):
raise ValueError('unequal sample sizes')
mx1 = sum(x1) / len(x1)
mx2 = sum(x2) / len(x2)
s2x1 = sum((xi - mx1)**2 for xi in x1) / (len(x1) - 1)
s2x2 = sum((xi - mx2)**2 for xi in x2) / (len(x2) - 1)
sx1x2 = sqrt((s2x1 + s2x2) / 2)
return (mx1 - mx2) / (sx1x2 * sqrt(2 / len(x1)))
# Time to run MoveToFront encoding->decoding on different branches on mobydick.txt
ll = [.238, .233, .238, .218, .223, .224, .243]
array = [.266, .258, .247, .256, .250, .257, .248]
print('t-value: %s' % ttest(ll, array))
print('degrees of freedom: %s' % (2 * len(ll) - 2))
# t-value: -5.401857486891129
# degrees of freedom: 12
# This means a 99% probability that ll is faster than array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment