Repository navigation
BUG: Follow up #10701, keep raising for a constant 2-D ccf and pacf - #10751
Merged
Merged
Conversation
statsmodels#10701 returns NaN for a constant series before any shape check. Invalid input that raised on main therefore returned NaN: ccf with a constant 2-D x (or y) raised "in1 and in2 should have the same dimensionality" in ccovf and pacf of a constant (n, 2) array raised in the method (for example "expecting a vector" for yw). Only run the constant early return in ccf when both inputs are 1-D, and in pacf when x is a vector or one column; all other shapes take the path of main and raise as before. Valid input is unchanged. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The bullet of statsmodels#10701 describes changed results of existing functions, so it belongs at the end of "Behavior changes" and not in the middle of the Time series analysis bug fixes. Say what users saw before: acf was nan at all lags when the mean of the constant is exact and showed autocorrelations near 1 (0.99 at lag 1 for 100 observations of 101.37) otherwise, and pacf was nan only for some constants and methods. Add that qstat and pvalues of acf and acorr_ljungbox are nan for a constant series. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Follow-up to statsmodels#10701. Add tests that invalid shapes of a constant series still raise in ccf (x or y with ndim 2) and pacf (constant (n, 2) array, same error as for a non-constant array), and that a single constant column gives NaN. Parametrize the acf and ccf constant tests over adjusted, fft, missing and nlags (the nlags=None branch of the ccf shortcut was untested) and check that the shortcut comes before the fft path of scipy, which warns about NaN, for a series that is constant once the NaN is dropped. Drop test_pacf_yw_constant_warning: it pinned the minimum-norm lstsq values of a singular system, and test_yw_singular covers the warning. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow up to #10701 (
acf,ccfandpacffor a constant series, xref #10647). Its review left the points below. #10647 was closed by the merge of #10701 (its body hascloses #10647 (partially, tsa module)), butOLS.rsquaredof a constantendogis not changed by it (it returns -28.6 for a constant 123.456 and -inf for 101.37), so this PR saysxrefand the issue may need to be reopened or split.NumPy's guide.
Changes
BUG:BUG: reject constant series in acf and ccf, fix pacf demeaning #10701 returnsnanfor a constant series before any check of the shape, so input that raised on main now returnsnan:ccfof a constant(20, 1)or(20, 2)x(ory) raisedin1 and in2 should have the same dimensionalityinccovf, andpacfof a constant(30, 2)array raised the error of its method (for exampleexpecting a vectorfor the Yule-Walker methods,x is required to have ndim 1for others). The shortcut ofccfnow runs only ifx.ndim == y.ndim == 1and the one ofpacfonly ifx.size == nobs(a vector or one column); every other shape takes the path of main and raises as before. Valid input is unchanged: over 5408ccfcalls the old and the new code give identical values. The NaN-awarex_validandy_validofccfare kept on purpose: without them a series that is constant once the NaN is dropped goes into the fft path of scipy, which warns about NaN.DOC:Release note: moved from the middle of the 'Time series analysis' bug fixes to the end of 'Behavior changes' (AGENTS.md: changed results of existing functions go there; the other three follow-ups of this batch, for BUG: Pass p to the submodels in the truncated count score #10736, BUG: Fix NegativeBinomialP hessian for p other than 1 and 2 #10737 and BUG: include finite exact-diffuse innovations in concentrated loglike (closes #10741) #10742, keep the note under 'Bug Fixes' like BUG: Preserve seasonal and sparse lags in ARIMA GLS whitening #10713 and BUG: Fix cross derivatives in ZeroInflatedPoisson hessian #10723, so move whichever you prefer). It now says what a user saw before:acfwasnanat all lags if the mean of the constant is exactly representable and otherwise showed autocorrelations near 1 from the rounding error of the mean (0.99 at lag 1 for 100 observations of 101.37, Ljung-Box statistic 100.98), andpacfreturned numbers ornandepending on the method and the constant. Checked on the code before BUG: reject constant series in acf and ccf, fix pacf demeaning #10701 and now over 6 constants and 5 lengths: lag 0 ofacfwasnanin 8 of 30 cases and 1.0 in 22 of 30; lags 1 and up werenanor about 1; now lag 0 is 1.0 and the restnanin 30 of 30, and the Ljung-Box statistics and p-values arenanin 30 of 30. Forpacfthe lags 1 and up werenanin 0 of 30 cases for the Yule-Walker and OLS methods and in 8 of 30 for the Levinson-Durbin and Burg methods, and arenanin 30 of 30 for every method now. The note namesqstatandpvaluesofacfandacorr_ljungbox.TST:test_pacf_yw_constant_warningis dropped: it pinned the minimum-norm values 1/k of a singular system for a constant series andtest_yw_singularcovers the warning.test_acf_ccf_constantis split intotest_acf_constant(constant, length,adjusted,fft,missing) andtest_ccf_constant(the same,nlagsNone and 5, NaN, the constant first or second, with warnings turned into errors, so that a shortcut that comes after the fft path of scipy fails; thenlags=Nonebranch was untested). New:test_ccf_constant_invalid_shape_raises(15 cases),test_pacf_constant_invalid_shape_raises(a constant(30, 2)array raises the same error as a non-constant one, 8 methods) andtest_pacf_constant_column(a single column is valid, 8 methods).Mutation checks (each applied to the code and reverted; all are caught): removing the
ndimgate ofccf(15 failures), thesize == nobscondition ofpacf(8) or allowing only 1-D inpacf(6);x_valid = xandy_valid = y(16 each); a wrong length of the returned array (64); confidence intervals ofccfthat are notnan(128); removing the lag-0 line ofacf(48); the zeroing of the demeaned series inacovfwithout and with missing values (8 and 20).Not changed
pacf_yw,pacf_olsandpacf_burgcalled directly still return numbers for a constant series (for 123.456:[1, 1, 0.5],[1, 0.9999, 0.5]and[1, 1, 5e-17]), thenp.errstateblocks ofpacf_burgandlevinson_durbinthat BUG: reject constant series in acf and ccf, fix pacf demeaning #10701 added are still not tested,ccfwith two(n, 1)inputs is not rejected on main (it returns an(n, n)array) and is left as it is, andOLS.rsquaredof a constantendog(see above).Tests
statsmodels/tsa/tests/test_stattools.py: 908 passed, 1 skipped, 2 xfailed,test_diagnostic.py: 225 passed, on Python 3.13 and on Python 3.10 (NumPy 1.23.5, SciPy 1.8.1);statsmodels/tsa/arima/: 223 passed, 3 xfailed andtest_ar.py: 4599 passed on Python 3.13.ruff checkandgit diff --checkpass.AI Disclosure
Contributions must comply with the statsmodels AI Policy.
Tick exactly one of the following. If AI tools were used, replace the
<...>placeholders in the second option with the tool name(s) and how the tool's output was
used.
Tool(s): Claude Code (Claude Sonnet 5.5).
Used for: reviewing BUG: reject constant series in acf and ccf, fix pacf demeaning #10701, and writing and checking the follow-up changes.
I have personally read, understood, and can explain every line of this diff, and
have verified the statistical/numerical correctness of the change.
🤖 Generated with Claude Code