def testCovariance(self): vla = tfd.VectorLaplaceDiag(loc=tf.zeros([2, 3], dtype=tf.float32), validate_args=True) self.assertAllClose(2. * np.diag(np.ones([3], dtype=np.float32)), self.evaluate(vla.covariance())) vla = tfd.VectorLaplaceDiag(loc=tf.zeros([3], dtype=tf.float32), scale_identity_multiplier=[3., 2.], validate_args=True) self.assertAllEqual([2], vla.batch_shape) self.assertAllEqual([3], vla.event_shape) self.assertAllClose( 2. * np.array([[[3., 0, 0], [0, 3, 0], [0, 0, 3]], [[2, 0, 0], [0, 2, 0], [0, 0, 2]]])**2., self.evaluate(vla.covariance())) vla = tfd.VectorLaplaceDiag(loc=tf.zeros([3], dtype=tf.float32), scale_diag=[[3., 2, 1], [4, 5, 6]], validate_args=True) self.assertAllEqual([2], vla.batch_shape) self.assertAllEqual([3], vla.event_shape) self.assertAllClose( 2. * np.array([[[3., 0, 0], [0, 2, 0], [0, 0, 1]], [[4, 0, 0], [0, 5, 0], [0, 0, 6]]])**2., self.evaluate(vla.covariance()))
def testVariance(self): vla = tfd.VectorLaplaceDiag(loc=tf.zeros([2, 3], dtype=tf.float32)) self.assertAllClose(2. * np.ones([3], dtype=np.float32), self.evaluate(vla.variance())) vla = tfd.VectorLaplaceDiag(loc=tf.zeros([3], dtype=tf.float32), scale_identity_multiplier=[3., 2.]) self.assertAllClose(2. * np.array([[3., 3, 3], [2, 2, 2]])**2., self.evaluate(vla.variance())) vla = tfd.VectorLaplaceDiag(loc=tf.zeros([3], dtype=tf.float32), scale_diag=[[3., 2, 1], [4, 5, 6]]) self.assertAllClose(2. * np.array([[3., 2, 1], [4, 5, 6]])**2., self.evaluate(vla.variance()))
def test_passing_in_laplace_plus_defaults_is_same_as_laplace(self): d = 10 scale_diag = rng.rand(d) + np.float64(1.2) loc = rng.randn(d) vlap = tfd.VectorLaplaceDiag(loc=loc, scale_diag=scale_diag, validate_args=True) sasvlap = tfd.VectorSinhArcsinhDiag(loc=loc, scale_diag=scale_diag, distribution=tfd.Laplace( np.float64(0.), np.float64(1.)), validate_args=True) x = rng.randn(5, d) vlap_pdf, sasvlap_pdf = self.evaluate([vlap.prob(x), sasvlap.prob(x)]) self.assertAllClose(vlap_pdf, sasvlap_pdf) vlap_samps, sasvlap_samps = self.evaluate([ vlap.sample(10000, seed=tfp_test_util.test_seed()), sasvlap.sample(10000, seed=tfp_test_util.test_seed()) ]) self.assertAllClose(loc, sasvlap_samps.mean(axis=0), atol=0.1) self.assertAllClose(vlap_samps.mean(axis=0), sasvlap_samps.mean(axis=0), atol=0.1) self.assertAllClose(vlap_samps.std(axis=0), sasvlap_samps.std(axis=0), atol=0.1)
def testStddev(self): vla = tfd.VectorLaplaceDiag(loc=tf.zeros([2, 3], dtype=tf.float32)) self.assertAllClose( np.sqrt(2) * np.ones([3], dtype=np.float32), self.evaluate(vla.stddev())) vla = tfd.VectorLaplaceDiag(loc=tf.zeros([3], dtype=tf.float32), scale_identity_multiplier=[3., 2.]) self.assertAllClose( np.sqrt(2) * np.array([[3., 3, 3], [2, 2, 2]]), self.evaluate(vla.stddev())) vla = tfd.VectorLaplaceDiag(loc=tf.zeros([3], dtype=tf.float32), scale_diag=[[3., 2, 1], [4, 5, 6]]) self.assertAllClose( np.sqrt(2) * np.array([[3., 2, 1], [4, 5, 6]]), self.evaluate(vla.stddev()))
def testSample(self): mu = np.array([-1., 1]) diag = np.array([1., -2]) dist = tfd.VectorLaplaceDiag(mu, diag, validate_args=True) seed = test_util.test_seed() samps = self.evaluate(dist.sample(int(2e4), seed=seed)) cov_mat = 2. * self.evaluate(tf.linalg.diag(diag))**2 self.assertAllClose(mu, samps.mean(axis=0), atol=0., rtol=0.10) self.assertAllClose(cov_mat, np.cov(samps.T), atol=0.15, rtol=0.10)
def testSample(self): mu = [-1., 1] diag = [1., -2] dist = tfd.VectorLaplaceDiag(mu, diag, validate_args=True) seed = tfp_test_util.test_seed(hardcoded_seed=0, set_eager_seed=False) samps = self.evaluate(dist.sample(int(1e4), seed=seed)) cov_mat = 2. * self.evaluate(tf.linalg.diag(diag))**2 self.assertAllClose(mu, samps.mean(axis=0), atol=0., rtol=0.05) self.assertAllClose(cov_mat, np.cov(samps.T), atol=0.05, rtol=0.05)
def testDistWithBatchShapeOneThenTransformedThroughSoftplus(self): # This complex combination of events resulted in a loss of static shape # information when tf.get_static_value(self._needs_rotation) was # being used incorrectly (resulting in always rotating). # Batch shape = [1], event shape = [3] mu = tf.zeros((1, 3)) diag = tf.ones((1, 3)) base_dist = tfd.VectorLaplaceDiag(mu, diag, validate_args=True) dist = tfd.TransformedDistribution( base_dist, validate_args=True, bijector=tfp.bijectors.Softplus()) samps = dist.sample(5) # Shape [5, 1, 3]. self.assertAllEqual([5, 1], dist.log_prob(samps).shape)
def testSampleWithBroadcastScale(self): # mu corresponds to a 2-batch of 3-variate normals mu = np.zeros([2, 3]) # diag corresponds to no batches of 3-variate normals diag = np.ones([3]) dist = tfd.VectorLaplaceDiag(mu, diag, validate_args=True) mean = dist.mean() self.assertAllEqual([2, 3], mean.shape) self.assertAllClose(mu, self.evaluate(mean)) n = int(1e4) samps = self.evaluate(dist.sample(n, seed=tfp_test_util.test_seed())) cov_mat = 2. * self.evaluate(tf.linalg.diag(diag))**2 sample_cov = np.matmul( samps.transpose([1, 2, 0]), samps.transpose([1, 0, 2])) / n self.assertAllClose(mu, samps.mean(axis=0), atol=0.10, rtol=0.05) self.assertAllClose([cov_mat, cov_mat], sample_cov, atol=0.10, rtol=0.05)
def testSingularScaleRaises(self): mu = [-1., 1] diag = [1., 0] dist = tfd.VectorLaplaceDiag(mu, diag, validate_args=True) with self.assertRaisesOpError("Singular"): self.evaluate(dist.sample(seed=test_util.test_seed()))
def testMeanWithBroadcastLoc(self): mu = [-1.] diag = [1., -5] dist = tfd.VectorLaplaceDiag(mu, diag, validate_args=True) self.assertAllEqual([-1., -1.], self.evaluate(dist.mean()))
def testMean(self): mu = [-1., 1] diag = [1., -5] dist = tfd.VectorLaplaceDiag(mu, diag, validate_args=True) self.assertAllEqual(mu, self.evaluate(dist.mean()))
def testVectorParams(self): mu = [-1.] diag = [-5.] dist = tfd.VectorLaplaceDiag(mu, diag, validate_args=True) self.assertAllEqual([3, 1], dist.sample(3, seed=test_util.test_seed()).shape)
def testScalarParams(self): mu = -1. diag = -5. with self.assertRaisesRegexp(ValueError, "at least 1 dimension"): tfd.VectorLaplaceDiag(mu, diag)