diff --git a/python/prophet/diagnostics.py b/python/prophet/diagnostics.py index 6a45db6..f5bcead 100644 --- a/python/prophet/diagnostics.py +++ b/python/prophet/diagnostics.py @@ -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( diff --git a/python/prophet/tests/test_diagnostics.py b/python/prophet/tests/test_diagnostics.py index fda4896..8410e60 100644 --- a/python/prophet/tests/test_diagnostics.py +++ b/python/prophet/tests/test_diagnostics.py @@ -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"],