Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions python/prophet/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def generate_cutoffs(
-------
list of pd.Timestamp
"""
# Each pass subtracts `period` from the cutoff, so anything but a positive
# period leaves it where it was and the loop below never terminates.
if period <= pd.Timedelta(0):
raise ValueError('period must be positive.')
# Last cutoff is 'latest date in data - horizon' date
cutoff = df['ds'].max() - horizon
if cutoff < df['ds'].min():
Expand Down
13 changes: 13 additions & 0 deletions python/prophet/tests/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,19 @@ def test_cross_validation_default_value_check(self, ts_short, backend):
assert ((df_cv1["y"] - df_cv2["y"]) ** 2).sum() == pytest.approx(0.0)
assert ((df_cv1["yhat"] - df_cv2["yhat"]) ** 2).sum() == pytest.approx(0.0)

@pytest.mark.parametrize("period", ["0 days", "-7 days"])
def test_cross_validation_nonpositive_period_raises(self, period):
"""A non-positive period must not be accepted.

Each pass of generate_cutoffs subtracts `period` from the cutoff, so
anything but a positive value leaves it where it was: the loop makes no
progress, appends a cutoff every iteration, and never returns.
"""
m = make_cross_validation_model()

with pytest.raises(ValueError, match="period must be positive"):
diagnostics.cross_validation(m, horizon="2 days", period=period)

def test_cross_validation_custom_cutoffs(self, ts_short, backend):
m = Prophet(stan_backend=backend)
m.fit(ts_short)
Expand Down
Loading