-
Notifications
You must be signed in to change notification settings - Fork 750
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
Implemented moving average #926
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b50273f
Add moving average
8452e4f
Add moving average
9258b5f
Add moving average - added tests
05c38ff
context_length now optional::forecast_start is now used::updated tests
c9d190b
refinments added
65b0e1e
Merge branch 'master' into moving-average
melopeo d1de8cc
added assert + forecast_start updates
0e54455
forecast_start updates
b5fc305
forecast_start updates + assert
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -26,7 +26,7 @@ | |||||||||||||||||||||||||||
from gluonts.model.forecast import Forecast, SampleForecast | ||||||||||||||||||||||||||||
from gluonts.model.predictor import FallbackPredictor, RepresentablePredictor | ||||||||||||||||||||||||||||
from gluonts.model.trivial.constant import ConstantPredictor | ||||||||||||||||||||||||||||
from gluonts.support.pandas import frequency_add | ||||||||||||||||||||||||||||
from gluonts.support.pandas import forecast_start | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
class MeanPredictor(RepresentablePredictor, FallbackPredictor): | ||||||||||||||||||||||||||||
|
@@ -70,7 +70,7 @@ def predict_item(self, item: DataEntry) -> SampleForecast: | |||||||||||||||||||||||||||
std = np.nanstd(target) | ||||||||||||||||||||||||||||
normal = np.random.standard_normal(self.shape) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
start_date = frequency_add(item["start"], len(item["target"])) | ||||||||||||||||||||||||||||
start_date = forecast_start(item) | ||||||||||||||||||||||||||||
return SampleForecast( | ||||||||||||||||||||||||||||
samples=std * normal + mean, | ||||||||||||||||||||||||||||
start_date=start_date, | ||||||||||||||||||||||||||||
|
@@ -79,6 +79,65 @@ def predict_item(self, item: DataEntry) -> SampleForecast: | |||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
class MovingAveragePredictor(RepresentablePredictor): | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
A :class:`Predictor` that predicts the moving average based on the | ||||||||||||||||||||||||||||
last `context_length` elements of the input target. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
If `prediction_length` = 1, the output is the moving average | ||||||||||||||||||||||||||||
based on the last `context_length` elements of the input target. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
If `prediction_length` > 1, the output is the moving average based on the | ||||||||||||||||||||||||||||
last `context_length` elements of the input target, where | ||||||||||||||||||||||||||||
previously calculated moving averages are appended at the end of the input target. | ||||||||||||||||||||||||||||
Hence, for `prediction_length` larger than `context_length`, there will be | ||||||||||||||||||||||||||||
cases where the moving average is calculated on top of previous moving averages. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Parameters | ||||||||||||||||||||||||||||
---------- | ||||||||||||||||||||||||||||
context_length | ||||||||||||||||||||||||||||
Length of the target context used to condition the predictions. | ||||||||||||||||||||||||||||
prediction_length | ||||||||||||||||||||||||||||
Length of the prediction horizon. | ||||||||||||||||||||||||||||
freq | ||||||||||||||||||||||||||||
Frequency of the predicted data. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
@validated() | ||||||||||||||||||||||||||||
def __init__( | ||||||||||||||||||||||||||||
self, | ||||||||||||||||||||||||||||
prediction_length: int, | ||||||||||||||||||||||||||||
freq: str, | ||||||||||||||||||||||||||||
context_length: Optional[int] = None, | ||||||||||||||||||||||||||||
) -> None: | ||||||||||||||||||||||||||||
super().__init__(freq=freq, prediction_length=prediction_length) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# assert ( | ||||||||||||||||||||||||||||
# context_length >= 1 | ||||||||||||||||||||||||||||
# ), "The value of `context_length` should be >= 1" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
self.context_length = context_length | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def predict_item(self, item: DataEntry) -> SampleForecast: | ||||||||||||||||||||||||||||
target = item["target"].tolist() | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
for _ in range(self.prediction_length): | ||||||||||||||||||||||||||||
if self.context_length is not None: | ||||||||||||||||||||||||||||
window = target[-self.context_length :] | ||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
window = target | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
target.append(np.nanmean(window)) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
start_date = forecast_start(item) | ||||||||||||||||||||||||||||
return SampleForecast( | ||||||||||||||||||||||||||||
samples=np.array([target[-self.prediction_length :]]), | ||||||||||||||||||||||||||||
start_date=start_date, | ||||||||||||||||||||||||||||
freq=self.freq, | ||||||||||||||||||||||||||||
item_id=item.get(FieldName.ITEM_ID), | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
class MeanEstimator(Estimator): | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
An `Estimator` that computes the mean targets in the training data, | ||||||||||||||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# A copy of the License is located at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# or in the "license" file accompanying this file. This file is distributed | ||
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
# express or implied. See the License for the specific language governing | ||
# permissions and limitations under the License. | ||
|
||
# First-party imports | ||
from gluonts.dataset.common import ListDataset | ||
from gluonts.model.trivial.mean import MovingAveragePredictor | ||
|
||
# Third-party imports | ||
import numpy as np | ||
import pytest | ||
|
||
|
||
def get_predictions( | ||
target, prediction_length=1, context_length=1, freq="D", start="2020" | ||
): | ||
mp = MovingAveragePredictor( | ||
prediction_length=prediction_length, | ||
context_length=context_length, | ||
freq=freq, | ||
) | ||
|
||
ds = ListDataset([{"target": target, "start": start}], freq=freq) | ||
item = next(iter(ds)) | ||
predictions = mp.predict_item(item).mean | ||
|
||
return predictions | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"data, expected_output, prediction_length, context_length", | ||
[ | ||
([1, 1, 1], [1], 1, 1), | ||
([1, 1, 1], [1, 1], 2, 1), | ||
([1, 1, 1], [1, 1, 1], 3, 1), | ||
([1, 1, 1], [1], 1, 2), | ||
([1, 1, 1], [1, 1], 2, 2), | ||
([1, 1, 1], [1, 1, 1], 3, 2), | ||
([1, 1, 1], [1], 1, 3), | ||
([1, 1, 1], [1, 1], 2, 3), | ||
([1, 1, 1], [1, 1, 1], 3, 3), | ||
([], [np.nan] * 1, 1, 1), | ||
([], [np.nan] * 2, 2, 1), | ||
([], [np.nan] * 3, 3, 1), | ||
([np.nan], [np.nan] * 1, 1, 1), | ||
([1, 3, np.nan], [2], 1, 3), | ||
([1, 3, np.nan], [2, 2.5], 2, 3), | ||
([1, 3, np.nan], [2, 2.5, 2.25], 3, 3), | ||
([1, 2, 3], [3], 1, 1), | ||
([1, 2, 3], [3, 3], 2, 1), | ||
([1, 2, 3], [3, 3, 3], 3, 1), | ||
([1, 2, 3], [2.5], 1, 2), | ||
([1, 2, 3], [2.5, 2.75], 2, 2), | ||
([1, 2, 3], [2.5, 2.75, 2.625], 3, 2), | ||
([1, 2, 3], [2], 1, 3), | ||
([1, 2, 3], [2, 7 / 3], 2, 3), | ||
([1, 2, 3], [2, 7 / 3, 22 / 9], 3, 3), | ||
([1, 1, 1], [1], 1, None), | ||
([1, 1, 1], [1, 1], 2, None), | ||
([1, 1, 1], [1, 1, 1], 3, None), | ||
([1, 3, np.nan], [2], 1, None), | ||
([1, 3, np.nan], [2, 2], 2, None), | ||
([1, 3, np.nan], [2, 2, 2], 3, None), | ||
], | ||
) | ||
def testing(data, expected_output, prediction_length, context_length): | ||
|
||
predictions = get_predictions( | ||
data, | ||
prediction_length=prediction_length, | ||
context_length=context_length, | ||
) | ||
|
||
np.testing.assert_equal(predictions, expected_output) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should still have a check when
context_length
is an integer.