RunningStat

RunningStat is a PHP library for online statistical algorithms. The most recent version implements two useful algorithms. The first is an algorithm for estimating higher-order moments (variance and standard deviation) of a random variable without having to store individual observations. It also keeps track of the arithmetic mean and extrema (min and max). Together these measurements describe the central tendency and statistical dispersion of a population.

The second algorithm is the P-Square algorithm, as described in "The P-Square Algorithm for Dynamic Calculation of Percentiles and Histograms without Storing Observations," Communications of the ACM, October 1985 by R. Jain and I. Chlamtac.

Installing

To use it inside your application, simply run composer require wikimedia/running-stat, or add a dependency in your composer.json.

If you prefer using git, that's also an option: git clone https://gerrit.wikimedia.org/r/RunningStat.git, and autoload the library however you wish to.

If you wish to develop and contribute on the library, see developer account for gaining access to our code review system.

Usage

use Wikimedia\RunningStat;

$rstat = new RunningStat();
foreach ( array(
  49.7168, 74.3804,  7.0115, 96.5769, 34.9458,
  36.9947, 33.8926, 89.0774, 23.7745, 73.5154,
  86.1322, 53.2124, 16.2046, 73.5130, 10.4209,
  42.7299, 49.3330, 47.0215, 34.9950, 18.2914,
) as $sample ) {
  $rstat->push( $sample );
}


printf(
  "n = %d; min = %.2f; max = %.2f; mean = %.2f; variance = %.2f; stddev = %.2f\n",
  count( $rstat ),
  $rstat->min,
  $rstat->max,
  $rstat->getMean(),
  $rstat->getVariance(),
  $rstat->getStdDev()
);
// Output:
// n = 20; min = 7.01; max = 96.58; mean = 47.59; variance = 725.71; stddev = 26.94

Code stewardship