Exemplo n.º 1
0
    def test_mc_t_two_sample(self):
        """Test gives correct results with valid input data."""
        # Verified against R's t.test() and Deducer::perm.t.test().

        # With numpy array as input.
        exp = (-0.11858541225631833, 0.90756579317867436)
        I = np.array([7.2, 7.1, 9.1, 7.2, 7.3, 7.2, 7.5])
        II = np.array([8.8, 7.5, 7.7, 7.6, 7.4, 6.7, 7.2])
        obs = mc_t_two_sample(I, II)
        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 999)
        self.assertCorrectPValue(0.8, 0.9, mc_t_two_sample, [I, II],
                                 p_val_idx=3)

        # With python list as input.
        exp = (-0.11858541225631833, 0.90756579317867436)
        I = [7.2, 7.1, 9.1, 7.2, 7.3, 7.2, 7.5]
        II = [8.8, 7.5, 7.7, 7.6, 7.4, 6.7, 7.2]
        obs = mc_t_two_sample(I, II)
        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 999)
        self.assertCorrectPValue(0.8, 0.9, mc_t_two_sample, [I, II],
                                 p_val_idx=3)

        exp = (-0.11858541225631833, 0.45378289658933718)
        obs = mc_t_two_sample(I, II, tails='low')
        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 999)
        self.assertCorrectPValue(0.4, 0.47, mc_t_two_sample, [I, II],
                                 {'tails': 'low'}, p_val_idx=3)

        exp = (-0.11858541225631833, 0.54621710341066287)
        obs = mc_t_two_sample(I, II, tails='high', permutations=99)
        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 99)
        self.assertCorrectPValue(0.4, 0.62, mc_t_two_sample, [I, II],
                                 {'tails': 'high', 'permutations': 99},
                                 p_val_idx=3)

        exp = (-2.8855783649036986, 0.99315596652421401)
        obs = mc_t_two_sample(I, II, tails='high',
                              permutations=99, exp_diff=1)
        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 99)
        self.assertCorrectPValue(0.55, 0.99, mc_t_two_sample, [I, II],
                                 {'tails': 'high', 'permutations': 99,
                                  'exp_diff': 1}, p_val_idx=3)
Exemplo n.º 2
0
    def test_mc_t_two_sample(self):
        """Test gives correct results with valid input data."""
        # Verified against R's t.test() and Deducer::perm.t.test().

        # With numpy array as input.
        exp = (-0.11858541225631833, 0.90756579317867436)
        I = np.array([7.2, 7.1, 9.1, 7.2, 7.3, 7.2, 7.5])
        II = np.array([8.8, 7.5, 7.7, 7.6, 7.4, 6.7, 7.2])
        obs = mc_t_two_sample(I, II)
        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 999)
        self.assertCorrectPValue(0.8, 0.9, mc_t_two_sample, [I, II],
                                 p_val_idx=3)

        # With python list as input.
        exp = (-0.11858541225631833, 0.90756579317867436)
        I = [7.2, 7.1, 9.1, 7.2, 7.3, 7.2, 7.5]
        II = [8.8, 7.5, 7.7, 7.6, 7.4, 6.7, 7.2]
        obs = mc_t_two_sample(I, II)
        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 999)
        self.assertCorrectPValue(0.8, 0.9, mc_t_two_sample, [I, II],
                                 p_val_idx=3)

        exp = (-0.11858541225631833, 0.45378289658933718)
        obs = mc_t_two_sample(I, II, tails='low')
        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 999)
        self.assertCorrectPValue(0.4, 0.47, mc_t_two_sample, [I, II],
                                 {'tails': 'low'}, p_val_idx=3)

        exp = (-0.11858541225631833, 0.54621710341066287)
        obs = mc_t_two_sample(I, II, tails='high', permutations=99)
        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 99)
        self.assertCorrectPValue(0.4, 0.62, mc_t_two_sample, [I, II],
                                 {'tails': 'high', 'permutations': 99},
                                 p_val_idx=3)

        exp = (-2.8855783649036986, 0.99315596652421401)
        obs = mc_t_two_sample(I, II, tails='high',
                              permutations=99, exp_diff=1)
        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 99)
        self.assertCorrectPValue(0.55, 0.99, mc_t_two_sample, [I, II],
                                 {'tails': 'high', 'permutations': 99,
                                  'exp_diff': 1}, p_val_idx=3)
Exemplo n.º 3
0
 def test_mc_t_two_sample_no_perms(self):
     """Test gives empty permutation results if no perms are given."""
     exp = (-0.11858541225631833, 0.90756579317867436, [], None)
     I = np.array([7.2, 7.1, 9.1, 7.2, 7.3, 7.2, 7.5])
     II = np.array([8.8, 7.5, 7.7, 7.6, 7.4, 6.7, 7.2])
     obs = mc_t_two_sample(I, II, permutations=0)
     np.testing.assert_allclose(obs[0], exp[0])
     np.testing.assert_allclose(obs[1], exp[1])
     self.assertEqual(obs[2], exp[2])
     self.assertEqual(obs[3], exp[3])
Exemplo n.º 4
0
 def test_mc_t_two_sample_no_perms(self):
     """Test gives empty permutation results if no perms are given."""
     exp = (-0.11858541225631833, 0.90756579317867436, [], None)
     I = np.array([7.2, 7.1, 9.1, 7.2, 7.3, 7.2, 7.5])
     II = np.array([8.8, 7.5, 7.7, 7.6, 7.4, 6.7, 7.2])
     obs = mc_t_two_sample(I, II, permutations=0)
     np.testing.assert_allclose(obs[0], exp[0])
     np.testing.assert_allclose(obs[1], exp[1])
     self.assertEqual(obs[2], exp[2])
     self.assertEqual(obs[3], exp[3])
Exemplo n.º 5
0
 def test_mc_t_two_sample_unbalanced_obs(self):
     """Test gives correct results with unequal number of obs per sample."""
     # Verified against R's t.test() and Deducer::perm.t.test().
     exp = (-0.10302479888889175, 0.91979753020527177)
     I = np.array([7.2, 7.1, 9.1, 7.2, 7.3, 7.2])
     II = np.array([8.8, 7.5, 7.7, 7.6, 7.4, 6.7, 7.2])
     obs = mc_t_two_sample(I, II)
     np.testing.assert_allclose(obs[:2], exp)
     self.assertEqual(len(obs[2]), 999)
     self.assertCorrectPValue(0.8, 0.9, mc_t_two_sample, [I, II],
                              p_val_idx=3)
Exemplo n.º 6
0
 def test_mc_t_two_sample_unbalanced_obs(self):
     """Test gives correct results with unequal number of obs per sample."""
     # Verified against R's t.test() and Deducer::perm.t.test().
     exp = (-0.10302479888889175, 0.91979753020527177)
     I = np.array([7.2, 7.1, 9.1, 7.2, 7.3, 7.2])
     II = np.array([8.8, 7.5, 7.7, 7.6, 7.4, 6.7, 7.2])
     obs = mc_t_two_sample(I, II)
     np.testing.assert_allclose(obs[:2], exp)
     self.assertEqual(len(obs[2]), 999)
     self.assertCorrectPValue(0.8, 0.9, mc_t_two_sample, [I, II],
                              p_val_idx=3)
Exemplo n.º 7
0
    def test_mc_t_two_sample_no_permuted_variance(self):
        """Test with chance of getting no variance with some perms."""
        # Verified against R's t.test() and Deducer::perm.t.test().
        x = np.array([1, 1, 2])
        y = np.array([2, 2, 1])

        exp = (-0.70710678118654791, 0.51851851851851838)
        obs = mc_t_two_sample(x, y, permutations=10000)

        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 10000)
        self.assertCorrectPValue(0.97, 1.0, mc_t_two_sample, [x, y],
                                 {'permutations': 10000}, p_val_idx=3)
Exemplo n.º 8
0
    def test_mc_t_two_sample_no_permuted_variance(self):
        """Test with chance of getting no variance with some perms."""
        # Verified against R's t.test() and Deducer::perm.t.test().
        x = np.array([1, 1, 2])
        y = np.array([2, 2, 1])

        exp = (-0.70710678118654791, 0.51851851851851838)
        obs = mc_t_two_sample(x, y, permutations=10000)

        np.testing.assert_allclose(obs[:2], exp)
        self.assertEqual(len(obs[2]), 10000)
        self.assertCorrectPValue(0.97, 1.0, mc_t_two_sample, [x, y],
                                 {'permutations': 10000}, p_val_idx=3)
Exemplo n.º 9
0
    def test_mc_t_two_sample_no_variance(self):
        """Test input with no variance. Should match Deducer::perm.t.test."""
        x = np.array([1, 1, 1])
        y = np.array([2, 2, 2])

        exp = (float('-inf'), 0.0)
        obs = mc_t_two_sample(x, y, permutations=10000)

        self.assertEqual(obs[:2], exp)
        self.assertEqual(len(obs[2]), 10000)
        self.assertCorrectPValue(0.09, 0.11, mc_t_two_sample, [x, y],
                                 {'permutations': 10000}, p_val_idx=3)

        exp = (float('inf'), 0.0)
        obs = mc_t_two_sample(y, x, permutations=10000)

        self.assertEqual(obs[:2], exp)
        self.assertEqual(len(obs[2]), 10000)
        self.assertCorrectPValue(0.09, 0.11, mc_t_two_sample, [y, x],
                                 {'permutations': 10000}, p_val_idx=3)

        exp = (float('-inf'), 1.0)
        obs = mc_t_two_sample(x, y, permutations=10000, tails='high')

        self.assertEqual(obs[:2], exp)
        self.assertEqual(len(obs[2]), 10000)
        self.assertCorrectPValue(0.9999, 1.0, mc_t_two_sample, [x, y],
                                 {'permutations': 10000, 'tails': 'high'},
                                 p_val_idx=3)

        exp = (float('-inf'), 0.0)
        obs = mc_t_two_sample(x, y, permutations=10000, tails='low')

        self.assertEqual(obs[:2], exp)
        self.assertEqual(len(obs[2]), 10000)
        self.assertCorrectPValue(0.04, 0.051, mc_t_two_sample, [x, y],
                                 {'permutations': 10000, 'tails': 'low'},
                                 p_val_idx=3)
