public static < E > double crossEntropy ( Counter < E > from , Counter < E > to ) { double tot2 = to . totalCount ( ) ; double result = 0.0 ; for ( E key : from . keySet ( ) ) { double count1 = from . getCount ( key ) ; if ( count1 == 0.0 ) { continue ; } double count2 = to . getCount ( key ) ; double logFract = Math . log ( count2 / tot2 ) ; if ( logFract == Double . NEGATIVE_INFINITY ) { return Double . NEGATIVE_INFINITY ; // can't recover } result += count1 * ( logFract / LOG_E_2 ) ; // express it in log base 2 } return result ; }
Note that this implementation doesn t normalize the from Counter . It does however normalize the to Counter . Result is meaningless if any of the counts are negative .