def test_exponential_poisson_N(self): kgpm = KInflatedGammaPoissonModel([ReachPoint([100], [100])]) self.assertAlmostEqual(kgpm._exponential_poisson_N(19971, 7992), 9769, delta=1) self.assertAlmostEqual(kgpm._exponential_poisson_N(20000, 10000), 13333.33, delta=1)
def test_exponential_poisson_N_from_beta(self): kgpm = KInflatedGammaPoissonModel([ReachPoint([100], [100])]) self.assertAlmostEqual(kgpm._exponential_poisson_N_from_beta( 19971, 7992, 2.41), 9416.0, delta=1) self.assertAlmostEqual( kgpm._exponential_poisson_N_from_beta(30000, 10000, 2), 10000.0)
def test_exponential_poisson_reach(self): kgpm = KInflatedGammaPoissonModel([ReachPoint([100], [100])]) self.assertAlmostEqual( kgpm._exponential_poisson_reach(20000, 10000, 3.0), 8000.0) self.assertAlmostEqual(kgpm._exponential_poisson_reach( 19971, 12560, 26.19), 7888.78, delta=1)
def test_fit_frequency_one(self): data = HeterogeneousImpressionGenerator(10000, gamma_shape=1.0, gamma_scale=3)() publisher = PublisherData(FixedPriceGenerator(0.1)(data)) dataset = DataSet([publisher], f"Exponential-poisson") spend_fraction = 0.5 spend = dataset._data[0].max_spend * spend_fraction point = dataset.reach_by_spend([spend], max_frequency=1) gm = GoergModel([point]) gm_reach = gm.by_spend([spend]).reach() kgpm = KInflatedGammaPoissonModel([point]) kgpm._fit() kgpm_reach = kgpm.by_spend([spend]).reach() self.assertAlmostEqual(gm_reach, kgpm_reach, delta=1)
def test_fit_exponential_poisson_model(self): p1 = ReachPoint([20000], [10000]) kgpm1 = KInflatedGammaPoissonModel([p1]) N1, dist1 = kgpm1._fit_exponential_poisson_model(p1) self.assertAlmostEqual(N1, 13333.33, delta=1) self.assertAlmostEqual(dist1._alpha, 1.0) self.assertAlmostEqual(dist1._beta, 2.0, delta=0.1) p2 = ReachPoint([19971], [7993, 4815, 2914, 1759, 1011, 604, 355, 214, 122, 75]) kgpm2 = KInflatedGammaPoissonModel([p2]) N2, dist2 = kgpm1._fit_exponential_poisson_model(p2) self.assertAlmostEqual(N2, 8564, delta=1) self.assertAlmostEqual(dist2._alpha, 1.0) self.assertAlmostEqual(dist2._beta, 1.8, delta=0.1)
def test_by_impressions(self, mock_fit_point): mock_fit_point.return_value = ( 10000, KInflatedGammaPoissonDistribution(5.0, 2.0, []), ) # Imax = 25000, N = 10000, alpha = 5, beta = 2 h_training = [7412, 4233, 2014, 842, 320, 112, 37, 11, 2] rp = ReachPoint([15000], h_training, [200.0]) kgpm = KInflatedGammaPoissonModel([rp]) kgpm._fit() rp = kgpm.by_impressions([10000], max_frequency=5) h_expected = np.array([6056, 2629, 925, 283, 78]) h_actual = np.array([int(rp.reach(i)) for i in range(1, 6)]) total_error = np.sum((h_expected - h_actual)**2 / h_expected) self.assertAlmostEqual(rp.spends[0], 133.0, delta=1) for i in range(len(h_actual)): self.assertTrue( (h_actual[i] - h_expected[i])**2 / h_actual[i] < 0.1, f"Discrepancy found at position {i}. " f"Got {h_actual[i]} Expected {h_expected[i]}", )
def test_fit_gamma_poisson(self): # mean = 5, var = 6 N, alpha, beta = 10000, 8, 0.5 data = HeterogeneousImpressionGenerator(10000, gamma_shape=alpha, gamma_scale=beta)() publisher = PublisherData(FixedPriceGenerator(0.1)(data)) dataset = DataSet([publisher], f"Exponential-poisson") spend_fraction = 0.5 spend = dataset._data[0].max_spend * spend_fraction point = dataset.reach_by_spend([spend]) gm = GoergModel([point]) gm_reach = gm.by_spend([spend]).reach() kgpm = KInflatedGammaPoissonModel([point]) kgpm.print_fit_header() kgpm.print_fit("true", 0.0, N, alpha, beta, []) kgpm._fit() kgpm_reach = kgpm.by_spend([spend]).reach() self.assertAlmostEqual(gm_reach, kgpm_reach, delta=100)
def test_exponential_poisson_beta(self): kgpm = KInflatedGammaPoissonModel([ReachPoint([100], [100])]) self.assertAlmostEqual( kgpm._exponential_poisson_beta(30000, 10000, 10000), 2.0) self.assertAlmostEqual( kgpm._exponential_poisson_beta(20000, 10000, 8000), 3.0)