def test_correlation_matrix(self): """Correlations in matrix should match values from R""" a = [2, 4, 6, 8] b = [1.5, 1.4, 1.2, 1.1] c = [15, 10, 5, 20] m = correlation_matrix([a, b, c]) self.assertFloatEqual(m[0], [1.0]) self.assertFloatEqual(m[1], [correlation(b, a)[0], 1.0]) self.assertFloatEqual(m[2], [correlation(c, a)[0], correlation(c, b)[0], 1.0])
def test_correlation(self): """Correlations and significance should match R's cor.test()""" x = [1, 2, 3, 5] y = [0, 0, 0, 0] z = [1, 1, 1, 1] a = [2, 4, 6, 8] b = [1.5, 1.4, 1.2, 1.1] c = [15, 10, 5, 20] bad = [1, 2, 3] # originally gave r = 1.0000000002 self.assertFloatEqual(correlation(x, x), (1, 0)) self.assertFloatEqual(correlation(x, y), (0, 1)) self.assertFloatEqual(correlation(y, z), (0, 1)) self.assertFloatEqualAbs(correlation(x, a), (0.9827076, 0.01729), 1e-5) self.assertFloatEqualAbs(correlation(x, b), (-0.9621405, 0.03786), 1e-5) self.assertFloatEqualAbs(correlation(x, c), (0.3779645, 0.622), 1e-3) self.assertEqual(correlation(bad, bad), (1, 0))