Exemplo n.º 10
0
    def test_mc_t_two_sample_no_variance(self):
        """Test input with no variance. Should match Deducer::perm.t.test."""
        x = np.array([1, 1, 1])
        y = np.array([2, 2, 2])

        exp = (float('-inf'), 0.0)
        obs = mc_t_two_sample(x, y, permutations=10000)

        self.assertEqual(obs[:2], exp)
        self.assertEqual(len(obs[2]), 10000)
        self.assertCorrectPValue(0.09, 0.11, mc_t_two_sample, [x, y],
                                 {'permutations': 10000}, p_val_idx=3)

        exp = (float('inf'), 0.0)
        obs = mc_t_two_sample(y, x, permutations=10000)

        self.assertEqual(obs[:2], exp)
        self.assertEqual(len(obs[2]), 10000)
        self.assertCorrectPValue(0.09, 0.11, mc_t_two_sample, [y, x],
                                 {'permutations': 10000}, p_val_idx=3)

        exp = (float('-inf'), 1.0)
        obs = mc_t_two_sample(x, y, permutations=10000, tails='high')

        self.assertEqual(obs[:2], exp)
        self.assertEqual(len(obs[2]), 10000)
        self.assertCorrectPValue(0.9999, 1.0, mc_t_two_sample, [x, y],
                                 {'permutations': 10000, 'tails': 'high'},
                                 p_val_idx=3)

        exp = (float('-inf'), 0.0)
        obs = mc_t_two_sample(x, y, permutations=10000, tails='low')

        self.assertEqual(obs[:2], exp)
        self.assertEqual(len(obs[2]), 10000)
        self.assertCorrectPValue(0.04, 0.051, mc_t_two_sample, [x, y],
                                 {'permutations': 10000, 'tails': 'low'},
                                 p_val_idx=3)
Exemplo n.º 11
0
    def test_mc_t_two_sample_single_obs_sample(self):
        """Test works correctly with one sample having a single observation."""
        sample = np.array([4.02, 3.88, 3.34, 3.87, 3.18])
        x = np.array([3.02])
        exp = (-1.5637254, 0.1929248)
        obs = mc_t_two_sample(x, sample)
        np.testing.assert_allclose(obs[:2], exp)
        np.testing.assert_allclose(len(obs[2]), 999)
        self.assertTrue(0.0 <= obs[3] <= 1.0)

        exp = (1.5637254, 0.1929248)
        obs = mc_t_two_sample(sample, x)
        np.testing.assert_allclose(obs[:2], exp)
        np.testing.assert_allclose(len(obs[2]), 999)
        self.assertTrue(0.0 <= obs[3] <= 1.0)

        # Test the case where we can have no variance in the permuted lists.
        x = np.array([1, 1, 2])
        y = np.array([1])
        exp = (0.5, 0.666666666667)
        obs = mc_t_two_sample(x, y)
        np.testing.assert_allclose(obs[:2], exp)
        np.testing.assert_allclose(len(obs[2]), 999)
        self.assertTrue(0.0 <= obs[3] <= 1.0)
Exemplo n.º 12
0
    def test_mc_t_two_sample_single_obs_sample(self):
        """Test works correctly with one sample having a single observation."""
        sample = np.array([4.02, 3.88, 3.34, 3.87, 3.18])
        x = np.array([3.02])
        exp = (-1.5637254, 0.1929248)
        obs = mc_t_two_sample(x, sample)
        np.testing.assert_allclose(obs[:2], exp)
        np.testing.assert_allclose(len(obs[2]), 999)
        self.assertTrue(0.0 <= obs[3] <= 1.0)

        exp = (1.5637254, 0.1929248)
        obs = mc_t_two_sample(sample, x)
        np.testing.assert_allclose(obs[:2], exp)
        np.testing.assert_allclose(len(obs[2]), 999)
        self.assertTrue(0.0 <= obs[3] <= 1.0)

        # Test the case where we can have no variance in the permuted lists.
        x = np.array([1, 1, 2])
        y = np.array([1])
        exp = (0.5, 0.666666666667)
        obs = mc_t_two_sample(x, y)
        np.testing.assert_allclose(obs[:2], exp)
        np.testing.assert_allclose(len(obs[2]), 999)
        self.assertTrue(0.0 <= obs[3] <= 1.0)
Exemplo n.º 13
0
 def test_mc_t_two_sample_no_mc(self):
     """Test no MC stats if initial t-test is bad."""
     x = np.array([1, 1, 1])
     y = np.array([0, 0, 0])
     self.assertEqual(mc_t_two_sample(x, x), (None, None, [], None))
Exemplo n.º 14
0
 def test_mc_t_two_sample_no_mc(self):
     """Test no MC stats if initial t-test is bad."""
     x = np.array([1, 1, 1])
     y = np.array([0, 0, 0])
     self.assertEqual(mc_t_two_sample(x, x), (None, None, [], None))