docstr: Test if the noise std is around expected.
docstr_tok: ["Test","if","the","noise","std","is","around","expected","."]
code: def test_private_gradvalues_noise(self):
      """Test if the noise std is around expected."""
      runs = 100
      alpha = 0.0001
      for use_norm_accumulation in [True, False]:
          for microbatch in [1, 10, self.ntrain]:
              for noise_multiplier in [0.1, 10.0]:
                  for l2_norm_clip in [0.01, 0.1]:
                      gv_priv = objax.Jit(
                          objax.privacy.dpsgd.PrivateGradValues(
                              self.loss,
                              self.model_vars,
                              noise_multiplier,
                              l2_norm_clip,
                              microbatch,
                              batch_axis=(0, 0),
                              use_norm_accumulation=use_norm_accumulation,
                          )
                      )
                      # Repeat the run and collect all gradients.
                      g_privs = []
                      for i in range(runs):
                          g_priv, v_priv = gv_priv(
                              self.data, self.labels
                          )
                          g_privs.append(
                              np.concatenate(
                                  [g_n.reshape(-1) for g_n in g_priv]
                              )
                          )
                          np.testing.assert_allclose(
                              v_priv[0],
                              self.loss(self.data, self.labels)[0],
                              atol=1e-7,
                          )
                      g_privs = np.array(g_privs)
  
                      # Compute empirical std and expected std.
                      std_empirical = np.std(g_privs, axis=0, ddof=1)
                      std_theoretical = (
                          l2_norm_clip
                          * noise_multiplier
                          / (self.ntrain // microbatch)
                      )
  
                      # Conduct chi-square test for correct expected standard
                      # deviation.
                      chi2_value = (
                          (runs - 1)
                          * std_empirical**2
                          / std_theoretical**2
                      )
                      chi2_cdf = chi2.cdf(chi2_value, runs - 1)
                      self.assertTrue(
                          np.all(alpha <= chi2_cdf)
                          and np.all(chi2_cdf <= 1.0 - alpha)
                      )
  
                      # Conduct chi-square test for incorrect expected standard
                      # deviations: expect failure.
                      chi2_value = (
                          (runs - 1)
                          * std_empirical**2
                          / (1.25 * std_theoretical) ** 2
                      )
                      chi2_cdf = chi2.cdf(chi2_value, runs - 1)
                      self.assertFalse(
                          np.all(alpha <= chi2_cdf)
                          and np.all(chi2_cdf <= 1.0 - alpha)
                      )
  
                      chi2_value = (
                          (runs - 1)
                          * std_empirical**2
                          / (0.75 * std_theoretical) ** 2
                      )
                      chi2_cdf = chi2.cdf(chi2_value, runs - 1)
                      self.assertFalse(
                          np.all(alpha <= chi2_cdf)
                          and np.all(chi2_cdf <= 1.0 - alpha)
                      )
  
imports: import inspect
  import unittest
  from typing import Tuple, Dict, List
  import jax
  import jax.numpy
  import numpy
  from scipy.stats.distributions import chi2
  import objax
  from objax.typing import JaxArray
  from objax.zoo.dnnet import DNNet
label_desc: Use image node_id 4 for calling the TestPrivateGradValues obj's underlying member method code with example usage: obj.test_private_gradvalues_noise() without return types