++ed by:

7 PAUSE users
3 non-PAUSE users.

Colin Kuskie

# NAME

Statistics::Descriptive - Module of basic descriptive statistical functions.

# SYNOPSIS

``````  use Statistics::Descriptive;
\$stat = Statistics::Descriptive::Full->new();
\$var  = \$stat->variance();
\$tm   = \$stat->trimmed_mean(.25);``````

# DESCRIPTION

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.

# METHODS

## Sparse Methods

\$stat = Statistics::Descriptive::Sparse->new();

Create a new sparse statistics object.

Adds data to the statistics variable. The cached statistical values are updated automatically.

\$stat->count();

Returns the number of data items.

\$stat->mean();

Returns the mean of the data.

\$stat->sum();

Returns the sum of the data.

\$stat->variance();

Returns the variance of the data. Division by n-1 is used.

\$stat->standard_deviation();

Returns the standard deviation of the data. Division by n-1 is used.

\$stat->min();

Returns the minimum value of the data set.

\$stat->mindex();

Returns the index of the minimum value of the data set.

\$stat->max();

Returns the maximum value of the data set.

\$stat->maxdex();

Returns the index of the maximum value of the data set.

\$stat->sample_range();

Returns the sample range (max - min) of the data set.

## Full Methods

\$stat = Statistics::Descriptive::Full->new();

Create a new statistics object that inherits from Statistics::Descriptive::Sparse so that it contains all the methods described above.

Adds data to the statistics variable. All of the sparse statistical values are updated and cached. Cached values from full methods are deleted since they are no longer valid.

Note: Calling add_data with an empty array will delete all of your cached values!

\$stat->get_data();

Returns a copy of the data array.

\$stat->sort_data();

Sort the stored data and update the mindex and maxdex methods. This method uses perl's internal sort.

\$stat->presorted(1);

If called with a non-zero argument, this method sets a flag that says the data is already sorted and need not be sorted again. Since some of the methods in this class require sorted data, this saves some time. If you supply sorted data to the object, call this method to prevent the data from being sorted again. The flag is cleared whenever add_data is called. Calling the method without an argument returns the value of the flag.

\$stat->median();

Sorts the data and returns the median value of the data.

\$stat->harmonic_mean();

Returns the harmonic mean of the data.

\$stat->geometric_mean();

Returns the geometric mean of the data.

\$stat->mode();

Returns the mode of the data.

\$stat->trimmed_mean(ltrim[,utrim]);

`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.

\$stat->frequency_distribution();

`frequency_distribution(partitions)` slices the data into `partition` sets 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.

\$stat->least_squares_fit();
\$stat->least_squares_fit(@x);

`least_squares_fit()` performs a least squares fit on the data, assuming a domain of 1,2,3... It returns an array of two elements; the value in the zeroth position is the constant (x^0) term and the value in the first position is the coeffiecient of the x^1 term. `least_squares_fit(@x)` uses the values in `@x` as the domain.

# REPORTING ERRORS

When reporting errors, please include the following to help me out:

• Your version of perl. This can be obtained by typing perl `-v` at the command line.

• Which version of Statistics::Descriptive you're using. As you can see below, I do make mistakes. Unfortunately for me, right now there are thousands of CD's with the version of this module with the bugs in it. Fortunately for you, I'm a very patient module maintainer.

• Details about what the error is. Try to narrow down the scope of the problem and send me code that I can run to verify and track it down.

# REFERENCES

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 <colink@latticesemi.com>. 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 <jason@wagner.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

# REVISION HISTORY

v2.2

Rolled into March 1998.

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.

v2.1

August 1997

Fixed errors in statistics algorithms caused by changing the interface.

v2.0

August 1997

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.

v1.1

April 1995

v1.0

March 1995

Released to comp.lang.perl and placed on archive sites.

v.20

December 1994

Complete rewrite after extensive and invaluable e-mail correspondence with Anno Siegel.

v.10

December 1994

Initital concept, released to perl5-porters list.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 551:

'=item' outside of any '=over'

=over without closing =back