Esempio n. 1
0
    def test_dkwm_mean_one_sample_assertion(self):
        rng = np.random.RandomState(seed=0)
        num_samples = 5000

        # Test that the test assertion agrees that the mean of the standard
        # uniform distribution is 0.5.
        samples = rng.uniform(size=num_samples).astype(np.float32)
        self.evaluate(
            st.assert_true_mean_equal_by_dkwm(samples,
                                              0.,
                                              1.,
                                              0.5,
                                              false_fail_rate=1e-6))

        # Test that the test assertion confirms that the mean of the
        # standard uniform distribution is not 0.4.
        with self.assertRaisesOpError("true mean greater than expected"):
            self.evaluate(
                st.assert_true_mean_equal_by_dkwm(samples,
                                                  0.,
                                                  1.,
                                                  0.4,
                                                  false_fail_rate=1e-6))

        # Test that the test assertion confirms that the mean of the
        # standard uniform distribution is not 0.6.
        with self.assertRaisesOpError("true mean smaller than expected"):
            self.evaluate(
                st.assert_true_mean_equal_by_dkwm(samples,
                                                  0.,
                                                  1.,
                                                  0.6,
                                                  false_fail_rate=1e-6))
Esempio n. 2
0
    def testWeibullSample(self):
        concentration = self.dtype(4.)
        scale = self.dtype(1.)
        n = int(100e3)

        weibull = tfd.Weibull(concentration=self.make_input(concentration),
                              scale=self.make_input(scale),
                              validate_args=True)

        samples = weibull.sample(n, seed=test_util.test_seed())
        sample_values = self.evaluate(samples)

        low = self.dtype(0.)
        high = self.dtype(np.inf)

        self.assertAllClose(stats.weibull_min.mean(c=concentration,
                                                   scale=scale,
                                                   loc=0),
                            sample_values.mean(),
                            rtol=0.01)

        self.evaluate(
            st.assert_true_mean_equal_by_dkwm(
                samples,
                low=low,
                high=high,
                expected=weibull.mean(),
                false_fail_rate=self.dtype(1e-6)))

        self.assertAllClose(stats.weibull_min.var(c=concentration,
                                                  scale=scale,
                                                  loc=0),
                            sample_values.var(),
                            rtol=0.01)
Esempio n. 3
0
 def testMean(self, dtype):
     testee_lkj = tfd.LKJ(dimension=3,
                          concentration=dtype([1., 3., 5.]),
                          validate_args=True)
     num_samples = 20000
     results = testee_lkj.sample(sample_shape=[num_samples])
     mean = testee_lkj.mean()
     self.assertEqual(mean.shape, [3, 3, 3])
     check1 = st.assert_true_mean_equal_by_dkwm(samples=results,
                                                low=-1.,
                                                high=1.,
                                                expected=mean,
                                                false_fail_rate=1e-6)
     check2 = assert_util.assert_less(
         st.min_discrepancy_of_true_means_detectable_by_dkwm(
             num_samples,
             low=-1.,
             high=1.,
             # Smaller false fail rate because of different batch sizes between
             # these two checks.
             false_fail_rate=1e-7,
             false_pass_rate=1e-6),
         # 4% relative error
         0.08)
     self.evaluate([check1, check2])
Esempio n. 4
0
    def testTriangularSample(self):
        low = self._dtype([-3.] * 4)
        high = np.arange(7., 11., dtype=self._dtype)
        peak = np.array([0.] * 4, dtype=self._dtype)
        tri = self._create_triangular_dist(low, high, peak)
        num_samples = int(3e6)
        samples = tri.sample(num_samples, seed=tfp_test_util.test_seed())

        detectable_discrepancies = self.evaluate(
            st.min_discrepancy_of_true_means_detectable_by_dkwm(
                num_samples,
                low,
                high,
                false_fail_rate=self._dtype(1e-6),
                false_pass_rate=self._dtype(1e-6)))
        below_threshold = detectable_discrepancies <= 0.05
        self.assertTrue(np.all(below_threshold))

        self.evaluate(
            st.assert_true_mean_equal_by_dkwm(
                samples,
                low=low,
                high=high,
                expected=tri.mean(),
                false_fail_rate=self._dtype(1e-6)))
