Class Seasonal
Inherits From: StructuralTimeSeries
Formal representation of a seasonal effect model.
A seasonal effect model posits a fixed set of recurring, discrete 'seasons', each of which is active for a fixed number of timesteps and, while active, contributes a different effect to the time series. These are generally not meteorological seasons, but represent regular recurring patterns such as hour-of-day or day-of-week effects. Each season lasts for a fixed number of timesteps. The effect of each season drifts from one occurrence to the next following a Gaussian random walk:
effects[season, occurrence[i]] = (
effects[season, occurrence[i-1]] + Normal(loc=0., scale=drift_scale))
The drift_scale
parameter governs the standard deviation of the random walk;
for example, in a day-of-week model it governs the change in effect from this
Monday to next Monday.
Examples
A seasonal effect model representing day-of-week seasonality on hourly data:
day_of_week = tfp.sts.Seasonal(num_seasons=7,
num_steps_per_season=24,
observed_time_series=y,
name='day_of_week')
A seasonal effect model representing month-of-year seasonality on daily data, with explicit priors:
month_of_year = tfp.sts.Seasonal(
num_seasons=12,
num_steps_per_season=[31, 28, 31, 30, 30, 31, 31, 31, 30, 31, 30, 31],
drift_scale_prior=tfd.LogNormal(loc=-1., scale=0.1),
initial_effect_prior=tfd.Normal(loc=0., scale=5.),
name='month_of_year')
Note that a general implementation of month-of-year seasonality would require additional logic; this version works over time periods not involving a leap year.
A model representing both day-of-week and hour-of-day seasonality, on hourly data:
day_of_week = tfp.sts.Seasonal(num_seasons=7,
num_steps_per_season=24,
observed_time_series=y,
name='day_of_week')
hour_of_day = tfp.sts.Seasonal(num_seasons=24,
num_steps_per_season=1,
observed_time_series=y,
name='hour_of_day')
model = tfp.sts.Sum(components=[day_of_week, hour_of_day],
observed_time_series=y)
__init__
__init__(
num_seasons,
num_steps_per_season=1,
drift_scale_prior=None,
initial_effect_prior=None,
observed_time_series=None,
name=None
)
Specify a seasonal effects model.
Args:
num_seasons
: Scalar Pythonint
number of seasons.num_steps_per_season
: Pythonint
number of steps in each season. This may be either a scalar (shape[]
), in which case all seasons have the same length, or a NumPy array of shape[num_seasons]
. Default value: 1.drift_scale_prior
: optionaltfd.Distribution
instance specifying a prior on thedrift_scale
parameter. IfNone
, a heuristic default prior is constructed based on the providedobserved_time_series
. Default value:None
.initial_effect_prior
: optionaltfd.Distribution
instance specifying a normal prior on the initial effect of each season. This may be either a scalartfd.Normal
prior, in which case it applies independently to every season, or it may be multivariate normal (e.g.,tfd.MultivariateNormalDiag
) with event shape[num_seasons]
, in which case it specifies a joint prior across all seasons. IfNone
, a heuristic default prior is constructed based on the providedobserved_time_series
. Default value:None
.observed_time_series
: optionalfloat
Tensor
of shapebatch_shape + [T, 1]
(omitting the trailing unit dimension is also supported whenT > 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). Default value:None
.name
: the name of this model component. Default value: 'Seasonal'.
Properties
batch_shape
Static batch shape of models represented by this component.
Returns:
batch_shape
: Atf.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.
num_seasons
Number of seasons.
num_steps_per_season
Number of steps per season.
parameters
List of Parameter(name, prior, bijector) namedtuples for this model.
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
: ObservedTensor
trajectories of shapesample_shape + batch_shape + [num_timesteps, 1]
(the trailing1
dimension is optional ifnum_timesteps > 1
), wherebatch_shape
should matchself.batch_shape
(the broadcast batch shape of all priors on parameters for this structural time series model).
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=None,
initial_state_prior=None,
initial_step=0
)
Instantiate this model as a Distribution over specified num_timesteps
.
Args:
num_timesteps
: Pythonint
number of timesteps to model.param_vals
: a list ofTensor
parameter values in order corresponding toself.parameters
, or a dict mapping from parameter names to values.initial_state_prior
: an optionalDistribution
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
: optionalint
specifying the initial timestep to model. This is relevant when the model contains time-varying components, e.g., holidays or seasonality.
Returns:
dist
: aLinearGaussianStateSpaceModel
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
: Scalarint
Tensor
number of timesteps to model.initial_step
: Optional scalarint
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
: Pythonint
random seed.
Returns:
trajectories
:float
Tensor
of shapetrajectories_sample_shape + params_sample_shape + [num_timesteps, 1]
containing all sampled trajectories.param_samples
: list of sampled parameter valueTensor
s, in order corresponding toself.parameters
, each of shapeparams_sample_shape + prior.batch_shape + prior.event_shape
.