code: def resample_deterministic_minimum_error(
      log_probs,
      event_size,
      sample_shape,
      seed=None,
      name="resample_deterministic_minimum_error",
  ):
      """Deterministic minimum error resampler for sequential Monte Carlo.
  
        The return value of this function is similar to sampling with
  
        ```python
        expanded_sample_shape = tf.concat([sample_shape, [event_size]]), axis=-1)
        tfd.Categorical(logits=log_probs).sample(expanded_sample_shape)`
        ```
  
        but with values chosen deterministically so that the empirical distribution
        is as close as possible to the specified distribution.
        (Note that the empirical distribution can only exactly equal the requested
        distribution if multiplying every probability by `event_size` gives
        an integer. So in general this is a biased "sampler".)
        It is intended to provide a good representative sample, suitable for use
        with some Sequential Monte Carlo algorithms.
  
      This function is based on Algorithm #3 in [Maskell et al. (2006)][1].
  
      Args:
        log_probs: a tensor-valued batch of discrete log probability distributions.
        event_size: the dimension of the vector considered a single draw.
        sample_shape: the `sample_shape` determining the number of draws. Because
          this resampler is deterministic it simply replicates the draw you
          would get for `sample_shape=[1]`.
        seed: This argument is unused but is present so that this function shares
          its interface with the other resampling functions.
          Default value: None
        name: Python `str` name for ops created by this method.
          Default value: `None` (i.e., `'resample_deterministic_minimum_error'`).
  
      Returns:
        resampled_indices: a tensor of samples.
  
      #### References
      [1]: S. Maskell, B. Alun-Jones and M. Macleod. A Single Instruction Multiple
           Data Particle Filter.
           In 2006 IEEE Nonlinear Statistical Signal Processing Workshop.
           http://people.ds.cam.ac.uk/fanf2/hermes/doc/antiforgery/stats.pdf
  
      """
      del seed
  
      with tf.name_scope(
          name or "resample_deterministic_minimum_error"
      ):
          sample_shape = tf.convert_to_tensor(
              sample_shape, dtype_hint=tf.int32
          )
          log_probs = dist_util.move_dimension(
              log_probs, source_idx=0, dest_idx=-1
          )
          probs = tf.math.exp(log_probs)
          prob_shape = ps.shape(probs)
          pdf_size = prob_shape[-1]
          # If we could draw fractional numbers of samples we would
          # choose `ideal_numbers` for the number of each element.
          ideal_numbers = event_size * probs
          # We approximate the ideal numbers by truncating to integers
          # and then repair the counts starting with the one with the
          # largest fractional error and working our way down.
          first_approximation = tf.floor(ideal_numbers)
          missing_fractions = ideal_numbers - first_approximation
          first_approximation = ps.cast(
              first_approximation, dtype=tf.int32
          )
          fraction_order = tf.argsort(missing_fractions, axis=-1)
          # We sort the integer parts and fractional parts together.
          batch_dims = ps.rank_from_shape(prob_shape) - 1
          first_approximation = tf.gather_nd(
              first_approximation,
              fraction_order[..., tf.newaxis],
              batch_dims=batch_dims,
          )
          missing_fractions = tf.gather_nd(
              missing_fractions,
              fraction_order[..., tf.newaxis],
              batch_dims=batch_dims,
          )
          sample_defect = event_size - tf.reduce_sum(
              first_approximation, axis=-1, keepdims=True
          )
          unpermuted = tf.broadcast_to(tf.range(pdf_size), prob_shape)
          increments = tf.cast(
              unpermuted >= pdf_size - sample_defect,
              dtype=first_approximation.dtype,
          )
          counts = first_approximation + increments
          samples = _samples_from_counts(
              fraction_order, counts, event_size
          )
          result_shape = tf.concat(
              [sample_shape, prob_shape[:-1], [event_size]], axis=0
          )
          # Replicate sample up to batch size.
          # TODO(dpiponi): rather than replicating, spread the "error" over
          # multiple samples with a minimum-discrepancy sequence.
          resampled = tf.broadcast_to(samples, result_shape)
          return dist_util.move_dimension(
              resampled, source_idx=-1, dest_idx=0
          )
  
file_path: probability/tensorflow_probability/python/experimental/mcmc/weighted_resampling.py
imports: import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.distributions import exponential
  from tensorflow_probability.python.distributions import uniform
  from tensorflow_probability.python.internal import distribution_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.math.gradient import value_and_gradient
  from tensorflow_probability.python.mcmc.internal import util
label_desc: Use image node_id 9 for calling a global function with example usage: resample_deterministic_minimum_error(log_probs, event_size, sample_shape, seed, name) and returns: dist_util