code: def test_noise_variance_posterior_matches_expected(self):
      # Generate a synthetic regression task.
      num_features = 5
      num_outputs = 20
      design_matrix, _, targets = self.evaluate(
          self._random_regression_task(
              num_features=num_features,
              num_outputs=num_outputs,
              batch_shape=[2],
              seed=test_util.test_seed(),
          )
      )
  
      observation_noise_variance_prior_concentration = 0.03
      observation_noise_variance_prior_scale = 0.015
      # Posterior on noise variance if all weights are zero.
      naive_posterior = inverse_gamma.InverseGamma(
          concentration=(
              observation_noise_variance_prior_concentration
              + num_outputs / 2.0
          ),
          scale=(
              observation_noise_variance_prior_scale
              + tf.reduce_sum(tf.square(targets), axis=-1) / 2.0
          ),
      )
  
      # Compare to sampler with weights constrained to near-zero.
      # We can do this by reducing the width of the slab (here),
      # or by reducing the probability of the slab (below). Both should give
      # equivalent noise posteriors.
      tight_slab_sampler = spike_and_slab.SpikeSlabSampler(
          design_matrix,
          weights_prior_precision=tf.eye(num_features) * 1e6,
          observation_noise_variance_prior_concentration=(
              observation_noise_variance_prior_concentration
          ),
          observation_noise_variance_prior_scale=(
              observation_noise_variance_prior_scale
          ),
      )
      self.assertAllClose(
          tight_slab_sampler.observation_noise_variance_posterior_concentration,
          naive_posterior.concentration,
      )
      self.assertAllClose(
          tight_slab_sampler._initialize_sampler_state(
              targets=targets,
              nonzeros=tf.ones([num_features], dtype=tf.bool),
              observation_noise_variance=1.0,
          ).observation_noise_variance_posterior_scale,
          naive_posterior.scale,
          atol=1e-2,
      )
  
      downweighted_slab_sampler = spike_and_slab.SpikeSlabSampler(
          design_matrix,
          observation_noise_variance_prior_concentration=(
              observation_noise_variance_prior_concentration
          ),
          observation_noise_variance_prior_scale=(
              observation_noise_variance_prior_scale
          ),
      )
      self.assertAllClose(
          (
              downweighted_slab_sampler.observation_noise_variance_posterior_concentration
          ),
          naive_posterior.concentration,
      )
      self.assertAllClose(
          downweighted_slab_sampler._initialize_sampler_state(
              targets=targets,
              nonzeros=tf.zeros([num_features], dtype=tf.bool),
              observation_noise_variance=1.0,
          ).observation_noise_variance_posterior_scale,
          naive_posterior.scale,
      )
  
imports: from absl.testing import parameterized
  import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.distributions import inverse_gamma
  from tensorflow_probability.python.distributions import normal_conjugate_posteriors
  from tensorflow_probability.python.experimental.sts_gibbs import spike_and_slab
  from tensorflow_probability.python.internal import samplers
  from tensorflow_probability.python.internal import test_util
  from tensorflow_probability.python.mcmc.internal import util
label_desc: Use image node_id 6 for calling the SpikeAndSlabTest obj's underlying member method code with example usage: obj.test_noise_variance_posterior_matches_expected() without return types