Autocorrelation and stationarity
Quick answer
Autocorrelation measures association between a series and its lagged version. Weak stationarity means constant mean and variance with covariance depending only on lag, not absolute time. Examples are static Python snippets tested locally on CPU; Statsmodels and scikit-learn are required for selected examples. ByteMind execution is unverified.
Alternating sequence ACF
y=[1,-1,1,-1]m=sum(y)/len(y);z=[v-m for v in y]print(sum(z[t]*z[t-1] for t in range(1,len(z)))/sum(v*v for v in z))Expected output (locally tested): -0.75. The unadjusted lag-one ACF uses the full centered sum of squares.
How it works
For n observations with mean m, one common sample ACF at lag k is sum((y[t]-m)*(y[t-k]-m), t=k..n-1) / sum((y[t]-m)^2, t=0..n-1). This is the unadjusted convention; alternatives use n-k denominators or separate means. At lag zero it is 1 for nonconstant data, and a constant series makes this formula undefined. Strong ACF at a seasonal lag suggests repeating structure, but trending nonstationary series may exhibit misleading high correlations. Strict stationarity requires joint distributions to be invariant under time shifts; weak stationarity is a second-moment property. Differencing d[t]=y[t]-y[t-1] can remove a linear trend; seasonal differencing uses y[t]-y[t-s]. Neither guarantees stationarity; over-differencing can amplify noise. Examine plots, rolling statistics and residual ACF using development data only. ADF tests a unit-root null under its specification; KPSS tests a stationarity null under its specification. Failure to reject is not proof; sample size, structural breaks, lag choice and deterministic terms matter. ML regression does not universally require stationary inputs, but distribution shifts still matter. ACF is association, not causation.
Native autocorrelation agreement
from statsmodels.tsa.stattools import acfprint([round(float(v),3) for v in acf([1,-1,1,-1],nlags=2,fft=False,adjusted=False)])Expected output (locally tested): [1.0, -0.75, 0.5]. The explicit unadjusted convention matches the manual calculation.
Common mistakes
Do not divide by zero for a constant series, treat ACF as a causal effect, or declare stationarity merely because one test did not reject its null.
Practice and next steps
Compute differences for [3,5,7,9], then compare the original and differenced plots and explain why the constant differenced series has undefined normalized ACF.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Autocorrelation and stationarity — knowledge check
Apply autocorrelation and stationarity to small examples and debugging scenarios.
What is weak stationarity?
What is the unadjusted ACF of a nonconstant series at lag zero?
What happens to normalized ACF for a constant sequence?
What does first differencing calculate?
Does failing to reject the ADF null prove a unit root?