Ejemplo n.º 1
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))
Ejemplo n.º 2
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))
Ejemplo n.º 3
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()
Ejemplo n.º 4
0
    def testSingleJackknifeBucketRaisesException(self):
        data = pd.DataFrame({"X": [1, 2, 3, 4, 5], "Y": [1, 1, 1, 1, 1]})

        metric = metrics.Sum("X")
        se_method = standard_errors.Jackknife(unit="Y")

        with self.assertRaises(ValueError):
            core.Analyze(data).with_standard_errors(se_method).calculate(
                metric).run()
Ejemplo n.º 5
0
    def testBadWhereRaisesError(self):
        data = pd.DataFrame({
            "X": (1, 2, 3, 10, 20, 30, 100, 200, 300),
            "Y": (0, 1, 2, 3, 4, 5, 6, 7, 8)
        })

        metric = metrics.Sum("X")
        with self.assertRaises(ValueError):
            core.Analyze(data).where("X + Y").calculate(metric).run()
Ejemplo n.º 6
0
    def testNamingCalculations(self):
        data = pd.DataFrame({"X": [1, 2, 3, 4, 5]})

        metric = metrics.Sum("X", name="X-Total")
        output = core.Analyze(data).calculate(metric).run()

        correct = pd.DataFrame(np.array([[15]]), columns=["X-Total"])

        self.assertTrue(output.equals(correct))
Ejemplo n.º 7
0
  def testSumWithWeights(self):
    df = pd.DataFrame({"X": [1, 2, 3, 4]})
    weights = np.array([3, 2, 1, 1])

    metric = metrics.Sum("X")

    output = metric(df, weights)

    correct = 14

    self.assertEqual(output, correct)
Ejemplo n.º 8
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))
Ejemplo n.º 9
0
    def testMultipleCalculations(self):
        data = pd.DataFrame({"X": [1, 2, 3, 4, 5]})

        output = core.Analyze(data).calculate(
            [metrics.Sum("X"), metrics.Mean("X")]).run()

        correct = pd.DataFrame(np.array([[15, 3.0]]),
                               columns=["sum(X)", "mean(X)"])
        correct[["sum(X)"]] = correct[["sum(X)"]].astype(int)

        self.assertTrue(output.equals(correct))
Ejemplo n.º 10
0
    def testWhere(self):
        data = pd.DataFrame({
            "X": (1, 2, 3, 10, 20, 30, 100, 200, 300),
            "Y": (0, 1, 2, 3, 4, 5, 6, 7, 8)
        })

        metric = metrics.Sum("X")
        output = core.Analyze(data).where("Y >= 4").calculate(metric).run()

        correct = pd.DataFrame(np.array([[650]]), columns=["sum(X)"])

        self.assertTrue(output.equals(correct))
Ejemplo n.º 11
0
    def testJackknife(self):
        data = pd.DataFrame({"X": range(11)})

        metric = metrics.Sum("X")
        se_method = standard_errors.Jackknife()
        output = core.Analyze(data).with_standard_errors(se_method).calculate(
            metric).run()

        correct = pd.DataFrame(np.array([[55.0, 10.0]]),
                               columns=("sum(X)", "sum(X) Jackknife SE"))

        self.assertTrue(output.equals(correct))
Ejemplo n.º 12
0
    def testPercentageDifference(self):
        data = pd.DataFrame({
            "X": [1, 3, 2, 3, 1, 4],
            "Condition": [0, 0, 0, 1, 1, 1]
        })
        weights = np.ones(6)

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

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

        self.assertEqual(100 * (8 - 6) / 6, output)
Ejemplo n.º 13
0
    def testJackknifeBadSample(self):
        data = pd.DataFrame({"X": range(22), "Y": ([0] * 11) + ([1] * 11)})

        metric = metrics.Sum("X")
        se_method = standard_errors.Jackknife()
        output = core.Analyze(data).split_by("Y").with_standard_errors(
            se_method).calculate(metric).run()

        correct = pd.DataFrame(np.array([[55.0, 10.0], [176.0, 10.0]]),
                               columns=("sum(X)", "sum(X) Jackknife SE"))

        correct.index.name = "Y"

        self.assertTrue(output.equals(correct))
