예제 #1
0
    def testDataframeRelativeToJackknife(self):
        df = pd.DataFrame({
            "X": range(11),
            "Y": np.concatenate((np.zeros(6), np.ones(5))),
            "Z": np.concatenate((np.zeros(3), np.ones(8)))
        })

        metric = metrics.Distribution("X", ["Z"])
        se_method = standard_errors.Jackknife()
        output = core.Analyze(df).relative_to(
            comparisons.AbsoluteDifference(
                "Y",
                0)).with_standard_errors(se_method).calculate(metric).run()

        correct = pd.DataFrame(
            np.array([[-0.2, 0.18100283490], [0.2, 0.18100283490]]),
            columns=[
                "X Distribution Absolute Difference",
                "X Distribution Absolute Difference Jackknife SE"
            ],
            index=pd.MultiIndex(levels=[[1.], [0., 1.]],
                                labels=[[0, 0], [0, 1]],
                                names=["Y", "Z"]))

        self.assertTrue(
            all(output.index == correct.index)
            and all(output.columns == correct.columns)
            and np.all(abs(output.values - correct.values) < 1e-10))
예제 #2
0
    def testShuffledDataframeRelativeToJackknife(self):
        # Same as test above, but also testing that reordering the data doesn't
        # change results, up to order.
        df = pd.DataFrame({
            "X": range(11),
            "Y": np.concatenate((np.zeros(6), np.ones(5))),
            "Z": np.concatenate((np.zeros(3), np.ones(8)))
        })

        metric = metrics.Distribution("X", ["Z"])
        se_method = standard_errors.Jackknife()
        output = core.Analyze(df.iloc[np.random.permutation(11)]).relative_to(
            comparisons.AbsoluteDifference(
                "Y",
                0)).with_standard_errors(se_method).calculate(metric).run()
        output = (output.reset_index().sort_values(by=["Y", "Z"]).set_index(
            ["Y", "Z"]))

        correct = pd.DataFrame(
            np.array([[-0.2, 0.18100283490], [0.2, 0.18100283490]]),
            columns=[
                "X Distribution Absolute Difference",
                "X Distribution Absolute Difference Jackknife SE"
            ],
            index=pd.MultiIndex(levels=[[1.], [0., 1.]],
                                labels=[[0, 0], [0, 1]],
                                names=["Y", "Z"]))
        correct = (correct.reset_index().sort_values(by=["Y", "Z"]).set_index(
            ["Y", "Z"]))

        self.assertTrue(
            all(output.index == correct.index)
            and all(output.columns == correct.columns)
            and np.all(abs(output.values - correct.values) < 1e-10))
