code: def test_normal_integral_mean_and_var_correctly_estimated(self):
      n = 1000
      # This test is almost identical to the similarly named test in
      # monte_carlo_test.py. The only difference is that we use the Halton
      # samples instead of the random samples to evaluate the expectations.
      # MC with pseudo random numbers converges at the rate of 1/ Sqrt(N)
      # (N=number of samples). For QMC in low dimensions, the expected convergence
      # rate is ~ 1/N. Hence we should only need 1e3 samples as compared to the
      # 1e6 samples used in the pseudo-random monte carlo.
      mu_p = tf.constant([-1.0, 1.0], dtype=tf.float64)
      mu_q = tf.constant([0.0, 0.0], dtype=tf.float64)
      sigma_p = tf.constant([0.5, 0.5], dtype=tf.float64)
      sigma_q = tf.constant([1.0, 1.0], dtype=tf.float64)
      p = normal.Normal(loc=mu_p, scale=sigma_p)
      q = normal.Normal(loc=mu_q, scale=sigma_q)
  
      cdf_sample = sample_halton_sequence_lib.sample_halton_sequence(
          2, num_results=n, dtype=tf.float64, seed=test_util.test_seed()
      )
      q_sample = q.quantile(cdf_sample)
  
      # Compute E_p[X].
      e_x = monte_carlo.expectation_importance_sampler(
          f=lambda x: x,
          log_p=p.log_prob,
          sampling_dist_q=q,
          z=q_sample,
          seed=42,
      )
  
      # Compute E_p[X^2].
      e_x2 = monte_carlo.expectation_importance_sampler(
          f=tf.square,
          log_p=p.log_prob,
          sampling_dist_q=q,
          z=q_sample,
          seed=1412,
      )
  
      stddev = tf.sqrt(e_x2 - tf.square(e_x))
      # Keep the tolerance levels the same as in monte_carlo_test.py.
      self.assertEqual(p.batch_shape, e_x.shape)
      self.assertAllClose(
          self.evaluate(p.mean()), self.evaluate(e_x), rtol=0.01
      )
      self.assertAllClose(
          self.evaluate(p.stddev()), self.evaluate(stddev), rtol=0.02
      )
  
file_path: probability/tensorflow_probability/python/mcmc/sample_halton_sequence_test.py
imports: import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.distributions import normal
  from tensorflow_probability.python.internal import monte_carlo
  from tensorflow_probability.python.internal import test_util
  from tensorflow_probability.python.mcmc import sample_halton_sequence_lib
label_desc: Use image node_id 6 for calling the HaltonSequenceTest obj's underlying member method code with example usage: obj.test_normal_integral_mean_and_var_correctly_estimated() without return types