docstr: Compare to the explicit reparameterization derivative.
  
  Defining x to be the output from a gamma sampler with rate=1, and defining y
  to be the actual gamma sample (defined by y = x / rate), we have:
  
  dx / dconcentration = d igammainv(concentration, x) / dconcentration,
  where u = igamma(concentration, x).
  
  Therefore, we have:
  
  dy / dconcentration = (1 / rate) * d igammainv(
    concentration, y * rate) / dconcentration,
  where u = igamma(concentration, y * rate)
  
  We also have dy / drate = -(x / rate^2) = -y / rate.
  
  Args:
    dtype: TensorFlow dtype to perform the computations in.
docstr_tok: ["Compare","to","the","explicit","reparameterization","derivative",".","Defining","x","to","be","the","output","from","a","gamma","sampler","with","rate=1",",","and","defining","y","to","be","the","actual","gamma","sample","(","defined","by","y","=","x","\/","rate",")",",","we","have",":","dx","\/","dconcentration","=","d","igammainv","(","concentration",",","x",")","\/","dconcentration",",","where","u","=","igamma","(","concentration",",","x",")",".","Therefore",",","we","have",":","dy","\/","dconcentration","=","(","1","\/","rate",")","*","d","igammainv","(","concentration",",","y","*","rate",")","\/","dconcentration",",","where","u","=","igamma","(","concentration",",","y","*","rate",")","We","also","have","dy","\/","drate","=","-","(","x","\/","rate^2",")","=","-y","\/","rate",".","Args",":","dtype",":","TensorFlow","dtype","to","perform","the","computations","in","."]
code: def testCompareToExplicitGradient(self, dtype):
      """Compare to the explicit reparameterization derivative.
  
      Defining x to be the output from a gamma sampler with rate=1, and defining y
      to be the actual gamma sample (defined by y = x / rate), we have:
  
      dx / dconcentration = d igammainv(concentration, x) / dconcentration,
      where u = igamma(concentration, x).
  
      Therefore, we have:
  
      dy / dconcentration = (1 / rate) * d igammainv(
        concentration, y * rate) / dconcentration,
      where u = igamma(concentration, y * rate)
  
      We also have dy / drate = -(x / rate^2) = -y / rate.
  
      Args:
        dtype: TensorFlow dtype to perform the computations in.
      """
      concentration_n = 4
      concentration_np = (
          np.arange(concentration_n).astype(dtype)[..., np.newaxis]
          + 1.0
      )
      concentration = tf.constant(concentration_np)
      rate_n = 3
      rate_np = np.arange(rate_n).astype(dtype) + 1.0
      rate = tf.constant(rate_np)
      num_samples = 2
  
      def gen_samples(concentration, rate):
          return gamma_lib.Gamma(concentration, rate).sample(
              num_samples, seed=test_util.test_seed()
          )
  
      samples, [concentration_grad, rate_grad] = self.evaluate(
          gradient.value_and_gradient(
              gen_samples, [concentration, rate]
          )
      )
      self.assertEqual(
          samples.shape, (num_samples, concentration_n, rate_n)
      )
      self.assertEqual(concentration_grad.shape, concentration.shape)
      self.assertEqual(rate_grad.shape, rate.shape)
      # Sum over the first 2 dimensions since these are batch dimensions.
      self.assertAllClose(
          rate_grad, np.sum(-samples / rate_np, axis=(0, 1))
      )
  
      # Compute the gradient by computing the derivative of gammaincinv
      # over each entry and summing.
      def expected_grad(s, c, r):
          u = sp_special.gammainc(c, s * r)
          delta = 1e-4
          return (
              sp_misc.derivative(
                  lambda x: sp_special.gammaincinv(x, u),
                  c,
                  dx=delta * c,
              )
              / r
          )
  
      self.assertAllClose(
          concentration_grad,
          np.sum(
              expected_grad(samples, concentration_np, rate_np),
              axis=(0, 2),
          )[..., np.newaxis],
          rtol=1e-3,
      )
  
imports: import functools
  from absl.testing import parameterized
  import numpy
  from scipy import misc
  from scipy import special
  from scipy import stats
  import tensorflow.compat.v2
  from tensorflow_probability.python.bijectors import exp
  from tensorflow_probability.python.distributions import gamma
  from tensorflow_probability.python.distributions import kullback_leibler
  from tensorflow_probability.python.distributions.internal import statistical_testing
  from tensorflow_probability.python.internal import implementation_selection
  from tensorflow_probability.python.internal import test_util
  from tensorflow_probability.python.math import gradient
label_desc: Use image node_id 8 for calling the GammaSamplingTest obj's underlying member method code with example usage: obj.testCompareToExplicitGradient(dtype) and returns: gamma_lib, unknown