Implementation:Online ml River Stats EWMean
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Statistics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
EWMean computes the exponentially weighted mean of a data stream.
Description
This statistic calculates the exponentially weighted moving average (EWMA), giving more weight to recent observations while exponentially decreasing the weight of older values. The fading factor parameter controls how quickly old observations are discounted. The implementation uses Rust for performance optimization and is based on Finch's incremental calculation method.
Usage
Use EWMean when you need a mean that adapts quickly to changes in the data stream. This is particularly useful for detecting trends, smoothing noisy data, and monitoring systems where recent values are more relevant than historical ones. Higher fading factors (closer to 1) make the statistic more responsive to recent changes.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/stats/ewmean.py
Signature
class EWMean(stats.base.Univariate):
def __init__(self, fading_factor=0.5):
if not 0 <= fading_factor <= 1:
raise ValueError("q is not comprised between 0 and 1")
self.fading_factor = fading_factor
self._ewmean = _rust_stats.RsEWMean(fading_factor)
Import
from river import stats
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | numbers.Number | Yes | Value to update the statistic with |
| fading_factor | float | Yes (init) | Fading factor between 0 and 1 (default: 0.5) |
Outputs
| Name | Type | Description |
|---|---|---|
| get() | float | Current exponentially weighted mean |
Usage Examples
from river import stats
# Create exponentially weighted mean with fading factor 0.5
ewm = stats.EWMean(fading_factor=0.5)
X = [1, 3, 5, 4, 6, 8, 7, 9, 11]
for x in X:
ewm.update(x)
print(ewm.get())
# Output:
# 1.0
# 2.0
# 3.5
# 3.75
# 4.875
# 6.4375
# 6.71875
# 7.859375
# 9.4296875
# Higher fading factor for more responsiveness
ewm_responsive = stats.EWMean(fading_factor=0.9)
for x in [10, 20, 30]:
ewm_responsive.update(x)
print(f"EWMA: {ewm_responsive.get():.2f}")
# Output shows faster adaptation to new values