Repository navigation
BUG: reject constant series in acf and ccf, fix pacf demeaning - #10701
Conversation
|
I think this is not the correct decision. Constant series have a clear acf - all exactly 0, in the same sense that the Corr(a,b)=0 if a and b are constants. Need to think more about pacf. Do you know what other statistical software do? |
|
Thanks for the feedback! Regarding what other statistical software packages and libraries do for a constant series:
Also, in statsmodels prior to this PR, when the mean was exact (e.g. Raising Given that R, SciPy, NumPy, and Pandas all return |
|
Yes, it seems |
bashtage
left a comment
There was a problem hiding this comment.
Ptp saves a pass through the array. Also should return nan if not already in these cases.
| ) | ||
| x = array_like(x, "x") | ||
| x_valid = x[~np.isnan(x)] if missing in ("drop", "conservative") else x | ||
| if x_valid.size > 0 and x_valid.max() == x_valid.min(): |
| # then repeats the calculation and its warnings | ||
| with np.errstate(all="ignore"): | ||
| x = x - x.mean() | ||
| if x.size > 0 and x.max() == x.min(): |
| x = array_like(x, "x") | ||
| if demean: | ||
| x = x - x.mean() | ||
| if x.size > 0 and x.max() == x.min(): |
…#10701 - In acf and ccf, return NaN (and NaN confint/qstats) for constant series. - In acf, _pacf_yw_levinson, and pacf_burg, use np.ptp(x) == 0. - Update tests for acf, ccf, and acorr_ljungbox to assert NaN results. References statsmodels#10647
|
I think it need a few more changes:
|
|
Looks like there is a pyodide failure that needs fixing. Pyodide doesn't handle linear algebra errors correctly as a rule. |
|
Pull the warning test into a new test and skip on WASM. |
1c8b7f2 to
649b02c
Compare
A constant series carries no autocorrelation information. When x.mean() is not exactly representable in floating point (e.g. 123.456), x - x.mean() leaves a constant residual (~1e-14) that was misinterpreted as near-perfect autocorrelation. - In acf and pacf (all methods), return 1.0 for lag 0 and NaN for lags 1 and higher when the input is constant. - In acovf, check for constant series in both the demean and missing branches, returning exact zeros. - In ccf, return NaN for all lags when either series is constant. - Silence 0/0 scalar division warnings in levinson_durbin and pacf_burg. - Add release note and versionchanged notes for 0.15.1. - Add comprehensive parametrized tests across methods, constants, lengths, and missing values, skipping linear algebra warnings on WASM. Closes statsmodels#10647
649b02c to
2152cbc
Compare
|
Thanks for the review. I've addressed all your feedback, updated the tests, added the release notes, rebased and squashed the commits. |
|
Thanks for the fixes. |
#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 #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 #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]>
BUG: Follow up #10701, keep raising for a constant 2-D ccf and pacf
A constant series carries no autocorrelation information. When
x.mean()is not exactly representable in floating point (e.g.123.456),x - x.mean()leaves a constant residual (~1e-14) that was misinterpreted as near-perfect autocorrelation (acf = 0.996,pacf = 1.0, Ljung-Boxp = 0.0).This PR addresses the time series (
tsa) portion of #10647:In
pacf(all methods) andacf, return 1.0 for lag 0 andNaNfor lags 1 and higher when the input series is constant, with valid confidence intervals ([1.0, 1.0]at lag 0,NaNelsewhere).In
acovf, check for constant series in both regular and missing (missing in ("drop", "conservative")) branches usingnp.ptp, ensuring exact zero demeaned values.In
ccf, returnNaNfor all lags when either series is constant (with or without missing values).Silence 0/0 scalar division warnings in
levinson_durbinandpacf_burg.Add release note in
version0.15.1.rstand.. versionchanged:: 0.15.1notes in docstrings.Add parametrized regression tests in
test_stattoolsandtest_diagnosticcovering all PACF estimation methods, exact/inexact constants, lengths, and missing value options, skipping linear algebra warnings on Pyodide/WASM.closes BUG: acf, acorr_ljungbox, kpss and OLS rsquared return confident results on a constant series when the mean is not exactly representable (adfuller already rejects constant input) #10647 (partially, tsa module)
tests added / passed.
code/documentation is well formatted.
properly formatted commit message. See
NumPy's guide.
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):
Gemini / Antigravity.Used for:
drafting the implementation, and writing regression tests.I have personally read, understood, and can explain every line of this diff, and
have verified the statistical/numerical correctness of the change.
Details
Notes:
needed for doc changes.
then show that it is fixed with the new code.
verify your changes are well formatted by running
ruffis installed. While passing this test is not required, it is good practice and it helpimprove code quality in
statsmodels.AI Policy for what disclosure
and review is expected of you before submitting.