sub chisquaredVal {
my @data = @_;
@data = @{$data[0]} if(@data == 1 and ref($data[0]));
return "There's no data!" if(!@data);
return "Not enough data!" if(@data == 1);
# return "Malformed data!" if(grep { /\D/ } @data);
my $degrees_of_freedom = scalar(@data) - 1;
my ($chisquare, $num_samples, $expected, $i) = (0, 0, 0, 0);
if (! ref($chitable[$degrees_of_freedom])) {
return "I can't handle ".scalar(@data)." choices without a better table.";
}
foreach (@data) { $num_samples += $_ }
$expected = $num_samples / scalar(@data);
# return "There's no data!" unless $expected;
foreach (@data) {
$chisquare += (($_ - $expected) ** 2) / $expected;
}
return $chisquare;
}
####
#! perl -slw
use strict;
use Statistics::ChiSquare qw[ chisquare chisquaredVal ];
use Math::Random::MT;
use Data::Dump qw[ pp ];
my $mt = Math::Random::MT->new();
our $N //= 1e6;
our $ASIZE //= 4;
our $T //= 4;
sub shuffle {
$a = $_ + $mt->rand( @_ - $_ ),
$b = $_[$_],
$_[$_] = $_[$a],
$_[$a] = $b
for 0 .. $#_;
return @_;
}
my @data = ( 1 .. $ASIZE );
my @chi;
for( 1 .. $T ) {
my %tests;
++$tests{ join '', shuffle( @data ) } for 1 .. $N;
push @chi, chisquaredVal( values %tests );
}
####
print chisquare( @chi );
__END__
C:\test>chiSquareChiSquare -ASIZE=7 -N=2e2 -T=1e2
There's a >99.5% chance, and a <100% chance, that this data is random.
C:\test>chiSquareChiSquare -ASIZE=7 -N=2e2 -T=1e2
There's a >90% chance, and a <95% chance, that this data is random.
C:\test>chiSquareChiSquare -ASIZE=7 -N=2e2 -T=1e2
There's a >99.5% chance, and a <100% chance, that this data is random.