# tfp.sts.SemiLocalLinearTrend

## Class `SemiLocalLinearTrend`

Formal representation of a semi-local linear trend model.

Inherits From: `StructuralTimeSeries`

Like the `LocalLinearTrend` model, a semi-local linear trend posits a latent `level` and `slope`, with the level component updated according to the current slope plus a random walk:

``````level[t] = level[t-1] + slope[t-1] + Normal(0., level_scale)
``````

The slope component in a `SemiLocalLinearTrend` model evolves according to a first-order autoregressive (AR1) process with potentially nonzero mean:

``````slope[t] = (slope_mean +
autoregressive_coef * (slope[t-1] - slope_mean) +
Normal(0., slope_scale))
``````

Unlike the random walk used in `LocalLinearTrend`, a stationary AR1 process (coefficient in `(-1, 1)`) maintains bounded variance over time, so a `SemiLocalLinearTrend` model will often produce more reasonable uncertainties when forecasting over long timescales.

## `__init__`

View source

``````__init__(
level_scale_prior=None,
slope_mean_prior=None,
slope_scale_prior=None,
autoregressive_coef_prior=None,
initial_level_prior=None,
initial_slope_prior=None,
observed_time_series=None,
constrain_ar_coef_stationary=True,
constrain_ar_coef_positive=False,
name=None
)
``````

Specify a semi-local linear trend model.

#### Args:

• `level_scale_prior`: optional `tfd.Distribution` instance specifying a prior on the `level_scale` parameter. If `None`, a heuristic default prior is constructed based on the provided `observed_time_series`. Default value: `None`.
• `slope_mean_prior`: optional `tfd.Distribution` instance specifying a prior on the `slope_mean` parameter. If `None`, a heuristic default prior is constructed based on the provided `observed_time_series`. Default value: `None`.
• `slope_scale_prior`: optional `tfd.Distribution` instance specifying a prior on the `slope_scale` parameter. If `None`, a heuristic default prior is constructed based on the provided `observed_time_series`. Default value: `None`.
• `autoregressive_coef_prior`: optional `tfd.Distribution` instance specifying a prior on the `autoregressive_coef` parameter. If `None`, the default prior is a standard `Normal(0., 1.)`. Note that the prior may be implicitly truncated by `constrain_ar_coef_stationary` and/or `constrain_ar_coef_positive`. Default value: `None`.
• `initial_level_prior`: optional `tfd.Distribution` instance specifying a prior on the initial level. If `None`, a heuristic default prior is constructed based on the provided `observed_time_series`. Default value: `None`.
• `initial_slope_prior`: optional `tfd.Distribution` instance specifying a prior on the initial slope. 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 priors not explicitly set will be given default values according to the scale of the observed time series (or batch of time series). May optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes a mask `Tensor` to specify timesteps with missing observations. Default value: `None`.
• `constrain_ar_coef_stationary`: if `True`, perform inference using a parameterization that restricts `autoregressive_coef` to the interval `(-1, 1)`, or `(0, 1)` if `force_positive_ar_coef` is also `True`, corresponding to stationary processes. This will implicitly truncates the support of `autoregressive_coef_prior`. Default value: `True`.
• `constrain_ar_coef_positive`: if `True`, perform inference using a parameterization that restricts `autoregressive_coef` to be positive, or in `(0, 1)` if `constrain_ar_coef_stationary` is also `True`. This will implicitly truncate the support of `autoregressive_coef_prior`. Default value: `False`.
• `name`: the name of this model component. Default value: 'SemiLocalLinearTrend'.

## Properties

### `batch_shape`

Static batch shape of models represented by this component.

#### Returns:

• `batch_shape`: A `tf.TensorShape` 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`. It may be partially defined or unknown.

### `initial_state_prior`

Prior distribution on the initial latent state (level and scale).

### `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.

## Methods

### `batch_shape_tensor`

View source

``````batch_shape_tensor()
``````

Runtime batch shape of models represented by this component.

#### Returns:

• `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()`.

### `joint_log_prob`

View source

``````joint_log_prob(observed_time_series)
``````

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

#### Args:

• `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). May optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes a mask `Tensor` to specify timesteps with missing observations.

#### Returns:

• `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.

### `make_state_space_model`

View source

``````make_state_space_model(
num_timesteps,
param_vals=None,
initial_state_prior=None,
initial_step=0
)
``````

Instantiate this model as a Distribution over specified `num_timesteps`.

#### Args:

• `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.
• `initial_state_prior`: an optional `Distribution` instance overriding the default prior on the model's initial state. This is used in forecasting ("today's prior is yesterday's posterior").
• `initial_step`: optional `int` specifying the initial timestep to model. This is relevant when the model contains time-varying components, e.g., holidays or seasonality.

#### Returns:

• `dist`: a `LinearGaussianStateSpaceModel` Distribution object.

### `prior_sample`

View source

``````prior_sample(
num_timesteps,
initial_step=0,
params_sample_shape=(),
trajectories_sample_shape=(),
seed=None
)
``````

Sample from the joint prior over model parameters and trajectories.

#### Args:

• `num_timesteps`: Scalar `int` `Tensor` number of timesteps to model.
• `initial_step`: Optional scalar `int` `Tensor` specifying the starting timestep. Default value: 0.
• `params_sample_shape`: Number of possible worlds to sample iid from the parameter prior, or more generally, `Tensor` `int` shape to fill with iid samples. Default value: .
• `trajectories_sample_shape`: For each sampled set of parameters, number of trajectories to sample, or more generally, `Tensor` `int` shape to fill with iid samples. Default value: .
• `seed`: Python `int` random seed.

#### Returns:

• `trajectories`: `float` `Tensor` of shape `trajectories_sample_shape + params_sample_shape + [num_timesteps, 1]` containing all sampled trajectories.
• `param_samples`: list of sampled parameter value `Tensor`s, in order corresponding to `self.parameters`, each of shape `params_sample_shape + prior.batch_shape + prior.event_shape`.