Esempio n. 5
0
 def testRejection4D(self):
     num_samples = int(1e5)  # Chosen for a small min detectable discrepancy
     det_bounds = np.array([0.0], dtype=np.float32)
     exact_volumes = [four_by_four_volume()]
     (rej_weights, rej_proposal_volume
      ) = corr.correlation_matrix_volume_rejection_samples(det_bounds,
                                                           4,
                                                           [num_samples, 1],
                                                           dtype=np.float32,
                                                           seed=45)
     # shape of rej_weights: [num_samples, 1, 4, 4]
     chk1 = st.assert_true_mean_equal_by_dkwm(rej_weights,
                                              low=0.,
                                              high=rej_proposal_volume,
                                              expected=exact_volumes,
                                              false_fail_rate=1e-6)
     chk2 = tf1.assert_less(
         st.min_discrepancy_of_true_means_detectable_by_dkwm(
             num_samples,
             low=0.,
             high=rej_proposal_volume,
             false_fail_rate=1e-6,
             false_pass_rate=1e-6),
         # Going for about a 10% relative error
         1.1)
     with tf.control_dependencies([chk1, chk2]):
         rej_weights = tf.identity(rej_weights)
     self.evaluate(rej_weights)
Esempio n. 6
0
 def testRejection2D(self):
     num_samples = int(1e5)  # Chosen for a small min detectable discrepancy
     det_bounds = np.array(
         [0.01, 0.02, 0.03, 0.04, 0.05, 0.3, 0.35, 0.4, 0.5],
         dtype=np.float32)
     exact_volumes = two_by_two_volume(det_bounds)
     (rej_weights, rej_proposal_volume
      ) = corr.correlation_matrix_volume_rejection_samples(det_bounds,
                                                           2,
                                                           [num_samples, 9],
                                                           dtype=np.float32,
                                                           seed=43)
     # shape of rej_weights: [num_samples, 9, 2, 2]
     chk1 = st.assert_true_mean_equal_by_dkwm(rej_weights,
                                              low=0.,
                                              high=rej_proposal_volume,
                                              expected=exact_volumes,
                                              false_fail_rate=1e-6)
     chk2 = tf1.assert_less(
         st.min_discrepancy_of_true_means_detectable_by_dkwm(
             num_samples,
             low=0.,
             high=rej_proposal_volume,
             # Correct the false fail rate due to different broadcasting
             false_fail_rate=1.1e-7,
             false_pass_rate=1e-6),
         0.036)
     with tf.control_dependencies([chk1, chk2]):
         rej_weights = tf.identity(rej_weights)
     self.evaluate(rej_weights)
 def VerifyMean(self, dim):
     num_samples = int(7e4)
     uniform = tfp.distributions.SphericalUniform(batch_shape=[2, 1],
                                                  dimension=dim,
                                                  dtype=self.dtype,
                                                  validate_args=True,
                                                  allow_nan_stats=False)
     samples = uniform.sample(num_samples, seed=test_util.test_seed())
     sample_mean = tf.reduce_mean(samples, axis=0)
     true_mean, sample_mean = self.evaluate([uniform.mean(), sample_mean])
     check1 = st.assert_true_mean_equal_by_dkwm(samples=samples,
                                                low=-(1. + 1e-7),
                                                high=1. + 1e-7,
                                                expected=true_mean,
                                                false_fail_rate=1e-6)
     check2 = assert_util.assert_less(
         st.min_discrepancy_of_true_means_detectable_by_dkwm(
             num_samples,
             low=-1.,
             high=1.,
             # Smaller false fail rate because of different batch sizes between
             # these two checks.
             false_fail_rate=1e-7,
             false_pass_rate=1e-6),
         # 4% relative error
         0.08)
     self.evaluate([check1, check2])
 def testBetaQuotientSample(self):
   a = tf.random.uniform(
       shape=[2, 1, 1, 1],
       minval=1., maxval=5., seed=test_util.test_seed(), dtype=self.dtype)
   b = tf.random.uniform(
       shape=[1, 3, 1, 1],
       minval=1., maxval=5., seed=test_util.test_seed(), dtype=self.dtype)
   c = tf.random.uniform(
       shape=[1, 1, 5, 1],
       minval=1., maxval=5., seed=test_util.test_seed(), dtype=self.dtype)
   d = tf.random.uniform(
       shape=[1, 1, 1, 7],
       minval=1., maxval=5., seed=test_util.test_seed(), dtype=self.dtype)
   beta_quotient = tfd.BetaQuotient(a, b, c, d, validate_args=True)
   # TODO(b/179283344): Increase this to 3e5 when CPU-only gamma sampler is
   # fixed.
   n = int(3e4)
   samples = beta_quotient.sample(n, seed=test_util.test_seed())
   sample_values = self.evaluate(samples)
   self.assertEqual(sample_values.shape, (n, 2, 3, 5, 7))
   self.assertFalse(np.any(sample_values < 0.0))
   self.evaluate(
       st.assert_true_mean_equal_by_dkwm(
           samples,
           low=self.dtype(0.),
           high=self.dtype(np.inf),
           expected=beta_quotient.mean(),
           false_fail_rate=self.dtype(1e-6)))
