Skip to content

BUG: Follow up #10701, keep raising for a constant 2-D ccf and pacf - #10751

Merged
bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-acf-constant-followup
Nov 16, 2026
Merged

bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-acf-constant-followup

Conversation

@bashtage

Copy link
Copy Markdown
Member

Follow up to #10701 (acf, ccf and pacf for a constant series, xref #10647). Its review left the points below. #10647 was closed by the merge of #10701 (its body has closes #10647 (partially, tsa module)), but OLS.rsquared of a constant endog is not changed by it (it returns -28.6 for a constant 123.456 and -inf for 101.37), so this PR says xref and the issue may need to be reopened or split.

  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

Changes

  • BUG: BUG: reject constant series in acf and ccf, fix pacf demeaning #10701 returns nan for a constant series before any check of the shape, so input that raised on main now returns nan: ccf of a constant (20, 1) or (20, 2) x (or y) raised in1 and in2 should have the same dimensionality in ccovf, and pacf of a constant (30, 2) array raised the error of its method (for example expecting a vector for the Yule-Walker methods, x is required to have ndim 1 for others). The shortcut of ccf now runs only if x.ndim == y.ndim == 1 and the one of pacf only if x.size == nobs (a vector or one column); every other shape takes the path of main and raises as before. Valid input is unchanged: over 5408 ccf calls the old and the new code give identical values. The NaN-aware x_valid and y_valid of ccf are 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: acf was nan at 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), and pacf returned numbers or nan depending 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 of acf was nan in 8 of 30 cases and 1.0 in 22 of 30; lags 1 and up were nan or about 1; now lag 0 is 1.0 and the rest nan in 30 of 30, and the Ljung-Box statistics and p-values are nan in 30 of 30. For pacf the lags 1 and up were nan in 0 of 30 cases for the Yule-Walker and OLS methods and in 8 of 30 for the Levinson-Durbin and Burg methods, and are nan in 30 of 30 for every method now. The note names qstat and pvalues of acf and acorr_ljungbox.
  • TST: test_pacf_yw_constant_warning is dropped: it pinned the minimum-norm values 1/k of a singular system for a constant series and test_yw_singular covers the warning. test_acf_ccf_constant is split into test_acf_constant (constant, length, adjusted, fft, missing) and test_ccf_constant (the same, nlags None 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; the nlags=None branch 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) and test_pacf_constant_column (a single column is valid, 8 methods).

Mutation checks (each applied to the code and reverted; all are caught): removing the ndim gate of ccf (15 failures), the size == nobs condition of pacf (8) or allowing only 1-D in pacf (6); x_valid = x and y_valid = y (16 each); a wrong length of the returned array (64); confidence intervals of ccf that are not nan (128); removing the lag-0 line of acf (48); the zeroing of the demeaned series in acovf without and with missing values (8 and 20).

Not changed

  • pacf_yw, pacf_ols and pacf_burg called 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]), the np.errstate blocks of pacf_burg and levinson_durbin that BUG: reject constant series in acf and ccf, fix pacf demeaning #10701 added are still not tested, ccf with two (n, 1) inputs is not rejected on main (it returns an (n, n) array) and is left as it is, and OLS.rsquared of a constant endog (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 and test_ar.py: 4599 passed on Python 3.13.
  • ruff check and git diff --check pass.

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.

  • No AI tools were used to develop this pull request.
  • AI tools were 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

bashtage and others added 3 commits November 15, 2026 18:43
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]>
@bashtage
bashtage marked this pull request as ready for review November 15, 2026 22:51
@bashtage
bashtage merged commit 3d2f5c5 into statsmodels:main Nov 16, 2026
14 checks passed
@bashtage
bashtage deleted the wip-acf-constant-followup branch November 20, 2026 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant