#!/usr/local/bin/perl $stats="5366 1219 2602 2718 8377 1785 1280 3058 5903 70 800 3431 2319 5470 6526 1896 539 4660 5453 6767 3108 652 1388 765 1564 78 18104 "; $includespace=0; $verbose=0; $N=16; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $_ = $stats; @s=split ; $I = $#s ; $I++ if ($includespace); print "# alphabet of size I = $I\n" ; $tot=0; for ($i=0;$i<$I;$i++ ) { $tot += $s[$i] ; } for ($i=0;$i<$I;$i++ ) { $p[$i] = $s[$i]/$tot ; $l[$i] = -log($p[$i])/log(2.0) ; printf STDERR "%3d %8.4g %8.4g\n" , $i , $p[$i] , $l[$i] ; } # find DKL # i=1 interprets every b incorrectlu as an a # for ($i=0;$i<$I;$i++ ) { print "D_KL between i=0 (true) and i=$i (hypothesis): " if($verbose); $gap = 0.0 ; $Sgap = 0.0; $D = 0.0; $S=0.0; $iii = $i ; for ($ii=0;$ii<$I;$ii++ ) { $delta = $l[$ii] - $l[$iii] ; # gap in bits between truth and rival i # find mean gap, and sd $gap += $p[$iii] * $delta ; $Sgap += $p[$iii] * $delta * $delta ; # find mean number of bits, and sd $D += $p[$iii] * $l[$ii] ; $S += $p[$iii] * $l[$ii] * $l[$ii] ; $iii ++ ; $iii = 0 if ($iii>=$I) ; } $vgap = $Sgap - $gap * $gap ; $sdgap = sqrt($vgap) ; $v = $S - $D*$D ; $sd = sqrt($v) ; if ($i==0) { $H=$D ; } printf "%3d %8.4g %8.4g %8.4g " , $i , $D , $D-$H, $sd ; printf "%8.4g %8.4g %8.4g %8.4g %8.4g %8.4g\n" , $gap , $sdgap , $S , $v , $Sgap , $vgap ; } print STDERR "# gnuplot\n set xrange [-1:$I] \n set noyzeroaxis "; print STDERR 'plot "DKL" u 1:2 w boxes'."\n"; print STDERR 'plot "DKL" u 1:3:4 w boxerr'."\n"; print STDERR 'plot "DKL" u 1:2:4 w boxerr'."\n"; print STDERR 'N=16'."\n"; print STDERR 'plot "DKL" u 1:(N*$2):(sqrt(N)*$4) w boxerr'."\n"; # this ones error bars are wrong print STDERR 'plot "DKL" u 1:(N*$3):(sqrt(N)*$4) w boxerr'."\n"; print STDERR 'plot "DKL" u 1:(N*$5):(sqrt(N)*$6) w boxerr'."\n";