public static double HistogramEntropy(this int[] values)
{
int total = 0;
double entropy = 0;
double p;
// calculate total amount of hits
for (int i = 0; i < values.Length; i++)
total += values[i];
if (total != 0)
{
// for all values
for (int i = 0; i < values.Length; i++)
{
// get item's probability
p = (double)values[i] / total;
// calculate entropy
if (p != 0)
entropy += (-p * Math.Log(p, 2));
}
}
return entropy;
}