def cohen_d(n1, n2): """ Compute Cohen's $d$ for two independent samples n1, n2, defined as: d = \frac{\=x1 - \=x2}{s} where $s$, the pooled standard deviation, is s = \sqrt{\frac{\sum_{i=1}^{n} (x_{1,i} - \=x1) ^ 2 + \sum_{i=1}^{n} (x_{2,i} - \=x2) ^ 2}{n1 + n2}} This latter definition comes from: J. Hartung, G. Knapp, & B.K. Sinha. 2008. Statistical meta-analysis with application. Hoboken, NJ: Wiley. (p. 14) >>> from csv import DictReader >>> from collections import defaultdict >>> species2petal_width = defaultdict(list) >>> for row in DictReader(open('iris.csv', 'r')): ... species = row['Species'] ... width = row['Petal.Width'] ... species2petal_width[species].append(float(width)) >>> round(cohen_d(*species2petal_width.values()), 3) 2.955 """ mu1 = mean(n1) mu2 = mean(n2) diff = abs(mu1 - mu2) size = len(n1) + len(n2) return diff / sqrt((sse(n1, mu1) + sse(n2, mu2)) / size)
def chisquare(n): """ Compute one-way chi-square statistic """ mu = mean(n) chisq = sse(n, mu) / mu return chisq
def glass_Delta(n1, n2): """ Compute Glass's $\Delta$, a variant on Cohen's $d$ for two independent samples; the denominator is replaced with standard deviation for the control group, which is the first sample here. This definition comes from: L.V. Hedges & I. Olkin. 1985. Staistical methods for meta-analysis. Orlando: Academic Press. (p. 78) >>> from csv import DictReader >>> from collections import defaultdict >>> species2petal_width = defaultdict(list) >>> for row in DictReader(open('iris.csv', 'r')): ... species = row['Species'] ... width = row['Petal.Width'] ... species2petal_width[species].append(float(width)) >>> round(glass_Delta(*species2petal_width.values()), 3) 2.575 """ mu1 = mean(n1) mu2 = mean(n2) return abs(mu1 - mu2) / sqrt(sse(n1, mu1) / len(n1))