runstats

2.0.0 · active · verified Thu Apr 16

RunStats is an Apache2 licensed Python module for computing online statistics and linear regression in a single pass. It is designed for efficiently processing large data streams or generators where previous values are not retained, making it suitable for long-running systems. The library, currently at version 2.0.0, is actively maintained and provides numerically stable calculations for various statistical measures and regression coefficients. [1, 2, 3]

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `Statistics` class to compute running statistics like mean, standard deviation, min, and max, and the `Regression` class to calculate slope, intercept, and correlation for a stream of (x, y) pairs. Values are added one by one using the `push` method. [2]

import random
from runstats import Statistics, Regression

# --- Statistics Example ---
stats = Statistics()
for _ in range(100):
    stats.push(random.random() * 100)

print(f"Statistics Count: {len(stats)}")
print(f"Statistics Mean: {stats.mean():.2f}")
print(f"Statistics Std Dev: {stats.stddev():.2f}")
print(f"Statistics Min: {stats.minimum():.2f}")
print(f"Statistics Max: {stats.maximum():.2f}")

# --- Regression Example ---
regr = Regression()
def linear_noisy_func(x_coord):
    alpha, beta = 1.5, 5.0
    noise = (2 * (random.random() - 0.5))
    return alpha * x_coord + beta + noise

for i in range(100):
    x_val = i * 0.1
    y_val = linear_noisy_func(x_val)
    regr.push(x_val, y_val)

print(f"\nRegression Count: {len(regr)}")
print(f"Regression Slope: {regr.slope():.2f}")
print(f"Regression Intercept: {regr.intercept():.2f}")
print(f"Regression Correlation: {regr.correlation():.2f}")

view raw JSON →