Created
March 17, 2015 04:48
-
-
Save VijayKrishna/6531cc2a2adc307c2c92 to your computer and use it in GitHub Desktop.
Clone of http://pastebin.com/1SC7PYvM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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