class_docstr: A running R-hat diagnostic.
  
  `RunningPotentialScaleReduction` uses Gelman and Rubin (1992)'s potential
  scale reduction (also known as R-hat) for chain convergence [1].
  
  If multiple independent R-hat computations are desired across a latent
  state, one should use a (possibly nested) collection for initialization
  parameters `independent_chain_ndims` and `shape`. Subsequent chain states
  used to update the streaming R-hat should mimic their identical structure.
  
  `RunningPotentialScaleReduction` also assumes that incoming samples have shape
  `[Ci1, Ci2,...,CiD] + A`. Dimensions `0` through `D - 1` index the
  `Ci1 x ... x CiD` independent chains to be tested for convergence to the same
  target. The remaining dimensions, `A`, represent the event shape and hence,
  can have any shape (even empty, which implies scalar samples). The number of
  independent chain dimensions is defined by the `independent_chain_ndims`
  parameter at initialization.
  
  `RunningPotentialScaleReduction` is meant to serve general streaming R-hat.
  For a specialized version that fits streaming over MCMC samples, see
  `PotentialScaleReductionReducer` in `tfp.experimental.mcmc`.
  
  #### References
  
  [1]: Andrew Gelman and Donald B. Rubin. Inference from Iterative Simulation
       Using Multiple Sequences. _Statistical Science_, 7(4):457-472, 1992.
docstr: Computes the potential scale reduction for samples accumulated so far.
  
  Returns:
    rhat: An estimate of the R-hat.
docstr_tok: ["Computes","the","potential","scale","reduction","for","samples","accumulated","so","far",".","Returns",":","rhat",":","An","estimate","of","the","R-hat","."]
code: def potential_scale_reduction(self):
      """Computes the potential scale reduction for samples accumulated so far.
  
      Returns:
        rhat: An estimate of the R-hat.
      """
  
      def _finalize_for_one_state(chain_ndims, chain_variances):
          """Calculates R-hat for one group of Markov chains."""
          # using notation from Brooks and Gelman (1998),
          # n := num samples / chain; m := number of chains
          n = chain_variances.num_samples
          shape = chain_variances.mean.shape
          m = tf.cast(
              functools.reduce(
                  (lambda x, y: x * y), (shape[:chain_ndims])
              ),
              n.dtype,
          )
  
          # b/n is the between-chain variance (the variance of the chain means)
          b_div_n = diagnostic._reduce_variance(  # pylint:disable=protected-access
              tf.convert_to_tensor(chain_variances.mean),
              axis=ps.range(chain_ndims),
              biased=False,
          )
  
          # W is the within sequence variance (the mean of the chain variances)
          sum_of_chain_squared_residuals = tf.reduce_sum(
              chain_variances.sum_squared_residuals,
              axis=ps.range(chain_ndims),
          )
          w = sum_of_chain_squared_residuals / (m * (n - 1))
  
          # the `true_variance_estimate` is denoted as sigma^2_+ in the 1998 paper
          true_variance_estimate = ((n - 1) / n) * w + b_div_n
          return ((m + 1.0) / m) * true_variance_estimate / w - (
              n - 1.0
          ) / (m * n)
  
      return tf.nest.map_structure(
          _finalize_for_one_state,
          self.independent_chain_ndims,
          self.chain_variances,
          check_types=False,
      )
  
imports: import functools
  import math
  import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.internal import dtype_util
  from tensorflow_probability.python.internal import nest_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.mcmc import diagnostic
  from tensorflow.python.util import nest
label_desc: Use image node_id 7 for calling the RunningPotentialScaleReduction obj's underlying member method code with example usage: obj.potential_scale_reduction() and returns: tf, unknown