code: def _test(self, probs, params, dist):
      g = tf.Graph()
      with g.as_default():
          tf.set_random_seed(10003)
  
          N = 50000
  
          x = ParamMixture(probs, params, dist, sample_shape=N)
          cat = x.cat
          components = x.components
  
          marginal_logp = x.marginal_log_prob(x)
          cond_logp = x.log_prob(x)
  
          comp_means = components.mean()
          comp_stddevs = components.stddev()
          marginal_mean = x.mean()
          marginal_stddev = x.stddev()
          marginal_var = x.variance()
  
      sess = self.test_session(graph=g)
      with self.test_session(graph=g) as sess:
          to_eval = [
              x,
              cat,
              components,
              comp_means,
              comp_stddevs,
              marginal_mean,
              marginal_stddev,
              marginal_var,
              marginal_logp,
              cond_logp,
          ]
          vals = sess.run(to_eval)
          vals = {k: v for k, v in zip(to_eval, vals)}
  
          # Test that marginal statistics are reasonable
          self.assertAllClose(
              vals[x].mean(0), vals[marginal_mean], rtol=0.01, atol=0.01
          )
          self.assertAllClose(
              vals[x].std(0),
              vals[marginal_stddev],
              rtol=0.01,
              atol=0.01,
          )
          self.assertAllClose(
              vals[x].var(0), vals[marginal_var], rtol=0.01, atol=0.01
          )
  
          # Test that per-component statistics are reasonable
          for k in range(x.num_components):
              selector = vals[cat] == k
              self.assertAllClose(
                  selector.mean(), probs[k], rtol=0.01, atol=0.01
              )
              x_k = vals[x][selector]
              self.assertAllClose(
                  x_k.mean(0), vals[comp_means][k], rtol=0.05, atol=0.05
              )
              self.assertAllClose(
                  x_k.std(0),
                  vals[comp_stddevs][k],
                  rtol=0.05,
                  atol=0.05,
              )
  
          n_bins = 100
          x_hists = np.zeros((n_bins,) + vals[x].shape[1:])
          hist_centers = np.zeros_like(x_hists)
          x_axis = np.zeros((N,) + vals[x].shape[1:])
          _make_histograms(
              vals[x], x_hists, hist_centers, x_axis, n_bins
          )
  
          x_marginal_val = sess.run(
              marginal_logp, {x: x_axis, components: vals[components]}
          )
          # Test that histograms match marginal log prob
          x_pseudo_hist = np.exp(x_marginal_val[:n_bins])
          self.assertAllClose(
              x_pseudo_hist.sum(0) * (x_axis[1] - x_axis[0]),
              1.0,
              rtol=0.1,
              atol=0.1,
          )
          x_pseudo_hist /= x_pseudo_hist.sum(0, keepdims=True)
          self.assertLess(
              abs(x_pseudo_hist - x_hists).sum(0).mean(), 0.1
          )
  
          # Test that histograms match conditional log prob
          for k in range(probs.shape[-1]):
              k_cat = k + np.zeros(x_axis.shape, np.int32)
              x_vals_k = sess.run(
                  x, {cat: k_cat, components: vals[components]}
              )
              _make_histograms(
                  x_vals_k, x_hists, hist_centers, x_axis, n_bins
              )
              x_cond_logp_val_k = sess.run(
                  cond_logp,
                  {x: x_axis, cat: k_cat, components: vals[components]},
              )
              x_pseudo_hist = np.exp(x_cond_logp_val_k[:n_bins])
              self.assertAllClose(
                  x_pseudo_hist.sum(0) * (x_axis[1] - x_axis[0]),
                  1.0,
                  rtol=0.1,
                  atol=0.1,
              )
              x_pseudo_hist /= x_pseudo_hist.sum(0, keepdims=True)
              self.assertLess(
                  abs(x_pseudo_hist - x_hists).sum(0).mean(), 0.1
              )
  
file_path: edward/tests/models/param_mixture_stats_test.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import edward
  import numpy
  import tensorflow
  from edward.models import Beta, Normal, ParamMixture
label_desc: Use image node_id 1 for calling the test_param_mixture_class obj's underlying member method code with example usage: obj._test(probs, params, dist) without return types