| 1 |
RunningStat |
| 2 |
=========== |
| 3 |
|
| 4 |
RunningStat computes the central tendency, shape, and extrema of a set of |
| 5 |
points online, in constant space. It uses a neat one-pass algorithm for |
| 6 |
calculating variance, described here: |
| 7 |
<https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm> |
| 8 |
|
| 9 |
This particular implementation adapts a sample C++ implementation by John D. |
| 10 |
Cook to PHP. See also: |
| 11 |
|
| 12 |
* <http://www.johndcook.com/standard_deviation.html> |
| 13 |
* <http://www.johndcook.com/skewness_kurtosis.html> |
| 14 |
|
| 15 |
RunningStat instances can be combined. The resultant RunningStat has the same |
| 16 |
state it would have had if it had been used to accumulate each point. This |
| 17 |
property is attractive because it allows separate threads of execution to |
| 18 |
process a stream in parallel. More importantly, individual points can be |
| 19 |
accumulated in stages, without loss of fidelity, at intermediate points in the |
| 20 |
aggregation process. JavaScript profiling samples can be accumulated in the |
| 21 |
user's browser and be combined with measurements from other browsers on the |
| 22 |
profiling data aggregator. Functions that are called multiple times in the |
| 23 |
course of a profiled web request can be accumulated in MediaWiki prior to being |
| 24 |
transmitted to the profiling data aggregator. |
| 25 |
|
| 26 |
Usage |
| 27 |
----- |
| 28 |
Here is how you use it: |
| 29 |
|
| 30 |
```php |
| 31 |
use Wikimedia\RunningStat; |
| 32 |
|
| 33 |
$rstat = new RunningStat(); |
| 34 |
foreach ( [ |
| 35 |
49.7168, 74.3804, 7.0115, 96.5769, 34.9458, |
| 36 |
36.9947, 33.8926, 89.0774, 23.7745, 73.5154, |
| 37 |
86.1322, 53.2124, 16.2046, 73.5130, 10.4209, |
| 38 |
42.7299, 49.3330, 47.0215, 34.9950, 18.2914, |
| 39 |
] as $sample ) { |
| 40 |
$rstat->addObservation( $sample ); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
printf( |
| 45 |
"n = %d; min = %.2f; max = %.2f; mean = %.2f; variance = %.2f; stddev = %.2f\n", |
| 46 |
count( $rstat ), |
| 47 |
$rstat->min, |
| 48 |
$rstat->max, |
| 49 |
$rstat->getMean(), |
| 50 |
$rstat->getVariance(), |
| 51 |
$rstat->getStdDev() |
| 52 |
); |
| 53 |
// Output: |
| 54 |
// n = 20; min = 7.01; max = 96.58; mean = 47.59; variance = 725.71; stddev = 26.94 |
| 55 |
``` |
| 56 |
|
| 57 |
License |
| 58 |
------- |
| 59 |
GPL-2.0-or-later |