Ejemplo n.º 14
0
    def testSplitBy(self):
        data = pd.DataFrame({
            "X": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            "Y": [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
        })

        metric = metrics.Sum("X")
        output = core.Analyze(data).calculate(metric).split_by("Y").run()

        correct = pd.DataFrame({
            "sum(X)": [1, 5, 9, 13, 17],
            "Y": [1, 2, 3, 4, 5]
        })
        correct = correct.set_index("Y")

        self.assertTrue(output.equals(correct))
Ejemplo n.º 15
0
    def testSplitJackknife(self):
        data = pd.DataFrame({
            "X": np.array([range(11) + [5] * 10]).flatten(),
            "Y": np.array([[0] * 11 + [1] * 10]).flatten()
        })

        metric = metrics.Sum("X")
        se_method = standard_errors.Jackknife()
        output = core.Analyze(data).split_by("Y").with_standard_errors(
            se_method).calculate(metric).run()

        rowindex = pd.Index([0, 1], name="Y")
        correct = pd.DataFrame(np.array([[55.0, 10.0], [50.0, 0.0]]),
                               columns=("sum(X)", "sum(X) Jackknife SE"),
                               index=rowindex)

        self.assertTrue(output.equals(correct))
Ejemplo n.º 16
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))
Ejemplo n.º 17
0
    def testMultipleSplitBy(self):
        data = pd.DataFrame({
            "X": [4, 5, 6, 7, 0, 1, 2, 3],
            "Y": [1, 1, 1, 1, 0, 0, 0, 0],
            "Z": [0, 0, 1, 1, 0, 0, 1, 1]
        })

        metric = metrics.Sum("X")
        output = core.Analyze(data).split_by(["Y",
                                              "Z"]).calculate(metric).run()

        correct = pd.DataFrame({
            "sum(X)": [1, 5, 9, 13],
            "Y": [0, 0, 1, 1],
            "Z": [0, 1, 0, 1]
        })
        correct = correct.set_index(["Y", "Z"])

        self.assertTrue(output.equals(correct))
Ejemplo n.º 18
0
    def testSortFalse(self):
        data = pd.DataFrame({
            "X": [6, 5, 4, 7, 0, 1, 2, 3],
            "Y": [1, 1, 1, 1, 0, 0, 0, 0],
            "Z": [1, 0, 0, 1, 0, 0, 1, 1]
        })

        metric = metrics.Sum("X")
        output = core.Analyze(data).split_by(
            ["Y", "Z"]).calculate(metric).run(sort=False)

        correct = pd.DataFrame({
            "sum(X)": [13, 9, 1, 5],
            "Y": [1, 1, 0, 0],
            "Z": [1, 0, 0, 1]
        })
        correct = correct.set_index(["Y", "Z"])

        self.assertTrue(output.equals(correct))
Ejemplo n.º 19
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))
Ejemplo n.º 20
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))
Ejemplo n.º 21
0
    def testFiftyCIs(self):
        data = pd.DataFrame({"X": range(11)})

        metric = metrics.Sum("X")
        se_method = standard_errors.Jackknife(confidence=0.50)
        output = core.Analyze(data).with_standard_errors(se_method).calculate(
            metric).run()

        multiplier = scipy.stats.t.ppf(0.75, 10)
        correct_sd = 10.0

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

        correct = pd.DataFrame(np.array(
            [[correct_mean, correct_lower, correct_upper]]),
                               columns=("sum(X)", "sum(X) Jackknife CI-lower",
                                        "sum(X) Jackknife CI-upper"))

        self.assertTrue(output.equals(correct))
Ejemplo n.º 22
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))