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

This commit is contained in:
Thomas Chia 2024-05-18 15:08:15 +01:00 committed by GitHub
parent 7dfedae3f8
commit 36421b70f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 0 deletions

View file

@ -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(

View file

@ -241,6 +241,13 @@ class TestPerformanceMetrics:
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"],