Implementation:Online ml River Stats AutoCorr
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Statistics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
AutoCorr measures the serial correlation between the current value and a value seen n steps before.
Description
This statistic computes the Pearson correlation between the current value and the value observed at a specified lag. It maintains a sliding window to track past values and uses the PearsonCorr statistic to calculate the correlation coefficient. This is useful for detecting patterns and dependencies in time series data where current values may be related to previous observations.
Usage
Use AutoCorr when you need to detect autocorrelation in streaming time series data, such as identifying periodic patterns, seasonality, or temporal dependencies. Common applications include financial data analysis, sensor readings, and any sequential data where past values may influence current ones.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/stats/auto_corr.py
Signature
class AutoCorr(stats.base.Univariate):
def __init__(self, lag: int):
self.window: collections.deque[numbers.Number] = collections.deque(maxlen=lag)
self.lag = lag
self.pearson = stats.PearsonCorr(ddof=1)
Import
from river import stats
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | numbers.Number | Yes | Value to update the statistic with |
| lag | int | Yes (init) | Number of steps to look back for correlation calculation |
Outputs
| Name | Type | Description |
|---|---|---|
| get() | float | Current autocorrelation coefficient (between -1 and 1) |
Usage Examples
from river import stats
# Create autocorrelation with lag of 1
auto_corr = stats.AutoCorr(lag=1)
# Update with values
for x in [0.25, 0.5, 0.2, -0.05]:
auto_corr.update(x)
print(auto_corr.get())
# Output:
# 0
# 0
# -1.0
# 0.103552
# Different lag value
auto_corr = stats.AutoCorr(lag=2)
for x in [0.25, 0.5, 0.2, -0.05]:
auto_corr.update(x)
print(auto_corr.get())
# Output:
# 0
# 0
# 0
# -1.0