Implementation:Online ml River Stats IQR
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Statistics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
IQR computes the interquartile range of a data stream incrementally.
Description
This statistic calculates the difference between the upper and lower quartiles (typically the 75th and 25th percentiles) of streaming data. The interquartile range is a robust measure of statistical dispersion that is less sensitive to outliers than standard deviation. The implementation uses Rust for performance and supports custom quantile boundaries.
Usage
Use IQR when you need a robust measure of spread in streaming data that is resistant to outliers. This is particularly useful in anomaly detection, outlier identification, quality control, and data cleaning where extreme values should not overly influence the measure of variability.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/stats/iqr.py
Signature
class IQR(stats.base.Univariate):
def __init__(self, q_inf=0.25, q_sup=0.75):
super().__init__()
if q_inf >= q_sup:
raise ValueError("q_inf must be strictly less than q_sup")
self.q_inf = q_inf
self.q_sup = q_sup
self._iqr = _rust_stats.RsIQR(self.q_inf, self.q_sup)
self._is_updated = False
Import
from river import stats
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | numbers.Number | Yes | Value to update the statistic with |
| q_inf | float | Yes (init) | Lower quantile boundary, default: 0.25 |
| q_sup | float | Yes (init) | Upper quantile boundary, default: 0.75 |
Outputs
| Name | Type | Description |
|---|---|---|
| get() | float or None | Current interquartile range (q_sup - q_inf) |
Usage Examples
from river import stats
# Create IQR with default quartiles (25th and 75th percentiles)
iqr = stats.IQR(q_inf=0.25, q_sup=0.75)
# Update with values
for i in range(0, 1001):
iqr.update(i)
if i % 100 == 0:
print(f"After {i} values: IQR = {iqr.get()}")
# Output:
# After 0 values: IQR = 0.0
# After 100 values: IQR = 50.0
# After 200 values: IQR = 100.0
# After 300 values: IQR = 150.0
# After 400 values: IQR = 200.0
# After 500 values: IQR = 250.0
# After 600 values: IQR = 300.0
# After 700 values: IQR = 350.0
# After 800 values: IQR = 400.0
# After 900 values: IQR = 450.0
# After 1000 values: IQR = 500.0
# Custom quantile ranges
iqr_90 = stats.IQR(q_inf=0.05, q_sup=0.95)
for x in range(100):
iqr_90.update(x)
print(f"90% range (5th to 95th percentile): {iqr_90.get():.2f}")