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))
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))
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()
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()
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()
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))
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)
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))
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))
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))
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))
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)
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))