Esempio n. 9
0
  def _testSampleLogProbExact(
      self, concentrations, det_bounds, dim, means,
      num_samples=int(1e5), dtype=np.float32, target_discrepancy=0.1, seed=42):
    # For test methodology see the comment in
    # _testSampleConsistentLogProbInterval, except that this test
    # checks those parameter settings where the true volume is known
    # analytically.
    concentration = np.array(concentrations, dtype=dtype)
    det_bounds = np.array(det_bounds, dtype=dtype)
    means = np.array(means, dtype=dtype)
    # Add a tolerance to guard against some of the importance_weights exceeding
    # the theoretical maximum (importance_maxima) due to numerical inaccuracies
    # while lower bounding the determinant. See corresponding comment in
    # _testSampleConsistentLogProbInterval.
    high_tolerance = 1e-6

    testee_lkj = tfd.LKJ(
        dimension=dim, concentration=concentration, validate_args=True)
    x = testee_lkj.sample(num_samples, seed=seed)
    importance_weights = (
        tf.exp(-testee_lkj.log_prob(x)) * _det_ok_mask(x, det_bounds))
    importance_maxima = (1. / det_bounds) ** (concentration - 1) * tf.exp(
        testee_lkj._log_normalization())

    chk1 = st.assert_true_mean_equal_by_dkwm(
        importance_weights, low=0., high=importance_maxima + high_tolerance,
        expected=means, false_fail_rate=1e-6)
    chk2 = tf.assert_less(
        st.min_discrepancy_of_true_means_detectable_by_dkwm(
            num_samples, low=0., high=importance_maxima + high_tolerance,
            false_fail_rate=1e-6, false_pass_rate=1e-6),
        dtype(target_discrepancy))
    self.evaluate([chk1, chk2])
  def test_dkwm_mean_one_sample_assertion(self):
    rng = np.random.RandomState(seed=0)
    num_samples = 5000

    # Test that the test assertion agrees that the mean of the standard
    # uniform distribution is 0.5.
    samples = rng.uniform(size=num_samples).astype(np.float32)
    self.evaluate(st.assert_true_mean_equal_by_dkwm(
        samples, 0., 1., 0.5, false_fail_rate=1e-6))

    # Test that the test assertion confirms that the mean of the
    # standard uniform distribution is not 0.4.
    with self.assertRaisesOpError("true mean greater than expected"):
      self.evaluate(st.assert_true_mean_equal_by_dkwm(
          samples, 0., 1., 0.4, false_fail_rate=1e-6))

    # Test that the test assertion confirms that the mean of the
    # standard uniform distribution is not 0.6.
    with self.assertRaisesOpError("true mean smaller than expected"):
      self.evaluate(st.assert_true_mean_equal_by_dkwm(
          samples, 0., 1., 0.6, false_fail_rate=1e-6))
