Skip to content

Commit d4609c2

Browse files
davmretensorflower-gardener
authored andcommitted
Interpret NaNs as missing values in STS observed_time_series arguments.
This supports Pandas Series and DataFrame objects returned by the recently-added `regularize_series`. Note that these objects were *already* supported, insofar as they convert automatically to numpy arrays, but now they can use NaNs to represent missing values. We also now raise an error if the input is a Pandas instance indexed by a DatetimeIndex with no set frequency. PiperOrigin-RevId: 383679050
1 parent 2e9c757 commit d4609c2

15 files changed

+156
-69
lines changed

tensorflow_probability/python/sts/components/autoregressive.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,11 @@ def __init__(self,
315315
Default value: `None`.
316316
observed_time_series: optional `float` `Tensor` of shape
317317
`batch_shape + [T, 1]` (omitting the trailing unit dimension is also
318-
supported when `T > 1`), specifying an observed time series.
318+
supported when `T > 1`), specifying an observed time series. Any `NaN`s
319+
are interpreted as missing observations; missingness may be also be
320+
explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
319321
Any priors not explicitly set will be given default values according to
320-
the scale of the observed time series (or batch of time series). May
321-
optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes
322-
a mask `Tensor` to specify timesteps with missing observations.
322+
the scale of the observed time series (or batch of time series).
323323
Default value: `None`.
324324
name: the name of this model component.
325325
Default value: 'Autoregressive'.

