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 Linear Model BayesianLinearRegression

From Leeroopedia


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

Overview

Bayesian linear regression provides probabilistic predictions with automatic feature scaling and uncertainty quantification in O(n²) time complexity.

Description

This implementation uses a Bayesian approach to linear regression, maintaining a posterior distribution over model parameters. It computes predictions by updating mean and covariance matrices using Sherman-Morrison formulas for efficiency. The model can output either point estimates or full predictive distributions (Gaussian). Unlike standard linear regression, features don't require preprocessing as the Bayesian framework handles scaling naturally. An optional smoothing parameter enables concept drift adaptation by gradually forgetting past data.

Usage

Use Bayesian linear regression when you need uncertainty estimates with predictions, want to avoid manual feature scaling, or require robustness to hyperparameter choices. The smoothing parameter is particularly useful for non-stationary environments with concept drift. Note that O(n²) complexity makes it slower than standard linear regression for high-dimensional problems.

Code Reference

Source Location

Signature

class BayesianLinearRegression(base.Regressor):
    def __init__(self, alpha=1, beta=1, smoothing: float | None = None):
        self.alpha = alpha
        self.beta = beta
        self.smoothing = smoothing

Import

from river import linear_model

I/O Contract

Parameters

Parameter Type Default Description
alpha float 1 Prior precision parameter
beta float 1 Noise precision parameter
smoothing float or None None Forgetting factor for concept drift (0-1)

Input

Method Input Type Description
learn_one x: dict, y: float Features and target value
predict_one x: dict, with_dist: bool Features and optional distribution flag

Output

Method Output Type Description
predict_one float or Gaussian Point estimate or full distribution
predict_many pd.Series Batch predictions

Usage Examples

from river import datasets
from river import evaluate
from river import linear_model
from river import metrics

dataset = datasets.TrumpApproval()
model = linear_model.BayesianLinearRegression()
metric = metrics.MAE()

evaluate.progressive_val_score(dataset, model, metric)
# MAE: 0.586...

# Get point prediction
x, _ = next(iter(dataset))
model.predict_one(x)
# 43.852...

# Get full predictive distribution
model.predict_one(x, with_dist=True)
# 𝒩(μ=43.85..., σ=1.00...)

# Example with smoothing for concept drift
import itertools
import random

def random_data(coefs, n, seed=42):
    rng = random.Random(seed)
    for _ in range(n):
        x = {i: rng.random() for i, c in enumerate(coefs)}
        y = sum(c * xi for c, xi in zip(coefs, x.values()))
        yield x, y

model = linear_model.BayesianLinearRegression(smoothing=0.8)
dataset = itertools.chain(
    random_data([0.1, 3], 100),
    random_data([10, -2], 100)
)
metric = metrics.MAE()
evaluate.progressive_val_score(dataset, model, metric)
# MAE: 0.159...

Related Pages

Page Connections

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