Esempio n. 11
0
    def _testSampleLogProbExact(self,
                                concentrations,
                                det_bounds,
                                dim,
                                means,
                                num_samples=int(1e5),
                                dtype=np.float32,
                                target_discrepancy=0.1,
                                input_output_cholesky=False,
                                seed=42):
        # For test methodology see the comment in
        # _testSampleConsistentLogProbInterval, except that this test
        # checks those parameter settings where the true volume is known
        # analytically.
        concentration = np.array(concentrations, dtype=dtype)
        det_bounds = np.array(det_bounds, dtype=dtype)
        means = np.array(means, dtype=dtype)
        # Add a tolerance to guard against some of the importance_weights exceeding
        # the theoretical maximum (importance_maxima) due to numerical inaccuracies
        # while lower bounding the determinant. See corresponding comment in
        # _testSampleConsistentLogProbInterval.
        high_tolerance = 1e-6

        testee_lkj = tfd.LKJ(dimension=dim,
                             concentration=concentration,
                             input_output_cholesky=input_output_cholesky,
                             validate_args=True)
        x = testee_lkj.sample(num_samples, seed=seed)
        importance_weights = (
            tf.exp(-testee_lkj.log_prob(x)) *
            _det_ok_mask(x, det_bounds, input_output_cholesky))
        importance_maxima = (1. / det_bounds)**(concentration - 1) * tf.exp(
            testee_lkj._log_normalization())

        chk1 = st.assert_true_mean_equal_by_dkwm(importance_weights,
                                                 low=0.,
                                                 high=importance_maxima +
                                                 high_tolerance,
                                                 expected=means,
                                                 false_fail_rate=1e-6)
        chk2 = assert_util.assert_less(
            st.min_discrepancy_of_true_means_detectable_by_dkwm(
                num_samples,
                low=0.,
                high=importance_maxima + high_tolerance,
                false_fail_rate=1e-6,
                false_pass_rate=1e-6), dtype(target_discrepancy))
        self.evaluate([chk1, chk2])
 def testRejection4D(self):
   num_samples = int(1e5)  # Chosen for a small min detectable discrepancy
   det_bounds = np.array([0.0], dtype=np.float32)
   exact_volumes = [four_by_four_volume()]
   (rej_weights,
    rej_proposal_volume) = corr.correlation_matrix_volume_rejection_samples(
        det_bounds, 4, [num_samples, 1], dtype=np.float32, seed=45)
   # shape of rej_weights: [num_samples, 1, 4, 4]
   chk1 = st.assert_true_mean_equal_by_dkwm(
       rej_weights, low=0., high=rej_proposal_volume, expected=exact_volumes,
       false_fail_rate=1e-6)
   chk2 = tf.assert_less(
       st.min_discrepancy_of_true_means_detectable_by_dkwm(
           num_samples, low=0., high=rej_proposal_volume,
           false_fail_rate=1e-6, false_pass_rate=1e-6),
       # Going for about a 10% relative error
       1.1)
   with tf.control_dependencies([chk1, chk2]):
     rej_weights = tf.identity(rej_weights)
   self.evaluate(rej_weights)
Esempio n. 13
0
 def testMean(self, dtype):
   testee_lkj = tfd.LKJ(dimension=3, concentration=dtype([1., 3., 5.]))
   num_samples = 20000
   results = testee_lkj.sample(sample_shape=[num_samples])
   mean = testee_lkj.mean()
   self.assertEqual(mean.shape, [3, 3, 3])
   check1 = st.assert_true_mean_equal_by_dkwm(
       samples=results, low=-1., high=1.,
       expected=mean,
       false_fail_rate=1e-6)
   check2 = tf.assert_less(
       st.min_discrepancy_of_true_means_detectable_by_dkwm(
           num_samples, low=-1., high=1.,
           # Smaller false fail rate because of different batch sizes between
           # these two checks.
           false_fail_rate=1e-7,
           false_pass_rate=1e-6),
       # 4% relative error
       0.08)
   self.evaluate([check1, check2])
