ShannonEntropy { public static double computeWithFrequencies(Collection<Double> frequencies) { if (frequencies.contains(0D)) { throw new IllegalArgumentException( "Values with null frequency are not permitted when computing Shannon entropy"); } if (frequencies.isEmpty()) { return Double.MAX_VALUE; } double entropy = frequencies.stream().mapToDouble(d -> d).map(s -> s * Math.log(s)).sum(); entropy = (-entropy / Math.log(2)); return Math.abs(entropy); }  static double computeWithFrequencies(Collection<Double> frequencies); static double computeWithValues(Collection<T> values); static double maxEntropy(int cardinality);  }
@Test(expected = IllegalArgumentException.class) public void should_not_compute_entropy_for_a_value_with_frequency_of_zero() { List frequencies = Arrays.asList(0.25, 0.25, 0.25, 0.0); ShannonEntropy.computeWithFrequencies(frequencies); }