Message Boards Message Boards

1
|
5355 Views
|
14 Replies
|
17 Total Likes
View groups...
Share
Share this post:

How to calculate the entropy of a discrete probability distribution?

We know that the entropy of a discrete random variable X is:

enter image description here

And that the Poisson distribution has (credit to Wikipedia) PMF

enter image description here

and entropy

Poisson distribution entropy

So (for example) is there a simple (textbook) way to derive the entropy from the distribution?

In general, I need to derive the entropy for many discrete probability distributions given a PMF that is determined by combinatorial analysis. (It seems Mathematica calls it the PDF for discrete distributions?)

I suspect that it's actually not trivial, based on this paper, but I'm very far from being an expert, which is why I'm asking.

Thanks, cheers.

Jeremy

POSTED BY: Jeremy Murphy
14 Replies

Thanks, Vitaliy. Yes, you would think that generalization of Shannon entropy would do the job, but I had a quick play with it and wasn't able to get a result. I probably just don't know what I'm doing, though.

POSTED BY: Jeremy Murphy

Jeremy,

the entropy for a distribution $p(k)$ is defined as

$$ -\sum_{k=0}^\infty p(k)\ln p(k) $$

This is what I did:

(* definition of Poisson probability: *)
p = \[Lambda]^k Exp[-\[Lambda]]/k!; 

(* entropy - does not give a closed expression: *)
-Sum[p Log[p], {k, 0, Infinity}]  

(* expanding Log[p]: *)
logp = FunctionExpand[Log[p], Assumptions -> {k >= 0, \[Lambda] > 0}] /. Gamma[1 + n_] :> n!

(* doing summation term-wise: *)
-Simplify[Sum[p #, {k, 0, Infinity}] & /@ logp]

This gives finally your wanted result:

enter image description here

Hard to tell how to do it for any other distribution, so - I do not know if this really helps.

POSTED BY: Henrik Schachner

Thanks, Henrik. Any example is helpful at the moment; it might help me to derive a general solution.

POSTED BY: Jeremy Murphy
Posted 2 years ago

Henrik, I'm completely new to Mathematica, so I understand most of what you've written there, but could you briefly explain, or link to an explanation, of a few of things:

  • After the end of FunctionExpand, what is that "/." operator and the expression with Gamma doing? (I know what the Gamma function itself is.)
  • What is the # symbol in the last Sum doing?
  • What is the meaning of "&/@" in the last Sum?

Thanks.

POSTED BY: Updating Name

Simpler:

 p = \[Lambda]^k Exp[-\[Lambda]]/k!;
 -Sum[#, {k, 0, Infinity}] & /@ (p Log[p] // PowerExpand // Expand)

 (*\[Lambda] - \[Lambda]*Log[\[Lambda]] - Sum[-((\[Lambda]^k*Log[k!])/(E^\[Lambda]*k!)), {k, 0, Infinity}]*)

Test:

Integrate[Sin[x] + Cos[x] + Exp[-Pi*x^2 - 1/x], x](*Can't integrate all terms*)

but:

Integrate[#, x] & /@ (Sin[x] + Cos[x] + Exp[-Pi*x^2 - 1/x])
(*-Cos[x] + Integrate[E^(-x^(-1) - Pi*x^2), x] + Sin[x]*)
POSTED BY: Mariusz Iwaniuk

Mariusz, thank you, that's a great simplification! It works very well.

It takes several seconds to produce an answer, which is not a problem at the moment, but are there additional constraints/assumptions I could add to help it solve faster? Is it just a matter of experimenting and see what makes a difference?

POSTED BY: Jeremy Murphy

I successfully solved another pmf, but then I accidentally lost the equation after copying down the result. Now I can't recreate the result!

This is what I'm trying:

q = ((T - 1)/T)^i;
p = (1/T) q;
pmf = if[i = n, q, p];
-Sum[#, {n, 0, Infinity}] & /@ (pmf Log[pmf] // PowerExpand // Expand)

And I previously got a result of:

-((-1 + T) Log[-1 + T]) + T Log[T]

which I verified numerically against other data I have. But now when I run the above commands, I get a very ambiguous result that is hardly simplified at all.

What am I going wrong in trying to recreate the result? It seemed to easy the first time.

POSTED BY: Jeremy Murphy

Jeremy,

one problem is syntax: It probably should read:

pmf = If[i == n, q, p];

and: Where is the dependence on n, what is "i"? If it is some constant parameter it ought to be contained in your wanted result.

POSTED BY: Henrik Schachner

Thanks, Henrik, yes it took me a while before I realized that Mathematica uses = for assignment. Apologies for the half-baked plea for help, I did figure it out by myself in the end.

n is the size of the discrete random variable minus one, and i is the index you might say of the outcome, so when calculating numerical values of entropy for a drv of finite size, it computes essentially computes p[i] until i == n where it then computes q[i]. But my mathematics is not very strong and I didn't know how to express that in a way Mathematica would understand, so I actually just got rid of q altogether. The numerical results are spot on, but I'm not sure if the symbolic result is right, given that I dropped the case of when it reaches n (since n is Infinity in the analysis).

I also seemed to be having problems with variable declarations leaking from previous calculations. As in, the k variable from calculating Poisson still existed and so if I used k again in the definition of p then it didn't work. I can't explain in detail as I haven't had time to experiment, but essentially I couldn't run the Poisson calculation twice -- the second time would give me an error.

Finally I made the mistake of changing pmf to p in the Sum without removing the definition fo pmf and so the # symbol still referred to pmf although I didn't want it to. Rookie mistakes.

This is what I did in the end, but with a lot of Clear commands preceding it:

q = ((T - 1)/T)^i;
p = (1/T) q;
Simplify[-Sum[#, {i, 0, Infinity}] & /@ (p Log[p] // PowerExpand // Expand)]

Giving the aforementioned result:

-((-1 + T) Log[-1 + T]) + T Log[T]

Thanks for your help.

POSTED BY: Jeremy Murphy

I'm completely new to Mathematica ...

OK then, wellcome, you are ahead of a steep learning curve - but you can be sure: Learning Mathematica is worth any effort! Regarding your questions:

  • "/." means ReplaceAll: I am using it here because I want the result in terms of factorial (and not Gamma function);
  • "# &" is the short form of Function and refers to a "pure function";
  • "/@" is the short form of Map;

Mathematica has a terrific documentation - one needs to learn using it excessively.

POSTED BY: Henrik Schachner

Thanks for the quick intro, Henrik!

POSTED BY: Jeremy Murphy

Maybe this helps.

POSTED BY: Mariusz Iwaniuk

Thanks, Mariusz. That question on the Mathematica overflow is closely related, and it helped me find other people asking the same question as me on there!

POSTED BY: Jeremy Murphy
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract