#!/usr/local/bin/perl # report the entropy of this distribution # one line per prob. lengths optional $i = 0 ; $c = 0 ; $h = 0 ; while( <> ) { $i ++ ; ( $x[$i] , $l[$i] , $rest ) = split ; $c += $x[$i] ; } while( $i > 0 ) { $x = $x[$i]/$c ; $l = $l[$i] ; $h -= $x * log ( $x) ; $L += $l * $x ; $i -- ; } $h /= log ( 2.0 ) ; printf "I=%2d, c=%5f, H=%5f, L=%5f\n" , $i , $c , $h , $L ;