tensorflow_probability/python/sts/components/dynamic_regression.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,13 @@ def __init__(self,
257257
Must have event shape `[num_features]`. If `None`, a weakly-informative
258258
Normal(0., 10.) prior is used.
259259
Default value: `None`.
260-
observed_time_series: `float` `Tensor` of shape `batch_shape + [T, 1]`
261-
(omitting the trailing unit dimension is also supported when `T > 1`),
262-
specifying an observed time series. Any priors not explicitly set will
263-
be given default values according to the scale of the observed time
264-
series (or batch of time series). May optionally be an instance of
265-
`tfp.sts.MaskedTimeSeries`, which includes a mask `Tensor` to specify
266-
timesteps with missing observations.
260+
observed_time_series: optional `float` `Tensor` of shape
261+
`batch_shape + [T, 1]` (omitting the trailing unit dimension is also
262+
supported when `T > 1`), specifying an observed time series. Any `NaN`s
263+
are interpreted as missing observations; missingness may be also be
264+
explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
265+
Any priors not explicitly set will be given default values according to
266+
the scale of the observed time series (or batch of time series).
267267
Default value: `None`.
268268
name: Python `str` for the name of this component.
269269
Default value: 'DynamicLinearRegression'.

tensorflow_probability/python/sts/components/local_level.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,11 @@ def __init__(self,
272272
Default value: `None`.
273273
observed_time_series: optional `float` `Tensor` of shape
274274
`batch_shape + [T, 1]` (omitting the trailing unit dimension is also
275-
supported when `T > 1`), specifying an observed time series.
275+
supported when `T > 1`), specifying an observed time series. Any `NaN`s
276+
are interpreted as missing observations; missingness may be also be
277+
explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
276278
Any priors not explicitly set will be given default values according to
277-
the scale of the observed time series (or batch of time series). May
278-
optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes
279-
a mask `Tensor` to specify timesteps with missing observations.
279+
the scale of the observed time series (or batch of time series).
280280
Default value: `None`.
281281
name: the name of this model component.
282282
Default value: 'LocalLevel'.

tensorflow_probability/python/sts/components/local_linear_trend.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,11 @@ def __init__(self,
341341
Default value: `None`.
342342
observed_time_series: optional `float` `Tensor` of shape
343343
`batch_shape + [T, 1]` (omitting the trailing unit dimension is also
344-
supported when `T > 1`), specifying an observed time series.
344+
supported when `T > 1`), specifying an observed time series. Any `NaN`s
345+
are interpreted as missing observations; missingness may be also be
346+
explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
345347
Any priors not explicitly set will be given default values according to
346-
the scale of the observed time series (or batch of time series). May
347-
optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes
348-
a mask `Tensor` to specify timesteps with missing observations.
348+
the scale of the observed time series (or batch of time series).
349349
Default value: `None`.
350350
name: the name of this model component.
351351
Default value: 'LocalLinearTrend'.

tensorflow_probability/python/sts/components/seasonal.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,11 @@ def __init__(self,
803803
Default value: `True`.
804804
observed_time_series: optional `float` `Tensor` of shape
805805
`batch_shape + [T, 1]` (omitting the trailing unit dimension is also
806-
supported when `T > 1`), specifying an observed time series.
806+
supported when `T > 1`), specifying an observed time series. Any `NaN`s
807+
are interpreted as missing observations; missingness may be also be
808+
explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
807809
Any priors not explicitly set will be given default values according to
808-
the scale of the observed time series (or batch of time series). May
809-
optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes
810-
a mask `Tensor` to specify timesteps with missing observations.
810+
the scale of the observed time series (or batch of time series).
811811
Default value: `None`.
812812
name: the name of this model component.
813813
Default value: 'Seasonal'.

tensorflow_probability/python/sts/components/semilocal_linear_trend.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,11 @@ def __init__(self,
348348
Default value: `None`.
349349
observed_time_series: optional `float` `Tensor` of shape
350350
`batch_shape + [T, 1]` (omitting the trailing unit dimension is also
351-
supported when `T > 1`), specifying an observed time series.
351+
supported when `T > 1`), specifying an observed time series. Any `NaN`s
352+
are interpreted as missing observations; missingness may be also be
353+
explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
352354
Any priors not explicitly set will be given default values according to
353-
the scale of the observed time series (or batch of time series). May
354-
optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes
355-
a mask `Tensor` to specify timesteps with missing observations.
355+
the scale of the observed time series (or batch of time series).
356356
Default value: `None`.
357357
constrain_ar_coef_stationary: if `True`, perform inference using a
358358
parameterization that restricts `autoregressive_coef` to the interval

tensorflow_probability/python/sts/components/smooth_seasonal.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,11 @@ def __init__(self,
389389
is constructed based on the provided `observed_time_series`.
390390
observed_time_series: optional `float` `Tensor` of shape
391391
`batch_shape + [T, 1]` (omitting the trailing unit dimension is also
392-
supported when `T > 1`), specifying an observed time series.
392+
supported when `T > 1`), specifying an observed time series. Any `NaN`s
393+
are interpreted as missing observations; missingness may be also be
394+
explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
393395
Any priors not explicitly set will be given default values according to
394-
the scale of the observed time series (or batch of time series). May
395-
optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes
396-
a mask `Tensor` to specify timesteps with missing observations.
396+
the scale of the observed time series (or batch of time series).
397397
Default value: `None`.
398398
name: the name of this model component.
399399
Default value: 'SmoothSeasonal'.

tensorflow_probability/python/sts/components/sum.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,11 @@ def __init__(self,
408408
Default value: `None`.
409409
observed_time_series: optional `float` `Tensor` of shape
410410
`batch_shape + [T, 1]` (omitting the trailing unit dimension is also
411-
supported when `T > 1`), specifying an observed time series. This is
412-
used to set the constant offset, if not provided, and to construct a
413-
default heuristic `observation_noise_scale_prior` if not provided. May
414-
optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes
415-
a mask `Tensor` to specify timesteps with missing observations.
411+
supported when `T > 1`), specifying an observed time series. Any `NaN`s
412+
are interpreted as missing observations; missingness may be also be
413+
explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
414+
Any priors not explicitly set will be given default values according to
415+
the scale of the observed time series (or batch of time series).
416416
Default value: `None`.
417417
name: Python `str` name of this model component; used as `name_scope`
418418
for ops created by this class.

tensorflow_probability/python/sts/decomposition.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ def decompose_by_component(model, observed_time_series, parameter_samples):
124124
Args:
125125
model: An instance of `tfp.sts.Sum` representing a structural time series
126126
model.
127-
observed_time_series: `float` `Tensor` of shape
128-
`batch_shape + [num_timesteps, 1]` (omitting the trailing unit dimension
129-
is also supported when `num_timesteps > 1`), specifying an observed time
130-
series. May optionally be an instance of `tfp.sts.MaskedTimeSeries`, which
131-
includes a mask `Tensor` to specify timesteps with missing observations.
127+
observed_time_series: optional `float` `Tensor` of shape
128+
`batch_shape + [T, 1]` (omitting the trailing unit dimension is also
129+
supported when `T > 1`), specifying an observed time series. Any `NaN`s
130+
are interpreted as missing observations; missingness may be also be
131+
explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
132132
parameter_samples: Python `list` of `Tensors` representing posterior
133133
samples of model parameters, with shapes `[concat([
134134
[num_posterior_draws], param.prior.batch_shape,

tensorflow_probability/python/sts/fitting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ def fit_with_hmc(model,
244244
observed_time_series: `float` `Tensor` of shape
245245
`concat([sample_shape, model.batch_shape, [num_timesteps, 1]])` where
246246
`sample_shape` corresponds to i.i.d. observations, and the trailing `[1]`
247-
dimension may (optionally) be omitted if `num_timesteps > 1`. May
248-
optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes
249-
a mask `Tensor` to specify timesteps with missing observations.
247+
dimension may (optionally) be omitted if `num_timesteps > 1`. Any `NaN`s
248+
are interpreted as missing observations; missingness may be also be
249+
explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
250250
num_results: Integer number of Markov chain draws.
251251
Default value: `100`.
252252
num_warmup_steps: Integer number of steps to take before starting to

0 commit comments

Comments
 (0)