code: def _resample_latents(
      observed_residuals,
      level_scale,
      observation_noise_scale,
      initial_state_prior,
      slope_scale=None,
      seasonal_components=None,
      seasonal_drift_scales=None,
      is_missing=None,
      sample_shape=(),
      seed=None,
  ):
      """Uses Durbin-Koopman sampling to resample the model's latents.
  
      Durbin-Koopman sampling [1] is an efficient algorithm to sample from the
      posterior latents of a linear Gaussian state space model. This method
      implements the algorithm.
  
      [1] Durbin, J. and Koopman, S.J. (2002) A simple and efficient simulation
          smoother for state space time series analysis.
  
      Args:
        observed_residuals: Float `Tensor` of shape `[..., num_observations]`,
          specifying the centered observations `(x - loc)`.
        level_scale: Float scalar `Tensor` (may contain batch dimensions) specifying
          the standard deviation of the level random walk steps.
        observation_noise_scale: Float scalar `Tensor` (may contain batch
          dimensions) specifying the standard deviation of the observation noise.
        initial_state_prior: instance of `tfd.MultivariateNormalLinearOperator`.
        slope_scale: Optional float scalar `Tensor` (may contain batch dimensions)
          specifying the standard deviation of slope random walk steps. If provided,
          a `LocalLinearTrend` model is used, otherwise, a `LocalLevel` model is
          used.
        seasonal_components: An optional list of sts.Season components. This and
          `seasonal_drift_scales` must have the same number of seasonal components.
        seasonal_drift_scales: An optional float scalar `Tensor` (may contain batch
          dimensions) of shape [num_seasonal_components].
        is_missing: Optional `bool` `Tensor` missingness mask.
        sample_shape: Optional `int` `Tensor` shape of samples to draw.
        seed: `int` `Tensor` of shape `[2]` controlling stateless sampling.
  
      Returns:
        latents: Float `Tensor` resampled latent level and (optional) seasonal,
           of shape `[..., num_timesteps, latent_size]`, where `...` concatenates
           the sample shape with any batch shape from `observed_time_series`.
           The level latents are returned first, then each of the seasonal
           components.
      """
      if seasonal_components is None:
          seasonal_components = []
  
      num_timesteps = prefer_static.shape(observed_residuals)[-1]
      if slope_scale is None:
          local_ssm = local_level.LocalLevelStateSpaceModel(
              num_timesteps=num_timesteps,
              initial_state_prior=initial_state_prior,
              observation_noise_scale=observation_noise_scale,
              level_scale=level_scale,
          )
      else:
          local_ssm = (
              local_linear_trend.LocalLinearTrendStateSpaceModel(
                  num_timesteps=num_timesteps,
                  initial_state_prior=initial_state_prior,
                  observation_noise_scale=observation_noise_scale,
                  level_scale=level_scale,
                  slope_scale=slope_scale,
              )
          )
  
      if seasonal_components:
          # It is assumed the the local ssm is added in first.
          ssms = [local_ssm]
          # Add an SSM for each season.
          for season_index, seasonal_component in enumerate(
              seasonal_components
          ):
              seasonal_drift_scale = seasonal_drift_scales[
                  ..., season_index
              ]
              ssms.append(
                  seasonal_component.make_state_space_model(
                      num_timesteps=num_timesteps,
                      initial_state_prior=seasonal_component.initial_state_prior,
                      param_vals={},
                      drift_scale=seasonal_drift_scale,
                      # Set this to exactly 0, since we want the observation noise
                      # of the entire additive space to match observation_noise_scale,
                      # but that is already specified on the local ssm.
                      observation_noise_scale=0.0,
                  )
              )
          # No observation_noise_scale is needed here since it is taken from the local
          # ssm (which is equivalent).
          ssm = sum_lib.AdditiveStateSpaceModel(ssms)
      else:
          # Only the local value needs to be sampled, no addition needed.
          ssm = local_ssm
  
      return ssm.posterior_sample(
          observed_residuals[..., tf.newaxis],
          sample_shape=sample_shape,
          mask=is_missing,
          seed=seed,
      )
  
imports: import collections
  import itertools
  import numpy
  import six
  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 categorical
  from tensorflow_probability.python.distributions import distribution
  from tensorflow_probability.python.distributions import gamma
  from tensorflow_probability.python.distributions import inverse_gamma
  from tensorflow_probability.python.distributions import mixture_same_family
  from tensorflow_probability.python.distributions import mvn_diag
  from tensorflow_probability.python.distributions import mvn_linear_operator
  from tensorflow_probability.python.distributions import normal
  from tensorflow_probability.python.distributions import normal_conjugate_posteriors
  from tensorflow_probability.python.distributions import transformed_distribution
  from tensorflow_probability.python.experimental.distributions import mvn_precision_factor_linop
  from tensorflow_probability.python.experimental.sts_gibbs import dynamic_spike_and_slab
  from tensorflow_probability.python.experimental.sts_gibbs import sample_parameters
  from tensorflow_probability.python.experimental.sts_gibbs import spike_and_slab
  from tensorflow_probability.python.internal import distribution_util
  from tensorflow_probability.python.internal import dtype_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import reparameterization
  from tensorflow_probability.python.internal import samplers
  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 sum
  from tensorflow_probability.python.sts.internal import util
label_desc: Use image node_id 10 for calling a global function with example usage: _resample_latents(observed_residuals, level_scale, observation_noise_scale, initial_state_prior, slope_scale, seasonal_components, seasonal_drift_scales, is_missing, sample_shape, seed) and returns: ssm