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 Proba Gaussian

From Leeroopedia


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

Overview

Online Gaussian (normal) distribution with incremental mean and variance updates.

Description

Implements univariate and multivariate Gaussian distributions that update incrementally as new data arrives. Supports PDF, CDF, sampling, and reversible updates. The univariate version tracks mean (μ) and standard deviation (σ), while the multivariate version maintains a full covariance matrix using Welford's online algorithm.

Usage

Use for Naive Bayes classifiers, anomaly detection, or any application requiring online probability density estimation. The multivariate version is essential for Gaussian discriminant analysis and correlated feature modeling.

Code Reference

Source Location

Signature

class Gaussian(base.ContinuousDistribution):
    def __init__(self, seed=None):
        ...

    def update(self, x, w=1.0):
        ...

    def revert(self, x, w=1.0):
        ...

    def __call__(self, x) -> float:  # PDF
        ...

    def cdf(self, x) -> float:
        ...

    def sample(self) -> float:
        ...

class MultivariateGaussian(base.MultivariateContinuousDistribution):
    def __init__(self, seed=None):
        ...

    def update(self, x: dict):
        ...

    def __call__(self, x: dict[str, float]) -> float:  # PDF
        ...

    def cdf(self, x: dict[str, float]) -> float:
        ...

    def sample(self) -> dict[str, float]:
        ...

Import

from river import proba

Usage Examples

from river import proba

# Univariate Gaussian
p = proba.Gaussian()
p.update(6)
p.update(7)

print(p)  # 𝒩(μ=6.500, σ=0.707)
print(f"PDF(6.5) = {p(6.5):.4f}")
print(f"CDF(6.5) = {p.cdf(6.5):.4f}")
print(f"Sample: {p.sample():.2f}")

# Multivariate
import pandas as pd
import numpy as np

np.random.seed(42)
X = pd.DataFrame(
    np.random.random((8, 3)),
    columns=["red", "green", "blue"]
)

mv_p = proba.MultivariateGaussian(seed=42)
for x in X.to_dict(orient="records"):
    mv_p.update(x)

print(f"\nMean: {mv_p.mu}")
print(f"PDF: {mv_p(x):.4f}")
print(f"Sample: {mv_p.sample()}")

Related Pages

Page Connections

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