Skip to content

Commit

Permalink
Merge pull request #6961 from bashtage/das-soham/master
Browse files Browse the repository at this point in the history
    BUG: SARIMAX throwing different errors when length of endogenous var is too low
  • Loading branch information
bashtage committed Aug 10, 2020
2 parents 9d68d73 + badfc60 commit 5e3c2ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions statsmodels/tsa/statespace/mlemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4280,6 +4280,13 @@ def plot_diagnostics(self, variable=0, lags=10, fig=None, figsize=None,
self.filter_results.standardized_forecasts_error[variable, d:],
index=ix)

if resid.shape[0] < max(d, lags):
raise ValueError(
"Length of endogenous variable must be larger the the number "
"of lags used in the model and the number of observations "
"burned in the log-likelihood calculation."
)

# Top-left: residuals vs time
ax = fig.add_subplot(221)
resid.dropna().plot(ax=ax)
Expand Down
24 changes: 24 additions & 0 deletions statsmodels/tsa/statespace/tests/test_sarimax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2726,3 +2726,27 @@ def test_dynamic_str():
actual = res.get_prediction(index[-24],
dynamic=dynamic.strftime("%Y-%m-%d"))
assert_allclose(actual.predicted_mean, desired.predicted_mean)


@pytest.mark.matplotlib
def test_plot_too_few_obs(reset_randomstate):
# GH 6173
# SO https://stackoverflow.com/questions/55930880/
# arima-models-plot-diagnostics-share-error/58051895#58051895
mod = sarimax.SARIMAX(
np.random.normal(size=10), order=(10, 0, 0), enforce_stationarity=False
)
results = mod.fit()
with pytest.raises(ValueError, match="Length of endogenous"):
results.plot_diagnostics(figsize=(15, 5))
y = np.random.standard_normal(9)
mod = sarimax.SARIMAX(
y,
order=(1, 1, 1),
seasonal_order=(1, 1, 0, 12),
enforce_stationarity=False,
enforce_invertibility=False,
)
results = mod.fit()
with pytest.raises(ValueError, match="Length of endogenous"):
results.plot_diagnostics(figsize=(30, 15))
2 changes: 1 addition & 1 deletion statsmodels/tsa/stattools.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ def acf(
if not (qstat or alpha):
return acf
if alpha is not None:
varacf = np.ones(nlags + 1) / nobs
varacf = np.ones_like(acf) / nobs
varacf[0] = 0
varacf[1] = 1.0 / nobs
varacf[2:] *= 1 + 2 * np.cumsum(acf[1:-1] ** 2)
Expand Down

0 comments on commit 5e3c2ef

Please sign in to comment.