The Autocorrelation Function (ACF) measures the correlation of a time series with a lagged copy of itself. It is fundamental for identifying repeating patterns, seasonal effects, and determining the order of time series models like ARMA/ARIMA.
## Historical Context
Autocorrelation was formalized by statisticians in the early 20th century, with key contributions from Udny Yule (1927) and Gilbert Walker. The concept became central to time series analysis with Box and Jenkins' influential 1970 work on ARIMA models.
In financial markets, ACF reveals whether past returns predict future returns. A significant positive ACF at lag 1 suggests momentum; significant negative ACF suggests mean reversion. White noise (truly random data) should exhibit near-zero ACF at all lags.
## Architecture & Physics
The ACF indicator uses a sliding window (RingBuffer) to maintain the last `N` data points. While the theoretical formula suggests O(N) complexity per update, the implementation employs running sums where possible and periodic resynchronization to manage floating-point drift.
### Core Components
1.**RingBuffer**: Maintains the sliding window of `period` values
2.**Running Sums**: Tracks sum and sum of squares for mean/variance calculation
3.**Autocovariance Calculation**: Computes correlation at the specified lag
4.**Resync Mechanism**: Recalculates sums every 1000 updates to prevent drift
## Mathematical Foundation
### Autocorrelation Coefficient
The ACF at lag $k$ is defined as:
$$ r_k = \frac{\gamma_k}{\gamma_0} $$
where:
* $\gamma_k$ is the autocovariance at lag $k$
* $\gamma_0$ is the variance (autocovariance at lag 0)
- Constant series returns 0 (no correlation beyond lag 0)
- Alternating sequence produces negative ACF
- AR(1) process produces ACF ≈ φ^k
## Common Pitfalls
1.**Period vs Lag Constraint**: Period must be greater than `lag + 1`. Common mistake is setting period = lag, which produces undefined results.
2.**Warmup Period**: ACF requires a full window (`period` values) to be meaningful. Values during warmup may be unreliable.
3.**Non-Stationarity**: ACF assumes stationarity. Trending data should be differenced first.
4.**Significance Testing**: ACF values should be tested against confidence bounds. For white noise, 95% confidence bounds are approximately $\pm 1.96/\sqrt{n}$.
5.**Lag Selection**: Higher lags require larger periods for statistical significance. Rule of thumb: period ≥ 4 × lag.