예제 #3
0
    def testRelativeToSplitJackknife(self):
        data = pd.DataFrame({
            "X": [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8],
            "Y": [1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3],
            "Z": [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
        })

        metric = metrics.Sum("X")
        comparison = comparisons.AbsoluteDifference("Z", 0)
        se_method = standard_errors.Jackknife()
        output = core.Analyze(data).split_by("Y").relative_to(
            comparison).with_standard_errors(se_method).calculate(
                metric).run()

        rowindex = pd.MultiIndex(levels=[[1, 2, 3], [1]],
                                 labels=[[0, 1, 2], [0, 0, 0]],
                                 names=["Y", "Z"])
        correct = pd.DataFrame(
            np.array([[-3.0,
                       np.sqrt(5 * np.var([0, -1, -2, -3, -4, -5]))],
                      [-3.0, np.sqrt(5 * np.var([3, 2, 1, -8, -7, -6]))],
                      [-3.0,
                       np.sqrt(5 * np.var([6, 5, 4, -11, -10, -9]))]]),
            columns=("sum(X) Absolute Difference",
                     "sum(X) Absolute Difference Jackknife SE"),
            index=rowindex)

        self.assertTrue(output.equals(correct))
예제 #4
0
    def testNinetyFiveCIsWithComparison(self):
        data = pd.DataFrame({
            "X": range(11),
            "Y": [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
        })

        metric = metrics.Sum("X")
        comparison = comparisons.AbsoluteDifference("Y", 0)
        se_method = standard_errors.Jackknife(confidence=0.95)
        output = core.Analyze(data).with_standard_errors(
            se_method).relative_to(comparison).calculate(metric).run()

        multiplier = scipy.stats.t.ppf(0.975, 10)
        correct_mean = 25
        correct_buckets = [
            15., 16., 17., 18., 19., 25., 26., 27., 28., 29., 30.
        ]
        m = sum(correct_buckets) / len(correct_buckets)
        r = sum([(b - m)**2 for b in correct_buckets])
        correct_sd = np.sqrt(r * (len(correct_buckets) - 1) /
                             len(correct_buckets))

        correct_lower = correct_mean - multiplier * correct_sd
        correct_upper = correct_mean + multiplier * correct_sd

        rowindex = pd.Index([1], name="Y")
        correct = pd.DataFrame(
            {
                "sum(X) Absolute Difference": correct_mean,
                "sum(X) Absolute Difference Jackknife CI-lower": correct_lower,
                "sum(X) Absolute Difference Jackknife CI-upper": correct_upper
            },
            index=rowindex)

        self.assertTrue(output.equals(correct))
예제 #5
0
    def testComparisonBaslineGivesError(self):
        data = pd.DataFrame({"X": [1, 2, 3, 4, 5], "Y": [1, 1, 1, 1, 1]})

        metric = metrics.Sum("X")
        comparison = comparisons.AbsoluteDifference("Y", 0)

        with self.assertRaises(ValueError):
            core.Analyze(data).relative_to(comparison).calculate(metric).run()
예제 #6
0
    def testAbsoluteDifference(self):
        data = pd.DataFrame({
            "X": [1, 3, 2, 3, 1, 4],
            "Condition": [0, 0, 0, 1, 1, 1]
        })
        weights = np.ones(6)

        comparison = comparisons.AbsoluteDifference("Condition", 0)
        comparison.precalculate_factors(data)

        metric = metrics.Sum("X")
        output = comparison(data, weights, metric).values[0]

        self.assertEqual(2, output)
예제 #7
0
    def testRelativeToJackknifeSingleComparisonBaselineSecond(self):
        data = pd.DataFrame({"X": [1, 2, 3, 4, 5, 6], "Y": [0, 0, 0, 1, 1, 1]})

        metric = metrics.Sum("X")
        comparison = comparisons.AbsoluteDifference("Y", 1)
        se_method = standard_errors.Jackknife()
        output = core.Analyze(data).relative_to(
            comparison).with_standard_errors(se_method).calculate(
                metric).run()

        rowindex = pd.Index([0], name="Y")
        correct = pd.DataFrame(
            np.array([[-9.0, np.sqrt(5 * np.var([12, 11, 10, 5, 4, 3]))]]),
            columns=("sum(X) Absolute Difference",
                     "sum(X) Absolute Difference Jackknife SE"),
            index=rowindex)

        self.assertTrue(output.equals(correct))
예제 #8
0
    def testRelativeTo(self):
        data = pd.DataFrame({
            "X": [1, 2, 3, 10, 20, 30, 100, 200, 300],
            "Y": [0, 0, 0, 1, 1, 1, 2, 2, 2]
        })

        metric = metrics.Sum("X")
        comparison = comparisons.AbsoluteDifference("Y", 0)
        output = core.Analyze(data).relative_to(comparison).calculate(
            metric).run()

        correct = pd.DataFrame({
            "sum(X) Absolute Difference": [60 - 6, 600 - 6],
            "Y": [1, 2]
        })

        correct = correct.set_index("Y")

        self.assertTrue(output.equals(correct))
예제 #9
0
    def testRelativeToSplitsWithNoAlternativeGivesNaN(self):
        data = pd.DataFrame({
            "X": [1, 2, 3, 4],
            "Y": [0, 0, 0, 1],
            "Z": [0, 0, 1, 1]
        })

        metric = metrics.Sum("X")
        comparison = comparisons.AbsoluteDifference("Y", 0)
        output = core.Analyze(data).split_by("Z").relative_to(
            comparison).calculate(metric).run()

        correct = pd.DataFrame({
            "sum(X) Absolute Difference": [np.nan, 4 - 3],
            "Z": [0, 1],
            "Y": [1, 1]
        })
        correct = correct.set_index(["Z", "Y"])

        self.assertTrue(output.equals(correct))
예제 #10
0
    def testMultipleCalculationsRelativeTo(self):
        data = pd.DataFrame({
            "X": (1, 2, 3, 10, 20, 30, 100, 200, 300),
            "Y": (0, 1, 2, 3, 4, 5, 6, 7, 8),
            "Experiment": ("Control", "Control", "Control", "Exp1", "Exp1",
                           "Exp1", "Exp2", "Exp2", "Exp2")
        })

        comparison = comparisons.AbsoluteDifference("Experiment", "Control")
        output = core.Analyze(data).relative_to(comparison).calculate(
            (metrics.Sum("X"), metrics.Sum("Y"))).run()

        correct = pd.DataFrame(
            {
                "sum(X) Absolute Difference": (60 - 6, 600 - 6),
                "sum(Y) Absolute Difference": (12 - 3, 21 - 3)
            },
            index=pd.Index(("Exp1", "Exp2"), name="Experiment"))

        self.assertTrue(output.equals(correct))
예제 #11
0
    def testRelativeToSplit(self):
        data = pd.DataFrame({
            "X": [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8],
            "Y": [0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2],
            "Z": [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
        })

        metric = metrics.Sum("X")
        comparison = comparisons.AbsoluteDifference("Y", 0)
        output = core.Analyze(data).split_by("Z").relative_to(
            comparison).calculate(metric).run()

        correct = pd.DataFrame({
            "sum(X) Absolute Difference": [13 - 5, 23 - 5, 14 - 4, 22 - 4],
            "Z": [0, 0, 1, 1],
            "Y": [1, 2, 1, 2]
        })

        correct = correct.set_index(["Z", "Y"])

        self.assertTrue(output.equals(correct))
예제 #12
0
    def testDoubleComparisonDefinitionRaisesException(self):
        data = pd.DataFrame({"X": [1, 2, 3, 4, 5]})

        comparison = comparisons.AbsoluteDifference("X", 0)
        with self.assertRaises(ValueError):
            core.Analyze(data).relative_to(comparison).relative_to(comparison)