Example #1
0
    def _log_prob(self, x):
        alpha0 = tf.convert_to_tensor(self.concentration1_numerator)
        beta0 = tf.convert_to_tensor(self.concentration0_numerator)
        alpha1 = tf.convert_to_tensor(self.concentration1_denominator)
        beta1 = tf.convert_to_tensor(self.concentration0_denominator)

        alpha_sum = alpha0 + alpha1

        log_normalization = (tfp_math.lbeta(alpha0, beta0) +
                             tfp_math.lbeta(alpha1, beta1))
        log_normalization = log_normalization - tfp_math.lbeta(
            alpha_sum, tf.where(x > 1., beta0, beta1))

        b = tf.where(x > 1., 1. - beta1, 1 - beta0)
        c = alpha_sum + tf.where(x > 1., beta0, beta1)
        z = tf.where(x > 1., tf.math.reciprocal(x), x)

        # Here, c - a - b = beta0 + beta1 - 1, so the series always converges
        # conditionally.

        log_unnormalized_prob = tf.math.log(
            hypgeo.hyp2f1_small_argument(alpha_sum, b, c, z))
        log_unnormalized_prob = log_unnormalized_prob + tf.math.xlogy(
            tf.where(x > 1., -(alpha1 + 1.), alpha0 - 1.), x)

        return log_unnormalized_prob - log_normalization
 def testHyp2F1AtOne(self, dtype, rtol):
     seed_stream = test_util.test_seed_stream()
     a = self.GenParam(-10., 10., dtype, seed_stream())
     b = self.GenParam(-10., 10., dtype, seed_stream())
     # Ensure c > a + b so the evaluation is defined.
     c = a + b + 1.
     hyp2f1, a, b, c = self.evaluate(
         [tfp_math.hyp2f1_small_argument(a, b, c, dtype(1.)), a, b, c])
     scipy_hyp2f1 = scipy_special.hyp2f1(a, b, c, dtype(1.))
     self.assertAllClose(hyp2f1, scipy_hyp2f1, rtol=rtol)
 def testHyp2F1ShapeBroadcast(self, a_shape, b_shape, c_shape, z_shape):
     a = tf.zeros(a_shape, dtype=tf.float32)
     b = tf.zeros(b_shape, dtype=tf.float32)
     c = 10.5 * tf.ones(c_shape, dtype=tf.float32)
     z = tf.zeros(z_shape, dtype=tf.float32)
     broadcast_shape = functools.reduce(
         tf.broadcast_dynamic_shape, [a_shape, b_shape, c_shape, z_shape])
     hyp2f1 = tfp_math.hyp2f1_small_argument(a, b, c, z)
     broadcast_shape = self.evaluate(broadcast_shape)
     self.assertAllEqual(hyp2f1.shape, broadcast_shape)
Example #4
0
  def VerifyHyp2F1(
      self,
      dtype,
      rtol,
      a,
      b,
      c,
      z_lower=-0.9,
      z_upper=0.9):
    seed_stream = test_util.test_seed_stream()
    z = tf.random.uniform(
        [int(1e4)], seed=seed_stream(),
        minval=z_lower, maxval=z_upper, dtype=dtype)

    hyp2f1, a, b, c, z = self.evaluate([
        tfp_math.hyp2f1_small_argument(a, b, c, z), a, b, c, z])
    scipy_hyp2f1 = scipy_special.hyp2f1(a, b, c, z)
    self.assertAllClose(hyp2f1, scipy_hyp2f1, rtol=rtol)
  def VerifyHyp2F1(
      self,
      dtype,
      rtol,
      a,
      b,
      c,
      z_lower=-0.9,
      z_upper=0.9,
      use_mpmath=False):
    comparison_hyp2f1 = self._mpmath_hyp2f1 if use_mpmath else scipy_special.hyp2f1
    seed_stream = test_util.test_seed_stream()
    z = tf.random.uniform(
        [int(1e4)], seed=seed_stream(),
        minval=z_lower, maxval=z_upper, dtype=dtype)

    hyp2f1, a, b, c, z = self.evaluate([
        tfp_math.hyp2f1_small_argument(a, b, c, z), a, b, c, z])
    expected = comparison_hyp2f1(a, b, c, z)
    self.assertAllClose(hyp2f1, expected, rtol=rtol)