Attend the Women in ML Symposium on December 7 Register now


Stay organized with collections Save and categorize content based on your preferences.

Sum of structural time series components.

Inherits From: StructuralTimeSeries

Used in the notebooks

Used in the tutorials

This class enables compositional specification of a structural time series model from basic components. Given a list of component models, it represents an additive model, i.e., a model of time series that may be decomposed into a sum of terms corresponding to the component models.

Formally, the additive model represents a random process g[t] = f1[t] + f2[t] + ... + fN[t] + eps[t], where the f's are the random processes represented by the components, and eps[t] ~ Normal(loc=0, scale=observation_noise_scale) is an observation noise term. See the AdditiveStateSpaceModel documentation for mathematical details.

This model inherits the parameters (with priors) of its components, and adds an observation_noise_scale parameter governing the level of noise in the observed time series.


To construct a model combining a local linear trend with a day-of-week effect:

  local_trend = tfp.sts.LocalLinearTrend(
  day_of_week_effect = tfp.sts.Seasonal(
  additive_model = tfp.sts.Sum(
      components=[local_trend, day_of_week_effect],

  print([ for p in additive_model.parameters])
  # => `[observation_noise_scale,
  #      local_trend_level_scale,
  #      local_trend_slope_scale,
  #      day_of_week_effect_drift_scale`]

  # => `2`, `7`, `9`

components Python list of one or more StructuralTimeSeries instances. These must have unique names.
constant_offset optional float Tensor of shape broadcasting to concat([batch_shape, [num_timesteps]]) specifying a constant value added to the sum of outputs from the component models. This allows the components to model the shifted series observed_time_series - constant_offset. If None, this is set to the mean of the provided observed_time_series. Default value: None.
observation_noise_scale_prior optional tfd.Distribution instance specifying a prior on observation_noise_scale. If None, a heuristic default prior is constructed based on the provided observed_time_series. Default value: None.
observed_time_series optional float Tensor of shape batch_shape + [T, 1] (omitting the trailing unit dimension is also supported when T > 1), specifying an observed time series. Any NaNs are interpreted as missing observations; missingness may be also be explicitly specified by passing a tfp.sts.MaskedTimeSeries instance. Any priors not explicitly set will be given default values according to the scale of the observed time series (or batch of time series). Default value: None.
name Python str name of this model component; used as name_scope for ops created by this class. Default value: 'Sum'.

ValueError if components do not have unique names.

batch_shape Static batch shape of models represented by this component.
components List of component StructuralTimeSeries models.
components_by_name OrderedDict mapping component names to components.
constant_offset Constant value subtracted from observed data.
init_parameters Parameters used to instantiate this StructuralTimeSeries.
latent_size Python int dimensionality of the latent space in this model.
name Name of this model component.
parameters List of Parameter(name, prior, bijector) namedtuples for this model.



View source

Runtime batch shape of models represented by this component.

batch_shape int Tensor giving the broadcast batch shape of all model parameters. This should match the batch shape of derived state space models, i.e., self.make_state_space_model(...).batch_shape_tensor().


View source

Creates a deep copy.

**override_parameters_kwargs String/value dictionary of initialization arguments to override with new values.

copy A new instance of type(self) initialized from the union of self.init_parameters and override_parameters_kwargs, i.e., dict(self.init_parameters, **override_parameters_kwargs).


View source

Constructs the joint distribution over parameters and observed values.

observed_time_series Optional observed time series to model, as a Tensor or tfp.sts.MaskedTimeSeries instance having shape concat([batch_shape, trajectories_shape, num_timesteps, 1]). If an observed time series is provided, the num_timesteps, trajectories_shape, and mask arguments are ignored, and an unnormalized (pinned) distribution over parameter values is returned. Default value: None.
num_timesteps scalar int Tensor number of timesteps to model. This must be specified either directly or by passing an observed_time_series. Default value: 0.
trajectories_shape int Tensor shape of sampled trajectories for each set of parameter values. Ignored if an observed_time_series is passed. Default value: ().
initial_step Optional scalar int Tensor specifying the starting timestep. Default value: 0.
mask Optional bool Tensor having shape concat([batch_shape, trajectories_shape, num_timesteps]), in which True entries indicate that the series value at the corresponding step is missing and should be ignored. This argument should be passed only if observed_time_series is not specified or does not already contain a missingness mask; it is an error to pass both this argument and an observed_time_series value containing a missingness mask. Default value: None.
experimental_parallelize If True, use parallel message passing algorithms from tfp.experimental.parallel_filter to perform time series operations in O(log num_timesteps) sequential steps. The overall FLOP and memory cost may be larger than for the sequential implementations by a constant factor. Default value: False.

joint_distribution joint distribution of model parameters and observed trajectories. If no observed_time_series was specified, this is an instance of tfd.JointDistributionNamedAutoBatched with a random variable for each model parameter (with names and order matching self.parameters), plus a final random variable observed_time_series representing a trajectory(ies) conditioned on the parameters. If observed_time_series was specified, the return value is given by joint_distribution.experimental_pin( observed_time_series=observed_time_series) where joint_distribution is as just described, so it defines an unnormalized posterior distribution over the parameters.


The joint distribution can generate prior samples of parameters and trajectories:

from matplotlib import pylab as plt
import tensorflow_probability as tfp

# Sample and plot 100 trajectories from the prior.
model = tfp.sts.LocalLinearTrend()
prior_samples = model.joint_distribution(num_timesteps=200).sample([100])
  tf.linalg.matrix_transpose(prior_samples['observed_time_series'][..., 0]))

It also integrates with TFP inference APIs, providing a more flexible alternative to the STS-specific fitting utilities.

jd = model.joint_distribution(observed_time_series)

# Variational inference.
surrogate_posterior = (
losses =
parameter_samples = surrogate_posterior.sample(50)

# No U-Turn Sampler.
samples, kernel_results = tfp.experimental.mcmc.windowed_adaptive_nuts(
  n_draws=500, joint_dist=dist)


View source

Build the joint density log p(params) + log p(y|params) as a callable. (deprecated)

observed_time_series Observed Tensor trajectories of shape sample_shape + batch_shape + [num_timesteps, 1] (the trailing 1 dimension is optional if num_timesteps > 1), where batch_shape should match self.batch_shape (the broadcast batch shape of all priors on parameters for this structural time series model). Any NaNs are interpreted as missing observations; missingness may be also be explicitly specified by passing a tfp.sts.MaskedTimeSeries instance.

log_joint_fn A function taking a Tensor argument for each model parameter, in canonical order, and returning a Tensor log probability of shape batch_shape. Note that, unlike tfp.Distributions log_prob methods, the log_joint sums over the sample_shape from y, so that sample_shape does not appear in the output log_prob. This corresponds to viewing multiple samples in y as iid observations from a single model, which is typically the desired behavior for parameter inference.


View source

Build an ordered list of Distribution instances for component models.

num_timesteps Python int number of timesteps to model.
param_vals a list of Tensor parameter values in order corresponding to self.parameters, or a dict mapping from parameter names to values.
**linear_gaussian_ssm_kwargs Optional additional keyword arguments to to the base tfd.LinearGaussianStateSpaceModel constructor