Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/stochastic inputs #833

Merged
merged 4 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions darts/tests/models/forecasting/test_probabilistic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,24 @@ def _get_avgs(series):
"The difference between the mean forecast and the mean series is larger "
"than expected on component 1 for distribution {}".format(lkl),
)

def test_stochastic_inputs(self):
model = RNNModel(input_chunk_length=5)
model.fit(self.constant_ts, epochs=2)

# build a stochastic series
target_vals = self.constant_ts.values()
stochastic_vals = np.random.normal(
loc=target_vals, scale=1.0, size=(len(self.constant_ts), 100)
)
stochastic_vals = np.expand_dims(stochastic_vals, axis=1)
stochastic_series = TimeSeries.from_times_and_values(
self.constant_ts.time_index, stochastic_vals
)

# A deterministic model forecasting a stochastic series
# should return stochastic samples
preds = [model.predict(series=stochastic_series, n=10) for _ in range(2)]

# random samples should differ
self.assertFalse(np.alltrue(preds[0].values() == preds[1].values()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nitpicking but isn't there a slim chance that the predictions will be identical? :D we could add a seed to make sure they are not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, normally there is a seed earlier in the file which I believe should apply here :)

22 changes: 22 additions & 0 deletions darts/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,28 @@ def values(self, copy=True, sample=0) -> np.ndarray:
else:
return self._xa.values[:, :, sample]

def random_component_values(self, copy=True) -> np.array:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about an additional optional to randomly sample a quantile per sample?

I would assume this is similar to quantile regression based on stochastic input (without needing QuantileRegression as a likelihood)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you mean returning the exact quantile instead of sampling a sample at random? That's an interesting idea :) But would require a bit of adaptation elsewhere as we would still need to ensure this is applied on the output targets only, and not e.g. on the inputs or covariates. Let's keep the idea though.

"""
Return a 2-D array of shape (time, component), containing the values for
one sample taken uniformly at random among this series' samples.

Parameters
----------
copy
Whether to return a copy of the values, otherwise returns a view.
Leave it to True unless you know what you are doing.

Returns
-------
numpy.ndarray
The values composing one sample taken at random from the time series.
"""
sample = np.random.randint(low=0, high=self.n_samples)
if copy:
return np.copy(self._xa.values[:, :, sample])
else:
return self._xa.values[:, :, sample]

def all_values(self, copy=True) -> np.ndarray:
"""
Return a 3-D array of dimension (time, component, sample),
Expand Down
6 changes: 4 additions & 2 deletions darts/utils/data/horizon_based_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def __getitem__(
# determine the index of the time series.
ts_idx = idx // self.nr_samples_per_ts
ts_target = self.target_series[ts_idx]
target_vals = ts_target.values(copy=False)
target_vals = ts_target.random_component_values(copy=False)

raise_if_not(
len(target_vals)
Expand Down Expand Up @@ -168,7 +168,9 @@ def __getitem__(
f"({idx}-th sample)",
)

covariate = ts_covariate.values(copy=False)[cov_start:cov_end]
covariate = ts_covariate.random_component_values(copy=False)[
cov_start:cov_end
]

raise_if_not(
len(covariate) == len(past_target),
Expand Down
8 changes: 6 additions & 2 deletions darts/utils/data/inference_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ def __getitem__(
)

# extract past target values
past_target = target_series.values(copy=False)[-self.input_chunk_length :]
past_target = target_series.random_component_values(copy=False)[
-self.input_chunk_length :
]

# optionally, extract covariates
cov_past, cov_future = None, None
Expand All @@ -181,7 +183,9 @@ def __getitem__(
)

# extract covariate values and split into a past (historic) and future part
covariate = covariate_series.values(copy=False)[cov_start:cov_end]
covariate = covariate_series.random_component_values(copy=False)[
cov_start:cov_end
]
if self.input_chunk_length != 0: # regular models
cov_past, cov_future = (
covariate[: self.input_chunk_length],
Expand Down
6 changes: 4 additions & 2 deletions darts/utils/data/shifted_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def __getitem__(self, idx) -> Tuple[np.ndarray, Optional[np.ndarray], np.ndarray
# determine the index of the time series.
ts_idx = idx // self.max_samples_per_ts
ts_target = self.target_series[ts_idx]
target_vals = ts_target.values(copy=False)
target_vals = ts_target.random_component_values(copy=False)

# determine the actual number of possible samples in this time series
n_samples_in_ts = len(target_vals) - self.size_of_both_chunks + 1
Expand Down Expand Up @@ -582,7 +582,9 @@ def __getitem__(self, idx) -> Tuple[np.ndarray, Optional[np.ndarray], np.ndarray
f"that don't extend far enough into the future. ({idx}-th sample)",
)

covariate = ts_covariate.values(copy=False)[cov_start:cov_end]
covariate = ts_covariate.random_component_values(copy=False)[
cov_start:cov_end
]

raise_if_not(
len(covariate)
Expand Down