RunStats: Computing Statistics and Regression in One Pass

RunStats is an Apache2 licensed Python module for online statistics and online regression. Statistics and regression summaries are computed in a single pass. Previous values are not recorded in summaries.

Long running systems often generate numbers summarizing performance. It could be the latency of a response or the time between requests. It’s often useful to use these numbers in summary statistics like the arithmetic mean, minimum, standard deviation, etc. When many values are generated, computing these summaries can be computationally intensive. It may even be infeasible to keep every recorded value. In such cases computing online statistics and online regression is necessary.

In other cases, you may only have one opportunity to observe all the recorded values. Python’s generators work exactly this way. Traditional methods for calculating the variance and other higher moments requires multiple passes over the data. With generators, this is not possible and so computing statistics in a single pass is necessary.

The Python RunStats module was designed for these cases by providing a pair of classes for computing online summary statistics and online linear regression in a single pass. Summary objects work on sequences which may be larger than memory or disk space permit. They may also be efficiently combined together to create aggregate summaries.


  • Pure-Python
  • Fully Documented
  • 100% Test Coverage
  • Numerically Stable
  • Optional Cython-optimized Extension (20-40 times faster)
  • Statistics summary computes mean, variance, standard deviation, skewness, kurtosis, minimum and maximum.
  • Regression summary computes slope, intercept and correlation.
  • Developed on Python 3.7
  • Tested on CPython 2.7, 3.4, 3.5, 3.6, 3.7 and PyPy, PyPy3
  • Tested using Travis CI


Installing RunStats is simple with pip:

$ pip install runstats

If you want the Cython-optimized version then first install Cython:

$ pip install cython
$ pip install runstats

You can access documentation in the interpreter with Python’s built-in help function:

>>> from runstats import Statistics, Regression
>>> help(Statistics)
>>> help(Regression)


The Python RunStats module provides two types for computing running Statistics and Regression. The Regression object leverages Statistics internally for its calculations. Each can be initialized without arguments:

>>> from runstats import Statistics, Regression
>>> stats = Statistics()
>>> regr = Regression()

Statistics objects support four methods for modification. Use push to add values to the summary, clear to reset the summary, sum to combine Statistics summaries and multiply to weight summary Statistics by a scalar.

>>> for num in range(10):
...     stats.push(num)
>>> stats.mean()
>>> stats.maximum()
>>> stats += stats
>>> stats.mean()
>>> stats.variance()
>>> len(stats)
>>> stats *= 2
>>> len(stats)
>>> stats.clear()
>>> len(stats)
>>> stats.minimum() is None

Use the Python built-in len for the number of pushed values. Unfortunately the Python min and max built-ins may not be used for the minimum and maximum as sequences are instead expected. There are instead minimum and maximum methods which are provided for that purpose:

>>> import random
>>> random.seed(0)
>>> for __ in range(1000):
...     stats.push(random.random())
>>> len(stats)
>>> min(stats)
Traceback (most recent call last):
TypeError: iteration over non-sequence
>>> stats.minimum()
>>> stats.maximum()

Statistics summaries provide five measures of a series: mean, variance, standard deviation, skewness and kurtosis:

>>> stats = Statistics([1, 2, 5, 12, 5, 2, 1])
>>> stats.mean()
>>> stats.variance()
>>> stats.stddev()
>>> stats.skewness()
>>> stats.kurtosis()

All internal calculations use Python’s float type.

Like Statistics, the Regression type supports some methods for modification: push, clear and sum:

>>> regr.clear()
>>> len(regr)
>>> for num in range(10):
...     regr.push(num, num + 5)
>>> len(regr)
>>> regr.slope()
>>> more = Regression((num, num + 5) for num in range(10, 20))
>>> total = regr + more
>>> len(total)
>>> total.slope()
>>> total.intercept()
>>> total.correlation()

Regression summaries provide three measures of a series of pairs: slope, intercept and correlation. Note that, as a regression, the points need not exactly lie on a line:

>>> regr = Regression([(1.2, 1.9), (3, 5.1), (4.9, 8.1), (7, 11)])
>>> regr.slope()
>>> regr.intercept()
>>> regr.correlation()

Both constructors accept an optional iterable that is consumed and pushed into the summary. Note that you may pass a generator as an iterable and the generator will be entirely consumed.

All internal calculations are based entirely on the C++ code by John Cook as posted in a couple of articles:

The pure-Python and Cython-optimized versions of RunStats are each directly available if preferred.

>>> from runstats.core import Statistics, Regression  # pure-Python
>>> from import Statistics, Regression  # Cython-optimized

When importing from runstats the fast version is preferred and the core version is used as fallback. Micro-benchmarking Statistics and Regression by calling push repeatedly shows the Cython-optimized extension as 20-40 times faster than the pure-Python extension.


Copyright 2013-2019 Grant Jenks

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.