#!/usr/local/bin/perl -w # # finds capacity of a channel # by tweaking input distbn # # $I = number of inputs # $J = number of outputs # # reads the Q matrix from stdin: each column is the # probab over outputs given one input. # $verbose = 0 ; $eta = 0.1 ; # step size $eps = 0.0000000000000001 ; # warn if get negative change beyond this $tol = 0.000000001 ; # when to stop changing (delta I(X;Y)) $T = 1000 ; # number of steps # eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; # # read in the Q_j|i matrix. For consistency with textbook, # let probs be column vectors. each input is a column. # @Q = <> ; $_ = $Q[0] ; @p = split ; $J = $#Q + 1 ; $I = $#p + 1 ; # make associative array $fail = 0 ; foreach $j ( 1 .. $J ){ $_ = $Q[$j-1] ; @p = split ; if ( $#p != $I-1 ) { $fail = 1 ; print "row $j wrong number items\n" ; } foreach $i ( 1 ..$I ){ $q{"$j.$i"} = $p[$i-1] ; } } exit if $fail ; foreach $i ( 1 .. $I) { $sum[$i] = 0.0 ; } print "q[j|i]:\n-------\n" ; foreach $j ( 1 ..$J){ foreach $i ( 1 .. $I){ $thisq = $q{"$j.$i"} ; print "q[$j|$i] = $thisq\t" ; $sum[$i] += 1.0 * $thisq ; } print "\n" ; } foreach $i ( 1 .. $I){ print " ------\t" ; } print "\n" ; $norm = 0 ; foreach $i ( 1 .. $I){ print " $sum[$i]\t" ; if ( $sum[$i] != 1.0 ) { $norm = 1 ; } } print "\n" ; if ($norm) { print "Normalized q[j|i]:\n-------\n" ; foreach $j ( 1 ..$J){ foreach $i ( 1 .. $I){ $q{"$j.$i"} /= $sum[$i] ; $thisq = $q{"$j.$i"} ; print "q[$j|$i] = $thisq\t" ; } print "\n" ; } } # # start with uniform distbn foreach $i ( 1 ..$I){ $lx[$i] = 0.0 ; } $done = 0 ; ALOOP: foreach $t ( 1.. $T ) { &make_qx ; &make_py ; &IxY ; if ( $t > 1 ) { $delta = $IXY - $IXYlast ; if ( $delta < $tol ) { $done = 1 ; } } $IXYlast = $IXY ; if ( $done || ($t==$T) || !($t%20) || $t < 5 ) { &report ; } if ( $done ) { last ALOOP ; } if ( !$done || ($t < $T) ) { &tweak ; } } if ( $done ) { print "converged to tolerance $tol\n" ; } else { print "not converged to tolerance $tol\n" ; } ############3 report some more stuff print "inference P[i|j]:\n-------\n" ; foreach $j ( 1 ..$J){ foreach $i ( 1 .. $I){ # pi|j = qj|i pi / pj $thisq = $q{"$j.$i"} ; $posterior{"$i|$j"} = $thisq * $qx[$i] / $py[$j] ; printf "p[$i|$j] = %7.4g ",$posterior{"$i|$j"} ; } print "\n" ; } sub tweak { # if IxY is big, use input x more . foreach $i ( 1 ..$I){ $lx[$i] += $eta * ( $IxY[$i] - $IXY ) ; } } sub report { # print "----------------\n" ; print "log(q[x])\tq[x]\tI(x;Y)" ; print " ----------------------------------- t=$t\n" ; foreach $i ( 1 ..$I){ printf "%6.4g\t" , $lx[$i] ; printf "%6.4g\t" , $qx[$i] ; printf "%6.4g\n" , $IxY[$i] ; } print "p[y]:\t" ; foreach $j ( 1 ..$J){ printf "%6.3g\t" , $py[$j] ; } printf " I(X;Y) = %7.5g" , $IXY ; if ( $t > 1 ) { if ( $delta < -$eps ) { print "EEEK!" ; } print " change = $delta\n" ; } else { print "\n" ; } } sub make_qx { $cum = 0.0 ; foreach $i ( 1 ..$I){ $qx[$i] = exp($lx[$i]); $cum += $qx[$i] ; } foreach $i ( 1 ..$I){ $qx[$i] = $qx[$i] / $cum ; } $qx[0] = $cum ; } sub make_py { foreach $j ( 1 ..$J){ # y $py[$j] = 0.0 ; foreach $i ( 1 ..$I){ # x $py[$j] += $q{"$j.$i"} * $qx[$i] ; } # print "py[$j] = $py[$j]\n" ; } } sub IxY { # evaluates the Gallager particular case mutual information # I(x;Y) = sum_y P(y|x) log [ P(y|x)/P(y) ] $IXY = 0.0 ; # for finding average foreach $i ( 1 ..$I){ # x $IxY[$i] = 0.0 ; foreach $j ( 1 ..$J){ # y $thisq = $q{"$j.$i"} ; print "i=$i , j = $j: " if ($verbose ) ; print $q{"$j.$i"}." <- q , py -> $py[$j] " if ($verbose ) ; if ( $thisq > 0.0 ) { $IxY[$i] += $thisq * log( $thisq / $py[$j] ) ; } print " => $IxY[$i]\n" if ($verbose ) ; } $IxY[$i] /= log(2.0) ; $IXY += $qx[$i] * $IxY[$i] ; # for finding average } }