code: def test_categorical_resampler_chi2(self):
      strm = test_util.test_seed_stream()
      # Test categorical resampler using chi-squared test.
      if self.use_xla and tf.executing_eagerly():
          self.skipTest(
              "No need to test XLA under all execution regimes."
          )
  
      num_probs = 50
      num_distributions = 3
      unnormalized_probs = uniform.Uniform(
          low=self.dtype(0), high=self.dtype(1.0)
      ).sample([num_distributions, num_probs], seed=strm)
      probs = unnormalized_probs / tf.reduce_sum(
          unnormalized_probs, axis=-1, keepdims=True
      )
  
      # chi-squared test is valid as long as `num_samples` is
      # large compared to `num_probs`.
      num_particles = 10000
      num_samples = 2
  
      sample = self.maybe_compiler(resample_independent)(
          tf.math.log(
              dist_util.move_dimension(probs, source_idx=-1, dest_idx=0)
          ),
          num_particles,
          [num_samples],
          seed=strm,
      )
      elems = tf.range(num_probs)
      initializer = tf.zeros(
          [num_samples, num_distributions], dtype=sample.dtype
      )
      counts = tf.scan(
          lambda _, x: tf.reduce_sum(  # pylint: disable=g-long-lambda
              tf.cast(tf.equal(sample, x), sample.dtype), axis=0
          ),
          elems,
          initializer,
      )
      counts = dist_util.move_dimension(
          tf.cast(counts, self.dtype), source_idx=0, dest_idx=-1
      )
      expected_samples = probs * num_particles
      chi2 = tf.reduce_sum(
          (counts - expected_samples) ** 2 / expected_samples, axis=-1
      )
      self.assertAllLess(
          chi2_lib.Chi2(df=self.dtype(num_probs - 1)).cdf(chi2), 0.99995
      )
  
imports: import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.distributions import chi2
  from tensorflow_probability.python.distributions import poisson
  from tensorflow_probability.python.distributions import uniform
  from tensorflow_probability.python.experimental.mcmc.weighted_resampling import _resample_using_log_points
  from tensorflow_probability.python.experimental.mcmc.weighted_resampling import _scatter_nd_batch
  from tensorflow_probability.python.experimental.mcmc.weighted_resampling import resample
  from tensorflow_probability.python.experimental.mcmc.weighted_resampling import resample_deterministic_minimum_error
  from tensorflow_probability.python.experimental.mcmc.weighted_resampling import resample_independent
  from tensorflow_probability.python.experimental.mcmc.weighted_resampling import resample_stratified
  from tensorflow_probability.python.experimental.mcmc.weighted_resampling import resample_systematic
  from tensorflow_probability.python.internal import distribution_util
  from tensorflow_probability.python.internal import test_util
label_desc: Use image node_id 1 for calling the _SMCResamplersTest obj's underlying member method code with example usage: obj.test_categorical_resampler_chi2() without return types