code: def _detect_anomalies_inner(
      observed_time_series,
      seasonal_structure,
      anomaly_threshold=0.01,
      num_warmup_steps=50,
      num_samples=100,
      use_gibbs_predictive_dist=True,
      seed=None,
  ):
      """Helper function for `detect_anomalies` to cache `tf.function` traces."""
      with tf.name_scope("build_default_model_for_gibbs_sampling"):
          (
              observed_mean,
              observed_stddev,
              _,
          ) = sts_util.empirical_statistics(observed_time_series)
          # Center the series to have mean 0 and stddev 1. Alternately, we could
          # rescale the priors to match the series, but this is simpler.
          observed_mean = observed_mean[
              ..., tf.newaxis
          ]  # Broadcast with num_steps.
          observed_stddev = observed_stddev[..., tf.newaxis]
          observed_time_series = observed_time_series._replace(
              time_series=(
                  observed_time_series.time_series
                  - observed_mean[..., tf.newaxis]
              )
              / observed_stddev[..., tf.newaxis]
          )
  
          (
              model,
              posterior_samples,
          ) = _fit_seasonal_model_with_gibbs_sampling(
              observed_time_series,
              seasonal_structure=seasonal_structure,
              num_results=num_samples,
              num_warmup_steps=num_warmup_steps,
              seed=seed,
          )
          if use_gibbs_predictive_dist:
              predictive_dist = gibbs_sampler.one_step_predictive(
                  model, posterior_samples, thin_every=10
              )
          else:
              # Rebuild the model using appropriate Seasonal components, throwing away
              # the seasonal effects fit by the Gibbs sampler. Instead, the predictive
              # model incorporates the seasonal effects in its state space, so their
              # posterior is tracked exactly (rather than by sampling) and is updated
              # with each step of the series. This avoids the learning-from-the-future
              # behavior of the Gibbs-estimated effects.
              seasonal_model = model.components[
                  0
              ] + default_model.model_from_seasonal_structure(  # LocalLinearTrend component.
                  seasonal_structure,
                  observed_time_series,
                  # Gibbs sampling didn't fit a drift scale(s).
                  allow_drift=False,
              )
              parameter_samples = gibbs_sampler.model_parameter_samples_from_gibbs_samples(
                  model, posterior_samples
              )
              predictive_dist = one_step_predictive(
                  seasonal_model,
                  observed_time_series,
                  timesteps_are_event_shape=False,
                  parameter_samples=parameter_samples,
              )
  
          prob_lower = predictive_dist.cdf(
              observed_time_series.time_series[..., 0]
          )
          tail_probabilities = 2 * tf.minimum(
              prob_lower, 1 - prob_lower
          )
          (
              lower_limit,
              upper_limit,
              predictive_mean,
          ) = compute_predictive_bounds(
              predictive_dist, anomaly_threshold=anomaly_threshold
          )
          restore_scale = lambda x: x * observed_stddev + observed_mean
          return (
              restore_scale(lower_limit),
              restore_scale(upper_limit),
              restore_scale(predictive_mean),
              tail_probabilities,
          )
  
imports: import collections
  import datetime
  import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.distributions import inverse_gamma
  from tensorflow_probability.python.distributions import normal
  from tensorflow_probability.python.experimental.sts_gibbs import gibbs_sampler
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.math import root_search
  from tensorflow_probability.python.sts import default_model
  from tensorflow_probability.python.sts import regularization
  from tensorflow_probability.python.sts.forecast import one_step_predictive
  from tensorflow_probability.python.sts.internal import seasonality_util
  from tensorflow_probability.python.sts.internal import util
label_desc: Use image node_id 2 for calling a global function with example usage: _detect_anomalies_inner(observed_time_series, seasonal_structure, anomaly_threshold, num_warmup_steps, num_samples, use_gibbs_predictive_dist, seed) and returns: restore_scale,  restore_scale,  restore_scale,  tail_probabilities