Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment