def anova(groups): ''' Analysis of variance. ''' alls = ut.flatten(groups) aldsq = sqsum(alls) allm = mean(alls) dsqs = [sqsum(group) for group in groups] innerdsq = sum(dsqs) interparts = [(mean(group)-allm)**2*len(group) for group in groups] interdsq = sum(interparts) interdf = len(groups)-1 innerdfls = [len(group)-1 for group in groups] innerdf = sum(innerdfls) aldf = len(alls)-1 intermsq = interdsq/interdf innermsq = innerdsq/innerdf f = intermsq/innermsq fdist = cs.F_TABLE[innerdf][interdf] return (f, f>fdist)
def anovam(g1, g2): ''' Analysis of variance for multi group. ''' g1a = g1[0] g1b = g1[1] g2a = g2[0] g2b = g2[1] g1data = ut.flatten(g1) g2data = ut.flatten(g2) alldata = g1data+g2data allmean = mean(alldata) g1mean = mean(g1data) g2mean = mean(g2data) f1dev = ((g1mean-allmean)**2*len(g1data))+((g2mean-allmean)**2*len(g2data)) f2data1 = g1a+g2a f2data2 = g1b+g2b f2mean1 = mean(f2data1) f2mean2 = mean(f2data2) f2dev = ((f2mean1-allmean)**2*len(f2data1))+((f2mean2-allmean)**2*len(f2data2)) groupdev = (mean(g1a)-allmean)**2*len(g1a) + (mean(g1b)-allmean)**2*len(g1b) + (mean(g2a)-allmean)**2*len(g2a) + (mean(g2b)-allmean)**2*len(g2b) interdev = groupdev-f1dev-f2dev residev = sum(map(sqsum, [g1a,g1b,g2a,g2b])) f1df = len(g1)-1 f2df = len(g2)-1 interdf = f1df*f2df alldf = len(alldata)-1 residf = alldf-f1df-f2df-interdf f1msq = f1dev/f1df f2msq = f2dev/f2df intermsq = interdev/interdf resimsq = residev/residf fvalues = tuple([x/resimsq for x in [f1msq,f2msq,intermsq]]) fdist = cs.F_TABLE[residf][interdf] fresults = tuple([x>fdist for x in fvalues]) return (fvalues,fresults)
def test_flatten(self): '''test nested list flatten''' res = ut.flatten(self.sample_flatten) self.assertEqual(res, self.fx)