def testBetaBetaKL(self):
        with self.test_session() as sess:
            for shape in [(10, ), (4, 5)]:
                a1 = 6.0 * np.random.random(size=shape) + 1e-4
                b1 = 6.0 * np.random.random(size=shape) + 1e-4
                a2 = 6.0 * np.random.random(size=shape) + 1e-4
                b2 = 6.0 * np.random.random(size=shape) + 1e-4
                # Take inverse softplus of values to test BetaWithSoftplusAB
                a1_sp = np.log(np.exp(a1) - 1.0)
                b1_sp = np.log(np.exp(b1) - 1.0)
                a2_sp = np.log(np.exp(a2) - 1.0)
                b2_sp = np.log(np.exp(b2) - 1.0)

                d1 = beta_lib.Beta(a=a1, b=b1)
                d2 = beta_lib.Beta(a=a2, b=b2)
                d1_sp = beta_lib.BetaWithSoftplusAB(a=a1_sp, b=b1_sp)
                d2_sp = beta_lib.BetaWithSoftplusAB(a=a2_sp, b=b2_sp)

                kl_expected = (special.betaln(a2, b2) -
                               special.betaln(a1, b1) +
                               (a1 - a2) * special.digamma(a1) +
                               (b1 - b2) * special.digamma(b1) +
                               (a2 - a1 + b2 - b1) * special.digamma(a1 + b1))

                for dist1 in [d1, d1_sp]:
                    for dist2 in [d2, d2_sp]:
                        kl = kullback_leibler.kl(dist1, dist2)
                        kl_val = sess.run(kl)
                        self.assertEqual(kl.get_shape(), shape)
                        self.assertAllClose(kl_val, kl_expected)

                # Make sure KL(d1||d1) is 0
                kl_same = sess.run(kullback_leibler.kl(d1, d1))
                self.assertAllClose(kl_same, np.zeros_like(kl_expected))
Пример #2
0
    def testBetaModeInvalid(self):
        with session.Session():
            a = np.array([1., 2, 3])
            b = np.array([2., 4, 1.2])
            dist = beta_lib.Beta(a, b, allow_nan_stats=False)
            with self.assertRaisesOpError("Condition x < y.*"):
                dist.mode().eval()

            a = np.array([2., 2, 3])
            b = np.array([1., 4, 1.2])
            dist = beta_lib.Beta(a, b, allow_nan_stats=False)
            with self.assertRaisesOpError("Condition x < y.*"):
                dist.mode().eval()
    def testBetaSampleMultipleTimes(self):
        with self.test_session():
            a_val = 1.
            b_val = 2.
            n_val = 100

            random_seed.set_random_seed(654321)
            beta1 = beta_lib.Beta(a=a_val, b=b_val, name="beta1")
            samples1 = beta1.sample(n_val, seed=123456).eval()

            random_seed.set_random_seed(654321)
            beta2 = beta_lib.Beta(a=a_val, b=b_val, name="beta2")
            samples2 = beta2.sample(n_val, seed=123456).eval()

            self.assertAllClose(samples1, samples2)
Пример #4
0
 def testBetaProperty(self):
     a = [[1., 2, 3]]
     b = [[2., 4, 3]]
     with self.test_session():
         dist = beta_lib.Beta(a, b)
         self.assertEqual([1, 3], dist.concentration0.get_shape())
         self.assertAllClose(b, dist.concentration0.eval())
 def testAlphaProperty(self):
     a = [[1., 2, 3]]
     b = [[2., 4, 3]]
     with self.test_session():
         dist = beta_lib.Beta(a, b)
         self.assertEqual([1, 3], dist.a.get_shape())
         self.assertAllClose(a, dist.a.eval())
Пример #6
0
 def testBetaVariance(self):
     with session.Session():
         a = [1., 2, 3]
         b = [2., 4, 1.2]
         expected_variance = stats.beta.var(a, b)
         dist = beta_lib.Beta(a, b)
         self.assertEqual(dist.variance().get_shape(), (3, ))
         self.assertAllClose(expected_variance, dist.variance().eval())
Пример #7
0
 def testPdfXStretchedInBroadcastWhenLowerRank(self):
     with self.test_session():
         a = [[1., 2], [2., 3]]
         b = [[1., 2], [2., 3]]
         x = [.5, .5]
         pdf = beta_lib.Beta(a, b).prob(x)
         self.assertAllClose([[1., 3. / 2], [3. / 2, 15. / 8]], pdf.eval())
         self.assertEqual((2, 2), pdf.get_shape())
