tfp.substrates.numpy.sts.impute_missing_values
Stay organized with collections
Save and categorize content based on your preferences.
Runs posterior inference to impute the missing values in a time series.
tfp.substrates.numpy.sts.impute_missing_values(
model,
observed_time_series,
parameter_samples,
include_observation_noise=False,
timesteps_are_event_shape=True
)
This method computes the posterior marginals p(latent state | observations)
,
given the time series at observed timesteps (a missingness mask should
be specified using tfp.sts.MaskedTimeSeries
). It pushes this posterior back
through the observation model to impute a predictive distribution on the
observed time series. At unobserved steps, this is an imputed value; at other
steps it is interpreted as the model's estimate of the underlying noise-free
series.
Args |
model
|
tfp.sts.Sum instance defining an additive STS model.
|
observed_time_series
|
float Tensor of shape
concat([sample_shape, model.batch_shape, [num_timesteps, 1]]) where
sample_shape corresponds to i.i.d. observations, and the trailing [1]
dimension may (optionally) be omitted if num_timesteps > 1 . Any NaN s
are interpreted as missing observations; missingness may be also be
explicitly specified by passing a tfp.sts.MaskedTimeSeries instance.
|
parameter_samples
|
Python list of Tensors representing posterior
samples of model parameters, with shapes [concat([
[num_posterior_draws], param.prior.batch_shape,
param.prior.event_shape]) for param in model.parameters] . This may
optionally also be a map (Python dict ) of parameter names to
Tensor values.
|
include_observation_noise
|
If False , the imputed uncertainties
represent the model's estimate of the noise-free time series at each
timestep. If True , they represent the model's estimate of the range of
values that could be observed at each timestep, including any i.i.d.
observation noise.
Default value: False .
|
timesteps_are_event_shape
|
Deprecated, for backwards compatibility only.
If False , the predictive distribution will return per-timestep
probabilities
Default value: True .
|
Returns |
imputed_series_dist
|
a tfd.MixtureSameFamily instance with event shape
[num_timesteps] if timesteps_are_event_shape else [] and
batch shape concat([sample_shape, model.batch_shape,
[] if timesteps_are_event_shape else [num_timesteps]) , with
num_posterior_draws mixture components.
|
Example
To specify a time series with missing values, use tfp.sts.MaskedTimeSeries
:
time_series_with_nans = [-1., 1., np.nan, 2.4, np.nan, 5]
observed_time_series = tfp.sts.MaskedTimeSeries(
time_series=time_series_with_nans,
is_missing=tf.math.is_nan(time_series_with_nans))
Masked time series can be passed to tfp.sts
methods in place of a
observed_time_series
Tensor
:
# Build model using observed time series to set heuristic priors.
linear_trend_model = tfp.sts.LocalLinearTrend(
observed_time_series=observed_time_series)
model = tfp.sts.Sum([linear_trend_model],
observed_time_series=observed_time_series)
# Fit model to data
parameter_samples, _ = tfp.sts.fit_with_hmc(model, observed_time_series)
After fitting a model, impute_missing_values
will return a distribution
# Impute missing values
imputed_series_distribution = tfp.sts.impute_missing_values(
model, observed_time_series, parameter_samples=parameter_samples)
print('imputed means and stddevs: ',
imputed_series_distribution.mean(),
imputed_series_distribution.stddev())
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-11-21 UTC.
[null,null,["Last updated 2023-11-21 UTC."],[],[],null,["# tfp.substrates.numpy.sts.impute_missing_values\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/probability/blob/v0.23.0/tensorflow_probability/substrates/numpy/sts/forecast.py#L410-L546) |\n\nRuns posterior inference to impute the missing values in a time series.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tfp.experimental.substrates.numpy.sts.impute_missing_values`](https://www.tensorflow.org/probability/api_docs/python/tfp/substrates/numpy/sts/impute_missing_values)\n\n\u003cbr /\u003e\n\n tfp.substrates.numpy.sts.impute_missing_values(\n model,\n observed_time_series,\n parameter_samples,\n include_observation_noise=False,\n timesteps_are_event_shape=True\n )\n\nThis method computes the posterior marginals `p(latent state | observations)`,\ngiven the time series at observed timesteps (a missingness mask should\nbe specified using [`tfp.sts.MaskedTimeSeries`](../../../../tfp/sts/MaskedTimeSeries)). It pushes this posterior back\nthrough the observation model to impute a predictive distribution on the\nobserved time series. At unobserved steps, this is an imputed value; at other\nsteps it is interpreted as the model's estimate of the underlying noise-free\nseries.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `model` | [`tfp.sts.Sum`](../../../../tfp/sts/Sum) instance defining an additive STS model. |\n| `observed_time_series` | `float` `Tensor` of shape `concat([sample_shape, model.batch_shape, [num_timesteps, 1]])` where `sample_shape` corresponds to i.i.d. observations, and the trailing `[1]` dimension may (optionally) be omitted if `num_timesteps \u003e 1`. Any `NaN`s are interpreted as missing observations; missingness may be also be explicitly specified by passing a [`tfp.sts.MaskedTimeSeries`](../../../../tfp/sts/MaskedTimeSeries) instance. |\n| `parameter_samples` | Python `list` of `Tensors` representing posterior samples of model parameters, with shapes `[concat([ [num_posterior_draws], param.prior.batch_shape, param.prior.event_shape]) for param in model.parameters]`. This may optionally also be a map (Python `dict`) of parameter names to `Tensor` values. |\n| `include_observation_noise` | If `False`, the imputed uncertainties represent the model's estimate of the noise-free time series at each timestep. If `True`, they represent the model's estimate of the range of values that could be *observed* at each timestep, including any i.i.d. observation noise. Default value: `False`. |\n| `timesteps_are_event_shape` | Deprecated, for backwards compatibility only. If `False`, the predictive distribution will return per-timestep probabilities Default value: `True`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|-----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `imputed_series_dist` | a `tfd.MixtureSameFamily` instance with event shape `[num_timesteps] if timesteps_are_event_shape else []` and batch shape `concat([sample_shape, model.batch_shape, [] if timesteps_are_event_shape else [num_timesteps])`, with `num_posterior_draws` mixture components. |\n\n\u003cbr /\u003e\n\n#### Example\n\nTo specify a time series with missing values, use [`tfp.sts.MaskedTimeSeries`](../../../../tfp/sts/MaskedTimeSeries): \n\n time_series_with_nans = [-1., 1., np.nan, 2.4, np.nan, 5]\n observed_time_series = tfp.sts.MaskedTimeSeries(\n time_series=time_series_with_nans,\n is_missing=tf.math.is_nan(time_series_with_nans))\n\nMasked time series can be passed to [`tfp.sts`](../../../../tfp/sts) methods in place of a\n`observed_time_series` `Tensor`: \n\n # Build model using observed time series to set heuristic priors.\n linear_trend_model = tfp.sts.LocalLinearTrend(\n observed_time_series=observed_time_series)\n model = tfp.sts.Sum([linear_trend_model],\n observed_time_series=observed_time_series)\n\n # Fit model to data\n parameter_samples, _ = tfp.sts.fit_with_hmc(model, observed_time_series)\n\nAfter fitting a model, `impute_missing_values` will return a distribution \n\n # Impute missing values\n imputed_series_distribution = tfp.sts.impute_missing_values(\n model, observed_time_series, parameter_samples=parameter_samples)\n print('imputed means and stddevs: ',\n imputed_series_distribution.mean(),\n imputed_series_distribution.stddev())"]]