Skip to content

Commit

Permalink
BUG: Fix indexing with HoltWinters's forecast
Browse files Browse the repository at this point in the history
Fix index when using int indices

closes statsmodels#6549
  • Loading branch information
bashtage committed Jul 9, 2020
1 parent 0538c0c commit 17c3d6a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 12 additions & 3 deletions statsmodels/tsa/holtwinters.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,12 @@ def forecast(self, steps=1):
"""
try:
freq = getattr(self.model._index, 'freq', 1)
start = self.model._index[-1] + freq
end = self.model._index[-1] + steps * freq
if isinstance(freq, int):
start = self.model._index.shape[0]
end = start + steps - 1
else:
start = self.model._index[-1] + freq
end = self.model._index[-1] + steps * freq
return self.model.predict(self.params, start=start, end=end)
except (AttributeError, ValueError):
# May occur when the index does not have a freq
Expand Down Expand Up @@ -912,7 +916,10 @@ def predict(self, params, start=None, end=None):
"""
if start is None:
freq = getattr(self._index, 'freq', 1)
start = self._index[-1] + freq
if isinstance(freq, int):
start = self._index.shape[0] + freq
else:
start = self._index[-1] + freq
start, end, out_of_sample, prediction_index = self._get_prediction_index(
start=start, end=end)
if out_of_sample > 0:
Expand Down Expand Up @@ -1090,6 +1097,8 @@ def fit(self, smoothing_level=None, smoothing_slope=None, smoothing_seasonal=Non
# solution to parameters
_bounds = [bnd for bnd, flag in zip(bounds, xi) if flag]
lb, ub = np.asarray(_bounds).T.astype(float)
ub[np.isnan(ub)] = np.inf
lb[np.isnan(lb)] = -np.inf
initial_p = p[xi]

# Ensure strictly inbounds
Expand Down
11 changes: 11 additions & 0 deletions statsmodels/tsa/tests/test_holtwinters.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,3 +879,14 @@ def test_simulate_boxcox(austourists):
mean = np.mean(res, axis=1)

assert np.all(np.abs(mean - expected) < 5)


@pytest.mark.parametrize("ix", [10, 100, 1000, 2000])
def test_forecast_index(ix):
# GH 6549
ts_1 = pd.Series([85601, 89662, 85122, 84400, 78250, 84434, 71072, 70357, 72635, 73210],
index=range(ix, ix + 10))
model = ExponentialSmoothing(ts_1, trend='add', damped=False).fit()
index = model.forecast(steps=10).index
assert index[0] == ix + 10
assert index[-1] == ix + 19

0 comments on commit 17c3d6a

Please sign in to comment.