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 Quantile

From Leeroopedia


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

Overview

Quantile computes running quantiles using the P² (Piecewise-Parabolic) algorithm.

Description

This statistic estimates quantiles incrementally without storing all observations, using the P² algorithm for dynamic quantile calculation. It can compute any quantile (median, quartiles, percentiles, etc.) specified by the q parameter between 0 and 1. The implementation is optimized using Rust and includes both streaming and rolling window variants. The P² algorithm provides accurate estimates while maintaining constant memory usage.

Usage

Use Quantile when you need to track percentiles in streaming data without storing all values. Common applications include monitoring service level objectives (SLAs), outlier detection, data distribution analysis, and computing robust statistics like median (50th percentile) that are less sensitive to outliers than mean.

Code Reference

Source Location

Signature

class Quantile(stats.base.Univariate):
    def __init__(self, q: float = 0.5):
        super().__init__()
        if not 0 < q < 1:
            raise ValueError("q is not comprised between 0 and 1")
        self._quantile = _rust_stats.RsQuantile(q)
        self._is_updated = False
        self.q = q

class RollingQuantile(stats.base.RollingUnivariate):
    def __init__(self, q: float, window_size: int):
        super().__init__()
        if not 0 <= q <= 1:
            raise ValueError("q is not comprised between 0 and 1")
        self._rolling_quantile = _rust_stats.RsRollingQuantile(q, window_size)
        self.window_size_value = window_size
        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 float Yes (init) Quantile to compute, between 0 and 1 (default: 0.5 for median)
window_size int Yes (for Rolling) Size of the rolling window

Outputs

Name Type Description
get() float or None Current quantile estimate (None if no updates yet)

Usage Examples

from river import stats
import numpy as np

# Median estimation (50th percentile)
np.random.seed(42 * 1337)
mu, sigma = 0, 1
s = np.random.normal(mu, sigma, 500)

median = stats.Quantile(0.5)
for x in s:
    median.update(x)

print(f'Estimated median: {median.get():.4f}')
# Output: -0.0275

print(f'Actual median: {np.median(s):.4f}')
# Output: -0.0135

# 17th percentile
percentile_17 = stats.Quantile(0.17)
for x in s:
    percentile_17.update(x)

print(f'Estimated 17th percentile: {percentile_17.get():.4f}')
# Output: -0.8652

print(f'Actual 17th percentile: {np.percentile(s, 17):.4f}')
# Output: -0.9072

# Multiple quantiles for distribution analysis
q25 = stats.Quantile(0.25)
q50 = stats.Quantile(0.50)
q75 = stats.Quantile(0.75)
q95 = stats.Quantile(0.95)

data = list(range(1000))
np.random.shuffle(data)

for x in data:
    q25.update(x)
    q50.update(x)
    q75.update(x)
    q95.update(x)

print(f"25th percentile: {q25.get():.1f}")
print(f"50th percentile: {q50.get():.1f}")
print(f"75th percentile: {q75.get():.1f}")
print(f"95th percentile: {q95.get():.1f}")

# Rolling quantile
rolling_quantile = stats.RollingQuantile(q=0.5, window_size=101)

for i in range(1001):
    rolling_quantile.update(i)
    if i % 100 == 0:
        print(f"After {i}: Rolling median = {rolling_quantile.get():.1f}")

# Output:
# After 0: Rolling median = 0.0
# After 100: Rolling median = 50.0
# After 200: Rolling median = 150.0
# ...

Related Pages

Page Connections

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