Exemplo n.º 1
0
    def test_pmf_no_inflation(self):
        dist1 = KInflatedGammaPoissonDistribution(1.0, 1.0, [])
        self.assertAlmostEqual(dist1.pmf(1), gamma_poisson_pmf(0, 1.0, 1.0))
        self.assertAlmostEqual(dist1.pmf(2), gamma_poisson_pmf(1, 1.0, 1.0))

        dist2 = KInflatedGammaPoissonDistribution(3.0, 1.0, [])
        self.assertAlmostEqual(dist2.pmf(2), gamma_poisson_pmf(1, 3.0, 1.0))

        dist3 = KInflatedGammaPoissonDistribution(1.0, 4.0, [])
        self.assertAlmostEqual(dist3.pmf(2), gamma_poisson_pmf(1, 1.0, 4.0))
Exemplo n.º 2
0
 def test_knreach_no_inflation(self):
     dist = KInflatedGammaPoissonDistribution(1.0, 1.0, [])
     self.assertAlmostEqual(dist.knreach(1, 1, 0.5), 0.25)
     self.assertAlmostEqual(dist.knreach(2, 1, 0.5), 0)
     self.assertAlmostEqual(dist.knreach(1, 1, 0.5), 1.0 / 4.0)
     self.assertAlmostEqual(dist.knreach(1, np.array([1]), 0.5)[0], 1 / 4)
     self.assertAlmostEqual(
         dist.knreach(1, np.array([1, 2]), 0.5)[0], 1 / 4)
     self.assertAlmostEqual(
         dist.knreach(1, np.array([1, 2]), 0.5)[1], 1 / 8)
     self.assertAlmostEqual(dist.knreach(1, 3, 1 / 3),
                            3 * (1 / 3) * (2 / 3)**2 * (1 / 8))
Exemplo n.º 3
0
 def test_kreach_no_inflation(self):
     dist = KInflatedGammaPoissonDistribution(1.0, 1.0, [])
     self.assertAlmostEqual(dist.kreach([0], 0.5)[0], 1 / 3)
     self.assertAlmostEqual(dist.kreach([0], 1 / 3)[0], 1 / 2)
     self.assertAlmostEqual(dist.kreach([1], 1 / 3)[0], 3 / 8)
     self.assertAlmostEqual(dist.kreach([0, 1, 2], 1 / 3)[0], 1 / 2)
     self.assertAlmostEqual(dist.kreach([0, 1, 2], 1 / 3)[1], 3 / 8)
     self.assertAlmostEqual(dist.kreach([0, 1, 2], 1 / 3)[2], 3 / 32)
Exemplo n.º 4
0
    def test_pmf_with_partial_inflation(self):
        dist1 = KInflatedGammaPoissonDistribution(1.0, 1.0, [0.5])
        self.assertAlmostEqual(dist1.pmf(1), 0.5)

        scale_factor = 0.5 / (1.0 - gamma_poisson_pmf(0, 1.0, 1.0))
        expected = scale_factor * gamma_poisson_pmf(1, 1.0, 1.0)
        self.assertAlmostEqual(dist1.pmf(2), expected)

        dist2 = KInflatedGammaPoissonDistribution(3.0, 1.0, [0.5])
        scale_factor = 0.5 / (1.0 - gamma_poisson_pmf(0, 3.0, 1.0))
        expected = scale_factor * gamma_poisson_pmf(1, 3.0, 1.0)
        self.assertAlmostEqual(dist2.pmf(2), expected)
Exemplo n.º 5
0
 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]}",
         )
Exemplo n.º 6
0
 def test_pmf_with_full_inflation(self):
     dist = KInflatedGammaPoissonDistribution(1.0, 1.0, [0.75, 0.25])
     self.assertAlmostEqual(dist.pmf(1), 0.75)
     self.assertAlmostEqual(dist.pmf(2), 0.25)
     self.assertAlmostEqual(dist.pmf(3), 0.0)
Exemplo n.º 7
0
    def test_expected_value(self):
        dist1 = KInflatedGammaPoissonDistribution(1.0, 1.0, [])
        self.assertAlmostEqual(dist1.expected_value(), 2.0)

        dist2 = KInflatedGammaPoissonDistribution(2.0, 1.0, [])
        self.assertAlmostEqual(dist2.expected_value(), 3.0)

        dist3 = KInflatedGammaPoissonDistribution(1.0, 2.0, [])
        self.assertAlmostEqual(dist3.expected_value(), 3.0)

        dist4 = KInflatedGammaPoissonDistribution(1.0, 2.0, [1.0])
        self.assertAlmostEqual(dist4.expected_value(), 1.0)

        dist5 = KInflatedGammaPoissonDistribution(1.0, 2.0, [0.5, 0.5])
        self.assertAlmostEqual(dist5.expected_value(), 1.5)