1Statistics::DescriptiveU:s:eFrulClo(n3t)ributed Perl DocSutmaetnitsattiicosn::Descriptive::Full(3)
2
3
4
6 Statistics::Descriptive - Module of basic descriptive statistical
7 functions.
8
10 version 3.0701
11
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
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
39 version 3.0701
40
42 Sparse Methods
43 $stat = Statistics::Descriptive::Sparse->new();
44 Create a new sparse statistics object.
45
46 $stat->clear();
47 Effectively the same as
48
49 my $class = ref($stat);
50 undef $stat;
51 $stat = new $class;
52
53 except more efficient.
54
55 $stat->add_data(1,2,3);
56 Adds data to the statistics variable. The cached statistical
57 values are updated automatically.
58
59 $stat->count();
60 Returns the number of data items.
61
62 $stat->mean();
63 Returns the mean of the data.
64
65 $stat->sum();
66 Returns the sum of the data.
67
68 $stat->variance();
69 Returns the variance of the data. Division by n-1 is used.
70
71 $stat->standard_deviation();
72 Returns the standard deviation of the data. Division by n-1 is
73 used.
74
75 $stat->min();
76 Returns the minimum value of the data set.
77
78 $stat->mindex();
79 Returns the index of the minimum value of the data set.
80
81 $stat->max();
82 Returns the maximum value of the data set.
83
84 $stat->maxdex();
85 Returns the index of the maximum value of the data set.
86
87 $stat->sample_range();
88 Returns the sample range (max - min) of the data set.
89
90 Full Methods
91 Similar to the Sparse Methods above, any Full Method that is called
92 caches the current result so that it doesn't have to be recalculated.
93 In some cases, several values can be cached at the same time.
94
95 $stat = Statistics::Descriptive::Full->new();
96 Create a new statistics object that inherits from
97 Statistics::Descriptive::Sparse so that it contains all the
98 methods described above.
99
100 $stat->add_data(1,2,4,5);
101 Adds data to the statistics variable. All of the sparse
102 statistical values are updated and cached. Cached values from
103 Full methods are deleted since they are no longer valid.
104
105 Note: Calling add_data with an empty array will delete all of
106 your Full method cached values! Cached values for the sparse
107 methods are not changed
108
109 $stat->add_data_with_samples([{1 => 10}, {2 => 20}, {3 => 30},]);
110 Add data to the statistics variable and set the number of samples
111 each value has been built with. The data is the key of each
112 element of the input array ref, while the value is the number of
113 samples: [{data1 => smaples1}, {data2 => samples2}, ...].
114
115 NOTE: The number of samples is only used by the smoothing function
116 and is ignored otherwise. It is not equivalent to repeat count. In
117 order to repeat a certain datum more than one time call add_data()
118 like this:
119
120 my $value = 5;
121 my $repeat_count = 10;
122 $stat->add_data(
123 [ ($value) x $repeat_count ]
124 );
125
126 $stat->get_data();
127 Returns a copy of the data array.
128
129 $stat->get_data_without_outliers();
130 Returns a copy of the data array without outliers. The number
131 minimum of samples to apply the outlier filtering is
132 $Statistics::Descriptive::Min_samples_number, 4 by default.
133
134 A function to detect outliers need to be defined (see
135 "set_outlier_filter"), otherwise the function will return an undef
136 value.
137
138 The filtering will act only on the most extreme value of the data
139 set (i.e.: value with the highest absolute standard deviation from
140 the mean).
141
142 If there is the need to remove more than one outlier, the
143 filtering need to be re-run for the next most extreme value with
144 the initial outlier removed.
145
146 This is not always needed since the test (for example Grubb's
147 test) usually can only detect the most exreme value. If there is
148 more than one extreme case in a set, then the standard deviation
149 will be high enough to make neither case an outlier.
150
151 $stat->set_outlier_filter($code_ref);
152 Set the function to filter out the outlier.
153
154 $code_ref is the reference to the subroutine implementing the
155 filtering function.
156
157 Returns "undef" for invalid values of $code_ref (i.e.: not defined
158 or not a code reference), 1 otherwise.
159
160 · Example #1: Undefined code reference
161
162 my $stat = Statistics::Descriptive::Full->new();
163 $stat->add_data(1, 2, 3, 4, 5);
164
165 print $stat->set_outlier_filter(); # => undef
166
167 · Example #2: Valid code reference
168
169 sub outlier_filter { return $_[1] > 1; }
170
171 my $stat = Statistics::Descriptive::Full->new();
172 $stat->add_data( 1, 1, 1, 100, 1, );
173
174 print $stat->set_outlier_filter( \&outlier_filter ); # => 1
175 my @filtered_data = $stat->get_data_without_outliers();
176 # @filtered_data is (1, 1, 1, 1)
177
178 In this example the series is really simple and the outlier
179 filter function as well. For more complex series the outlier
180 filter function might be more complex (see Grubbs' test for
181 outliers).
182
183 The outlier filter function will receive as first parameter
184 the Statistics::Descriptive::Full object, as second the value
185 of the candidate outlier. Having the object in the function
186 might be useful for complex filters where statistics property
187 are needed (again see Grubbs' test for outlier).
188
189 $stat->set_smoother({ method => 'exponential', coeff => 0, });
190 Set the method used to smooth the data and the smoothing
191 coefficient. See "Statistics::Smoother" for more details.
192
193 $stat->get_smoothed_data();
194 Returns a copy of the smoothed data array.
195
196 The smoothing method and coefficient need to be defined (see
197 "set_smoother"), otherwise the function will return an undef
198 value.
199
200 $stat->sort_data();
201 Sort the stored data and update the mindex and maxdex methods.
202 This method uses perl's internal sort.
203
204 $stat->presorted(1);
205 $stat->presorted();
206 If called with a non-zero argument, this method sets a flag that
207 says the data is already sorted and need not be sorted again.
208 Since some of the methods in this class require sorted data, this
209 saves some time. If you supply sorted data to the object, call
210 this method to prevent the data from being sorted again. The flag
211 is cleared whenever add_data is called. Calling the method
212 without an argument returns the value of the flag.
213
214 $stat->skewness();
215 Returns the skewness of the data. A value of zero is no skew,
216 negative is a left skewed tail, positive is a right skewed tail.
217 This is consistent with Excel.
218
219 $stat->kurtosis();
220 Returns the kurtosis of the data. Positive is peaked, negative is
221 flattened.
222
223 $x = $stat->percentile(25);
224 ($x, $index) = $stat->percentile(25);
225 Sorts the data and returns the value that corresponds to the
226 percentile as defined in RFC2330:
227
228 · For example, given the 6 measurements:
229
230 -2, 7, 7, 4, 18, -5
231
232 Then F(-8) = 0, F(-5) = 1/6, F(-5.0001) = 0, F(-4.999) = 1/6,
233 F(7) = 5/6, F(18) = 1, F(239) = 1.
234
235 Note that we can recover the different measured values and how
236 many times each occurred from F(x) -- no information regarding
237 the range in values is lost. Summarizing measurements using
238 histograms, on the other hand, in general loses information
239 about the different values observed, so the EDF is preferred.
240
241 Using either the EDF or a histogram, however, we do lose
242 information regarding the order in which the values were
243 observed. Whether this loss is potentially significant will
244 depend on the metric being measured.
245
246 We will use the term "percentile" to refer to the smallest
247 value of x for which F(x) >= a given percentage. So the 50th
248 percentile of the example above is 4, since F(4) = 3/6 = 50%;
249 the 25th percentile is -2, since F(-5) = 1/6 < 25%, and F(-2)
250 = 2/6 >= 25%; the 100th percentile is 18; and the 0th
251 percentile is -infinity, as is the 15th percentile, which for
252 ease of handling and backward compatibility is returned as
253 undef() by the function.
254
255 Care must be taken when using percentiles to summarize a
256 sample, because they can lend an unwarranted appearance of
257 more precision than is really available. Any such summary
258 must include the sample size N, because any percentile
259 difference finer than 1/N is below the resolution of the
260 sample.
261
262 (Taken from: RFC2330 - Framework for IP Performance Metrics,
263 Section 11.3. Defining Statistical Distributions. RFC2330 is
264 available from: <http://www.ietf.org/rfc/rfc2330.txt> .)
265
266 If the percentile method is called in a list context then it will
267 also return the index of the percentile.
268
269 $x = $stat->quantile($Type);
270 Sorts the data and returns estimates of underlying distribution
271 quantiles based on one or two order statistics from the supplied
272 elements.
273
274 This method use the same algorithm as Excel and R language
275 (quantile type 7).
276
277 The generic function quantile produces sample quantiles
278 corresponding to the given probabilities.
279
280 $Type is an integer value between 0 to 4 :
281
282 0 => zero quartile (Q0) : minimal value
283 1 => first quartile (Q1) : lower quartile = lowest cut off (25%) of data = 25th percentile
284 2 => second quartile (Q2) : median = it cuts data set in half = 50th percentile
285 3 => third quartile (Q3) : upper quartile = highest cut off (25%) of data, or lowest 75% = 75th percentile
286 4 => fourth quartile (Q4) : maximal value
287
288 Example :
289
290 my @data = (1..10);
291 my $stat = Statistics::Descriptive::Full->new();
292 $stat->add_data(@data);
293 print $stat->quantile(0); # => 1
294 print $stat->quantile(1); # => 3.25
295 print $stat->quantile(2); # => 5.5
296 print $stat->quantile(3); # => 7.75
297 print $stat->quantile(4); # => 10
298
299 $stat->median();
300 Sorts the data and returns the median value of the data.
301
302 $stat->harmonic_mean();
303 Returns the harmonic mean of the data. Since the mean is
304 undefined if any of the data are zero or if the sum of the
305 reciprocals is zero, it will return undef for both of those cases.
306
307 $stat->geometric_mean();
308 Returns the geometric mean of the data.
309
310 my $mode = $stat->mode();
311 Returns the mode of the data. The mode is the most commonly
312 occurring datum. See
313 <http://en.wikipedia.org/wiki/Mode_%28statistics%29> . If all
314 values occur only once, then mode() will return undef.
315
316 $stat->trimmed_mean(ltrim[,utrim]);
317 "trimmed_mean(ltrim)" returns the mean with a fraction "ltrim" of
318 entries at each end dropped. "trimmed_mean(ltrim,utrim)" returns
319 the mean after a fraction "ltrim" has been removed from the lower
320 end of the data and a fraction "utrim" has been removed from the
321 upper end of the data. This method sorts the data before
322 beginning to analyze it.
323
324 All calls to trimmed_mean() are cached so that they don't have to
325 be calculated a second time.
326
327 $stat->frequency_distribution_ref($partitions);
328 $stat->frequency_distribution_ref(\@bins);
329 $stat->frequency_distribution_ref();
330 "frequency_distribution_ref($partitions)" slices the data into
331 $partition sets (where $partition is greater than 1) and counts
332 the number of items that fall into each partition. It returns a
333 reference to a hash where the keys are the numerical values of the
334 partitions used. The minimum value of the data set is not a key
335 and the maximum value of the data set is always a key. The number
336 of entries for a particular partition key are the number of items
337 which are greater than the previous partition key and less then or
338 equal to the current partition key. As an example,
339
340 $stat->add_data(1,1.5,2,2.5,3,3.5,4);
341 $f = $stat->frequency_distribution_ref(2);
342 for (sort {$a <=> $b} keys %$f) {
343 print "key = $_, count = $f->{$_}\n";
344 }
345
346 prints
347
348 key = 2.5, count = 4
349 key = 4, count = 3
350
351 since there are four items less than or equal to 2.5, and 3 items
352 greater than 2.5 and less than 4.
353
354 "frequency_distribution_refs(\@bins)" provides the bins that are
355 to be used for the distribution. This allows for non-uniform
356 distributions as well as trimmed or sample distributions to be
357 found. @bins must be monotonic and contain at least one element.
358 Note that unless the set of bins contains the range that the total
359 counts returned will be less than the sample size.
360
361 Calling "frequency_distribution_ref()" with no arguments returns
362 the last distribution calculated, if such exists.
363
364 my %hash = $stat->frequency_distribution($partitions);
365 my %hash = $stat->frequency_distribution(\@bins);
366 my %hash = $stat->frequency_distribution();
367 Same as "frequency_distribution_ref()" except that returns the
368 hash clobbered into the return list. Kept for compatibility
369 reasons with previous versions of Statistics::Descriptive and
370 using it is discouraged.
371
372 $stat->median_absolute_deviation()
373 The median absolute deviation.
374
375 $stat->summary()
376 Returns a textual summary of the distribution - min, max, median,
377 mean and quantiles.
378
379 (New in version 3.0700 .)
380
381 $stat->least_squares_fit();
382 $stat->least_squares_fit(@x);
383 "least_squares_fit()" performs a least squares fit on the data,
384 assuming a domain of @x or a default of 1..$stat->count(). It
385 returns an array of four elements "($q, $m, $r, $rms)" where
386
387 "$q and $m"
388 satisfy the equation C($y = $m*$x + $q).
389
390 $r is the Pearson linear correlation cofficient.
391
392 $rms
393 is the root-mean-square error.
394
395 If case of error or division by zero, the empty list is returned.
396
397 The array that is returned can be "coerced" into a hash structure
398 by doing the following:
399
400 my %hash = ();
401 @hash{'q', 'm', 'r', 'err'} = $stat->least_squares_fit();
402
403 Because calling "least_squares_fit()" with no arguments defaults
404 to using the current range, there is no caching of the results.
405
407 I read my email frequently, but since adopting this module I've added 2
408 children and 1 dog to my family, so please be patient about my response
409 times. When reporting errors, please include the following to help me
410 out:
411
412 · Your version of perl. This can be obtained by typing perl "-v" at
413 the command line.
414
415 · Which version of Statistics::Descriptive you're using. As you can
416 see below, I do make mistakes. Unfortunately for me, right now
417 there are thousands of CD's with the version of this module with
418 the bugs in it. Fortunately for you, I'm a very patient module
419 maintainer.
420
421 · Details about what the error is. Try to narrow down the scope of
422 the problem and send me code that I can run to verify and track it
423 down.
424
426 Current maintainer:
427
428 Shlomi Fish, <http://www.shlomifish.org/> , "shlomif@cpan.org"
429
430 Previously:
431
432 Colin Kuskie
433
434 My email address can be found at http://www.perl.com under Who's Who or
435 at: https://metacpan.org/author/COLINK .
436
438 Fabio Ponciroli & Adzuna Ltd. team (outliers handling)
439
441 RFC2330, Framework for IP Performance Metrics
442
443 The Art of Computer Programming, Volume 2, Donald Knuth.
444
445 Handbook of Mathematica Functions, Milton Abramowitz and Irene Stegun.
446
447 Probability and Statistics for Engineering and the Sciences, Jay
448 Devore.
449
451 Copyright (c) 1997,1998 Colin Kuskie. All rights reserved. This
452 program is free software; you can redistribute it and/or modify it
453 under the same terms as Perl itself.
454
455 Copyright (c) 1998 Andrea Spinelli. All rights reserved. This program
456 is free software; you can redistribute it and/or modify it under the
457 same terms as Perl itself.
458
459 Copyright (c) 1994,1995 Jason Kastner. All rights reserved. This
460 program is free software; you can redistribute it and/or modify it
461 under the same terms as Perl itself.
462
464 This program is free software; you can redistribute it and/or modify it
465 under the same terms as Perl itself.
466
468 Shlomi Fish <shlomif@cpan.org>
469
471 This software is copyright (c) 1997 by Jason Kastner, Andrea Spinelli,
472 Colin Kuskie, and others.
473
474 This is free software; you can redistribute it and/or modify it under
475 the same terms as the Perl 5 programming language system itself.
476
478 Please report any bugs or feature requests on the bugtracker website
479 <https://github.com/shlomif/perl-Statistics-Descriptive/issues>
480
481 When submitting a bug or request, please include a test-file or a patch
482 to an existing test-file that illustrates the bug or desired feature.
483
485 Perldoc
486 You can find documentation for this module with the perldoc command.
487
488 perldoc Statistics::Descriptive::Full
489
490 Websites
491 The following websites have more information about this module, and may
492 be of help to you. As always, in addition to those websites please use
493 your favorite search engine to discover more resources.
494
495 · MetaCPAN
496
497 A modern, open-source CPAN search engine, useful to view POD in
498 HTML format.
499
500 <https://metacpan.org/release/Statistics-Descriptive>
501
502 · Search CPAN
503
504 The default CPAN search engine, useful to view POD in HTML format.
505
506 <http://search.cpan.org/dist/Statistics-Descriptive>
507
508 · RT: CPAN's Bug Tracker
509
510 The RT ( Request Tracker ) website is the default bug/issue
511 tracking system for CPAN.
512
513 <https://rt.cpan.org/Public/Dist/Display.html?Name=Statistics-Descriptive>
514
515 · AnnoCPAN
516
517 The AnnoCPAN is a website that allows community annotations of Perl
518 module documentation.
519
520 <http://annocpan.org/dist/Statistics-Descriptive>
521
522 · CPAN Ratings
523
524 The CPAN Ratings is a website that allows community ratings and
525 reviews of Perl modules.
526
527 <http://cpanratings.perl.org/d/Statistics-Descriptive>
528
529 · CPANTS
530
531 The CPANTS is a website that analyzes the Kwalitee ( code metrics )
532 of a distribution.
533
534 <http://cpants.cpanauthors.org/dist/Statistics-Descriptive>
535
536 · CPAN Testers
537
538 The CPAN Testers is a network of smoke testers who run automated
539 tests on uploaded CPAN distributions.
540
541 <http://www.cpantesters.org/distro/S/Statistics-Descriptive>
542
543 · CPAN Testers Matrix
544
545 The CPAN Testers Matrix is a website that provides a visual
546 overview of the test results for a distribution on various
547 Perls/platforms.
548
549 <http://matrix.cpantesters.org/?dist=Statistics-Descriptive>
550
551 · CPAN Testers Dependencies
552
553 The CPAN Testers Dependencies is a website that shows a chart of
554 the test results of all dependencies for a distribution.
555
556 <http://deps.cpantesters.org/?module=Statistics::Descriptive>
557
558 Bugs / Feature Requests
559 Please report any bugs or feature requests by email to
560 "bug-statistics-descriptive at rt.cpan.org", or through the web
561 interface at
562 <https://rt.cpan.org/Public/Bug/Report.html?Queue=Statistics-Descriptive>.
563 You will be automatically notified of any progress on the request by
564 the system.
565
566 Source Code
567 The code is open to the world, and available for you to hack on. Please
568 feel free to browse it and play with it, or whatever. If you want to
569 contribute patches, please send me a diff or prod me to pull from your
570 repository :)
571
572 <https://github.com/shlomif/perl-Statistics-Descriptive>
573
574 git clone git://github.com/shlomif/perl-Statistics-Descriptive.git
575
576
577
578perl v5.28.0 2018-07-16 Statistics::Descriptive::Full(3)