#!/usr/bin/perl use strict; use warnings; my $num = @ARGV; my @x = @ARGV[0..$num/2-1]; # 期待値・理論値 my @y = @ARGV[$num/2..$#ARGV]; # 実測値 my $val = chisq(\@x, \@y); print "$val\n"; sub chisq { my ($x_ref, $y_ref) = @_; my $rv = 0; for (my $i = 0; $i < @$x_ref; $i++) { $rv += ($y_ref->[$i]-$x_ref->[$i])**2/$x_ref->[$i]; } return $rv; }
% chisq.pl 50 50 45 55 1 % chisq.pl 50 50 47 53 0.36 % chisq.pl 40 20 10 30 37 25 12 26 2.40833333333333
#!/usr/bin/perl use strict; use warnings; my @nums = @ARGV; for (my $x_max = 2; $x_max < @nums; $x_max++) { next if @nums % $x_max; my $y_max = @nums / $x_max; my ($u2, $m_ref) = chisqin(\@nums, $x_max, $y_max); print "table = $x_max x $y_max, chisquare = $u2\n"; for (my $j = 0; $j <= $y_max; $j++) { for (my $i = 0; $i <= $x_max; $i++) { print $m_ref->[$i][$j].(($i==$x_max or $j==$y_max)?"*":"")."\t"; } print "\n"; } print "\n"; } sub chisqin { my ($m_ref, $x_max, $y_max) = @_; my @mat; for (my $j = 0; $j < $y_max; $j++) { for (my $i = 0; $i < $x_max; $i++) { $mat[$i][$j] = $m_ref->[$x_max * $j + $i]; $mat[$x_max][$j] += $mat[$i][$j]; # x_sum $mat[$i][$y_max] += $mat[$i][$j]; # y_sum $mat[$x_max][$y_max] += $mat[$i][$j]; # all_sum } } my $u2; for (my $j = 0; $j < $y_max; $j++) { for (my $i = 0; $i < $x_max; $i++) { my $z = $mat[$x_max][$j] * $mat[$i][$y_max] / $mat[$x_max][$y_max]; $u2 += ($mat[$i][$j]-$z)**2/$z; } } return $u2, \@mat; }
% ./chisqin.pl 1 99 5 195 table = 2 x 2, chisquare = 0.76530612244898 1 99 100* 5 195 200* 6* 294* 300* % ./chisqin.pl 50 50 47 53 table = 2 x 2, chisquare = 0.180162145931338 50 50 100* 47 53 100* 97* 103* 200* % ./chisqin.pl 4 2 3 8 4 6 6 3 6 table = 3 x 3, chisquare = 0.186666666666667 4 2 3 9* 8 4 6 18* 6 3 6 15* 18* 9* 15* 42* % ./chisqin.pl 4 2 3 8 4 6 6 3 6 1 1 1 table = 2 x 6, chisquare = 7.74524582560296 4 2 6* 3 8 11* 4 6 10* 6 3 9* 6 1 7* 1 1 2* 24* 21* 45* table = 3 x 4, chisquare = 0.430263157894737 4 2 3 9* 8 4 6 18* 6 3 6 15* 1 1 1 3* 19* 10* 16* 45* table = 4 x 3, chisquare = 12.5141284584009 4 2 3 8 17* 4 6 6 3 19* 6 1 1 1 9* 14* 9* 10* 12* 45* table = 6 x 2, chisquare = 11.0582010582011 4 2 3 8 4 6 27* 6 3 6 1 1 1 18* 10* 5* 9* 9* 5* 7* 45*
#!/usr/bin/perl use strict; use warnings; my ($o11, $o12, $o21, $o22) = @ARGV; # my ($n1, $o11, $n2, $o21) = @ARGV; # my $o12 = $n1 - $o11; # my $o22 = $n2 - $o21; my $val = chisqi($o11, $o12, $o21, $o22); print "$val\n"; sub chisqi { my ($o11, $o12, $o21, $o22) = @_; return ($o11 + $o12 + $o21 + $o22)*(($o11*$o22-$o12*$o21)**2)/ (($o11+$o12)*($o11+$o21)*($o12+$o22)*($o21+$o22)); }
% ./chisqi.pl 1 99 5 195 0.76530612244898 % ./chisqi.pl 50 50 47 53 0.180162145931338 % ./chisqi.pl 50 50 55 45 0.50125313283208
上側確率 | 0.050 | 0.025 | 0.010 | 0.005 |
χ2 | 3.84146 | 5.02389 | 6.63490 | 7.87944 |