Implementation:Online ml River Conf Interval
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Conformal_Prediction, Uncertainty_Quantification, Statistics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Interval is a dataclass representing a prediction interval with lower and upper bounds for uncertainty quantification.
Description
The Interval class is a simple dataclass that encapsulates a prediction interval defined by lower and upper bounds. It provides a standardized way to represent and work with prediction intervals in conformal prediction methods.
The class includes convenient properties for computing the interval's center (midpoint) and width (range). It also implements the __contains__ method, allowing you to check if a value falls within the interval using Python's in operator.
This class is not intended to be used directly by users. Instead, it's returned automatically by conformal prediction methods when predictions are requested with uncertainty intervals via the with_interval parameter in predict_one methods.
Usage
The Interval class is used internally by River's conformal prediction methods (like RegressionJackknife) to represent prediction intervals. Users receive Interval objects when calling predict_one with with_interval=True and can use the interval's properties to assess prediction uncertainty and validity.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/conf/interval.py
Signature
@dataclasses.dataclass
class Interval:
lower: float
upper: float
Import
from river import conf
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| lower | float | Lower bound of the interval |
| upper | float | Upper bound of the interval |
| Property | Type | Description |
|---|---|---|
| center | float | Midpoint of the interval: (lower + upper) / 2 |
| width | float | Range of the interval: upper - lower |
| Method | Parameters | Return Type | Description |
|---|---|---|---|
| __contains__(x) | x: float | bool | Checks if x is within [lower, upper] |
Usage Examples
from river import conf
# Creating an interval directly (typically not done by users)
interval = conf.Interval(lower=8.5, upper=11.5)
print(f"Interval: [{interval.lower}, {interval.upper}]")
# Interval: [8.5, 11.5]
# Access properties
print(f"Center: {interval.center}")
# Center: 10.0
print(f"Width: {interval.width}")
# Width: 3.0
# Check if value is in interval
print(9.0 in interval) # True
print(12.0 in interval) # False
print(8.5 in interval) # True (inclusive)
print(11.5 in interval) # True (inclusive)
# Typical usage: With conformal prediction
from river import datasets
from river import linear_model
from river import preprocessing
from river import stats
dataset = datasets.TrumpApproval()
model = conf.RegressionJackknife(
(
preprocessing.StandardScaler() |
linear_model.LinearRegression(intercept_lr=.1)
),
confidence_level=0.9
)
validity = stats.Mean()
efficiency = stats.Mean()
for x, y in dataset:
# Get prediction with interval
interval = model.predict_one(x, with_interval=True)
# Interval is automatically an Interval object
print(f"Prediction interval: [{interval.lower:.2f}, {interval.upper:.2f}]")
print(f"Center: {interval.center:.2f}, Width: {interval.width:.2f}")
# Check validity: is true value in interval?
is_valid = y in interval
validity.update(is_valid)
efficiency.update(interval.width)
model.learn_one(x, y)
print(f"\nInterval validity: {validity.get():.3f}")
print(f"Average interval width: {efficiency.get():.3f}")
# Example: Tracking prediction uncertainty
from collections import deque
recent_widths = deque(maxlen=50)
for x, y in dataset.take(100):
interval = model.predict_one(x, with_interval=True)
recent_widths.append(interval.width)
# Alert if uncertainty is increasing
if len(recent_widths) == 50:
avg_width = sum(recent_widths) / len(recent_widths)
if interval.width > 1.5 * avg_width:
print(f"Warning: High uncertainty detected!")
print(f"Current width: {interval.width:.2f}")
print(f"Average width: {avg_width:.2f}")
model.learn_one(x, y)
# Example: Comparing intervals from different models
model_90 = conf.RegressionJackknife(
preprocessing.StandardScaler() | linear_model.LinearRegression(),
confidence_level=0.90
)
model_95 = conf.RegressionJackknife(
preprocessing.StandardScaler() | linear_model.LinearRegression(),
confidence_level=0.95
)
for x, y in dataset.take(50):
interval_90 = model_90.predict_one(x, with_interval=True)
interval_95 = model_95.predict_one(x, with_interval=True)
print(f"90% interval width: {interval_90.width:.2f}")
print(f"95% interval width: {interval_95.width:.2f}")
print(f"Ratio: {interval_95.width / interval_90.width:.2f}")
model_90.learn_one(x, y)
model_95.learn_one(x, y)
# Example: Custom analysis using intervals
class IntervalAnalyzer:
def __init__(self):
self.coverage = []
self.widths = []
def analyze(self, y_true, interval):
self.coverage.append(y_true in interval)
self.widths.append(interval.width)
def report(self):
coverage_rate = sum(self.coverage) / len(self.coverage)
avg_width = sum(self.widths) / len(self.widths)
return {
'coverage': coverage_rate,
'avg_width': avg_width,
'min_width': min(self.widths),
'max_width': max(self.widths)
}
analyzer = IntervalAnalyzer()
model = conf.RegressionJackknife(
preprocessing.StandardScaler() | linear_model.LinearRegression(),
confidence_level=0.90
)
for x, y in dataset.take(200):
interval = model.predict_one(x, with_interval=True)
analyzer.analyze(y, interval)
model.learn_one(x, y)
print(analyzer.report())