docstr: Tests the QMC evaluation of (x_j + x_{j+1} ...+x_{n})^2.
  
  A good test of QMC is provided by the function:
  
    f(x_1,..x_n, x_{n+1}, ..., x_{n+m}) = (x_{n+1} + ... x_{n+m} - m / 2)^2
  
  with the coordinates taking values in the unit interval. The mean and
  variance of this function (with the uniform distribution over the
  unit-hypercube) is exactly calculable:
  
    <f> = m / 12, Var(f) = m (5m - 3) / 360
  
  The purpose of the "shift" (if n > 0) in the coordinate dependence of the
  function is to provide a test for Halton sequence which exhibit more
  dependence in the higher axes.
  
  This test confirms that the mean squared error of RQMC estimation falls
  as O(N^(2-e)) for any e>0.
docstr_tok: ["Tests","the","QMC","evaluation","of","(","x_j","+","x_","{","j+1","}","...","+x_","{","n","}",")","^2",".","A","good","test","of","QMC","is","provided","by","the","function",":","f","(","x_1",",","..","x_n",",","x_","{","n+1","}",",","...",",","x_","{","n+m","}",")","=","(","x_","{","n+1","}","+","...","x_","{","n+m","}","-","m","\/","2",")","^2","with","the","coordinates","taking","values","in","the","unit","interval",".","The","mean","and","variance","of","this","function","(","with","the","uniform","distribution","over","the","unit-hypercube",")","is","exactly","calculable",":","<","f",">","=","m","\/","12",",","Var","(","f",")","=","m","(","5m","-","3",")","\/","360","The","purpose","of","the","``","shift","''","(","if","n",">","0",")","in","the","coordinate","dependence","of","the","function","is","to","provide","a","test","for","Halton","sequence","which","exhibit","more","dependence","in","the","higher","axes",".","This","test","confirms","that","the","mean","squared","error","of","RQMC","estimation","falls","as","O","(","N^","(","2-e",")",")","for","any","e",">","0","."]
code: def test_partial_sum_func_qmc(self):
      """Tests the QMC evaluation of (x_j + x_{j+1} ...+x_{n})^2.
  
      A good test of QMC is provided by the function:
  
        f(x_1,..x_n, x_{n+1}, ..., x_{n+m}) = (x_{n+1} + ... x_{n+m} - m / 2)^2
  
      with the coordinates taking values in the unit interval. The mean and
      variance of this function (with the uniform distribution over the
      unit-hypercube) is exactly calculable:
  
        <f> = m / 12, Var(f) = m (5m - 3) / 360
  
      The purpose of the "shift" (if n > 0) in the coordinate dependence of the
      function is to provide a test for Halton sequence which exhibit more
      dependence in the higher axes.
  
      This test confirms that the mean squared error of RQMC estimation falls
      as O(N^(2-e)) for any e>0.
      """
  
      n, m = 10, 10
      dim = n + m
      num_results_lo, num_results_hi = 1000, 10000
      replica = 100
      true_mean = m / 12.0
  
      def func_estimate(x):
          return tf.reduce_mean(
              tf.math.squared_difference(
                  tf.reduce_sum(x[..., -m:], axis=-1), m / 2.0
              ),
              axis=-1,
          )
  
      stream = test_util.test_seed_stream()
      sample_lo = sample_halton_sequence_lib.sample_halton_sequence(
          dim, num_results=replica * num_results_lo, seed=stream()
      )
      sample_hi = sample_halton_sequence_lib.sample_halton_sequence(
          dim, num_results=replica * num_results_hi, seed=stream()
      )
  
      sample_lo = tf.reshape(sample_lo, [replica, -1, dim])
      sample_hi = tf.reshape(sample_hi, [replica, -1, dim])
  
      f_lo = self.evaluate(func_estimate(sample_lo))
      f_hi = self.evaluate(func_estimate(sample_hi))
      var_lo = np.mean((f_lo - true_mean) ** 2, axis=0)
      var_hi = np.mean((f_hi - true_mean) ** 2, axis=0)
  
      # Expect that the variance scales as N^(-2-epsilon) (see section 3 of
      # https://arxiv.org/pdf/1706.02808.pdf). Thus, since
      # num_results_hi = 10 * num_results_lo, we expect to have
      #
      #   var_hi / var_lo ~= k / 100
      #
      # with k a fudge factor accounting for the residual N dependence of the QMC
      # error and the sampling error. Here we take log(k) = 1.5.
      log_rel_err = np.log(100 * var_hi / var_lo)
      self.assertAllLess(np.abs(log_rel_err), 1.5)
  
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 9 for calling the HaltonSequenceTest obj's underlying member method code with example usage: obj.test_partial_sum_func_qmc() and returns: tf