Java Entropy Calculate entropy(int numBins, double[] _f)

Here you can find the source of entropy(int numBins, double[] _f)

Description

entropy

License

Open Source License

Declaration

public static double entropy(int numBins, double[] _f) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by      //

public class Main {
    public static double entropy(int numBins, double[] _f) {
        double min = Double.POSITIVE_INFINITY, max = Double.NEGATIVE_INFINITY;

        for (double x : _f) {
            if (x < min)
                min = x;//from   w ww  .j  ava2  s .c o m
            if (x > max)
                max = x;
        }

        int[] v = new int[numBins];
        double width = max - min;

        for (int i = 0; i < _f.length; i++) {
            double x = _f[i];
            double x3 = (x - min) / width; // 0 to 1
            int bin = (int) (x3 * (numBins - 1)); // 0 to numBins - 1
            v[bin]++;
        }

        // Calculate entropy.
        double sum = 0.0;

        for (int i = 0; i < v.length; i++) {
            if (v[i] != 0) {
                double p = v[i] / (double) (numBins - 1);
                sum += p * Math.log(p);
            }
        }

        return -sum;
    }
}

Related

  1. entropy(byte[] f)
  2. entropy(double[] distribution)
  3. entropy(double[] p)
  4. entropy(double[] vals)
  5. entropy(int hist[])
  6. entropy(int total, double[] data)
  7. entropy(int[] histogram)
  8. longPseudoEntropy()