1

I'm looking at someone else's project (in c#) and there's an entropy calculation I don't understand. I haven't done any maths in ages and I've had to look up entropy and Log in pursuit of this, so I'm happy to have gotten this far but now I'm stuck.

The main body of the entropy calculation is as follows:

private float calculateEntropy() {
    float total = 0;
    float entropySum = 0;
    foreach (var module in this) {
        total += module.Prototype.Probability;
        entropySum += module.PLogP; 
    }
    return -1f / total * entropySum + Mathf.Log(total);
}

Further Information

The project contains a list of "modules", and each module has a different "probability" of attached to it. The "calculateEntropy" function calculates the entropy of these probabilities. There can be between 1 and 500 modules in the list, and each module can have a "Probability" ranging from 0.05 to 1. By the time we get to the equation at the bottom, "total" is the sum of these probabilities, and "entropySum" is the sum of PLogP for all modules. For avoidance of doubt, PLogP is defined elsewhere in the project as "Probability*(Ln(Probability))", or to give the actual code:

this.PLogP = this.Prototype.Probability * Mathf.Log(this.Prototype.Probability);

My question is about the last line of the calculateEntropy function.

As far as I can tell, this can be re-arranged slightly and expressed in the following way:

-Σ(P*Ln(P)) / Σ(P) + Ln(Σ(P))

The first part of this seems to be Shannon's entropy formula, as I've seen it elsewhere. The next bit (Σ(P)) is presumably needed because our probabilities don't equal 1, so we have to divide by whatever they actually equal. However, I don't know why we need the last expression on the end (Ln(Σ(P)). I think my problem is that I just don't know how to re-arrange the formula, but can anyone help?

I think I've included all the relevant info, but let me know if something's missing.

2 Answers2

1

tl;dr: Your original code's author does not normalize their probability distributions to sum to $1$.

Given a sequence of nonnegative numbers $\{x_j\}_j$, we can define a probability distribution via $$\mathbb{P}[j]=\frac{x_j}{\sum_k{x_k}}$$ This has the nice property that it preserves relative ratios: $$\frac{x_j}{x_k}=\frac{\mathbb{P}[j]}{\mathbb{P}[k]}$$

People commonly work with such sequences, and not actual probability distributions, for two reasons:

  1. Often it's easier to compute relative ratios for a real-world system than actual probabilities, so people choose one event $E$, set $x_E=1$, and then compute each other event weighted with $$x_j=\frac{x_j}{1}=\frac{x_j}{x_E}$$
  2. If you compute a probability distribution using floating-point arithmetic, it is possible (probable, even) that your distribution will not sum to exactly $1$. Renormalizing fixes this.

Suppose your module.PLogP and module.Probability describe these $\{x_j\}_j$ instead of true probabilities. Then the formula you give is \begin{align*} -\frac{\sum_j{x_j\log{(x_j)}}}{\sum_j{x_j}}+\log{\left(\sum_k{x_k}\right)}&=-\frac{\sum_j{x_j\log{(x_j)}}-\sum_j{x_j\log{\left(\sum_k{x_k}\right)}}}{\sum_j{x_j}} \\ &=-\frac{\sum_j{x_j\left(\log{(x_j)}-\log{\left(\sum_k{x_k}\right)}\right)}}{\sum_k{x_k}} \\ &=-\sum_j{\frac{x_j}{\sum_k{x_k}}\left(\log{(x_j)}-\log{\left(\sum_k{x_k}\right)}\right)} \\ &=-\sum_j{\frac{x_j}{\sum_k{x_k}}\log{\left(\frac{x_j}{\sum_k{x_k}}\right)}} \\ &=-\sum_j{\mathbb{P}[j]\log{(\mathbb{P}[j])}} \end{align*}

  • Hello, thanks for this answer. I'm unsure what the notation "{xj}j" means, and the same goes for "∑kxk". Could you explain that bit please? – samuelthomson Jan 01 '22 at 20:27
  • 1
    @samuelthomson: The comments seem to fit here. The second equality in the last part is jsut factoring out $x_j$, namely we have an expression of the form $$\sum x_j\cdot a-\sum x_j\cdot b=\sum x_j(a-b)$$ where $a=\log(x_j)$ and $b=\sum_k\log(x_k)$. Does that help? – String Jan 03 '22 at 13:25
  • Yes, thank you I didn't see that the brackets had moved! – samuelthomson Jan 03 '22 at 13:52
0

This is merely a different rendering of the other answer. To connect it even closer to the OP's notation we have $$ \begin{align} -\sum p\log p &= -\sum\frac{\texttt{P}}{\texttt{total}}\log\left(\frac{\texttt{P}} {\texttt{total}}\right)\\ &= -\sum\frac{\texttt{P}}{\texttt{total}}(\log\texttt P - \log\texttt{total})\\ &= -\frac{\sum\texttt{PlogP}}{\texttt{total}}+\log\texttt{total} = -\frac{\texttt{entropySum}}{\texttt{total}}+\log\texttt{total} \end{align} $$ where the simplification of the last term comes from the fact that $\texttt{total}$ is the normalizing factor: $$ \sum\frac{\texttt{P}}{\texttt{total}}=1\quad\text{since}\quad\texttt{total}=\sum\texttt P $$ and the fact that all involved sums iterate over all values of $p$ or $\texttt P$ whereas $\texttt{total}$ is constant w.r.t. the sums.

String
  • 18,395
  • Thanks for this answer, can you shed any light on the notation used in the other answer?

    I don't know how to read the notation notation in the equation after "we can define a probability distribution via...".

    – samuelthomson Jan 02 '22 at 20:46
  • @samuelthomson The essense of the notation in the other answer is thought to be that $x_1,...,x_k$ is some set of numbers that are not probabilities per se, since they do not sum to $1$. But then we map $x_j$ to a probability distribution by normalizing: Namely $\mathbb P[j]$ is thought to be the corrsponding probability for $x_j$ after normalization. It is equal to $x_j/total$. Let me know if all makes sense that far. – String Jan 02 '22 at 21:15
  • @samuelthomson: Note that $\sum_k x_k$ is shorthand for $total$ which is the sum of all the $P$'s. Your $P$'s equal the other answers $x_j$'s. – String Jan 02 '22 at 21:19
  • Thanks for your help, how you're getting the equation text to format correctly?

    I'm confused about the difference between "xj" and "xk".

    Also, you say that "∑kxk is shorthand for total". At the bottom of the original answer, there's a similar figure but with the "k" underneath the "∑". Is that just a slightly different way of writing the same thing?

    – samuelthomson Jan 02 '22 at 22:19
  • Is the difference that "xj" is just a single number in the sequence, and "xk" is the total sum of the numbers in the sequence? If that's the case then I don't get the difference between "xk" and "∑kxk". – samuelthomson Jan 02 '22 at 22:35
  • @samuelthomson: Well, $x_j$ and $x_k$ are essentially the same, except $j$ corresponds to picking out one element to form $\mathbb P[j]$ whereas $k$ is an independent index that spans all the possible indexes to sum all elements. – String Jan 02 '22 at 22:50
  • @samuelthomson: So $x_k$ is one element in the sum, whereas $\sum_k x_k$ is the sum of all elements. – String Jan 02 '22 at 22:50
  • I understand a bit more now, but I'm looking through the working out given by Jacob Manaker. There are 5 "equals" signs with different equations. I can see how the first equation is arrived at, with "∑jxj" on the bottom, but I don't understand how we get from there to the equation underneath, with "∑kxk" on the bottom (and without the "∑jxj" before the last log). At least part of this confusion is probably related to not understanding the difference between "∑jxj" and "∑kxk" still. – samuelthomson Jan 03 '22 at 13:10
  • 1
    @samuelthomson: There is no difference between $\sum_j{x_j}$ and $\sum_k{x_k}$. The math notation means in pseudo-Python sum(for each j in indices(x): x(j)) and sum(for each k in indices(x): x(k)). I change the "for-variable" indices to avoid collisions in my "nested for loops". – Jacob Manaker Jan 03 '22 at 22:37