Java Entropy Calculate discreteEntropy(int[] sequence, int numStates)

Here you can find the source of discreteEntropy(int[] sequence, int numStates)

Description

discrete Entropy

License

Open Source License

Declaration

public static double discreteEntropy(int[] sequence, int numStates) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static double discreteEntropy(int[] sequence, int[] p) {
        double v = 0.0;
        double vt = 0.0;
        int i = 0;
        int numStates = p.length;

        for (i = 0; i < numStates; i++) {
            p[i] = 0;//from  w w  w. j  a va2  s.c  o m
        }

        for (i = 0; i < sequence.length; i++) {
            p[sequence[i]]++;
        }
        for (i = 0; i < numStates; i++) {
            if (p[i] > 0) {
                vt = ((double) p[i]) / ((double) sequence.length);
                v = v - vt * Math.log(vt);
            }
        }

        if (v <= 0.0) {
            v = 0.0;
        }

        return v;
    }

    public static double discreteEntropy(int[] sequence, int numStates) {
        int[] p = new int[numStates];
        return discreteEntropy(sequence, p);
    }
}

Related

  1. calcEntropyOfDiscreteProbDist(Double[] probDist)
  2. computeEntropy(int pos, int neg)
  3. discretiseMaxEntropy(double data[], int numBins)
  4. entropy(byte[] f)
  5. entropy(double[] distribution)
  6. entropy(double[] p)