Skip to content

Instantly share code, notes, and snippets.

@wanderview
Last active January 2, 2016 09:49
Show Gist options
  • Save wanderview/8285920 to your computer and use it in GitHub Desktop.
Save wanderview/8285920 to your computer and use it in GitHub Desktop.
Python Median Test
import numpy
import scipy
from scipy import stats
def median_test(samples1, samples2):
median = numpy.median(samples1 + samples2)
below1, above1 = count_partition(samples1, median)
below2, above2 = count_partition(samples2, median)
below_exp = (below1 + below2) / 2;
above_exp = (above1 + above2) / 2;
result = scipy.stats.chisquare(
[below1, above1, below2, above2],
f_exp=[below_exp, above_exp, below_exp, above_exp]
);
return {"diff": result[0], "confidence": (1 - result[0])}
def count_partition(samples, cut_value):
smaller = 0.0;
larger = 0.0;
for v in samples:
if v > cut_value:
larger += 1.0
else:
smaller += 1.0
smaller_scaled = normalize(smaller, samples)
larger_scaled = normalize(larger, samples)
return smaller_scaled, larger_scaled
def normalize(value, samples):
return (value / len(samples)) + 1
#s1 = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21]
#s2 = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21]
print "MORE 20s"
s1 = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21]
s2 = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21]
r = median_test(s1, s2)
print r
print "MORE 21s"
s1 = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21]
s2 = [20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21]
r = median_test(s1, s2)
print r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment