Skip to content

Instantly share code, notes, and snippets.

@VijayKrishna
Created March 17, 2015 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VijayKrishna/6531cc2a2adc307c2c92 to your computer and use it in GitHub Desktop.
Save VijayKrishna/6531cc2a2adc307c2c92 to your computer and use it in GitHub Desktop.
public static double entropy(List<? extends Comparable> values) {
final Map<Comparable, Long> valueOccurances = new HashMap<Comparable, Long>();
for (Comparable value : values) {
Long valueOccurance = valueOccurances.get(value);
valueOccurances.put(value, valueOccurance == null ? 1L : ++valueOccurance);
}
double combinedEntropy = 0.0d;
for (Comparable value : valueOccurances.keySet()) {
double entropy = valueOccurances.get(value) / (double) values.size();
combinedEntropy += entropy * (Math.log(entropy) / Math.log(2));
}
return -combinedEntropy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment