1Statistics::DescriptiveU(s3e)r Contributed Perl DocumentaSttiaotnistics::Descriptive(3)
2
3
4

NAME

6       Statistics::Descriptive - Module of basic descriptive statistical
7       functions.
8

VERSION

10       version 3.0800
11

SYNOPSIS

13           use Statistics::Descriptive;
14           my $stat = Statistics::Descriptive::Full->new();
15           $stat->add_data(1,2,3,4);
16           my $mean = $stat->mean();
17           my $var = $stat->variance();
18           my $tm = $stat->trimmed_mean(.25);
19           $Statistics::Descriptive::Tolerance = 1e-10;
20

DESCRIPTION

22       This module provides basic functions used in descriptive statistics.
23       It has an object oriented design and supports two different types of
24       data storage and calculation objects: sparse and full. With the sparse
25       method, none of the data is stored and only a few statistical measures
26       are available. Using the full method, the entire data set is retained
27       and additional functions are available.
28
29       Whenever a division by zero may occur, the denominator is checked to be
30       greater than the value $Statistics::Descriptive::Tolerance, which
31       defaults to 0.0. You may want to change this value to some small
32       positive value such as 1e-24 in order to obtain error messages in case
33       of very small denominators.
34
35       Many of the methods (both Sparse and Full) cache values so that
36       subsequent calls with the same arguments are faster.
37

METHODS

39   Sparse Methods
40       $stat = Statistics::Descriptive::Sparse->new();
41            Create a new sparse statistics object.
42
43       $stat->clear();
44            Effectively the same as
45
46              my $class = ref($stat);
47              undef $stat;
48              $stat = new $class;
49
50            except more efficient.
51
52       $stat->add_data(1,2,3);
53            Adds data to the statistics variable. The cached statistical
54            values are updated automatically.
55
56       $stat->count();
57            Returns the number of data items.
58
59       $stat->mean();
60            Returns the mean of the data.
61
62       $stat->sum();
63            Returns the sum of the data.
64
65       $stat->variance();
66            Returns the variance of the data.  Division by n-1 is used.
67
68       $stat->standard_deviation();
69            Returns the standard deviation of the data. Division by n-1 is
70            used.
71
72       $stat->min();
73            Returns the minimum value of the data set.
74
75       $stat->mindex();
76            Returns the index of the minimum value of the data set.
77
78       $stat->max();
79            Returns the maximum value of the data set.
80
81       $stat->maxdex();
82            Returns the index of the maximum value of the data set.
83
84       $stat->sample_range();
85            Returns the sample range (max - min) of the data set.
86
87   Full Methods
88       Similar to the Sparse Methods above, any Full Method that is called
89       caches the current result so that it doesn't have to be recalculated.
90       In some cases, several values can be cached at the same time.
91
92       $stat = Statistics::Descriptive::Full->new();
93            Create a new statistics object that inherits from
94            Statistics::Descriptive::Sparse so that it contains all the
95            methods described above.
96
97       $stat->add_data(1,2,4,5);
98            Adds data to the statistics variable.  All of the sparse
99            statistical values are updated and cached.  Cached values from
100            Full methods are deleted since they are no longer valid.
101
102            Note:  Calling add_data with an empty array will delete all of
103            your Full method cached values!  Cached values for the sparse
104            methods are not changed
105
106       $stat->add_data_with_samples([{1 => 10}, {2 => 20}, {3 => 30},]);
107            Add data to the statistics variable and set the number of samples
108            each value has been built with. The data is the key of each
109            element of the input array ref, while the value is the number of
110            samples: [{data1 => smaples1}, {data2 => samples2}, ...].
111
112            NOTE: The number of samples is only used by the smoothing function
113            and is ignored otherwise. It is not equivalent to repeat count. In
114            order to repeat a certain datum more than one time call add_data()
115            like this:
116
117                my $value = 5;
118                my $repeat_count = 10;
119                $stat->add_data(
120                    [ ($value) x $repeat_count ]
121                );
122
123       $stat->get_data();
124            Returns a copy of the data array.
125
126       $stat->get_data_without_outliers();
127            Returns a copy of the data array without outliers. The number
128            minimum of samples to apply the outlier filtering is
129            $Statistics::Descriptive::Min_samples_number, 4 by default.
130
131            A function to detect outliers need to be defined (see
132            "set_outlier_filter"), otherwise the function will return an undef
133            value.
134
135            The filtering will act only on the most extreme value of the data
136            set (i.e.: value with the highest absolute standard deviation from
137            the mean).
138
139            If there is the need to remove more than one outlier, the
140            filtering need to be re-run for the next most extreme value with
141            the initial outlier removed.
142
143            This is not always needed since the test (for example Grubb's
144            test) usually can only detect the most exreme value. If there is
145            more than one extreme case in a set, then the standard deviation
146            will be high enough to make neither case an outlier.
147
148       $stat->set_outlier_filter($code_ref);
149            Set the function to filter out the outlier.
150
151            $code_ref is the reference to the subroutine implementing the
152            filtering function.
153
154            Returns "undef" for invalid values of $code_ref (i.e.: not defined
155            or not a code reference), 1 otherwise.
156
157            •   Example #1: Undefined code reference
158
159                    my $stat = Statistics::Descriptive::Full->new();
160                    $stat->add_data(1, 2, 3, 4, 5);
161
162                    print $stat->set_outlier_filter(); # => undef
163
164            •   Example #2: Valid code reference
165
166                    sub outlier_filter { return $_[1] > 1; }
167
168                    my $stat = Statistics::Descriptive::Full->new();
169                    $stat->add_data( 1, 1, 1, 100, 1, );
170
171                    print $stat->set_outlier_filter( \&outlier_filter ); # => 1
172                    my @filtered_data = $stat->get_data_without_outliers();
173                    # @filtered_data is (1, 1, 1, 1)
174
175                In this example the series is really simple and the outlier
176                filter function as well.  For more complex series the outlier
177                filter function might be more complex (see Grubbs' test for
178                outliers).
179
180                The outlier filter function will receive as first parameter
181                the Statistics::Descriptive::Full object, as second the value
182                of the candidate outlier. Having the object in the function
183                might be useful for complex filters where statistics property
184                are needed (again see Grubbs' test for outlier).
185
186       $stat->set_smoother({ method => 'exponential', coeff => 0, });
187            Set the method used to smooth the data and the smoothing
188            coefficient.  See "Statistics::Smoother" for more details.
189
190       $stat->get_smoothed_data();
191            Returns a copy of the smoothed data array.
192
193            The smoothing method and coefficient need to be defined (see
194            "set_smoother"), otherwise the function will return an undef
195            value.
196
197       $stat->sort_data();
198            Sort the stored data and update the mindex and maxdex methods.
199            This method uses perl's internal sort.
200
201       $stat->presorted(1);
202       $stat->presorted();
203            If called with a non-zero argument, this method sets a flag that
204            says the data is already sorted and need not be sorted again.
205            Since some of the methods in this class require sorted data, this
206            saves some time.  If you supply sorted data to the object, call
207            this method to prevent the data from being sorted again. The flag
208            is cleared whenever add_data is called.  Calling the method
209            without an argument returns the value of the flag.
210
211       $stat->skewness();
212            Returns the skewness of the data.  A value of zero is no skew,
213            negative is a left skewed tail, positive is a right skewed tail.
214            This is consistent with Excel.
215
216       $stat->kurtosis();
217            Returns the kurtosis of the data.  Positive is peaked, negative is
218            flattened.
219
220       $x = $stat->percentile(25);
221       ($x, $index) = $stat->percentile(25);
222            Sorts the data and returns the value that corresponds to the
223            percentile as defined in RFC2330:
224
225            •   For example, given the 6 measurements:
226
227                -2, 7, 7, 4, 18, -5
228
229                Then F(-8) = 0, F(-5) = 1/6, F(-5.0001) = 0, F(-4.999) = 1/6,
230                F(7) = 5/6, F(18) = 1, F(239) = 1.
231
232                Note that we can recover the different measured values and how
233                many times each occurred from F(x) -- no information regarding
234                the range in values is lost.  Summarizing measurements using
235                histograms, on the other hand, in general loses information
236                about the different values observed, so the EDF is preferred.
237
238                Using either the EDF or a histogram, however, we do lose
239                information regarding the order in which the values were
240                observed.  Whether this loss is potentially significant will
241                depend on the metric being measured.
242
243                We will use the term "percentile" to refer to the smallest
244                value of x for which F(x) >= a given percentage.  So the 50th
245                percentile of the example above is 4, since F(4) = 3/6 = 50%;
246                the 25th percentile is -2, since F(-5) = 1/6 < 25%, and F(-2)
247                = 2/6 >= 25%; the 100th percentile is 18; and the 0th
248                percentile is -infinity, as is the 15th percentile, which for
249                ease of handling and backward compatibility is returned as
250                undef() by the function.
251
252                Care must be taken when using percentiles to summarize a
253                sample, because they can lend an unwarranted appearance of
254                more precision than is really available.  Any such summary
255                must include the sample size N, because any percentile
256                difference finer than 1/N is below the resolution of the
257                sample.
258
259            (Taken from: RFC2330 - Framework for IP Performance Metrics,
260            Section 11.3.  Defining Statistical Distributions.  RFC2330 is
261            available from: <http://www.ietf.org/rfc/rfc2330.txt> .)
262
263            If the percentile method is called in a list context then it will
264            also return the index of the percentile.
265
266       $x = $stat->quantile($Type);
267            Sorts the data and returns estimates of underlying distribution
268            quantiles based on one or two order statistics from the supplied
269            elements.
270
271            This method use the same algorithm as Excel and R language
272            (quantile type 7).
273
274            The generic function quantile produces sample quantiles
275            corresponding to the given probabilities.
276
277            $Type is an integer value between 0 to 4 :
278
279              0 => zero quartile (Q0) : minimal value
280              1 => first quartile (Q1) : lower quartile = lowest cut off (25%) of data = 25th percentile
281              2 => second quartile (Q2) : median = it cuts data set in half = 50th percentile
282              3 => third quartile (Q3) : upper quartile = highest cut off (25%) of data, or lowest 75% = 75th percentile
283              4 => fourth quartile (Q4) : maximal value
284
285            Example :
286
287              my @data = (1..10);
288              my $stat = Statistics::Descriptive::Full->new();
289              $stat->add_data(@data);
290              print $stat->quantile(0); # => 1
291              print $stat->quantile(1); # => 3.25
292              print $stat->quantile(2); # => 5.5
293              print $stat->quantile(3); # => 7.75
294              print $stat->quantile(4); # => 10
295
296       $stat->median();
297            Sorts the data and returns the median value of the data.
298
299       $stat->harmonic_mean();
300            Returns the harmonic mean of the data.  Since the mean is
301            undefined if any of the data are zero or if the sum of the
302            reciprocals is zero, it will return undef for both of those cases.
303
304       $stat->geometric_mean();
305            Returns the geometric mean of the data.
306
307       my $mode = $stat->mode();
308            Returns the mode of the data. The mode is the most commonly
309            occurring datum.  See
310            <http://en.wikipedia.org/wiki/Mode_%28statistics%29> . If all
311            values occur only once, then mode() will return undef.
312
313       $stat->trimmed_mean(ltrim[,utrim]);
314            "trimmed_mean(ltrim)" returns the mean with a fraction "ltrim" of
315            entries at each end dropped. "trimmed_mean(ltrim,utrim)" returns
316            the mean after a fraction "ltrim" has been removed from the lower
317            end of the data and a fraction "utrim" has been removed from the
318            upper end of the data.  This method sorts the data before
319            beginning to analyze it.
320
321            All calls to trimmed_mean() are cached so that they don't have to
322            be calculated a second time.
323
324       $stat->frequency_distribution_ref($num_partitions);
325       $stat->frequency_distribution_ref(\@bins);
326       $stat->frequency_distribution_ref();
327            "frequency_distribution_ref($num_partitions)" slices the data into
328            $num_partitions sets (where $num_partitions is greater than 1) and
329            counts the number of items that fall into each partition. It
330            returns a reference to a hash where the keys are the numerical
331            values of the partitions used. The minimum value of the data set
332            is not a key and the maximum value of the data set is always a
333            key. The number of entries for a particular partition key are the
334            number of items which are greater than the previous partition key
335            and less then or equal to the current partition key. As an
336            example,
337
338               $stat->add_data(1,1.5,2,2.5,3,3.5,4);
339               $f = $stat->frequency_distribution_ref(2);
340               for (sort {$a <=> $b} keys %$f) {
341                  print "key = $_, count = $f->{$_}\n";
342               }
343
344            prints
345
346               key = 2.5, count = 4
347               key = 4, count = 3
348
349            since there are four items less than or equal to 2.5, and 3 items
350            greater than 2.5 and less than 4.
351
352            "frequency_distribution_refs(\@bins)" provides the bins that are
353            to be used for the distribution.  This allows for non-uniform
354            distributions as well as trimmed or sample distributions to be
355            found.  @bins must be monotonic and must contain at least one
356            element.  Note that unless the set of bins contains the full range
357            of the data, the total counts returned will be less than the
358            sample size.
359
360            Calling "frequency_distribution_ref()" with no arguments returns
361            the last distribution calculated, if such exists.
362
363       my %hash = $stat->frequency_distribution($partitions);
364       my %hash = $stat->frequency_distribution(\@bins);
365       my %hash = $stat->frequency_distribution();
366            Same as "frequency_distribution_ref()" except that it returns the
367            hash clobbered into the return list. Kept for compatibility
368            reasons with previous versions of Statistics::Descriptive and
369            using it is discouraged.
370
371       $stat->least_squares_fit();
372       $stat->least_squares_fit(@x);
373            "least_squares_fit()" performs a least squares fit on the data,
374            assuming a domain of @x or a default of 1..$stat->count().  It
375            returns an array of four elements "($q, $m, $r, $rms)" where
376
377            "$q and $m"
378                satisfy the equation C($y = $m*$x + $q).
379
380            $r  is the Pearson linear correlation cofficient.
381
382            $rms
383                is the root-mean-square error.
384
385            If case of error or division by zero, the empty list is returned.
386
387            The array that is returned can be "coerced" into a hash structure
388            by doing the following:
389
390              my %hash = ();
391              @hash{'q', 'm', 'r', 'err'} = $stat->least_squares_fit();
392
393            Because calling "least_squares_fit()" with no arguments defaults
394            to using the current range, there is no caching of the results.
395

REPORTING ERRORS

397       I read my email frequently, but since adopting this module I've added 2
398       children and 1 dog to my family, so please be patient about my response
399       times.  When reporting errors, please include the following to help me
400       out:
401
402       •   Your version of perl.  This can be obtained by typing perl "-v" at
403           the command line.
404
405       •   Which version of Statistics::Descriptive you're using.  As you can
406           see below, I do make mistakes.  Unfortunately for me, right now
407           there are thousands of CD's with the version of this module with
408           the bugs in it.  Fortunately for you, I'm a very patient module
409           maintainer.
410
411       •   Details about what the error is.  Try to narrow down the scope of
412           the problem and send me code that I can run to verify and track it
413           down.
414

AUTHOR

416       Current maintainer:
417
418       Shlomi Fish, <http://www.shlomifish.org/> , "shlomif@cpan.org"
419
420       Previously:
421
422       Colin Kuskie
423
424       My email address can be found at http://www.perl.com under Who's Who or
425       at: https://metacpan.org/author/COLINK .
426

CONTRIBUTORS

428       Fabio Ponciroli & Adzuna Ltd. team (outliers handling)
429

REFERENCES

431       RFC2330, Framework for IP Performance Metrics
432
433       The Art of Computer Programming, Volume 2, Donald Knuth.
434
435       Handbook of Mathematica Functions, Milton Abramowitz and Irene Stegun.
436
437       Probability and Statistics for Engineering and the Sciences, Jay
438       Devore.
439
441       Copyright (c) 1997,1998 Colin Kuskie. All rights reserved.  This
442       program is free software; you can redistribute it and/or modify it
443       under the same terms as Perl itself.
444
445       Copyright (c) 1998 Andrea Spinelli. All rights reserved.  This program
446       is free software; you can redistribute it and/or modify it under the
447       same terms as Perl itself.
448
449       Copyright (c) 1994,1995 Jason Kastner. All rights reserved.  This
450       program is free software; you can redistribute it and/or modify it
451       under the same terms as Perl itself.
452

LICENSE

454       This program is free software; you can redistribute it and/or modify it
455       under the same terms as Perl itself.
456

SUPPORT

458   Websites
459       The following websites have more information about this module, and may
460       be of help to you. As always, in addition to those websites please use
461       your favorite search engine to discover more resources.
462
463       •   MetaCPAN
464
465           A modern, open-source CPAN search engine, useful to view POD in
466           HTML format.
467
468           <https://metacpan.org/release/Statistics-Descriptive>
469
470       •   RT: CPAN's Bug Tracker
471
472           The RT ( Request Tracker ) website is the default bug/issue
473           tracking system for CPAN.
474
475           <https://rt.cpan.org/Public/Dist/Display.html?Name=Statistics-Descriptive>
476
477       •   CPANTS
478
479           The CPANTS is a website that analyzes the Kwalitee ( code metrics )
480           of a distribution.
481
482           <http://cpants.cpanauthors.org/dist/Statistics-Descriptive>
483
484       •   CPAN Testers
485
486           The CPAN Testers is a network of smoke testers who run automated
487           tests on uploaded CPAN distributions.
488
489           <http://www.cpantesters.org/distro/S/Statistics-Descriptive>
490
491       •   CPAN Testers Matrix
492
493           The CPAN Testers Matrix is a website that provides a visual
494           overview of the test results for a distribution on various
495           Perls/platforms.
496
497           <http://matrix.cpantesters.org/?dist=Statistics-Descriptive>
498
499       •   CPAN Testers Dependencies
500
501           The CPAN Testers Dependencies is a website that shows a chart of
502           the test results of all dependencies for a distribution.
503
504           <http://deps.cpantesters.org/?module=Statistics::Descriptive>
505
506   Bugs / Feature Requests
507       Please report any bugs or feature requests by email to
508       "bug-statistics-descriptive at rt.cpan.org", or through the web
509       interface at
510       <https://rt.cpan.org/Public/Bug/Report.html?Queue=Statistics-Descriptive>.
511       You will be automatically notified of any progress on the request by
512       the system.
513
514   Source Code
515       The code is open to the world, and available for you to hack on. Please
516       feel free to browse it and play with it, or whatever. If you want to
517       contribute patches, please send me a diff or prod me to pull from your
518       repository :)
519
520       <https://github.com/shlomif/perl-Statistics-Descriptive>
521
522         git clone git://github.com/shlomif/perl-Statistics-Descriptive.git
523

AUTHOR

525       Shlomi Fish <shlomif@cpan.org>
526

BUGS

528       Please report any bugs or feature requests on the bugtracker website
529       <https://github.com/shlomif/perl-Statistics-Descriptive/issues>
530
531       When submitting a bug or request, please include a test-file or a patch
532       to an existing test-file that illustrates the bug or desired feature.
533
535       This software is copyright (c) 1997 by Jason Kastner, Andrea Spinelli,
536       Colin Kuskie, and others.
537
538       This is free software; you can redistribute it and/or modify it under
539       the same terms as the Perl 5 programming language system itself.
540
541
542
543perl v5.34.0                      2021-07-22        Statistics::Descriptive(3)
Impressum