#!/usr/local/bin/perl -w # nsum.p reads an nsum file # which is a list of counts nsum[1]..nsum[N] of how often # each noise bit was present in a nasty noise vector. # it writes out the histogram # # r freq[r] expectedfreq[r] sd # also works out chi2 making use of the 5-in-a-bin rule (Brian Ripley) # # example usage in code/nFGC # nsum.p 10002.3.333e.814.0.81.0.81.1.030 # will automatically write to > 10002.3.333e.814.0.81.0.81.1.030.g # and 10002.3.333e.814.0.81.0.81.1.030.X # the latter being the chi2 test $verbose = 0 ; # eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $file = $ARGV[0]; print "$file \n" ; $_ = <>; @a=split; print "# ", $a[1] , " blocks " , $a[3] , " noise bits, N = " , $a[5] , " " , $a[6] , " " , $a[7] , " " , $a[8] ," " , $a[9] ," " , $a[10] , "\n" ; $B = $a[1] ; $b = $a[3] ; $N = $a[5] ; $p = $b / ( $B * $N ) ; $choose = 1 ; $tot = 0.0 ; for ( $r = 0 ; $r <= $B ; $r ++ ) { $pr[$r] = $choose * $p ** $r * (1.0-$p) ** ( $B - $r ) ; $tot += $pr[$r] ; $choose *= ( $B - $r ) / ($r+1.0) ; $c[$r] = 0 ; } if ( $verbose || $tot < 0.99 || $tot > 1.01 ) { print STDERR "check this is 1: $tot\n" ; } $n = 0 ; while ( <> ) { $n ++ ; ($r,$rest) = split ; $c[$r] ++ ; } $fileg = $file.".g" ; open ( FI , "> $fileg" ) ; $chi = 0.0 ; $cumc = 0 ; $cump = 0.0 ; # old stuff $bin = 0 ; $clumpp[$bin] = 0.0 ; $clumpc[$bin] = 0.0 ; $rfrom[$bin] = 0; # new stuff print FI "#r c[r] E-c[r] sd diff zdiff cum-count cum-prob diff(cum) diff(c-E)\n" ; # while writing the r bins, this loop also does accumulation bins for # another chi2 test. Using the expected counts only of course. for ( $r = 0 ; $r <= $B ; $r ++ ) { # # computations for the raw chi2 test # $sd = sqrt ( (1.0-$pr[$r]) * $pr[$r] * $N ) ; $diff = $c[$r] - $pr[$r] * $N ; $zdiff = $diff / $sd ; $cumc += $c[$r] ; $cump += $pr[$r] * $N ; printf FI "%2d %4d %11.5g %11.5g %11.5g %11.5g %8d %11.5g %11.5g\n" , $r , $c[$r] , $pr[$r] * $N , $sd , $diff , $zdiff , $cumc , $cump , $cumc - $cump ; $chi += $zdiff**2 ; # # clumping into bigger bins # $clumpp[$bin] += $pr[$r] * $N ; $clumpc[$bin] += $c[$r] ; $rto[$bin] = $r ; if ( $clumpp[$bin] > 5.0 ) { $bin ++ ; $clumpp[$bin] = 0.0 ; $clumpc[$bin] = 0.0 ; $rfrom[$bin] = $r+1 ; } } printf "# Raw Chi-squared = %8.5g nu = %d\n" , $chi , $B ; printf FI "# Raw Chi-squared = %8.5g nu = %d\n" , $chi , $B ; print STDOUT "# gnuplot\n" ; printf STDOUT "set xrange [-0.5:%f] ; set size 0.5,0.5; set noxzeroaxis ; set noyzeroaxis ; plot '%s' u 1:2 t 'data' w box, '%s' u 1:3:4 t 'null hypothesis' w error\n",($B+0.5) ,$fileg ,$fileg ; # # now do binned version # # first sort out last bin. # how many bins? maybe we just started an empty one. if ( $rfrom[$bin] > $B ) { $bin -- ; $nu = $bin ; } else { # the last bin must have been used. if ( $clumpp[$bin] < 5.0 ) { # combine with previous bin. $bin -- ; $clumpp[$bin] += $clumpp[$bin+1] ; $clumpc[$bin] += $clumpc[$bin+1] ; $rto[$bin] = $rto[$bin+1] ; $nu = $bin ; } else { # all should be well $nu = $bin ; } } $fileg = $file.".X" ; open ( FI , "> $fileg" ) ; # print STDERR "# Binned Chi2 test: nu = $nu (no. of bins is 1 greater)\n" ; $chi = 0.0 ; $cumc = 0 ; $cump = 0.0 ; # old stuff print FI "#r' c[r] E-c[r] sd diff zdiff cum-count cum-prob diff r-from r-to\n" ; # while writing the r bins, this loop also does accumulation bins for # another chi2 test. Using the expected counts only of course. for ( $r = 0 ; $r <= $nu ; $r ++ ) { # # computations for the raw chi2 test # $sd = sqrt ( (1.0-$clumpp[$r]/$N) * $clumpp[$r] ) ; $diff = $clumpc[$r] - $clumpp[$r] ; $zdiff = $diff / $sd ; $cumc += $clumpc[$r] ; $cump += $clumpp[$r] ; printf FI "%2d %4d %8.5g %8.5g %10.5g %10.5g %8d %10.5g %10.5g %2d %2d\n" , $r , $clumpc[$r] , $clumpp[$r] , $sd , $diff , $zdiff , $cumc , $cump , $cumc - $cump , $rfrom[$r] , $rto[$r] ; $chi += $zdiff**2 ; } printf "# binned Chi-squared = %8.5g nu = %d\n" , $chi , $nu ; printf FI "# binned Chi-squared = %8.5g nu = %d\n" , $chi , $nu ; printf STDOUT "pause -1 'press return'\n" ; printf STDOUT "set xrange [-0.5:%f] ; set size 0.5,0.5; set noxzeroaxis ; set noyzeroaxis ; plot '%s' u 1:2 t 'data' w box, '%s' u 1:3:4 t 'null hypothesis' w error\n",($nu+0.5) ,$fileg ,$fileg ;