Statistics::Running - Basic descriptive statistics (mean/stdev/min/max/skew/kurtosis) and discrete Probability Distribution (via histogram) over data without the need to store data points ever. OOP style.
Version 0.11
use Statistics::Running; my $ru = Statistics::Running->new(); for(1..100){ $ru->add(rand()); } print "mean: ".$ru->mean()."\n"; $ru->add(12345); print "mean: ".$ru->mean()."\n"; my $ru2 = Statistics::Running->new(); $ru2->histogram_enable({ 'num-bins' => 10, 'bin-width' => 0.01, 'left-boundary' => 0 }); for(1..100){ $ru2->add(rand()); } print "Probability Distribution of data:\n".$ru2->histogram_stringify()."\n"; # add two stat objects together (histograms are not!) my $ru3 = $ru + $ru2; print "mean of concatenated data: ".$ru3->mean()."\n"; $ru += $ru2; print "mean after appending data: ".$ru->mean()."\n"; print "stats: ".$ru->stringify()."\n"; # example output: print $ru2."\n"; N: 100, mean: 0.488978434779093, range: 0.0056063539679414 to 0.99129297226348, standard deviation: 0.298129905728534, kurtosis: -1.22046199974301, skewness: -0.0268827866000826, histogram: 0.000 - 0.010: 2 ##################################################### 0.010 - 0.020: 2 ##################################################### 0.020 - 0.030: 2 ##################################################### 0.030 - 0.041: 2 ##################################################### 0.041 - 0.051: 1 ########################### 0.051 - 0.062: 0 | 0.062 - 0.073: 2 ##################################################### 0.073 - 0.083: 0 | 0.083 - 0.094: 1 ###########################
Statistics are updated every time a new data point is added in. The common practice to calculate descriptive statistics for 5 data points as well as 1 billion points is to store them in an array, loop over the array to calculate the mean, then loop over the array again to calculate standard deviation, as Sum (x_i-mean)**2. Standard deviation is the reason data is stored in the array. This module uses B.P.Welford's method to calculate descriptive statistics by continually adjusting the stats and not storing a single data point. Except from the computational and environmental benefits of such an approach, B.P.Welford's method is also immune to accumulated precision errors. It is stable and accurate.
For more details on the method and its stability look at this: John D. Cook's article and C++ implementation
A version without the histogram exists under Statistics::Running::Tiny and is faster, obviously. About 25% faster.
There are three amazing things about B.P.Welford's algorithm implemented here:
The basis for the code in this module comes from John D. Cook's article and C++ implementation
Nothing, this is an Object Oriented module. Once you instantiate an object all its methods are yours.
Constructor, initialises internal variables.
Update our statistics after one more data point/sample (or an array of them) is presented to us.
my $ru1 = Statistics::Running->new(); for(1..100){ $ru1->add(rand()); print $ru1."\n"; }
Input can be a single data point (a scalar) or a reference to an array of data points.
Copy state of input object into current effectively making us like them. Our previous state is forgotten. After that adding a new data point into us will be with the new state copied.
my $ru1 = Statistics::Running->new(); for(1..100){ $ru1->add(rand()); } my $ru2 = Statistics::Running->new(); for(1..100){ $ru2->add(rand()); } # copy the state of ru1 into ru2. state of ru1 is forgotten. $ru2->copy_from($ru1);
Clone state of our object into a newly created object which is returned. Our object and returned object are identical at the time of cloning.
my $ru1 = Statistics::Running->new(); for(1..100){ $ru1->add(rand()); } my $ru2 = $ru1->clone();
Clear our internal state as if no data points have ever been added into us. As if we were just created. All state is forgotten and reset to zero, including histogram.
Returns the mean of all the data pushed in us
Returns the sum of all the data pushed in us (algebraic sum, not absolute sum)
Returns the sum of the absolute value of all the data pushed in us (this is not algebraic sum)
Returns the minimum data sample added in us
Returns the maximum data sample added in us
Returns the number of data points/samples inserted, and had their descriptive statistics calculated, so far.
Returns the variance of the data points/samples added onto us so far.
Returns the standard deviation of the data points/samples added onto us so far. This is the square root of the variance.
Returns the skewness of the data points/samples added onto us so far.
Returns the kurtosis of the data points/samples added onto us so far.
Concatenates our state with the input object's state and returns a newly created object with the combined state. Our object and input object are not modified. The overloaded symbol '+' points to this sub.
Appends input object's state into ours. Our state is modified. (input object's state is not modified) The overloaded symbol '+=' points to this sub.
Enables histogram logging by creating a histogram with specified parameters. These parameters can be of different formats:
my $ru1 = Statistics::Running->new(); $ru1->histogram_enable({ 'num-bins' => 10, 'bin-width' => 0.01, 'left-boundary' => 0 }); # or, 2 bins: 0-1 and 1-2 $ru1->histogram_enable({ '0:1' => 0, '1:2' => 1, }); # or, 2 bins: 0-1 and 1-2 $ru1->histogram_enable([0,1,2]);
$ru-
[histo-left-boundary, bin1_right_boundary, bin2_right_boundary, ... binN-1_right_boundary, histo-right-boundary]
Disable histogram logging, all existing histogram data is erased. Number of bins is forgotten, along with bin boundaries, etc.
Set existing histogram to zero counts.
Returns the count in bin specified by bin index (which is 0 to number-of-bins - 1)
Check if our state (number of samples and all internal state) is the same with input object's state. Equality here implies that ALL statistics are equal (within a small number Statistics::Running::SMALL_NUMBER_FOR_EQUALITY)
Check if our statistics only (and not sample size) are the same with input object. E.g. it checks mean, variance etc. but not sample size (as with the real equals()). It returns 0 on non-equality. 1 if equal.
Check if our histogram only (and not statitstics) are the same with input object. It returns 0 on non-equality. 1 if equal.
Returns a string description of descriptive statistics we know about (mean, standard deviation, kurtosis, skewness) as well as the number of data points/samples added onto us so far. Note that this method is not necessary because stringification is overloaded and the follow print $stats_obj."\n" is equivalent to print $stats_obj-stringify()."\n" >
print $stats_obj."\n"
print $stats_obj-
my $ru3 = $ru1 + $ru2
if( $ru2 == $ru3 ){ ... }
print $ru1."\n"
In testing if two objects are the same, their means, standard deviations etc. are compared. This is done using if( ($self-mean() - $other->mean()) < Statistics::Running::SMALL_NUMBER_FOR_EQUALITY ){ ... } >
if( ($self-
Run make bench for benchmarks which report the maximum number of data points inserted per second (in your system).
make bench
kurtosis()
skewness()
Andreas Hadjiprocopis, <bliako at cpan.org>
<bliako at cpan.org>
Please report any bugs or feature requests to bug-statistics-running at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Statistics-Running. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-statistics-running at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Statistics::Running
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Statistics-Running
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Statistics-Running
CPAN Ratings
http://cpanratings.perl.org/d/Statistics-Running
Search CPAN
http://search.cpan.org/dist/Statistics-Running/
Almaz
B.P.Welford, John Cook.
Copyright 2018-2019 Andreas Hadjiprocopis.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
To install Statistics::Running, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Statistics::Running
CPAN shell
perl -MCPAN -e shell install Statistics::Running
For more information on module installation, please visit the detailed CPAN module installation guide.