Statistics-Test-Sequence

 view release on metacpan or  search on metacpan

lib/Statistics/Test/Sequence.pm  view on Meta::CPAN

  use Data::Dumper;
  print "$metric\n";
  print "Frequencies:\n";
  print Dumper $actual_freq;
  print "Expected frequencies:\n";
  print Dumper $expected_freq;

=head1 DESCRIPTION

This module implements a sequence correlation test for random number
generators. It shows pairwise correlation between subsequent
random numbers.

The algorithm is as follows: (Following Blobel. Citation in SEE ALSO section.)

=over 2

=item *

Given C<N+1> random numbers C<u_j>.

=item *

For all C<j>, compare C<u_j> with C<u_j+1>. If C<u_j> is greater
then C<u_j+1>, assign a 0-Bit to the number. Otherwise, assign a
1-Bit.

=item *

Find all sequences of equal Bits. For every sequence, increment
a counter for the length C<k> of that sequence. (Regardless of whether it's
a sequence of 1's or 0's.)

=item *

For uncorrelated random numbers, the number of sequences C<N(k)>
of length C<k> in the set of C<N+1> random numbers is expected to be:

  N(k) = 2*((k^2+3*k+1)*N - (k^3+3*k^2-k-4)) / (k+3)!

=back

=head1 METHODS

=cut

=head2 new

Creates a new random number tester.

=cut

sub new {
    my $proto = shift;
    my $class = ref($proto)||$proto;

    my $self = {
        data => undef,
    };

    bless $self => $class;

    return $self;
}

=head2 set_data

Sets the random numbers to operate on. First argument
must be either an array reference to an array of random
numbers or a code reference.

If the first argument is a code reference, the second
argument must be an integer C<n>. The code reference is
called C<n>-times and its return values are used as
random numbers.

The code reference semantics are particularily useful if
you do not want to store all random numbers in memory at
the same time. You can write a subroutine that, for example,
generates and returns batches of 100 random numbers so no
more than 101 of these numbers will be in memory at the
same time. Note that if you return 100 numbers at once and
pass in C<n=50>, you will have a sequence of 5000 random
numbers.

=cut

sub set_data {
    my $self = shift;
    my $data = shift;
    if (_ARRAY($data)) {
        $self->{data} = $data;
        return 1;
    }
    elsif (_CODE($data)) {
        $self->{data} = $data;
        my $n = shift;
        if (not _POSINT($n)) {
            croak("'set_data' needs an integer as second argument if the first argument is a code reference.");
        }
        $self->{n} = $n;
        return 1;
    }
    else {
        croak("Invalid arguments to 'set_data'.");
    }
}

=head2 test

Runs the sequence test on the data that was previously set using
C<set_data>.

Returns three items: The first is the root mean square of the bin
residuals divided by the number of random numbers. It I<could> be
used as a measure for the quality of the random number generator
and should be as close to zero as possible. A better metric is to
compare the following two return values.

The second return value is a reference to the array of frequencies.
An example is in order here. Generating one million random numbers,

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.385 second using v1.00-cache-2.02-grep-82fe00e-cpan-9f2165ba459b )