code: def multinomial_goodness_of_fit(
      probs,
      counts,
      *,
      total_count=None,
      plot=False,
  ):
      """
      Pearson's chi^2 test, on possibly truncated data.
      https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test
  
      :param torch.Tensor probs: Vector of probabilities.
      :param torch.Tensor counts: Vector of counts.
      :param int total_count: Optional total count in case data is truncated,
          otherwise None.
      :param bool plot: Whether to print a histogram. Defaults to False.
      :returns: p-value of truncated multinomial sample.
      :rtype: float
      """
      assert probs.dim() == 1
      assert probs.shape == counts.shape
      if total_count is None:
          truncated = False
          total_count = int(counts.sum())
      else:
          truncated = True
          assert total_count >= counts.sum()
      if plot:
          print_histogram(probs, counts)
  
      chi_squared = 0
      dof = 0
      for p, c in zip(probs.tolist(), counts.tolist()):
          if abs(p - 1) < 1e-8:
              return 1 if c == total_count else 0
          assert p < 1, f"bad probability: {p:g}"
          if p > 0:
              mean = total_count * p
              variance = total_count * p * (1 - p)
              if not (variance > 1):
                  raise InvalidTest(
                      "Goodness of fit is inaccurate; use more samples"
                  )
              chi_squared += (c - mean) ** 2 / variance
              dof += 1
          else:
              warnings.warn("Zero probability in goodness-of-fit test")
              if c > 0:
                  return math.inf
  
      if not truncated:
          dof -= 1
  
      survival = chi2sf(chi_squared, dof)
      return survival
  
file_path: pyro/pyro/distributions/testing/gof.py
imports: import math
  import warnings
  import torch
  from .special import chi2sf
label_desc: Use image node_id 2 for calling a global function with example usage: multinomial_goodness_of_fit(probs, counts) and returns: survival, unknown, math