I need to calculate the entropy of a distribution from raw data. I found two approaches here and here.

But it seems they don't work for my case.

My data is:


And I have:

f1 = HistogramDistribution[d];
p1 = PDF[f1, x];

The entropy becomes:

-Expectation[Log[p1], x \[Distributed] f1]

which turns out to be negative: $-0.360647$. Entropy cannot be negative! What has gone wrong here?

  • 7
    $\begingroup$ From the Wikipedia: "One must take care in trying to apply properties of discrete entropy to differential entropy, since probability density functions can be greater than 1. For example, the uniform distribution $\mathcal{U}(0,1/2)$ has negative differential entropy $\int_0^{\frac12} -2\log(2)\,dx=-\log(2)$. Thus, differential entropy does not share all properties of discrete entropy." In other words: entropy can indeed be negative. $\endgroup$
    – Roman
    Feb 13, 2021 at 12:52
  • $\begingroup$ The recept from the second link works: NIntegrate[ With[{f = PDF[f1, x]}, If[f > 0, f Log[f], 0]], {x, -\[Infinity], \[Infinity]}] results in 0.360647. $\endgroup$
    – user64494
    Feb 13, 2021 at 12:59
  • 1
    $\begingroup$ @user64494 Thanks! However, in that formula the minus sign is not included. $\endgroup$
    – user77316
    Feb 13, 2021 at 13:00
  • 2
    $\begingroup$ Also keep in mind that for small datasets your entropy will depend strongly on the histogram bin size. I don't think it's a good measure of entropy, for this reason. $\endgroup$
    – Roman
    Feb 13, 2021 at 13:05
  • 1
    $\begingroup$ Take a look at KullbackLeiblerDivergence for an entropy measure that's always positive (against a reference distribution, that is). $\endgroup$ Feb 17, 2021 at 16:27

1 Answer 1


I believe the proposition that entropy cannot be negative applies only to categorical distributions, which are discrete distributions for which the categories have no intrinsic numerical values. For example, the outcome of a (stylized) coin flip can be characterized by two categories: Heads and Tails. One can associate the numbers 1 and 0 with those categories, but they are for convenience only. For example, with those numerical values the expectation of the random variable equals the probability of Heads. It is, however, the probability of Heads that is fundamental, not the expectation.

In the example given in the question, the distribution is continuous. Nevertheless, we can use the bin probabilities themselves --- purely as an example. We can obtain a categorical distribution by treating the bins as categories for which the bin centers (for example) are the category labels, which have no numerical significance. In this case, the entropy is given by \begin{equation} -\sum_i p_i\,\log(p_i) . \end{equation}

In Mathematica, one can do the following:

probs = Cases[p1, x_ * Boole[_] :> x/10];

which produces 1.94194.

  • $\begingroup$ I don't think this method makes sense. The result is dominated by the (arbitrary) bin size. What is the statistical meaning of the quantity you are calculating? $\endgroup$
    – Roman
    Feb 17, 2021 at 18:43
  • $\begingroup$ @Roman. I think you are correct in thinking that bin size matters. But the example I use this is not meant as a serious treatment of the histogram distribution, which as I mentioned is continuous. The example is only meant to illustrate how to use the probabilities from a categorical distribution to compute entropy. I hoped this illustration would help the OP see the connection between (1) the collection of probabilities that happened to show up in histogram distribution with the chosen bin-widths and (2) the implicit formula that I suspect the OP had in mind. I've made an edit. Does it help? $\endgroup$
    – mef
    Feb 17, 2021 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.