def testKumaraswamyModeInvalid(self): with session.Session(): a = np.array([1., 2, 3]) b = np.array([2., 4, 1.2]) dist = kumaraswamy_lib.Kumaraswamy(a, b, allow_nan_stats=False) with self.assertRaisesOpError( "Mode undefined for concentration1 <= 1."): dist.mode().eval() a = np.array([2., 2, 3]) b = np.array([1., 4, 1.2]) dist = kumaraswamy_lib.Kumaraswamy(a, b, allow_nan_stats=False) with self.assertRaisesOpError( "Mode undefined for concentration0 <= 1."): dist.mode().eval()
def testBProperty(self): a = [[1., 2, 3]] b = [[2., 4, 3]] with self.cached_session(): dist = kumaraswamy_lib.Kumaraswamy(a, b) self.assertEqual([1, 3], dist.concentration0.get_shape()) self.assertAllClose(b, dist.concentration0.eval())
def testKumaraswamySample(self): with self.cached_session(): a = 1. b = 2. kumaraswamy = kumaraswamy_lib.Kumaraswamy(a, b) n = constant_op.constant(100000) samples = kumaraswamy.sample(n) sample_values = samples.eval() self.assertEqual(sample_values.shape, (100000, )) self.assertFalse(np.any(sample_values < 0.0)) if not stats: return self.assertLess( stats.kstest( # Kumaraswamy is a univariate distribution. sample_values, lambda x: _kumaraswamy_cdf(1., 2., x))[0], 0.01) # The standard error of the sample mean is 1 / (sqrt(18 * n)) expected_mean = _kumaraswamy_moment(a, b, 1) self.assertAllClose(sample_values.mean(axis=0), expected_mean, atol=1e-2) expected_variance = _kumaraswamy_moment( a, b, 2) - _kumaraswamy_moment(a, b, 1)**2 self.assertAllClose(np.cov(sample_values, rowvar=0), expected_variance, atol=1e-1)
def testKumaraswamyMode(self): with session.Session(): a = np.array([1.1, 2, 3]) b = np.array([2., 4, 1.2]) expected_mode = _kumaraswamy_mode(a, b) dist = kumaraswamy_lib.Kumaraswamy(a, b) self.assertEqual(dist.mode().get_shape(), (3, )) self.assertAllClose(expected_mode, dist.mode().eval())
def testSimpleShapes(self): with self.cached_session(): a = np.random.rand(3) b = np.random.rand(3) dist = kumaraswamy_lib.Kumaraswamy(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)
def testPdfXStretchedInBroadcastWhenLowerRank(self): with self.cached_session(): a = [[1., 2], [2., 3]] b = [[1., 2], [2., 3]] x = [.5, .5] pdf = kumaraswamy_lib.Kumaraswamy(a, b).prob(x) expected_pdf = _kumaraswamy_pdf(a, b, x) self.assertAllClose(expected_pdf, pdf.eval()) self.assertEqual((2, 2), pdf.get_shape())
def testComplexShapesBroadcast(self): with self.cached_session(): a = np.random.rand(3, 2, 2) b = np.random.rand(2, 2) dist = kumaraswamy_lib.Kumaraswamy(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)
def testKumaraswamySampleMultipleTimes(self): with self.cached_session(): a_val = 1. b_val = 2. n_val = 100 random_seed.set_random_seed(654321) kumaraswamy1 = kumaraswamy_lib.Kumaraswamy(concentration1=a_val, concentration0=b_val, name="kumaraswamy1") samples1 = kumaraswamy1.sample(n_val, seed=123456).eval() random_seed.set_random_seed(654321) kumaraswamy2 = kumaraswamy_lib.Kumaraswamy(concentration1=a_val, concentration0=b_val, name="kumaraswamy2") samples2 = kumaraswamy2.sample(n_val, seed=123456).eval() self.assertAllClose(samples1, samples2)
def testKumaraswamyModeEnableAllowNanStats(self): with session.Session(): a = np.array([1., 2, 3]) b = np.array([2., 4, 1.2]) dist = kumaraswamy_lib.Kumaraswamy(a, b, allow_nan_stats=True) expected_mode = _kumaraswamy_mode(a, b) 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 = kumaraswamy_lib.Kumaraswamy(a, b, allow_nan_stats=True) expected_mode = _kumaraswamy_mode(a, b) expected_mode[0] = np.nan self.assertEqual((3, ), dist.mode().get_shape()) self.assertAllClose(expected_mode, dist.mode().eval())
def testKumaraswamyMean(self): with session.Session(): a = [1., 2, 3] b = [2., 4, 1.2] dist = kumaraswamy_lib.Kumaraswamy(a, b) self.assertEqual(dist.mean().get_shape(), (3, )) if not stats: return expected_mean = _kumaraswamy_moment(a, b, 1) self.assertAllClose(expected_mean, dist.mean().eval())
def testPdfAStretchedInBroadcastWhenSameRank(self): with self.cached_session(): a = [[1., 2]] b = [[1., 2]] x = [[.5, .5], [.3, .7]] dist = kumaraswamy_lib.Kumaraswamy(a, b) pdf = dist.prob(x) expected_pdf = _kumaraswamy_pdf(a, b, x) self.assertAllClose(expected_pdf, pdf.eval()) self.assertEqual((2, 2), pdf.get_shape())
def testPdfTwoBatchesNontrivialX(self): with self.cached_session(): a = [1., 2] b = [1., 2] x = [.3, .7] dist = kumaraswamy_lib.Kumaraswamy(a, b) pdf = dist.prob(x) expected_pdf = _kumaraswamy_pdf(a, b, x) self.assertAllClose(expected_pdf, pdf.eval()) self.assertEqual((2, ), pdf.get_shape())
def testKumaraswamyVariance(self): with session.Session(): a = [1., 2, 3] b = [2., 4, 1.2] dist = kumaraswamy_lib.Kumaraswamy(a, b) self.assertEqual(dist.variance().get_shape(), (3, )) if not stats: return expected_variance = _kumaraswamy_moment( a, b, 2) - _kumaraswamy_moment(a, b, 1)**2 self.assertAllClose(expected_variance, dist.variance().eval())
def testPdfUniformZeroBatch(self): with self.cached_session(): # This is equivalent to a uniform distribution a = 1. b = 1. x = np.array([.1, .2, .3, .5, .8], dtype=np.float32) dist = kumaraswamy_lib.Kumaraswamy(a, b) pdf = dist.prob(x) expected_pdf = _kumaraswamy_pdf(a, b, x) self.assertAllClose(expected_pdf, pdf.eval()) self.assertEqual((5, ), pdf.get_shape())
def testKumaraswamyEntropy(self): with session.Session(): a = np.array([1., 2, 3]) b = np.array([2., 4, 1.2]) dist = kumaraswamy_lib.Kumaraswamy(a, b) self.assertEqual(dist.entropy().get_shape(), (3, )) if not stats: return expected_entropy = ( 1 - 1. / a) + (1 - 1. / b) * _harmonic_number(b) + np.log( a * b) self.assertAllClose(expected_entropy, dist.entropy().eval())
def testPdfXProper(self): a = [[1., 2, 3]] b = [[2., 4, 3]] with self.cached_session(): dist = kumaraswamy_lib.Kumaraswamy(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 non-negative"): dist.prob([-1., 0.1, 0.5]).eval() with self.assertRaisesOpError("sample must be no larger than `1`"): dist.prob([.1, .2, 1.2]).eval()
def testKumaraswamySampleMultidimensional(self): with self.cached_session(): a = np.random.rand(3, 2, 2).astype(np.float32) b = np.random.rand(3, 2, 2).astype(np.float32) kumaraswamy = kumaraswamy_lib.Kumaraswamy(a, b) n = constant_op.constant(100000) samples = kumaraswamy.sample(n) sample_values = samples.eval() self.assertEqual(sample_values.shape, (100000, 3, 2, 2)) self.assertFalse(np.any(sample_values < 0.0)) if not stats: return self.assertAllClose(sample_values[:, 1, :].mean(axis=0), _kumaraswamy_moment(a, b, 1)[1, :], atol=1e-1)
def testKumaraswamyCdf(self): with self.cached_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 = kumaraswamy_lib.Kumaraswamy(a, b).cdf(x).eval() self.assertAllEqual(np.ones(shape, dtype=np.bool), 0. <= x) self.assertAllEqual(np.ones(shape, dtype=np.bool), 1. >= x) if not stats: return self.assertAllClose(_kumaraswamy_cdf(a, b, x), actual, rtol=1e-4, atol=0)