def testMH(self): data = pd.DataFrame({ "Clicks": [1, 3, 2, 3, 1, 2], "Conversions": [1, 0, 1, 2, 1, 1], "Id": [1, 2, 3, 1, 2, 3], "Condition": [0, 0, 0, 1, 1, 1] }) weights = np.ones(6) comparison = comparisons.MH("Condition", 0, "Id") comparison.precalculate_factors(data) metric = metrics.Ratio("Conversions", "Clicks") output = comparison(data, weights, metric).values[0] ka = np.array([2, 1, 1]) kb = np.array([1, 0, 1]) na = np.array([3, 1, 2]) nb = np.array([1, 3, 2]) w = 1 / (na + nb) correct = sum(ka * nb * w) / sum(kb * na * w) self.assertEqual(output, correct)
def testRatioWithWeights(self): df = pd.DataFrame({"X": [1, 1, 1, 2, 3, 4], "Y": [1, 2, 0, 1, 1, 1]}) weights = np.array([1, 1, 1, 2, 1, 1]) metric = metrics.Ratio("X", "Y") output = metric(df, weights) correct = 2.0 self.assertEqual(output, correct)
def testRatioZeroDivideByZero(self): df = pd.DataFrame({"X": [0, 0], "Y": [0, 0]}) weights = np.array([1, 1]) metric = metrics.Ratio("X", "Y") output = metric(df, weights) self.assertTrue(np.isnan(output))
def testRatioPositiveDivideByZero(self): df = pd.DataFrame({"X": [1, 1], "Y": [0, 0]}) weights = np.array([1, 1]) metric = metrics.Ratio("X", "Y") output = metric(df, weights) correct = np.inf self.assertEqual(output, correct)
def testJackknifeRatio(self): data = pd.DataFrame({"X": [1, 2, 3, 4], "Y": [4, 3, 2, 1]}) metric = metrics.Ratio("X", "Y") se_method = standard_errors.Jackknife() output = core.Analyze(data).with_standard_errors(se_method).calculate( metric).run() estimates = np.array([9 / 6, 8 / 7, 7 / 8, 6 / 9]) rss = ((estimates - estimates.mean())**2).sum() se = np.sqrt(rss * 3 / 4) correct = pd.DataFrame([[1.0, se]], columns=("X/Y", "X/Y Jackknife SE")) self.assertTrue(output.equals(correct))
def testMHZeroRateRaisesError(self): data = pd.DataFrame({ "Clicks": [2, 1, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0], "Conversions": [1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0], "Id": [1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3], "Condition": [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] }) weights = np.ones(12) comparison = comparisons.MH("Condition", 0, "Id") comparison.precalculate_factors(data) metric = metrics.Ratio("Conversions", "Clicks") with self.assertRaises(ValueError): comparison(data, weights, metric)