Guest User

Shannon Entropy Calculation

a guest
Sep 14th, 2012
1,421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1.  
  2.     public static double entropy(List<? extends Comparable> values) {
  3.  
  4.         final Map<Comparable, Long> valueOccurances = new HashMap<Comparable, Long>();
  5.  
  6.         for (Comparable value : values) {
  7.             Long valueOccurance = valueOccurances.get(value);
  8.             valueOccurances.put(value, valueOccurance == null ? 1L : ++valueOccurance);
  9.         }
  10.  
  11.         double combinedEntropy = 0.0d;
  12.  
  13.         for (Comparable value : valueOccurances.keySet()) {
  14.             double entropy = valueOccurances.get(value) / (double) values.size();
  15.             combinedEntropy += entropy * (Math.log(entropy) / Math.log(2));
  16.         }
  17.  
  18.         return -combinedEntropy;
  19.     }
Advertisement
Add Comment
Please, Sign In to add comment