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

Fix Issue #2577 - Zero Division Error in diagnostics.performance_metrics() causing failed assertion #2578

Merged
merged 4 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions python/prophet/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ def smape(df, w):
Dataframe with columns horizon and smape.
"""
sape = np.abs(df['y'] - df['yhat']) / ((np.abs(df['y']) + np.abs(df['yhat'])) / 2)
sape = sape.fillna(0)

Choose a reason for hiding this comment

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

are there tests covering these methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was a test looking at the mape metric underneath the wider test_performance_metrics() method, so I have extended that to test for zero values across all other metrics.

Choose a reason for hiding this comment

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

Great nice one

if w < 0:
return pd.DataFrame({'horizon': df['horizon'], 'smape': sape})
return rolling_mean_by_h(
Expand Down
8 changes: 4 additions & 4 deletions python/prophet/tests/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ def test_performance_metrics(self, ts_short, backend):
metrics=["coverage", "mse"],
)
assert set(df_horizon.columns) == {"coverage", "mse", "horizon"}
# Skip MAPE
df_cv.loc[0, "y"] = 0.0
# Handle zero y and yhat and skip MAPE
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can leave the MAPE skip test as-is and do the test for 0.0 y and 0.0 yhat afterwards.

I guess mdape doesn't have the same problem because it uses the rolling_median function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!

Yep, rolling_median doesn't get rid of the np.nan values, they're still included with the groupby.

df_cv["y"] = 0.0
df_cv["yhat"] = 0.0
df_horizon = diagnostics.performance_metrics(
df_cv,
metrics=["coverage", "mape"],
)
assert set(df_horizon.columns) == {"coverage", "horizon"}
assert set(df_horizon.columns) == {"coverage", "horizon", "mae", "mdape", "mse", "rmse", "smape"}
df_horizon = diagnostics.performance_metrics(
df_cv,
metrics=["mape"],
Expand Down