Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Online ml River Stats EWVar

From Leeroopedia


Knowledge Sources
Domains Online_Learning, Statistics
Last Updated 2026-02-08 16:00 GMT

Overview

EWVar computes the exponentially weighted variance of a data stream.

Description

This statistic calculates the exponentially weighted moving variance using the relationship Var(X) = Mean(x^2) - Mean(x)^2. It maintains exponentially weighted means of both x and x^2 internally to compute the variance. The fading factor parameter controls how quickly old observations are discounted. The implementation is optimized using Rust for performance.

Usage

Use EWVar when you need to track the variance of a stream while giving more importance to recent observations. This is useful for monitoring volatility in financial data, detecting changes in noise levels, quality control, and anomaly detection where recent variance patterns are more relevant than historical ones.

Code Reference

Source Location

Signature

class EWVar(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._ewvar = _rust_stats.RsEWVar(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 variance

Usage Examples

from river import stats

# Create exponentially weighted variance with fading factor 0.5
ewv = stats.EWVar(fading_factor=0.5)

X = [1, 3, 5, 4, 6, 8, 7, 9, 11]

for x in X:
    ewv.update(x)
    print(f"Value: {x}, EW Variance: {ewv.get():.4f}")

# Output:
# Value: 1, EW Variance: 0.0000
# Value: 3, EW Variance: 1.0000
# Value: 5, EW Variance: 2.7500
# Value: 4, EW Variance: 1.4375
# Value: 6, EW Variance: 1.9844
# Value: 8, EW Variance: 3.4336
# Value: 7, EW Variance: 1.7959
# Value: 9, EW Variance: 2.1990
# Value: 11, EW Variance: 3.5654

# Use with EWMean for complete statistics
ewm = stats.EWMean(fading_factor=0.5)
ewv = stats.EWVar(fading_factor=0.5)

for x in [10, 15, 12, 18, 20]:
    ewm.update(x)
    ewv.update(x)
    print(f"Mean: {ewm.get():.2f}, Variance: {ewv.get():.2f}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment