code: def test_probability_distribution_mean_covariance():
      rng = np.random.default_rng(42)
      n_bins = 10
      n_histos = 5000
      max_hist_entries = 10000
      rel_error = 0.1
      bin_entries = []
      for _ in range(n_histos):
          bin_probs = rng.normal(1.0, rel_error, size=n_bins)  # + basic
          bin_probs /= np.sum(bin_probs)
          bin_entries.append(
              rng.multinomial(max_hist_entries, bin_probs)
          )
      bin_entries = np.array(bin_entries)
  
      chi2s = []
      pvalues = []
  
      for i in range(n_histos):
          others = np.delete(bin_entries, [i], axis=0)
          (
              norm_mean,
              norm_cov,
          ) = probability_distribution_mean_covariance(others)
          (
              single_norm,
              single_cov,
          ) = probability_distribution_mean_covariance([bin_entries[i]])
  
          # total covariance
          cov = norm_cov
          variance = np.diagonal(cov)
  
          if np.linalg.cond(cov) < 0.1 / np.finfo(cov.dtype).eps:
              # We try to use the precision matrix (inverse covariance matrix) for the chi-squared calculation
              pm = linalg.inv(cov)
              chi_squared = np.dot(
                  (norm_mean - single_norm),
                  np.dot(pm, (norm_mean - single_norm)),
              )
          else:
              # If a covariance matrix is singular we fall back on using variances
              chi_squared = np.sum(
                  (norm_mean - single_norm) ** 2
                  / (variance + np.finfo(float).eps)
              )
  
          n_bins = len(bin_entries[i])
          p_value = stats.chi2.sf(chi_squared, n_bins - 1)
          chi2s.append(chi_squared)
          pvalues.append(p_value)
  
      h, _ = np.histogram(pvalues, bins=10, range=(0, 1), density=True)
      np.testing.assert_allclose(h, 1, rtol=1e-01, atol=0)
  
file_path: popmon/tests/popmon/stats/test_numpy.py
imports: import itertools
  import numpy
  from scipy import linalg, stats
  from popmon.stats.numpy import mean, probability_distribution_mean_covariance, quantile, std
label_desc: Use image node_id 6 for calling a global function with example usage: test_probability_distribution_mean_covariance() without return types