-
Notifications
You must be signed in to change notification settings - Fork 50
feat: add ARIMAPlus.predict_explain() to generate forecasts with explanation columns #1177
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
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
8bc1cca
feat: create arima_plus_predict_attribution method
rey-esp f6dd455
tmp: debug notes for time_series_arima_plus_model.predict_attribution
chelsea-lin b8ec20d
update test_arima_plus_predict_explain_default test and create test_a…
rey-esp 8056c92
Merge branch 'ml-predict-explain' of github.com:googleapis/python-big…
rey-esp 722181b
Merge branch 'ml-predict-explain' of github.com:googleapis/python-big…
rey-esp a161b33
update test_arima_plus_predict_explain_params test
rey-esp dd38aaf
Merge branch 'main' into ml-predict-explain
rey-esp 347c3c4
Revert "tmp: debug notes for time_series_arima_plus_model.predict_att…
chelsea-lin 54175c0
format and lint
rey-esp 75d9f91
Merge branch 'main' into ml-predict-explain
rey-esp 2634a91
Merge branch 'main' into ml-predict-explain
rey-esp 448e63a
Update bigframes/ml/forecasting.py
rey-esp 8347c4b
update predict explain params test
rey-esp 1fe2d37
update test
rey-esp 48c81ed
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] c22eec8
Merge branch 'main' into ml-predict-explain
rey-esp 706a1ae
add unit test file - bare bones
rey-esp 79a5359
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 7922d64
Merge branch 'main' into ml-predict-explain
rey-esp aba6aca
Merge branch 'main' into ml-predict-explain
rey-esp b9343cf
Merge branch 'main' into ml-predict-explain
rey-esp ac271ff
Merge branch 'main' into ml-predict-explain
rey-esp 3befd2e
fixed tests
rey-esp 3fbcb64
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 04d1fb4
lint
rey-esp 75e2994
Merge branch 'ml-predict-explain' of github.com:googleapis/python-big…
rey-esp 6bfb1d3
lint
rey-esp e2eb29d
fix test: float -> int
rey-esp 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -65,6 +65,42 @@ def test_arima_plus_predict_default( | |
) | ||
|
||
|
||
def test_arima_plus_predict_explain_default( | ||
time_series_arima_plus_model: forecasting.ARIMAPlus, | ||
): | ||
utc = pytz.utc | ||
predictions = time_series_arima_plus_model.predict_explain().to_pandas() | ||
assert predictions.shape[0] == 369 | ||
predictions = predictions[ | ||
predictions["time_series_type"] == "forecast" | ||
].reset_index(drop=True) | ||
assert predictions.shape[0] == 3 | ||
result = predictions[["time_series_timestamp", "time_series_data"]] | ||
expected = pd.DataFrame( | ||
{ | ||
"time_series_timestamp": [ | ||
datetime(2017, 8, 2, tzinfo=utc), | ||
datetime(2017, 8, 3, tzinfo=utc), | ||
datetime(2017, 8, 4, tzinfo=utc), | ||
], | ||
"time_series_data": [2727.693349, 2595.290749, 2370.86767], | ||
} | ||
) | ||
expected["time_series_data"] = expected["time_series_data"].astype( | ||
pd.Float64Dtype() | ||
) | ||
expected["time_series_timestamp"] = expected["time_series_timestamp"].astype( | ||
pd.ArrowDtype(pa.timestamp("us", tz="UTC")) | ||
) | ||
|
||
pd.testing.assert_frame_equal( | ||
result, | ||
expected, | ||
rtol=0.1, | ||
check_index_type=False, | ||
) | ||
|
||
|
||
def test_arima_plus_predict_params(time_series_arima_plus_model: forecasting.ARIMAPlus): | ||
utc = pytz.utc | ||
predictions = time_series_arima_plus_model.predict( | ||
|
@@ -96,6 +132,33 @@ def test_arima_plus_predict_params(time_series_arima_plus_model: forecasting.ARI | |
) | ||
|
||
|
||
def test_arima_plus_predict_explain_params( | ||
time_series_arima_plus_model: forecasting.ARIMAPlus, | ||
): | ||
predictions = time_series_arima_plus_model.predict_explain( | ||
horizon=4, confidence_level=0.9 | ||
).to_pandas() | ||
assert predictions.shape[0] >= 1 | ||
prediction_columns = set(predictions.columns) | ||
expected_columns = { | ||
"time_series_timestamp", | ||
"time_series_type", | ||
"time_series_data", | ||
"time_series_adjusted_data", | ||
"standard_error", | ||
"confidence_level", | ||
"prediction_interval_lower_bound", | ||
"trend", | ||
"seasonal_period_yearly", | ||
"seasonal_period_quarterly", | ||
"seasonal_period_monthly", | ||
"seasonal_period_weekly", | ||
"seasonal_period_daily", | ||
"holiday_effect", | ||
} | ||
assert expected_columns <= prediction_columns | ||
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. Thanks! |
||
|
||
|
||
def test_arima_plus_detect_anomalies( | ||
time_series_arima_plus_model: forecasting.ARIMAPlus, new_time_series_df | ||
): | ||
|
This file contains hidden or 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,58 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License 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. | ||
|
||
import re | ||
|
||
import pytest | ||
|
||
from bigframes.ml import forecasting | ||
|
||
|
||
def test_predict_explain_low_confidence_level(): | ||
confidence_level = -0.5 | ||
|
||
model = forecasting.ARIMAPlus() | ||
|
||
with pytest.raises( | ||
ValueError, | ||
match=re.escape( | ||
f"confidence_level must be [0.0, 1.0), but is {confidence_level}." | ||
), | ||
): | ||
model.predict_explain(horizon=4, confidence_level=confidence_level) | ||
|
||
|
||
def test_predict_high_explain_confidence_level(): | ||
confidence_level = 2.1 | ||
|
||
model = forecasting.ARIMAPlus() | ||
|
||
with pytest.raises( | ||
ValueError, | ||
match=re.escape( | ||
f"confidence_level must be [0.0, 1.0), but is {confidence_level}." | ||
), | ||
): | ||
model.predict_explain(horizon=4, confidence_level=confidence_level) | ||
|
||
|
||
def test_predict_explain_low_horizon(): | ||
horizon = -1 | ||
|
||
model = forecasting.ARIMAPlus() | ||
|
||
with pytest.raises( | ||
ValueError, match=f"horizon must be at least 1, but is {horizon}." | ||
): | ||
model.predict_explain(horizon=horizon, confidence_level=0.9) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.