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 Multinomial

From Leeroopedia


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

Overview

Multinomial distribution for tracking categorical event frequencies with online updates.

Description

Implements a multinomial distribution that maintains counts of categorical outcomes and computes probabilities. Supports initialization with prior observations, incremental updates, reversible operations, mode computation, and sampling. Can be wrapped with Rolling or TimeRolling for windowed probability estimation.

Usage

Use for class probability estimation in Naive Bayes classifiers, categorical feature modeling, or tracking event type distributions. Essential for multinomial Naive Bayes and any scenario requiring online categorical distribution estimation.

Code Reference

Source Location

Signature

class Multinomial(base.DiscreteDistribution):
    def __init__(self, events: dict | list | None = None, seed=None):
        ...

    def update(self, x):
        ...

    def revert(self, x):
        ...

    def __call__(self, x):  # Probability
        ...

    def sample(self):
        ...

    @property
    def mode(self):
        ...

Import

from river import proba

Usage Examples

from river import proba

# Initialize with prior observations
p = proba.Multinomial(['green'] * 3)
p.update('red')
print(f"P(red) = {p('red')}")  # 0.25

p.update('red')
p.update('red')
print(f"P(green) = {p('green')}")  # 0.5

# Revert updates
p.revert('red')
p.revert('red')
print(f"P(red) = {p('red')}")  # 0.25

# Rolling window
from river import utils

X = ['red', 'green', 'green', 'blue', 'blue']

dist = utils.Rolling(
    proba.Multinomial(),
    window_size=3
)

for x in X:
    dist.update(x)
    print(dist)
    print()

# Time rolling
import datetime as dt

X_time = ['red', 'green', 'green', 'blue']
days = [1, 2, 3, 4]

dist_time = utils.TimeRolling(
    proba.Multinomial(),
    period=dt.timedelta(days=2)
)

for x, day in zip(X_time, days):
    dist_time.update(x, t=dt.datetime(2019, 1, day))
    print(dist_time)
    print()

Related Pages

Page Connections

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