Пример #8
0
 def testPdfAlphaStretchedInBroadcastWhenLowerRank(self):
     with self.test_session():
         a = [1., 2]
         b = [1., 2]
         x = [[.5, .5], [.2, .8]]
         pdf = beta_lib.Beta(a, b).prob(x)
         self.assertAllClose([[1., 3. / 2], [1., 24. / 25]], pdf.eval())
         self.assertEqual((2, 2), pdf.get_shape())
Пример #9
0
 def testBetaEntropy(self):
     with session.Session():
         a = [1., 2, 3]
         b = [2., 4, 1.2]
         expected_entropy = stats.beta.entropy(a, b)
         dist = beta_lib.Beta(a, b)
         self.assertEqual(dist.entropy().get_shape(), (3, ))
         self.assertAllClose(expected_entropy, dist.entropy().eval())
Пример #10
0
 def testBetaMode(self):
     with session.Session():
         a = np.array([1.1, 2, 3])
         b = np.array([2., 4, 1.2])
         expected_mode = (a - 1) / (a + b - 2)
         dist = beta_lib.Beta(a, b)
         self.assertEqual(dist.mode().get_shape(), (3, ))
         self.assertAllClose(expected_mode, dist.mode().eval())
Пример #11
0
 def testBetaMean(self):
     with session.Session():
         a = [1., 2, 3]
         b = [2., 4, 1.2]
         expected_mean = stats.beta.mean(a, b)
         dist = beta_lib.Beta(a, b)
         self.assertEqual(dist.mean().get_shape(), (3, ))
         self.assertAllClose(expected_mean, dist.mean().eval())
Пример #12
0
 def testPdfAlphaStretchedInBroadcastWhenSameRank(self):
     with self.test_session():
         a = [[1., 2]]
         b = [[1., 2]]
         x = [[.5, .5], [.3, .7]]
         dist = beta_lib.Beta(a, b)
         pdf = dist.prob(x)
         self.assertAllClose([[1., 3. / 2], [1., 63. / 50]], pdf.eval())
         self.assertEqual((2, 2), pdf.get_shape())
Пример #13
0
 def testPdfTwoBatchesNontrivialX(self):
     with self.test_session():
         a = [1., 2]
         b = [1., 2]
         x = [.3, .7]
         dist = beta_lib.Beta(a, b)
         pdf = dist.prob(x)
         self.assertAllClose([1, 63. / 50], pdf.eval())
         self.assertEqual((2, ), pdf.get_shape())
Пример #14
0
 def testPdfTwoBatches(self):
     with self.test_session():
         a = [1., 2]
         b = [1., 2]
         x = [.5, .5]
         dist = beta_lib.Beta(a, b)
         pdf = dist.prob(x)
         self.assertAllClose([1., 3. / 2], pdf.eval())
         self.assertEqual((2, ), pdf.get_shape())
Пример #15
0
 def testSimpleShapes(self):
     with self.test_session():
         a = np.random.rand(3)
         b = np.random.rand(3)
         dist = beta_lib.Beta(a, b)
         self.assertAllEqual([], dist.event_shape_tensor().eval())
         self.assertAllEqual([3], dist.batch_shape_tensor().eval())
         self.assertEqual(tensor_shape.TensorShape([]), dist.event_shape)
         self.assertEqual(tensor_shape.TensorShape([3]), dist.batch_shape)
Пример #16
0
 def testComplexShapesBroadcast(self):
     with self.test_session():
         a = np.random.rand(3, 2, 2)
         b = np.random.rand(2, 2)
         dist = beta_lib.Beta(a, b)
         self.assertAllEqual([], dist.event_shape_tensor().eval())
         self.assertAllEqual([3, 2, 2], dist.batch_shape_tensor().eval())
         self.assertEqual(tensor_shape.TensorShape([]), dist.event_shape)
         self.assertEqual(tensor_shape.TensorShape([3, 2, 2]),
                          dist.batch_shape)
Пример #17
0
 def testPdfUniformZeroBatch(self):
     with self.test_session():
         # This is equivalent to a uniform distribution
         a = 1.
         b = 1.
         x = np.array([.1, .2, .3, .5, .8], dtype=np.float32)
         dist = beta_lib.Beta(a, b)
         pdf = dist.prob(x)
         self.assertAllClose([1.] * 5, pdf.eval())
         self.assertEqual((5, ), pdf.get_shape())
