Skip to content

Commit

Permalink
Fix Issue #2577 - Zero Division Error in diagnostics.performance_metr…
Browse files Browse the repository at this point in the history
…ics() causing failed assertion (#2578)
  • Loading branch information
ThomasChia committed May 18, 2024
1 parent 7dfedae commit 36421b7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
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)
if w < 0:
return pd.DataFrame({'horizon': df['horizon'], 'smape': sape})
return rolling_mean_by_h(
Expand Down
7 changes: 7 additions & 0 deletions python/prophet/tests/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ def test_performance_metrics(self, ts_short, backend):
metrics=["coverage", "mape"],
)
assert set(df_horizon.columns) == {"coverage", "horizon"}
# Handle zero y and yhat
df_cv["y"] = 0.0
df_cv["yhat"] = 0.0
df_horizon = diagnostics.performance_metrics(
df_cv,
)
assert set(df_horizon.columns) == {"coverage", "horizon", "mae", "mdape", "mse", "rmse", "smape"}
df_horizon = diagnostics.performance_metrics(
df_cv,
metrics=["mape"],
Expand Down

0 comments on commit 36421b7

Please sign in to comment.