Пример #1
0
 def test_shapes(self):
     """Tests the sample shapes."""
     sample_no_batch = self.evaluate(
         random_ops.multivariate_normal([2, 4], mean=[0.2, 0.1]))
     self.assertEqual(sample_no_batch.shape, (2, 4, 2))
     sample_batch = self.evaluate(
         random_ops.multivariate_normal([2, 4],
                                        mean=[[0.2, 0.1], [0., -0.1],
                                              [0., 0.1]]))
     self.assertEqual(sample_batch.shape, (2, 4, 3, 2))
Пример #2
0
    def test_general_mean_covariance(self):
        """Tests that the sample is correctly generated for general params."""
        mean = np.array([[1.0, 0.1], [0.1, 1.0]])
        covar = np.array([
            [[0.9, -0.1], [-0.1, 1.0]],
            [[1.1, -0.3], [-0.3, 0.6]],
        ])
        size = 30000
        sample = self.evaluate(
            random_ops.multivariate_normal([size],
                                           mean=mean,
                                           covariance_matrix=covar,
                                           seed=4567))

        np.testing.assert_array_equal(sample.shape, [size, 2, 2])
        np.testing.assert_array_almost_equal(np.mean(sample, axis=0),
                                             mean,
                                             decimal=1)
        np.testing.assert_array_almost_equal(np.cov(sample[:, 0, :],
                                                    rowvar=False),
                                             covar[0],
                                             decimal=1)
        np.testing.assert_array_almost_equal(np.cov(sample[:, 1, :],
                                                    rowvar=False),
                                             covar[1],
                                             decimal=1)
Пример #3
0
    def test_mean_and_scale(self):
        """Tests sample for scale specification."""
        mean = np.array([[1.0, 0.1], [0.1, 1.0]])
        scale = np.array([[0.4, -0.1], [0.22, 1.38]])

        covariance = np.matmul(scale, scale.transpose())
        size = 30000
        sample = self.evaluate(
            random_ops.multivariate_normal([size],
                                           mean=mean,
                                           scale_matrix=scale,
                                           seed=7534))

        np.testing.assert_array_equal(sample.shape, [size, 2, 2])
        np.testing.assert_array_almost_equal(np.mean(sample, axis=0),
                                             mean,
                                             decimal=1)
        np.testing.assert_array_almost_equal(np.cov(sample[:, 0, :],
                                                    rowvar=False),
                                             covariance,
                                             decimal=1)
        np.testing.assert_array_almost_equal(np.cov(sample[:, 1, :],
                                                    rowvar=False),
                                             covariance,
                                             decimal=1)
Пример #4
0
 def test_mean_default(self):
     """Tests that the default value of mean is 0."""
     covar = np.array([[1.0, 0.1], [0.1, 1.0]])
     sample = self.evaluate(
         random_ops.multivariate_normal([40000],
                                        covariance_matrix=covar,
                                        seed=1234))
     np.testing.assert_array_equal(sample.shape, [40000, 2])
     self.assertArrayNear(np.mean(sample, axis=0), [0.0, 0.0], 1e-2)
     self.assertArrayNear(
         np.cov(sample, rowvar=False).reshape([-1]), covar.reshape([-1]),
         2e-2)
Пример #5
0
    def test_covariance_default(self):
        """Tests that the default value of the covariance matrix is identity."""
        mean = np.array([[1.0, 0.1], [0.1, 1.0]])
        sample = self.evaluate(
            random_ops.multivariate_normal([10000], mean=mean))

        np.testing.assert_array_equal(sample.shape, [10000, 2, 2])
        np.testing.assert_array_almost_equal(np.mean(sample, axis=0),
                                             mean,
                                             decimal=1)
        np.testing.assert_array_almost_equal(np.cov(sample[:, 0, :],
                                                    rowvar=False),
                                             np.eye(2),
                                             decimal=1)
        np.testing.assert_array_almost_equal(np.cov(sample[:, 1, :],
                                                    rowvar=False),
                                             np.eye(2),
                                             decimal=1)