Esempio n. 14
0
  def testTriangularSample(self):
    low = self._dtype([-3.] * 4)
    high = np.arange(7., 11., dtype=self._dtype)
    peak = np.array([0.] * 4, dtype=self._dtype)
    tri = self._create_triangular_dist(low, high, peak)
    num_samples = int(3e6)
    samples = tri.sample(num_samples, seed=123)

    detectable_discrepancies = self.evaluate(
        st.min_discrepancy_of_true_means_detectable_by_dkwm(
            num_samples, low, high,
            false_fail_rate=self._dtype(1e-6),
            false_pass_rate=self._dtype(1e-6)))
    below_threshold = detectable_discrepancies <= 0.05
    self.assertTrue(np.all(below_threshold))

    self.evaluate(
        st.assert_true_mean_equal_by_dkwm(
            samples, low=low, high=high, expected=tri.mean(),
            false_fail_rate=self._dtype(1e-6)))
 def testRejection2D(self):
   num_samples = int(1e5)  # Chosen for a small min detectable discrepancy
   det_bounds = np.array(
       [0.01, 0.02, 0.03, 0.04, 0.05, 0.3, 0.35, 0.4, 0.5], dtype=np.float32)
   exact_volumes = two_by_two_volume(det_bounds)
   (rej_weights,
    rej_proposal_volume) = corr.correlation_matrix_volume_rejection_samples(
        det_bounds, 2, [num_samples, 9], dtype=np.float32, seed=43)
   # shape of rej_weights: [num_samples, 9, 2, 2]
   chk1 = st.assert_true_mean_equal_by_dkwm(
       rej_weights, low=0., high=rej_proposal_volume, expected=exact_volumes,
       false_fail_rate=1e-6)
   chk2 = tf.assert_less(
       st.min_discrepancy_of_true_means_detectable_by_dkwm(
           num_samples, low=0., high=rej_proposal_volume,
           # Correct the false fail rate due to different broadcasting
           false_fail_rate=1.1e-7, false_pass_rate=1e-6),
       0.036)
   with tf.control_dependencies([chk1, chk2]):
     rej_weights = tf.identity(rej_weights)
   self.evaluate(rej_weights)
Esempio n. 16
0
    def _testSampleLogProbExact(self,
                                concentrations,
                                det_bounds,
                                dim,
                                means,
                                num_samples=int(1e5),
                                target_discrepancy=0.1,
                                seed=42):
        # For test methodology see the comment in
        # _testSampleConsistentLogProbInterval, except that this test
        # checks those parameter settings where the true volume is known
        # analytically.
        concentration = np.array(concentrations, dtype=np.float32)
        det_bounds = np.array(det_bounds, dtype=np.float32)
        means = np.array(means, dtype=np.float32)

        testee_lkj = tfd.LKJ(dimension=dim,
                             concentration=concentration,
                             validate_args=True)
        x = testee_lkj.sample(num_samples, seed=seed)
        importance_weights = (tf.exp(-testee_lkj.log_prob(x)) *
                              _det_ok_mask(x, det_bounds))
        importance_maxima = (1. / det_bounds)**(concentration - 1) * tf.exp(
            testee_lkj._log_normalization())

        chk1 = st.assert_true_mean_equal_by_dkwm(importance_weights,
                                                 low=0.,
                                                 high=importance_maxima,
                                                 expected=means,
                                                 false_fail_rate=1e-6)
        chk2 = tf.assert_less(
            st.min_discrepancy_of_true_means_detectable_by_dkwm(
                num_samples,
                low=0.,
                high=importance_maxima,
                false_fail_rate=1e-6,
                false_pass_rate=1e-6), target_discrepancy)
        self.evaluate([chk1, chk2])