Java Entropy Calculate entropy(byte[] f)

Here you can find the source of entropy(byte[] f)

Description

entropy

License

Apache License

Declaration

public static float entropy(byte[] f) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static float entropy(byte[] f) {
        int counts[] = new int[256];
        float entropy = 0;
        float total = f.length;

        for (byte b : f)
            counts[b + 128]++;//  w w w . j  a va 2 s. c o  m
        for (int c : counts) {
            if (c == 0)
                continue;
            float p = c / total;

            /* Compute entropy per bit in byte.
             *
             * To compute entropy per byte compute log with base 256 = log(p)/log(256).
             */
            entropy -= p * Math.log(p) / Math.log(2);
        }

        return entropy;
    }
}

Related

  1. calcEntropyOfDiscreteProbDist(Double[] probDist)
  2. computeEntropy(int pos, int neg)
  3. discreteEntropy(int[] sequence, int numStates)
  4. discretiseMaxEntropy(double data[], int numBins)
  5. entropy(double[] distribution)
  6. entropy(double[] p)
  7. entropy(double[] vals)
  8. entropy(int hist[])