=head1 NAME
Statistics::Basic - A collection of very basic statistics modules
=head1 SYNOPSIS
use Statistics::Basic qw(:all);
These actually return objects, not numbers. The objects will interpolate as
nicely formated numbers (using L). Or the actual number will be
returned when the object is used as a number.
my $median = median( 1,2,3 );
my $mean = mean( [1,2,3]); # array refs are ok too
my $variance = variance( 1,2,3 );
my $stddev = stddev( 1,2,3 );
Although passing unblessed numbers and array refs to these functions works, it's
sometimes better to pass vector objects so the objects can reuse calculated
values.
my $v1 = $mean->query_vector;
my $variance = variance( $v1 );
my $stddev = stddev( $v1 );
Here, the mean used by the variance and the variance used by the standard
deviation will not need to be recalculated. Now consider these two
calculations.
my $covariance = covariance( [1 .. 3], [1 .. 3] );
my $correlation = correlation( [1 .. 3], [1 .. 3] );
The covariance above would need to be recalculated by the correlation when these
functions are called this way. But, if we instead built vectors first, that
wouldn't happen:
# $v1 is defined above
my $v2 = vector(1,2,3);
my $cov = covariance( $v1, $v2 );
my $cor = correlation( $v1, $v2 );
Now C<$cor> can reuse the variance calculated in C<$cov>.
All of the functions above return objects that interpolate or evaluate as a
single string or as a number. L and
L are different:
my $unimodal = mode(1,2,3,3);
my $multimodal = mode(1,2,3);
print "The modes are: $unimodal and $multimodal.\n";
print "The first is multimodal... " if $unimodal->is_multimodal;
print "The second is multimodal.\n" if $multimodal->is_multimodal;
In the first case, C<$unimodal> will interpolate as a string B function
correctly as a number. However, in the second case, trying to use
C<$multimodal> as a number will C an error -- it still interpolates fine
though.
my $lsf = leastsquarefit($v1, $v2);
This C<$lsf> will interpolate fine, showing C, but it will C if you try to use the object as a number.
my $v3 = $multimodal->query;
my ($alpha, $beta) = $lsf->query;
my $average = $mean->query;
All of the objects allow you to explicitly query, if you're not in the mood to
use L.
my @answers = (
$mode->query,
$median->query,
$stddev->query,
);
=head1 SHORTCUTS
The following shortcut functions can be used in place of calling the module's
C method directly.
They all take either array refs B lists as arguments, with the exception of
the shortcuts that need two vectors to process (e.g.
L).
=over
=item B
Returns a L object.
Arguments to C can be any of: an array ref, a list of numbers, or a
blessed vector object. If passed a blessed vector object, vector will just
return the vector passed in.
=item B B B
Returns a L object.
You can choose to call C as C or C. Arguments can be
any of: an array ref, a list of numbers, or a blessed vector object.
=item B
Returns a L object.
Arguments can be any of: an array ref, a list of numbers, or a blessed vector
object.
=item B
Returns a L object.
Arguments can be any of: an array ref, a list of numbers, or a blessed vector
object.
=item B B`
Returns a L object.
You can choose to call C as C``. Arguments can be any of: an
array ref, a list of numbers, or a blessed vector object. If you will also be
calculating the mean of the same list of numbers it's recommended to do this:
my $vec = vector(1,2,3);
my $mean = mean($vec);
my $var = variance($vec);
This would also work:
my $mean = mean(1,2,3);
my $var = variance($mean->query_vector);
This will calculate the same mean twice:
my $mean = mean(1,2,3);
my $var = variance(1,2,3);
If you really only need the variance, ignore the above and this is fine:
my $variance = variance(1,2,3,4,5);
=item B
Returns a L object.
Arguments can be any of: an array ref, a list of numbers, or a blessed vector
object. Pass a vector object to C to avoid recalculating the variance
and mean if applicable (see C).
=item B B
Returns a L object.
Arguments to C or C must be array ref or vector objects.
There must be precisely two arguments (or none, setting the vectors to two empty
ones), and they must be the same length.
=item B B B
Returns a L object.
Arguments to C or C/C must be array ref or vector
objects. There must be precisely two arguments (or none, setting the vectors to
two empty ones), and they must be the same length.
=item B B B
Returns a L object.
Arguments to C or C/C must be array ref or
vector objects. There must be precisely two arguments (or none, setting the
vectors to two empty ones), and they must be the same length.
=item B
Returns a L object.
Argument must be a blessed vector object. See the section on
L for more information on this.
=item B B
Returns two L objects.
Arguments to this function should be two vector arguments. See the section on
L for further information on this function.
=back
=head1 COMPUTED VECTORS
Sometimes it will be handy to have a vector computed from another (or at least
that updates based on the first). Consider the case of outliers:
my @a = ( (1,2,3) x 7, 15 );
my @b = ( (1,2,3) x 7 );
my $v1 = vector(@a);
my $v2 = vector(@b);
my $v3 = computed($v1);
$v3->set_filter(sub {
my $m = mean($v1);
my $s = stddev($v1);
grep { abs($_-$m) <= $s } @_;
});
This filter sets C<$v3> to always be equal to C<$v1> such that all the elements
that differ from the mean by more than a standard deviation are removed. As
such, C<"$v2" eq "$v3"> since C<15> is clearly an outlier by inspection.
print "$v1\n";
print "$v3\n";
... prints:
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 15]
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
=head1 MISSING VALUES
Something I get asked about quite a lot is, "can S::B handle missing values?"
The answer used to be, "that really depends on your data set, use
L," but I recently decided (5/29/09) that it was time to
just go ahead and add this feature.
Strictly speaking, the feature was already there. You simply need to add a
couple filters to your data. See C for the test
example.
This is what people usually mean when they ask if S::B can "handle" missing
data:
my $v1 = vector(1,2,3,undef,4);
my $v2 = vector(1,2,3,4, undef);
my $v3 = computed($v1);
my $v4 = computed($v2);
$v3->set_filter(sub {
my @v = $v2->query;
map {$_[$_]} grep { defined $v[$_] and defined $_[$_] } 0 .. $#_;
});
$v4->set_filter(sub {
my @v = $v1->query;
map {$_[$_]} grep { defined $v[$_] and defined $_[$_] } 0 .. $#_;
});
print "$v1 $v2\n"; # prints: [1, 2, 3, _, 4] [1, 2, 3, 4, _]
print "$v3 $v4\n"; # prints: [1, 2, 3] [1, 2, 3]
But I've made it even simpler. Since this is such a common request, I have
provided a helper function to build the filters automatically:
my $v1 = vector(1,2,3,undef,4);
my $v2 = vector(1,2,3,4, undef);
my ($f1, $f2) = handle_missing_values($v1, $v2);
print "$f1 $f2\n"; # prints: [1, 2, 3] [1, 2, 3]
Note that in practice, you would still manipulate (insert, and shift) C<$v1> and
C<$v2>, I the computed vectors. But for correlations and the like, you
would use C<$f1> and C<$f2>.
$v1->insert(5);
$v2->insert(6);
my $correlation = correlation($f1, $f2);
You can still insert on C<$f1> and C<$f2>, but it updates the input vector
rather than the computed one (which is just a filter handler).
=head1 REUSE DETAILS
Most of the objects have a variety of query functions that allow you to extract
the objects used within. Although, the objects are smart enough to prevent
needless duplication. That is, the following would test would pass:
use Statistics::Basic qw(:all);
my $v1 = vector(1,2,3,4,5);
my $v2 = vector($v1);
my $sd = stddev( $v1 );
my $v3 = $sd->query_vector;
my $m1 = mean( $v1 );
my $m2 = $sd->query_mean;
my $m3 = Statistics::Basic::Mean->new( $v1 );
my $v4 = $m3->query_vector;
use Scalar::Util qw(refaddr);
use Test; plan tests => 5;
ok( refaddr($v1), refaddr($v2) );
ok( refaddr($v2), refaddr($v3) );
ok( refaddr($m1), refaddr($m2) );
ok( refaddr($m2), refaddr($m3) );
ok( refaddr($v3), refaddr($v4) );
# this is t/54_* in the distribution
Also, note that the mean is only calculated once even though we've calculated a
variance and a standard deviation above.
Suppose you'd like a copy of the L object that the
L object is using. All of the objects within should
be accessible with query functions as follows.
=head1 QUERY FUNCTIONS
=over
=item B
This method exists in all of the objects. L
is the only one that returns two values (alpha and beta) as a list.
L returns either the list of elements in the vector,
or reference to that array (depending on the context). All of the other
C methods return a single number, the number the module purports to
calculate.
=item B
Returns the L object used by
L and L.
=item B
Returns the first L object used by
L, L and
L.
=item B
Returns the second L object used by
L, and L.
=item B
Returns the L object used by
L and L.
=item B
Returns the L object used by
L.
=item B
Returns the first L object used by
L.
=item B
Returns the L object used by any of the single vector
modules.
=item B
Returns the first L object used by any of the two
vector modules.
=item B
Returns the second L object used by any of the two
vector modules.
=item B
L objects sometimes return L
objects instead of numbers. When C is true, the mode is a
vector, not a scalar.
=item B
L is meant for finding a line of best fit.
This function can be used to find the C for a given C based on the
calculated C<$beta> (slope) and C<$alpha> (y-offset).
=item B
L is meant for finding a line of best fit.
This function can be used to find the C for a given C based on the
calculated C<$beta> (slope) and C<$alpha> (y-offset).
This function can produce divide-by-zero errors since it must divide by the
slope to find the C value. (The slope should rarely be zero though, that's a
vertical line and would represent very odd data points.)
=back
=head1 INSERT and SET FUNCTIONS
These objects are all intended to be useful while processing long columns of
data, like data you'd find in a database.
=over
=item B
Vectors try to stay the same size when they accept new elements, FIFO style.
my $v1 = vector(1,2,3); # a 3 touple
$v1->insert(4); # still a 3 touple
print "$v1\n"; # prints: [2, 3, 4]
$v1->insert(7); # still a 3 touple
print "$v1\n"; # prints: [3, 4, 7]
All of the other L modules have this function too. The
modules that track two vectors will need two arguments to insert though.
my $mean = mean([1,2,3]);
$mean->insert(4);
print "mean: $mean\n"; # prints 3 ... (2+3+4)/3
my $correlation = correlation($mean->query_vector,
$mean->query_vector->copy);
print "correlation: $correlation\n"; # 1
$correlation->insert(3,4);
print "correlation: $correlation\n"; # 0.5
Also, note that the underlying vectors keep track of recalculating
automatically.
my $v = vector(1,2,3);
my $m = mean($v);
my $s = stddev($v);
The mean has not been calculated yet.
print "$s; $m\n"; # 0.82; 2
The mean has been calculated once (even though the L
uses it).
$v->insert(4); print "$s; $m\n"; 0.82; 3
$m->insert(5); print "$s; $m\n"; 0.82; 4
$s->insert(6); print "$s; $m\n"; 0.82; 5
The mean has been calculated thrice more and only thrice more.
=item B B
You can grow the vectors instead of sliding them (FIFO). For this, use
C (or C, same thing).
my $v = vector(1,2,3);
my $m = mean($v);
my $s = stddev($v);
$v->append(4); print "$s; $m\n"; 1.12; 2.5
$m->append(5); print "$s; $m\n"; 1.41; 3
$s->append(6); print "$s; $m\n"; 1.71; 1.71
print "$v\n"; # [1, 2, 3, 4, 5, 6]
print "$s\n"; # 1.71
Of course, with a correlation, or a covariance, it'd look more like this:
my $c = correlation([1,2,3], [3,4,5]);
$c->append(7,7);
print "c=$c\n"; # c=0.98
=item B
This allows you to set the vector to a known state. It takes either array ref
or vector objects.
my $v1 = vector(1,2,3);
my $v2 = $v1->copy;
$v2->set_vector([4,5,6]);
my $m = mean();
$m->set_vector([1,2,3]);
$m->set_vector($v2);
my $c = correlation();
$c->set_vector($v1,$v2);
$c->set_vector([1,2,3], [4,5,6]);
=item B
This sets the size of the vector. When the vector is made bigger, the vector is
filled to the new length with leading zeros (i.e., they are the first to be
kicked out after new Cs.
my $v = vector(1,2,3);
$v->set_size(7);
print "$v\n"; # [0, 0, 0, 0, 1, 2, 3]
my $m = mean();
$m->set_size(7);
print "", $m->query_vector, "\n";
# [0, 0, 0, 0, 0, 0, 0]
my $c = correlation([3],[3]);
$c->set_size(7);
print "", $c->query_vector1, "\n";
print "", $c->query_vector2, "\n";
# [0, 0, 0, 0, 0, 0, 3]
# [0, 0, 0, 0, 0, 0, 3]
=back
=head1 OPTIONS
Each of the following options can be specified on package import like this.
use Statistics::Basic qw(unbias=0); # start with unbias disabled
use Statistics::Basic qw(unbias=1); # start with unbias enabled
When specified on import, each option has certain defaults.
use Statistics::Basic qw(unbias); # start with unbias enabled
use Statistics::Basic qw(nofill); # start with nofill enabled
use Statistics::Basic qw(toler); # start with toler disabled
use Statistics::Basic qw(ipres); # start with ipres=2
Additionally, with the exception of L, they can all be accessed via
package variables of the same name in all upper case. Example:
# code code code
$Statistics::Basic::UNBIAS = 0; # turn UNBIAS off
# code code code
$Statistics::Basic::UNBIAS = 1; # turn it back on
# code code code
{
local $Statistics::Basic::DEBUG_STATS_B = 1; # debug, this block only
}
Special caveat: L can in fact be changed via the package var (e.g.,
C<$Statistics::Basic::TOLER=0.0001>). But, for speed reasons, it must be
L before any other packages are imported or it will
not actually do anything when changed.
=over 4
=item B
This module uses the B definition of variance.
If you wish to use the I, B definition, then set
the C<$Statistics::Basic::UNBIAS> true (possibly with
C.
=item B<$ENV{NOFILL}>
Sets the default value of L.
=item B<$ENV{IPRES}>
Sets the default value of L.
=item B<$ENV{TOLER}>
Sets the default value of L.
=back
=head1 OVERLOADS
All of the objects are true in numeric context. All of the objects print useful
strings when evaluated as a string. Most of the objects evaluate usefully as
numbers, although L objects,
L objects, and
L objects do not -- they instead raise an
error.
=head1 Author's note on Statistics::Descriptive
I've been asked a couple times now why I don't link to
L in my see also section. As a rule, I only link to
packages there that I think are related or that I actually used in the package
construction. I've never personally used Descriptive, but it surely seems to do
quite a lot more. In a sense, this package really doesn't do statistics, not
like a scientist would think about it anyway. So I always figured people could
find their own way to Descriptive anyway.
The one thing this package does do, that I don't think Descriptive does (correct
me if I'm wrong) is time difference computations. If there are say, 200 things
in the mean object, then after inserting (using this package) there'll still be
200 things, allowing the computation of a moving average, moving stddev, moving
correlation, etc. You might argue that this is rarely needed, but it is really
the only time I need to compute these things.
while( $data = $fetch_sth->fetchrow_arrayref ) {
$mean->insert($data);
$moving_avg_sth->execute(0 + $mean);
}
Since I opened the topic I'd also like to mention that I find this package
easier to use. That is a matter of taste and since I wrote this, you might say
I'm a little biased. Your mileage may vary.
=head1 AUTHOR
Paul Miller C<< >>
I am using this software in my own projects... If you find bugs, please
please please let me know. :) Actually, let me know if you find it handy at
all. Half the fun of releasing this stuff is knowing that people use it.
=head1 COPYRIGHT
Copyright 2012 Paul Miller -- Licensed under the LGPL version 2.
=head1 SEE ALSO
perl(1), L, L,
L,
L,
L,
L,
L,
L,
L,
L,
L,
L,
L,
L
=cut
`