Implementation:Online ml River Stats Sum
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Statistics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Sum computes the running sum of values in a data stream.
Description
This statistic maintains a cumulative sum of all values observed in the stream. It is one of the simplest statistics and updates in constant time O(1). The implementation includes a revert method, allowing it to be used with rolling windows to compute windowed sums. It serves as a building block for many other statistics and aggregations.
Usage
Use Sum when you need to track the cumulative total of streaming values. Common applications include counting events, aggregating quantities (sales, revenue, costs), monitoring total resource usage, accumulating errors or scores, and as a component in calculating other statistics like mean (sum/count) or moving averages.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/stats/summing.py
Signature
class Sum(stats.base.Univariate):
def __init__(self):
self.sum = 0.0
Import
from river import stats
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | numbers.Number | Yes | Value to add to the running sum |
Outputs
| Name | Type | Description |
|---|---|---|
| get() | float | Current cumulative sum |
Usage Examples
from river import stats
# Basic running sum
X = [-5, -3, -1, 1, 3, 5]
total = stats.Sum()
for x in X:
total.update(x)
print(f"Value: {x}, Sum: {total.get():.1f}")
# Output:
# Value: -5, Sum: -5.0
# Value: -3, Sum: -8.0
# Value: -1, Sum: -9.0
# Value: 1, Sum: -8.0
# Value: 3, Sum: -5.0
# Value: 5, Sum: 0.0
# Rolling sum
from river import utils
X = [1, -4, 3, -2, 2, 1]
rolling_sum = utils.Rolling(stats.Sum(), window_size=2)
for x in X:
rolling_sum.update(x)
print(f"Value: {x}, Rolling Sum: {rolling_sum.get():.1f}")
# Output:
# Value: 1, Rolling Sum: 1.0
# Value: -4, Rolling Sum: -3.0
# Value: 3, Rolling Sum: -1.0
# Value: -2, Rolling Sum: 1.0
# Value: 2, Rolling Sum: 0.0
# Value: 1, Rolling Sum: 3.0
# Accumulating sales revenue
revenue_sum = stats.Sum()
sales = [100, 250, 175, 300, 225]
for sale in sales:
revenue_sum.update(sale)
print(f"Sale: ${sale}, Total Revenue: ${revenue_sum.get():.2f}")
# Output:
# Sale: $100, Total Revenue: $100.00
# Sale: $250, Total Revenue: $350.00
# Sale: $175, Total Revenue: $525.00
# Sale: $300, Total Revenue: $825.00
# Sale: $225, Total Revenue: $1050.00
# Using sum with count to compute mean
total = stats.Sum()
count = stats.Count()
data = [10, 20, 30, 40, 50]
for x in data:
total.update(x)
count.update()
mean = total.get() / count.get()
print(f"Value: {x}, Mean so far: {mean:.1f}")
# Monitoring cumulative error
error_sum = stats.Sum()
predictions = [2.1, 3.8, 5.2, 4.9]
actuals = [2.0, 4.0, 5.0, 5.0]
for pred, actual in zip(predictions, actuals):
error = pred - actual
error_sum.update(error)
print(f"Pred: {pred}, Actual: {actual}, Cumulative Error: {error_sum.get():.2f}")