Skip to main content

opstats

Python implementation of an online parallel statistics calculator. This library will calculate the total, mean, variance, standard deviation, skewness and kurtosis. There are additional options for calculating covariance and correlation between two sequences of data points.

Online calculation is appropriate when you don't yet have the entire dataset in order to calculate the mean (e.g. in a streaming environment). It is more processor-intensive than the traditional methods however.

When combined with parallel computation, it can also be useful when the data is very large as it works in a single pass and can be distributed.

Installation

pip install opstats

Usage

Moment Calculator

For calculating the mean, variance (and standard deviation), skewness and kurtosis, use the MomentCalculator.

import random
from opstats import MomentCalculator
data_points = random.sample(range(1, 100), 20)
calc = MomentCalculator()
for d in data_points:
    calc.add(d)

result = calc.get()

The result will be a NamedTuple containing the computed moments up until this point. More data can subsequently be added and the result can be retrieved again.

Parallel Processing

Data can be split into multiple parts and processed in parallel. The resulting statistics can be combined using the aggregate_moments function.

from opstats import aggregate_moments
# Divide the sample data in half.
left_data = data_points[:len(data_points)//2]
right_data = data_points[len(data_points)//2:]
# Create stats for each half. 
left = MomentCalculator()
for d in left_data:
    left.add(d)

right = MomentCalculator()
for d in right_data:
    right.add(d)

# Combine the results.
result = aggregate_moments([left.get(), right.get()])

Covariance and Correlation

The CovarianceCalculator class and aggregate_covariance function work in the same manner as above for calculating the covariance and correlation between two sequences of data points.

Extended Statistics

When installed with pip install opstats[extended], cardinality and percentiles can also be estimated. Cardinality will be estimated with HyperLogLog and percentiles with T-Digest.

from opstats.extended import ExtendedCalculator
calc = ExtendedCalculator()
for d in data_points:
    calc.add(d)

result = calc.get()

This can also be calculated in parallel. Note the changes to using get_parallel() which returns an intermediate object and calculate() which computes the final values.

from opstats.extended import aggregate_extended
# Divide the sample data in half.
left_data = data_points[:len(data_points)//2]
right_data = data_points[len(data_points)//2:]
# Create stats for each half. 
left = ExtendedCalculator()
for d in left_data:
    left.add(d)

right = ExtendedCalculator()
for d in right_data:
    right.add(d)

# Combine the results.
result = aggregate_extended([left.get_parallel(), right.get_parallel()]).calculate()

Credits

Online calculator adapted from: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance (Terriberry, Timothy B)

Aggregation translated from: https://rdrr.io/cran/utilities/src/R/sample.decomp.R

Python HyperLogLog implementation: https://github.com/svpcom/hyperloglog

Python T-Digest implementation: https://github.com/CamDavidsonPilon/tdigest

Metadata

Release files for opstats 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opstats 1.2.0
File Size Uploaded
opstats-1.2.0.tar.gz 50.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for opstats 1.2.0
File Interpreter ABI Platform
opstats-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 91.8 kB

Release files / opstats-1.2.0.tar.gz

Download URL opstats-1.2.0.tar.gz
Size 50.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d81f8a4edc317fae46590725a52bfe350e51ca699aac15e83ff921d9f58c0f54
BLAKE2b-256 checksum
How to use checksums
35ea7df622c2828d93ab4e23213b1c1d427400e35acd176f65e86f60740bad73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release files / opstats-1.2.0-py3-none-any.whl

Download URL opstats-1.2.0-py3-none-any.whl
Size 41.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c54b703d58e6a49a8a74ff2016492ebd4fc5fbfe34d0be65aee30c92e0316a1
BLAKE2b-256 checksum
How to use checksums
64a7662cc16902dba1009f78b8dfc40abad7e1b190b8a6add088aabfdc68d799
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page