Statistics::Descriptive - Module of basic descriptive statistical functions. |
Statistics::Descriptive - Module of basic descriptive statistical functions.
use Statistics::Descriptive; $stat = Statistics::Descriptive::Full->new(); $stat->add_data(1,2,3,4); $mean = $stat->mean(); $var = $stat->variance(); $tm = $stat->trimmed_mean(.25); $Statistics::Descriptive::Tolerance = 1e-10;
This module provides basic functions used in descriptive statistics. It has an object oriented design and supports two different types of data storage and calculation objects: sparse and full. With the sparse method, none of the data is stored and only a few statistical measures are available. Using the full method, the entire data set is retained and additional functions are available.
Whenever a division by zero may occur, the denominator is checked to be
greater than the value $Statistics::Descriptive::Tolerance
, which
defaults to 0.0. You may want to change this value to some small
positive value such as 1e-24 in order to obtain error messages in case
of very small denominators.
Note: Calling add_data with an empty array will delete all of your Full method cached values!
For example, given the 6 measurements:
-2, 7, 7, 4, 18, -5
Then F(-8) = 0, F(-5) = 1/6, F(-5.0001) = 0, F(-4.999) = 1/6, F(7) = 5/6, F(18) = 1, F(239) = 1.
Note that we can recover the different measured values and how many times each occurred from F(x) -- no information regarding the range in values is lost. Summarizing measurements using histograms, on the other hand, in general loses information about the different values observed, so the EDF is preferred.
Using either the EDF or a histogram, however, we do lose information regarding the order in which the values were observed. Whether this loss is potentially significant will depend on the metric being measured.
We will use the term "percentile" to refer to the smallest value of x for which F(x) >= a given percentage. So the 50th percentile of the example above is 4, since F(4) = 3/6 = 50%; the 25th percentile is -2, since F(-5) = 1/6 < 25%, and F(-2) = 2/6 >= 25%; the 100th percentile is 18; and the 0th percentile is -infinity, as is the 15th percentile.
Care must be taken when using percentiles to summarize a sample, because they can lend an unwarranted appearance of more precision than is really available. Any such summary must include the sample size N, because any percentile difference finer than 1/N is below the resolution of the sample.
taken from: RFC2330 - Framework for IP Performance Metrics, Section 11.3. Defining Statistical Distributions
rfc2330 is available from: http://www.cis.ohio-state.edu/htbin/rfc/rfc2330.html
If the percentile method is called in a list context then it will also return the index of the percentile.
trimmed_mean(ltrim)
returns the mean with a fraction ltrim
of entries at each end dropped. trimmed_mean(ltrim,utrim)
returns the mean after a fraction ltrim
has been removed from the
lower end of the data and a fraction utrim
has been removed from the
upper end of the data. This method sorts the data before beginning
to analyze it.
frequency_distribution(partitions)
slices the data into partition
sets (where partition is greater than 1) and counts the number of items
that fall into each partition. It returns an associative array where
the keys are the numerical values of the partitions used. The minimum
value of the data set is not a key and the maximum value of the data
set is always a key. The number of entries for a particular partition
key are the number of items which are greater than the previous
partition key and less then or equal to the current partition key. As
an example,
$stat->add_data(1,1.5,2,2.5,3,3.5,4); %f = $stat->frequency_distribution(2); for (sort {$a <=> $b} keys %f) { print "key = $_, count = $f{$_}\n"; }
prints
key = 2.5, count = 4 key = 4, count = 3
since there are four items less than or equal to 2.5, and 3 items greater than 2.5 and less than 4.
least_squares_fit()
performs a least squares fit on the data,
assuming a domain of @x
or a default of 1..$stat->count(); It
returns an array of four elements ($q, $m, $r, $rms)
where
$q and $m
$r
$rms
If case of error or division by zero, the empty list is returned.
The array that is returned can be ``coerced'' into a hash structure by doing the following:
my %hash = (); @hash{'q', 'm', 'r', 'err'} = $stat->least_squares_fit();
I read 4 of the 5 perl newsgroups comp.lang.perl.{misc,moderated,modules,announce} and check my email at work frequently, so please feel free to post errors to either or both of those places. However, realize that if you post to the newsgroup it has the benefit of alerting other users of the problem. When reporting errors, please include the following to help me out:
-v
at
the command line.
My email address can be found at www.perl.com under Who's Who.
RFC2330, Framework for IP Performance Metrics
The Art of Computer Programming, Volume 2, Donald Knuth.
Handbook of Mathematica Functions, Milton Abramowitz and Irene Stegun.
Probability and Statistics for Engineering and the Sciences, Jay Devore.
Copyright (c) 1997,1998 Colin Kuskie. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Copyright (c) 1998 Andrea Spinelli. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Copyright (c) 1994,1995 Jason Kastner. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Code provided by Andrea Spinelli to prevent division by zero and to make consistent return values for undefined behavior. Andrea also provided a test bench for the module.
A bug fix for the calculation of frequency distributions. Thanks to Nick Tolli for alerting this to me.
Added 4 lines of code to Makefile.PL to make it easier for the ActiveState installation tool to use. Changes work fine in perl5.004_04, haven't tested them under perl5.005xx yet.
Fixed problem with sending 0's and -1's as data. The old 0 : true ? false thing. Use defined to fix.
Provided a fix for AUTOLOAD/DESTROY/Carp bug. Very strange.
Fixed errors in statistics algorithms caused by changing the interface.
Fixed errors in removing cached values (they weren't being removed!) and added sort_data and presorted methods.
June 1997
Transferred ownership of the module from Jason to Colin.
Rewrote OO interface, modified function distribution, added mindex, maxdex.
Added LeastSquaresFit and FrequencyDistribution.
Released to comp.lang.perl and placed on archive sites.
Complete rewrite after extensive and invaluable e-mail correspondence with Anno Siegel.
Initital concept, released to perl5-porters list.
Statistics::Descriptive - Module of basic descriptive statistical functions. |