Implementation:Online ml River Stats PeakToPeak
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Statistics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
PeakToPeak computes the running peak-to-peak range (maximum minus minimum) of a data stream.
Description
This statistic calculates the difference between the maximum and minimum values observed in streaming data. It provides a simple measure of the total range or spread of the data. The implementation includes PeakToPeak for the overall range and RollingPeakToPeak for the range within a sliding window. The basic version is optimized using Rust for performance.
Usage
Use PeakToPeak when you need a quick measure of data spread or range. Common applications include monitoring signal amplitude, detecting changes in data variability, quality control for checking if values stay within expected bounds, and normalization where you need to know the full range of values.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/stats/ptp.py
Signature
class PeakToPeak(stats.base.Univariate):
def __init__(self):
self._ptp = _rust_stats.RsPeakToPeak()
self._is_updated = False
class RollingPeakToPeak(stats.base.RollingUnivariate):
def __init__(self, window_size: int):
self.max = stats.RollingMax(window_size)
self.min = stats.RollingMin(window_size)
Import
from river import stats
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | numbers.Number | Yes | Value to update the statistic with |
| window_size | int | Yes (for Rolling) | Size of the rolling window |
Outputs
| Name | Type | Description |
|---|---|---|
| get() | float | Current peak-to-peak range (max - min) |
Usage Examples
from river import stats
# Basic peak-to-peak
X = [1, -4, 3, -2, 2, 4]
ptp = stats.PeakToPeak()
for x in X:
ptp.update(x)
print(f"Value: {x}, Peak-to-Peak: {ptp.get():.1f}")
# Output:
# Value: 1, Peak-to-Peak: 0.0
# Value: -4, Peak-to-Peak: 5.0
# Value: 3, Peak-to-Peak: 7.0
# Value: -2, Peak-to-Peak: 7.0
# Value: 2, Peak-to-Peak: 7.0
# Value: 4, Peak-to-Peak: 8.0
# Rolling peak-to-peak
X = [1, -4, 3, -2, 2, 1]
rolling_ptp = stats.RollingPeakToPeak(window_size=2)
for x in X:
rolling_ptp.update(x)
print(f"Value: {x}, Rolling P2P: {rolling_ptp.get()}")
# Output:
# Value: 1, Rolling P2P: 0
# Value: -4, Rolling P2P: 5
# Value: 3, Rolling P2P: 7
# Value: -2, Rolling P2P: 5
# Value: 2, Rolling P2P: 4
# Value: 1, Rolling P2P: 1
# Using for signal amplitude monitoring
amplitude_monitor = stats.PeakToPeak()
signal = [0, 5, 10, 5, 0, -5, -10, -5, 0]
for s in signal:
amplitude_monitor.update(s)
print(f"Signal amplitude: {amplitude_monitor.get():.1f}")
# Output: 20.0 (from -10 to 10)
# Range-based normalization
ptp_norm = stats.PeakToPeak()
min_val = stats.Min()
data = [10, 25, 15, 30, 5, 20]
for x in data:
ptp_norm.update(x)
min_val.update(x)
# Normalize to [0, 1]
range_val = ptp_norm.get()
min_v = min_val.get()
normalized = [(x - min_v) / range_val for x in data]
print(f"Normalized: {[f'{n:.2f}' for n in normalized]}")