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 Beta

From Leeroopedia


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

Overview

Beta distribution for modeling probability distributions over probabilities with conjugate prior properties.

Description

Implements the Beta distribution parameterized by alpha and beta, commonly used as a conjugate prior for Bernoulli and binomial distributions. Updates by incrementing alpha for successes and beta for failures. Provides PDF, CDF, mode, and sampling capabilities, making it ideal for Bayesian updating and Thompson sampling.

Usage

Use for Bayesian A/B testing, multi-armed bandits (Thompson sampling), or modeling uncertainty in binary event probabilities. Essential for online Bayesian inference when the true probability is unknown and must be learned.

Code Reference

Source Location

Signature

class Beta(base.ContinuousDistribution):
    def __init__(self, alpha: int = 1, beta: int = 1, seed: int | None = None):
        ...

    def update(self, x):
        ...

    def revert(self, x):
        ...

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

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

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

    @property
    def mode(self) -> float:
        ...

Import

from river import proba

Usage Examples

from river import proba

# Initialize with prior belief (81 successes, 219 failures)
beta = proba.Beta(alpha=81, beta=219)

# PDF at different probability values
print(f"PDF(0.21) = {beta(0.21):.4f}")
print(f"PDF(0.35) = {beta(0.35):.4f}")

# Update with new observations
for _ in range(100):
    beta.update(True)  # Success

for _ in range(200):
    beta.update(False)  # Failure

print(f"\nAfter updates:")
print(f"PDF(0.21) = {beta(0.21):.6f}")
print(f"PDF(0.35) = {beta(0.35):.4f}")
print(f"CDF(0.35) = {beta.cdf(0.35):.6f}")

# Sampling for Thompson sampling
samples = [beta.sample() for _ in range(5)]
print(f"\nSamples: {samples}")

# Mode (most likely value)
print(f"Mode: {beta.mode:.4f}")

Related Pages

Page Connections

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