Beispiel #1
0
    def test_mean_std(self):

        # test 1D case
        x = np.array([1, 2, 3, 4, 5])
        weights = np.array([0.2, 0.1, 2, 0.5, 1])

        ret = inputs.mean_std_weighted(x)
        self.assertTrue(abs(ret["mean"] - 3.0) < 1e-8)
        self.assertTrue(abs(ret["std"] - np.sqrt(2 * (4 + 1) / 5)) < 1e-8)

        ret = inputs.mean_std_weighted(x, np.ones(x.shape))
        self.assertTrue(abs(ret["mean"] - 3.0) < 1e-8)
        self.assertTrue(abs(ret["std"] - np.sqrt(2 * (4 + 1) / 5)) < 1e-8)

        ret = inputs.mean_std_weighted(x, weights)
        m = np.sum(weights * x) / np.sum(weights)
        s = np.sqrt(np.sum((x - m) ** 2 * weights) / np.sum(weights))
        self.assertTrue(abs(ret["mean"] - m) < 1e-8)
        self.assertTrue(abs(ret["std"] - s) < 1e-8)

        # 2D case
        x = np.array([[1, 2], [-0.5, 0.0], [3, -0.55]])
        weights = np.array([0.5, 2, 1.55])

        ret = inputs.mean_std_weighted(x, weights)

        sum_weights = np.sum(weights)
        m1 = (1.0 * 0.5 + -0.5 * 2 + 3 * 1.55) / sum_weights
        m2 = (2.0 * 0.5 + 0.0 * 2 + -0.55 * 1.55) / sum_weights
        self.assertTrue(np.allclose(ret["mean"], [m1, m2]))

        s1 = np.sqrt(((1.0 - m1) ** 2 * 0.5 + (-0.5 - m1) ** 2 * 2.0 + (3 - m1) ** 2 * 1.55) / sum_weights)
        s2 = np.sqrt(((2 - m2) ** 2 * 0.5 + (0.0 - m2) ** 2 * 2.0 + (-0.55 - m2) ** 2 * 1.55) / sum_weights)
        self.assertTrue(np.allclose(ret["std"], [s1, s2]))
    def test_pearson_weighted(self):
        from scipy.stats import pearsonr

        x = np.array([1, 2, 3, 4, 5])
        y = np.array([1.0, 1.5, -0.5, 3.4, 2.9])
        weights = np.array([1, 0, 0.5, 2, 1.5])

        r_no_wgt = pearsonr(x, y)[0]
        r_no_wgt_test = evaluation.pearsonr_weighted(x, y)
        r_ones_wgt = evaluation.pearsonr_weighted(x, y, np.ones(x.shape))

        self.assertTrue(abs(r_no_wgt - r_no_wgt_test) < 1e-12)
        self.assertTrue(abs(r_no_wgt - r_ones_wgt) < 1e-12)

        xm = mean_std_weighted(x, weights)
        ym = mean_std_weighted(y, weights)
        r_wgt = np.sum((x - xm['mean']) * (y - ym['mean']) * weights) / np.sum(weights)
        self.assertTrue((evaluation.pearsonr_weighted(x, y, weights) - r_wgt) < 1e-12)
Beispiel #3
0
    def test_pearson_weighted(self):
        from scipy.stats import pearsonr

        x = np.array([1, 2, 3, 4, 5])
        y = np.array([1.0, 1.5, -0.5, 3.4, 2.9])
        weights = np.array([1, 0, 0.5, 2, 1.5])

        r_no_wgt = pearsonr(x, y)[0]
        r_no_wgt_test = evaluation.pearsonr_weighted(x, y)
        r_ones_wgt = evaluation.pearsonr_weighted(x, y, np.ones(x.shape))

        self.assertTrue(abs(r_no_wgt - r_no_wgt_test) < 1e-12)
        self.assertTrue(abs(r_no_wgt - r_ones_wgt) < 1e-12)

        xm = mean_std_weighted(x, weights)
        ym = mean_std_weighted(y, weights)
        r_wgt = np.sum(
            (x - xm['mean']) * (y - ym['mean']) * weights) / np.sum(weights)
        self.assertTrue(
            (evaluation.pearsonr_weighted(x, y, weights) - r_wgt) < 1e-12)
Beispiel #4
0
    def test_mean_std(self):

        # test 1D case
        x = np.array([1, 2, 3, 4, 5])
        weights = np.array([0.2, 0.1, 2,0.5, 1])

        ret = inputs.mean_std_weighted(x)
        self.assertTrue(abs(ret['mean'] - 3.0) < 1e-8)
        self.assertTrue(abs(ret['std'] - np.sqrt(2 * (4 + 1) / 5)) < 1e-8)

        ret = inputs.mean_std_weighted(x, np.ones(x.shape))
        self.assertTrue(abs(ret['mean'] - 3.0) < 1e-8) 
        self.assertTrue(abs(ret['std'] - np.sqrt(2 * (4 + 1) / 5)) < 1e-8)

        ret = inputs.mean_std_weighted(x, weights)
        m = np.sum(weights * x) / np.sum(weights)
        s = np.sqrt(np.sum((x - m)**2 * weights) / np.sum(weights))
        self.assertTrue(abs(ret['mean'] - m) < 1e-8)
        self.assertTrue(abs(ret['std'] - s) < 1e-8)

        # 2D case
        x = np.array([[1, 2],
                     [-0.5, 0.0],
                     [3, -0.55]])
        weights = np.array([0.5, 2, 1.55])

        ret = inputs.mean_std_weighted(x, weights)

        sum_weights = np.sum(weights)
        m1 = (1.0 * 0.5 + -0.5 * 2 + 3 * 1.55) / sum_weights
        m2 = (2.0 * 0.5 + 0.0 * 2 + -0.55 * 1.55) / sum_weights
        self.assertTrue(np.allclose(ret['mean'], [m1, m2]))

        s1 = np.sqrt(((1.0 - m1) ** 2 * 0.5 + (-0.5 - m1)**2 * 2.0 + (3 - m1)**2 * 1.55) / sum_weights)
        s2 = np.sqrt(((2 - m2) ** 2 * 0.5 + (0.0 - m2)**2 * 2.0 + (-0.55 - m2)**2 * 1.55) / sum_weights)
        self.assertTrue(np.allclose(ret['std'], [s1, s2]))