Skip to content

Commit

Permalink
BUG: Ensure bestlag is defined in autolag
Browse files Browse the repository at this point in the history
Ensure that it is always defined even when all t-stats are small

closes #7014
  • Loading branch information
Kevin Sheppard committed Sep 4, 2020
1 parent 7d9d96b commit 4530b8c
Show file tree
Hide file tree
Showing 2 changed files with 418 additions and 233 deletions.
39 changes: 25 additions & 14 deletions statsmodels/tsa/stattools.py
Expand Up @@ -130,14 +130,17 @@ def _autolag(
elif method == "t-stat":
# stop = stats.norm.ppf(.95)
stop = 1.6448536269514722
# Default values to ensure that always set
bestlag = startlag + maxlag
icbest = 0.0
for lag in range(startlag + maxlag, startlag - 1, -1):
icbest = np.abs(results[lag].tvalues[-1])
bestlag = lag
if np.abs(icbest) >= stop:
bestlag = lag
icbest = icbest
# Break for first lag with a significant t-stat
break
else:
raise ValueError("Information Criterion %s not understood.") % method
raise ValueError(f"Information Criterion {method} not understood.")

if not regresults:
return icbest, bestlag
Expand Down Expand Up @@ -976,18 +979,21 @@ def pacf(x, nlags=None, method="ywadjusted", alpha=None):
consistently worse than the other options.
"""
nlags = int_like(nlags, "nlags", optional=True)
renames = {"ydu":"yda",
"ywu": "ywa",
"ywunbiased":"ywadjusted",
"ldunbiased":"ldadjusted",
"ld_unbiased":"ld_adjusted",
"ldu":"lda",
"ols-unbiased":"ols-adjusted"}
renames = {
"ydu": "yda",
"ywu": "ywa",
"ywunbiased": "ywadjusted",
"ldunbiased": "ldadjusted",
"ld_unbiased": "ld_adjusted",
"ldu": "lda",
"ols-unbiased": "ols-adjusted",
}
if method in renames:
warnings.warn(
f"{method} has been renamed {renames[method]}. After release 0.13, "
"using the old name will raise.",
FutureWarning)
FutureWarning,
)
method = renames[method]
methods = (
"ols",
Expand Down Expand Up @@ -1347,7 +1353,8 @@ def grangercausalitytests(x, maxlag, addconst=True, verbose=True):
if lags.min() <= 0 or lags.size == 0:
raise ValueError(
"maxlag must be a non-empty list containing only "
"positive integers")
"positive integers"
)

if x.shape[0] <= 3 * maxlag + int(addconst):
raise ValueError(
Expand Down Expand Up @@ -1871,9 +1878,13 @@ def kpss(x, regression="c", nlags=None, store=False):
look-up table. The actual p-value is {direction} than the p-value returned.
"""
if p_value == pvals[-1]:
warnings.warn(warn_msg.format(direction="smaller"), InterpolationWarning)
warnings.warn(
warn_msg.format(direction="smaller"), InterpolationWarning
)
elif p_value == pvals[0]:
warnings.warn(warn_msg.format(direction="greater"), InterpolationWarning)
warnings.warn(
warn_msg.format(direction="greater"), InterpolationWarning
)

crit_dict = {"10%": crit[0], "5%": crit[1], "2.5%": crit[2], "1%": crit[3]}

Expand Down

0 comments on commit 4530b8c

Please sign in to comment.