Пример #18
0
    def testBetaModeEnableAllowNanStats(self):
        with session.Session():
            a = np.array([1., 2, 3])
            b = np.array([2., 4, 1.2])
            dist = beta_lib.Beta(a, b, allow_nan_stats=True)

            expected_mode = (a - 1) / (a + b - 2)
            expected_mode[0] = np.nan
            self.assertEqual((3, ), dist.mode().get_shape())
            self.assertAllClose(expected_mode, dist.mode().eval())

            a = np.array([2., 2, 3])
            b = np.array([1., 4, 1.2])
            dist = beta_lib.Beta(a, b, allow_nan_stats=True)

            expected_mode = (a - 1) / (a + b - 2)
            expected_mode[0] = np.nan
            self.assertEqual((3, ), dist.mode().get_shape())
            self.assertAllClose(expected_mode, dist.mode().eval())
Пример #19
0
 def testBetaLogCdf(self):
   with self.test_session():
     shape = (30, 40, 50)
     for dt in (np.float32, np.float64):
       a = 10. * np.random.random(shape).astype(dt)
       b = 10. * np.random.random(shape).astype(dt)
       x = np.random.random(shape).astype(dt)
       actual = math_ops.exp(beta_lib.Beta(a, b).log_cdf(x)).eval()
       self.assertAllEqual(np.ones(shape, dtype=np.bool), 0. <= x)
       self.assertAllEqual(np.ones(shape, dtype=np.bool), 1. >= x)
       self.assertAllClose(stats.beta.cdf(x, a, b), actual, rtol=1e-4, atol=0)
Пример #20
0
 def testBetaSampleMultidimensional(self):
     with self.test_session():
         a = np.random.rand(3, 2, 2).astype(np.float32)
         b = np.random.rand(3, 2, 2).astype(np.float32)
         beta = beta_lib.Beta(a, b)
         n = constant_op.constant(100000)
         samples = beta.sample(n)
         sample_values = samples.eval()
         self.assertEqual(sample_values.shape, (100000, 3, 2, 2))
         self.assertFalse(np.any(sample_values < 0.0))
         self.assertAllClose(sample_values[:, 1, :].mean(axis=0),
                             stats.beta.mean(a, b)[1, :],
                             atol=1e-1)
Пример #21
0
 def testPdfXProper(self):
     a = [[1., 2, 3]]
     b = [[2., 4, 3]]
     with self.test_session():
         dist = beta_lib.Beta(a, b, validate_args=True)
         dist.prob([.1, .3, .6]).eval()
         dist.prob([.2, .3, .5]).eval()
         # Either condition can trigger.
         with self.assertRaisesOpError("sample must be positive"):
             dist.prob([-1., 0.1, 0.5]).eval()
         with self.assertRaisesOpError("sample must be positive"):
             dist.prob([0., 0.1, 0.5]).eval()
         with self.assertRaisesOpError("sample must be no larger than `1`"):
             dist.prob([.1, .2, 1.2]).eval()
Пример #22
0
 def testPdfXProper(self):
     a = [[1., 2, 3]]
     b = [[2., 4, 3]]
     with self.test_session():
         dist = beta_lib.Beta(a, b, validate_args=True)
         dist.pdf([.1, .3, .6]).eval()
         dist.pdf([.2, .3, .5]).eval()
         # Either condition can trigger.
         with self.assertRaisesOpError(
                 "(Condition x > 0.*|Condition x < y.*)"):
             dist.pdf([-1., 1, 1]).eval()
         with self.assertRaisesOpError("Condition x.*"):
             dist.pdf([0., 1, 1]).eval()
         with self.assertRaisesOpError("Condition x < y.*"):
             dist.pdf([.1, .2, 1.2]).eval()
Пример #23
0
 def testBetaSample(self):
   with self.test_session():
     a = 1.
     b = 2.
     beta = beta_lib.Beta(a, b)
     n = constant_op.constant(100000)
     samples = beta.sample(n)
     sample_values = samples.eval()
     self.assertEqual(sample_values.shape, (100000,))
     self.assertFalse(np.any(sample_values < 0.0))
     self.assertLess(
         stats.kstest(
             # Beta is a univariate distribution.
             sample_values,
             stats.beta(a=1., b=2.).cdf)[0],
         0.01)
     # The standard error of the sample mean is 1 / (sqrt(18 * n))
     self.assertAllClose(
         sample_values.mean(axis=0), stats.beta.mean(a, b), atol=1e-2)
     self.assertAllClose(
         np.cov(sample_values, rowvar=0), stats.beta.var(a, b), atol=1e-1)