Example #1
0
    def test_pearsonr_with_confidence(self):
        for noise in [0.01, 0.1, 0.2, 0.5, 1, 2, 5, 10]:
            x = np.random.uniform(0, 1, 100)
            y = x + np.random.normal(0, noise, 100)

            rho, p, lb, ub = stats.pearsonr_with_confidence(x, y)

            self.assertGreater(ub, rho)
            self.assertLess(lb, rho)

            self.assertLess(ub, 1)
            self.assertGreater(lb, -1)

            if p < 0.05:
                self.assertGreater(lb, 0)
            else:
                self.assertLess(lb, 0)

        for noise in [0.01, 0.1, 0.2, 0.5, 1, 2, 5, 10]:
            x = np.random.uniform(0, 1, 100)
            y = x**4 + np.random.normal(0, noise, 100)

            rho, p, lb, ub = stats.pearsonr_with_confidence(x, y)

            self.assertGreater(ub, rho)
            self.assertLess(lb, rho)

            self.assertLess(ub, 1)
            self.assertGreater(lb, -1)

            if p < 0.05:
                self.assertGreater(lb, 0)
            else:
                self.assertLess(lb, 0)
Example #2
0
    def test_pearsonr_with_confidence(self):
        for noise in [0.01, 0.1, 0.2, 0.5, 1, 2, 5, 10]:
            x = np.random.uniform(0, 1, 100)
            y = x + np.random.normal(0, noise, 100)

            rho, p, lb, ub = stats.pearsonr_with_confidence(x, y)

            self.assertGreater(ub, rho)
            self.assertLess(lb, rho)

            self.assertLess(ub, 1)
            self.assertGreater(lb, -1)

            if p < 0.05:
                self.assertGreater(lb, 0)
            else:
                self.assertLess(lb, 0)

        for noise in [0.01, 0.1, 0.2, 0.5, 1, 2, 5, 10]:
            x = np.random.uniform(0, 1, 100)
            y = x**4 + np.random.normal(0, noise, 100)

            rho, p, lb, ub = stats.pearsonr_with_confidence(x, y)

            self.assertGreater(ub, rho)
            self.assertLess(lb, rho)

            self.assertLess(ub, 1)
            self.assertGreater(lb, -1)

            if p < 0.05:
                self.assertGreater(lb, 0)
            else:
                self.assertLess(lb, 0)
Example #3
0
    def test_pearsonr_partial(self):

        x = np.random.normal(0, 1, 200)
        z = x + np.random.normal(0, .2, x.shape)
        y = 3 * z + np.random.normal(0, .1, x.shape)

        rho, p, lb, ub = stats.pearsonr_partial_with_confidence(x, y, [z])

        # find best fit line
        slope, icpt, _, _, _ = scipy.stats.linregress(z, y)
        y_prime = y - (slope * z + icpt)

        rho_correct, p_correct, lb_correct, ub_correct = stats.pearsonr_with_confidence(x, y_prime)

        self.assertAlmostEqual(rho, rho_correct)
        self.assertAlmostEqual(p, p_correct)
        self.assertAlmostEqual(lb, lb_correct)
        self.assertAlmostEqual(ub, ub_correct)
Example #4
0
    def test_pearsonr_partial(self):

        x = np.random.normal(0, 1, 200)
        z = x + np.random.normal(0, .2, x.shape)
        y = 3 * z + np.random.normal(0, .1, x.shape)

        rho, p, lb, ub = stats.pearsonr_partial_with_confidence(x, y, [z])

        # find best fit line
        slope, icpt, _, _, _ = scipy.stats.linregress(z, y)
        y_prime = y - (slope * z + icpt)

        rho_correct, p_correct, lb_correct, ub_correct = stats.pearsonr_with_confidence(
            x, y_prime)

        self.assertAlmostEqual(rho, rho_correct)
        self.assertAlmostEqual(p, p_correct)
        self.assertAlmostEqual(lb, lb_correct)
        self.assertAlmostEqual(ub, ub_correct)