docstr: Tests that seasonality is used, not regression, when it is best.
  
  This creates a situation where a regressor is added that explains
  the seasonality for only a portion of the timeseries, but seasonality
  explains it for whole time.
docstr_tok: ["Tests","that","seasonality","is","used",",","not","regression",",","when","it","is","best",".","This","creates","a","situation","where","a","regressor","is","added","that","explains","the","seasonality","for","only","a","portion","of","the","timeseries",",","but","seasonality","explains","it","for","whole","time","."]
code: def test_regression_does_not_explain_seasonal_variation(self):
      """Tests that seasonality is used, not regression, when it is best.
  
      This creates a situation where a regressor is added that explains
      the seasonality for only a portion of the timeseries, but seasonality
      explains it for whole time.
      """
      num_timesteps = 200
      tiled_seasonal_effect_one = [-2.0, -4.0, 2.0, 8.0, 5.0]
      tiled_seasonal_effect_two = [4.0, 6.0, -3.0, 0.0]
      seasonal_effect = [
          tiled_seasonal_effect_one[x % len(tiled_seasonal_effect_one)]
          + tiled_seasonal_effect_two[
              x % len(tiled_seasonal_effect_two)
          ]
          for x in range(num_timesteps)
      ]
      # Regression explains just a portion of the effect.
      regression_value = [
          seasonal_effect[x] if x < 25 else 0.0
          for x in range(num_timesteps)
      ]
      model, observed_time_series, _ = self._build_test_model(
          num_timesteps=num_timesteps,
          num_features=1,
          missing_prob=0.0,
          true_noise_scale=0.1,
          design_matrix=tf.convert_to_tensor(regression_value)[
              ..., tf.newaxis
          ],
          # Do not generate the observed series using the regression, just
          # use the tiled_residual_offset since the goal is to see what happens
          # when the regression explains a portion of the seasonal effect.
          weights=tf.constant([0.0]),
          weights_prior_scale=None,  # Default g-prior.
          sparse_weights_nonzero_prob=0.01,
          tiled_residual_offset=seasonal_effect,
          true_slope_scale=None,  # Simplify the test data to have no trend.
          seasonal_components=[
              seasonal.Seasonal(
                  name="season1",
                  num_seasons=len(tiled_seasonal_effect_one),
                  drift_scale_prior=transformed_distribution.TransformedDistribution(
                      bijector=invert.Invert(square.Square()),
                      distribution=inverse_gamma.InverseGamma(
                          0.01, 0.0001
                      ),
                  ),
                  initial_effect_prior=normal.Normal(0.0, 1.0),
              ),
              seasonal.Seasonal(
                  name="season2",
                  num_seasons=len(tiled_seasonal_effect_two),
                  drift_scale_prior=transformed_distribution.TransformedDistribution(
                      bijector=invert.Invert(square.Square()),
                      distribution=inverse_gamma.InverseGamma(
                          0.01, 0.0001
                      ),
                  ),
                  initial_effect_prior=normal.Normal(0.0, 1.0),
              ),
          ],
      )
  
      @tf.function(autograph=False)
      def do_sampling():
          return gibbs_sampler.fit_with_gibbs_sampling(
              model,
              observed_time_series,
              num_results=100,
              num_warmup_steps=100,
              seed=test_util.test_seed(sampler_type="stateless"),
          )
  
      samples = do_sampling()
      nonzero_probs = tf.reduce_mean(
          tf.cast(tf.not_equal(samples.weights, 0.0), tf.float32),
          axis=-2,
      )
      # Verify that essentially none of the sample are non-zero, since we
      # explained the timeseries with a seasonal effect. If the regression
      # has a defect introduced to not take the seasonal effect into account,
      # this becomes near 1. Similarly, if either of the seasonal components
      # are removed, it becomes near 1 - proving that multiple-seasonal components
      # is respected by regression.
      self.assertAllLess(nonzero_probs, 0.05)
  
imports: from absl.testing import parameterized
  import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.bijectors import identity
  from tensorflow_probability.python.bijectors import invert
  from tensorflow_probability.python.bijectors import square
  from tensorflow_probability.python.distributions import inverse_gamma
  from tensorflow_probability.python.distributions import lognormal
  from tensorflow_probability.python.distributions import mvn_diag
  from tensorflow_probability.python.distributions import normal
  from tensorflow_probability.python.distributions import student_t
  from tensorflow_probability.python.distributions import transformed_distribution
  from tensorflow_probability.python.distributions.linear_gaussian_ssm import linear_gaussian_update
  from tensorflow_probability.python.experimental.distributions import mvn_precision_factor_linop
  from tensorflow_probability.python.experimental.sts_gibbs import gibbs_sampler
  from tensorflow_probability.python.internal import samplers
  from tensorflow_probability.python.internal import test_util
  from tensorflow_probability.python.stats import sample_stats
  from tensorflow_probability.python.sts.components import local_level
  from tensorflow_probability.python.sts.components import local_linear_trend
  from tensorflow_probability.python.sts.components import regression
  from tensorflow_probability.python.sts.components import seasonal
  from tensorflow_probability.python.sts.components import semilocal_linear_trend
  from tensorflow_probability.python.sts.components import sum
  from tensorflow_probability.python.sts.forecast import forecast
  from tensorflow_probability.python.sts.internal import missing_values_util
  from tensorflow_probability.python.sts.internal import util
label_desc: Use image node_id 14 for calling the GibbsSamplerTests obj's underlying member method code with example usage: obj.test_regression_does_not_explain_seasonal_variation() and returns: gibbs_sampler