#!/usr/local/bin/perl -w # # David MacKay Dec 97 # # hamming.p # - writes the posterior prob of all four codewords for a given # normalized likelihood string # # $verbose = 0 ; $factor = 1 ; $N = 7 ; $stretch = 1.0 ; # factor for turning probabilty into a box $units = "in" ; # box units @like = ( .2,.2,.9,.2,.2,.2,.2 ) ; @like = ( .1,.4,.9,.1,.1,.1,.3 ) ; @code = ( "0000000" , "0001011" , "0010111" , "0011100" , "0100110" , "0101101" , "0110001" , "0111010" , "1000101" , "1001110" , "1010010" , "1011001" , "1100011" , "1101000" , "1110100" , "1111111") ; $Z = 0.0 ; foreach $c (@code) { $lc = 1.0 ; # for ($n=0 ; $n<$N ; $n ++ ) { # # print "$n " , substr($c,$n,1) , "\n" ; if ( substr($c,$n,1) eq "1" ) { # # print "$n:1 " ; # $lc = $lc * $like[$n] ; } else { # print "$n:0 " ; $lc = $lc * (1.0 - $like[$n]) ; } # } # $post{$c} = $lc ; $Z += $lc ; } $IZ = 1.0 / $Z ; for ($n=0 ; $n<$N ; $n ++ ) { # $marg[$n] = 0.0 ; $tot[$n] = 0.0 ; } # # # # print t , like , posterior prob and a box. # print '\begin{tabular}{clll} \hline' , "\n" ; print '$\bt$ & \multicolumn{1}{c}{likelihood } & \multicolumn{2}{c}{posterior probability} \\\\ \hline' , "\n%\n" ; foreach $c (@code) { print "\\tt $c & " ; printf "%-10.7f & " , $post{$c} * $factor ; $post{$c} *= $IZ ; $boxlength = $post{$c} * $stretch ; printf "%-10.4f " , $post{$c} ; printf "& %s " , &fbox($boxlength) ; print "\\\\ \n" ; for ($n=0 ; $n<$N ; $n ++ ) { # if ( substr($c,$n,1) eq "1" ) { # $marg[$n] += $post{$c} ; } else { } # $tot[$n] += $post{$c} ; } # } print '\end{tabular}' , "\n" ; print "% marginals\n" ; for ($n=0 ; $n<$N ; $n ++ ) { # printf "%-8.3f & " , $marg[$n] ; } # print "\\\\ \n" ; print "% sanity check\n" ; for ($n=0 ; $n<$N ; $n ++ ) { # printf "%-8.3f " , $tot[$n] ; } # print "\\\\ \n" ; print '\begin{array}{clllllll}' , "\n" ; print ' n & \multicolumn{2}{c}{\mbox{Likelihood}} & \multicolumn{4}{c}{\mbox{Posterior marginals}} \\\\', "\n" ; print ' & P(y_n|t_n=1) & P(y_n|t_n=0) & ' , "\n" ; print ' \multicolumn{2}{c}{P(t_n=1 | \by)} & \multicolumn{2}{c}{P(t_n=0 | \by)} \\\\ \hline ' , "\n" ; print "% marginals\n" ; for ($n=0 ; $n<$N ; $n ++ ) { # printf "%2d & %-8.1f & %-8.1f & %-8.3f & %s & %-8.3f & %s \\\\\n" , $n+1 , $like[$n] , 1.0 - $like[$n] , $marg[$n], &fbox($marg[$n]) , 1.0- $marg[$n] , &fbox(1.0-$marg[$n]) ; } # print '\end{array}' , "\n" ; sub fbox { local ($l) = @_ ; return ( sprintf ( "\\framebox[%-.3f%s]\{\} " , $l , $units ) ) ; }