Implementation:Online ml River Stats Cov
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Statistics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Cov computes the running covariance between two variables in an online streaming fashion.
Description
This statistic calculates the covariance between two variables incrementally as data arrives. It uses Welford's algorithm to maintain numerical stability while tracking the means of both variables and their covariance. The implementation supports a delta degrees of freedom (ddof) parameter for bias correction, and includes revert capabilities for use with rolling windows.
Usage
Use Cov when you need to measure the linear relationship between two variables in streaming data. This is essential for correlation analysis, principal component analysis, and understanding how two features vary together. It can be wrapped with utils.Rolling for windowed covariance calculations.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/stats/cov.py
Signature
class Cov(stats.base.Bivariate):
def __init__(self, ddof=1):
self.ddof = ddof
self.mean_x = stats.Mean()
self.mean_y = stats.Mean()
self.cov = 0
Import
from river import stats
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | numbers.Number | Yes | First variable value |
| y | numbers.Number | Yes | Second variable value |
| w | float | No | Weight for the observation (default: 1.0) |
| ddof | int | Yes (init) | Delta Degrees of Freedom (default: 1) |
Outputs
| Name | Type | Description |
|---|---|---|
| get() | float | Current covariance value |
Usage Examples
from river import stats
# Create covariance statistic
cov = stats.Cov()
# Update with paired values
x = [-2.1, -1, 4.3]
y = [3, 1.1, 0.12]
for xi, yi in zip(x, y):
cov.update(xi, yi)
print(cov.get())
# Output:
# 0.0
# -1.044999
# -4.286
# Rolling covariance with window
from river import utils
x = [-2.1, -1, 4.3, 1, -2.1, -1, 4.3]
y = [3, 1.1, .12, 1, 3, 1.1, .12]
rcov = utils.Rolling(stats.Cov(), window_size=3)
for xi, yi in zip(x, y):
rcov.update(xi, yi)
print(rcov.get())
# Output:
# 0.0
# -1.045
# -4.286
# -1.382
# -4.589
# -1.415
# -4.286