class_docstr: Test cases for random-number generating operators.
code: def _checkTruncatedNormalIsInRange(
      self, x, a, b, mu, sigma, count, stat_test
  ):
      def normal_cdf(x):
          return 0.5 * math.erfc(-x / math.sqrt(2))
  
      def normal_pdf(x):
          return math.exp(-(x**2) / 2.0) / math.sqrt(2 * math.pi)
  
      def probit(x):
          return self.evaluate(special_math.ndtri(x))
  
      y = self.evaluate(x)
  
      alpha = (a - mu) / sigma
      beta = (b - mu) / sigma
      z = normal_cdf(beta) - normal_cdf(alpha)
  
      self.assertEqual((y >= a).sum(), count)
      self.assertEqual((y <= b).sum(), count)
  
      # Skip statistical test for low probability regions.
      if not stat_test:
          return
  
      # For more information on these calculations, see:
      # Burkardt, John. "The Truncated Normal Distribution".
      # Department of Scientific Computing website. Florida State University.
      expected_mean = (
          mu + (normal_pdf(alpha) - normal_pdf(beta)) / z * sigma
      )
      actual_mean = np.mean(y, dtype=np.float64)
      if x.dtype == dtypes.bfloat16:
          atol = rtol = 1e-1
      else:
          atol = rtol = 2e-2
      self.assertAllClose(
          actual_mean, expected_mean, atol=atol, rtol=rtol
      )
  
      expected_median = (
          mu
          + probit((normal_cdf(alpha) + normal_cdf(beta)) / 2.0) * sigma
      )
      actual_median = np.median(y)
      self.assertAllClose(
          actual_median, expected_median, atol=atol, rtol=rtol
      )
  
      expected_variance = sigma**2 * (
          1
          + ((alpha * normal_pdf(alpha) - beta * normal_pdf(beta)) / z)
          - ((normal_pdf(alpha) - normal_pdf(beta)) / z) ** 2
      )
      actual_variance = np.var(y, dtype=np.float64)
      self.assertAllClose(
          actual_variance, expected_variance, atol=atol, rtol=rtol
      )
  
imports: import math
  from absl.testing import parameterized
  import numpy
  from tensorflow.compiler.tests import xla_test
  from tensorflow.python.framework import dtypes
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import random_ops
  from tensorflow.python.ops.distributions import special_math
  from tensorflow.python.platform import googletest
label_desc: Use image node_id 8 for calling the RandomOpsTest obj's underlying member method code with example usage: obj._checkTruncatedNormalIsInRange(x, a, b, mu, sigma, count, stat_test) and returns: unknown, unknown, self