![]() |
Formal representation of a smooth seasonal effect model.
Inherits From: StructuralTimeSeries
tfp.sts.SmoothSeasonal(
period, frequency_multipliers, allow_drift=True, drift_scale_prior=None,
initial_state_prior=None, observed_time_series=None, name=None
)
The smooth seasonal model uses a set of trigonometric terms in order to
capture a recurring pattern whereby adjacent (in time) effects are
similar. The model uses frequencies
calculated via:
frequencies[j] = 2. * pi * frequency_multipliers[j] / period
and then posits two latent states for each frequency
. The two latent states
associated with frequency j
drift over time via:
effect[t] = (effect[t - 1] * cos(frequencies[j]) +
auxiliary[t - 1] * sin(frequencies[j]) +
Normal(0., drift_scale))
auxiliary[t] = (-effect[t - 1] * sin(frequencies[j]) +
auxiliary[t - 1] * cos(frequencies[j]) +
Normal(0., drift_scale))
where effect
is the smooth seasonal effect and auxiliary
only appears as a
matter of construction. The interpretation of auxiliary
is thus not
particularly important.
Examples
A smooth seasonal effect model representing smooth weekly seasonality on daily data:
component = SmoothSeasonal(
period=7,
frequency_multipliers=[1, 2, 3],
initial_state_prior=tfd.MultivariateNormalDiag(scale_diag=tf.ones([6])),
)
Args | |
---|---|
period
|
positive scalar float Tensor giving the number of timesteps
required for the longest cyclic effect to repeat.
|
frequency_multipliers
|
One-dimensional float Tensor listing the
frequencies (cyclic components) included in the model, as multipliers of
the base/fundamental frequency 2. * pi / period . Each component is
specified by the number of times it repeats per period, and adds two
latent dimensions to the model. A smooth seasonal model that can
represent any periodic function is given by frequency_multipliers = [1,
2, ..., floor(period / 2)] . However, it is often desirable to enforce a
smoothness assumption (and reduce the computational burden) by dropping
some of the higher frequencies.
|
allow_drift
|
optional Python bool specifying whether the seasonal
effects can drift over time. Setting this to False
removes the drift_scale parameter from the model. This is
mathematically equivalent to
drift_scale_prior = tfd.Deterministic(0.) , but removing drift
directly is preferred because it avoids the use of a degenerate prior.
Default value: True .
|
drift_scale_prior
|
optional tfd.Distribution instance specifying a prior
on the drift_scale parameter. If None , a heuristic default prior is
constructed based on the provided observed_time_series .
Default value: None .
|
initial_state_prior
|
instance of tfd.MultivariateNormal representing
the prior distribution on the latent states. Must have event shape
[2 * len(frequency_multipliers)] . If None , a heuristic default prior
is constructed based on the provided observed_time_series .
|
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 .
|
name
|
the name of this model component. Default value: 'SmoothSeasonal'. |
Attributes | |
---|---|
allow_drift
|
Whether the seasonal effects are allowed to drift over time. |
batch_shape
|
Static batch shape of models represented by this component. |
frequency_multipliers
|
Multipliers of the fundamental frequency. |
initial_state_prior
|
Prior distribution on the initial latent states. |
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. |
period
|
The seasonal period. |
Methods
batch_shape_tensor
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
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
make_state_space_model(
num_timesteps, param_vals, 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
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: [] (i.e., draw a single sample and don't expand the
shape).
|
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: [] (i.e., draw a single sample and don't expand the
shape).
|
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 .
|