public static void normalizeExp ( AssociativeArray associativeArray ) { double max = max ( associativeArray . toFlatDataCollection ( ) ) ; double sum = 0.0 ; //Prevents numeric underflow by subtracting the max. References: http://www.youtube.com/watch?v=-RVM21Voo7Q for ( Map . Entry < Object , Object > entry : associativeArray . entrySet ( ) ) { Double value = Math . exp ( TypeInference . toDouble ( entry . getValue ( ) ) - max ) ; associativeArray . put ( entry . getKey ( ) , value ) ; sum += value ; } if ( sum != 0.0 ) { for ( Map . Entry < Object , Object > entry : associativeArray . entrySet ( ) ) { associativeArray . put ( entry . getKey ( ) , TypeInference . toDouble ( entry . getValue ( ) ) / sum ) ; } } }
Normalizes the exponentials of provided associative array by using the log - sum - exp trick .