News for hackers. Stuff that matters.
Re:Kullback–Leibler Divergence by jwarden on Tuesday August 22, 2023 @02:34PM (#37226815)
Here's how I describe KL Divergence, building up from simple to complex concepts.

surprisal: how surprised I am when I learn the value of X

  Suprisal(x) = -log p(X=x)
entropy: how surprised I expect to be

  H(p)        = 𝔼_X -log p(X) 
              = ∑_x p(X=x) * -log p(X=x)
cross-entropy: how surprised I expect Bob to be (if Bob's beliefs are q instead of p)

  H(p,q)      = 𝔼_X -log q(X) 
              = ∑_x q(X=x) * -log p(X=x)
KL divergence: how much *more* surprised I expect Bob to be than me

  Dkl(p || q) = H(p,q) - H(p,p)
              = ∑_x p(X=x) * log p(X=x)/q(X=x)
information gain: how much less surprised I expect Bob to be if he knew that Y=y

  IG(q|Y=y)   = Dkl(q(X|Y=y), q(X))
mutual information: how much information I expect to gain about X from learning the value of Y

  I(X;Y)      = 𝔼_Y Dkl(q(X|Y=y), q(X))
Re:Kullback–Leibler Divergence by mrfox321 on Tuesday August 22, 2023 @02:59PM (#37227089)
Typo in cross entropy, should be

p log q

Re:Kullback–Leibler Divergence by jwarden on Tuesday August 22, 2023 @04:16PM (#37227980)
Thank you, fixed.
Re:Kullback–Leibler Divergence by 3abiton on Tuesday August 22, 2023 @05:19PM (#37228671)
Forgive my peasant mind, I've heard about K-L divergence before, I just don't know what it is used for nor what's special about it compared to other metrics?
Re:Kullback–Leibler Divergence by JonyEpsilon on Tuesday August 22, 2023 @05:37PM (#37228858)
Practically speaking, it's a simple measure of how similar two probability distributions are, minimised (with value zero) when they are the same. So it's often used as a loss term in optimisations when you want two distributions to be pushed towards being similar. Sometimes this motivated by clever reasoning about information/probability ... but often it's more just "slap a KL on it", because it tends to work.
Re:Kullback–Leibler Divergence by zmmmmm on Tuesday August 22, 2023 @06:18PM (#37229312)
I think people who love math expressed in nomenclature well never understand those who don't ...

K-L divergence is the pretty much really dumb, obvious thing you would probably think up if someone asked you to compare two probability distributions.

You would start by going "oh I suppose I will look at how far apart they are at all the different values" and add it up. Then you would say, "oh, but 0.1 and 0.01 are really an order of magnitude apart, just like 0.01 and 0.001. Perhaps it will work better if I use the log of the probability". Then you would pause for thought and say, "Hmmmm hang on some of the values are really extreme but almost never happen, shouldn't it be weighted by how frequently they occur?".

But of course paragraphs of mathematical symbols are the way many people prefer to express this.

Re:Kullback–Leibler Divergence by pama on Tuesday August 22, 2023 @09:08PM (#37230676)
I agree, however the next step would be to symmetrize the KL divergence because otherwise we get a result that depends on the order that we parse the distributions.
Re:Kullback–Leibler Divergence by creata on Tuesday August 22, 2023 @10:49PM (#37231322)
> K-L divergence is the pretty much really dumb, obvious thing you would probably think up if someone asked you to compare two probability distributions.

I don't think if anyone was asked simply to "compare two probability distributions", they'd come up with something asymmetric like the KL divergence. You'd need to at least add that one of the distributions is the "real" distribution.

Re:Kullback–Leibler Divergence by sarma17 on Tuesday August 22, 2023 @06:49PM (#37229617)
qq, can you explain a few noob thoughts. Why is surprisal defined as `log (1/p(X=x)`? I'm lacking the intuition for 1. why did someone think to use 1/p? 2. why do we have a log term here.

Log shows up a lot but I don't think I ever really understood when/how do people decided to use it in their formulas. One KL divergence youtube video said, `let's normalize the p(P)/p(Q) using (log(p(P)/q(P)))^N` and then shows how to derived the KL formula. I'd also appreciate if you know how the N (sample size ig) is being used here.

For a lot of math stuff I endup being confused about how people use operations. Like using 1/p(X=x) is like magic to me because I don't understand what the context is when someone thinks about the problem and then decides to do something. What's their thinking process here, what tool or process do I not know about that makes me confused?

Re:Kullback–Leibler Divergence by eutectic on Tuesday August 22, 2023 @07:16PM (#37229841)
Negative log is the only decreasing function which satisfies f(x * y) = f(x) + f(y). If you have two independent events it makes sense that the total surprisal (information content) should be the sum of the surprisals of the two events.

Another way to see it is as a continuous generalization of the idea that you need n bits to represent 2^n equally likely alternatives.

Re:Kullback–Leibler Divergence by sarma17 on Tuesday August 22, 2023 @07:25PM (#37229905)
thanks for the reply but I don't think I understood what you said in the context of my question.

I'm stuck on why we care about surprisal as `log 1/p`.

Re:Kullback–Leibler Divergence by eutectic on Tuesday August 22, 2023 @07:25PM (#37229907)
log 1/p is just -log p
Re:Kullback–Leibler Divergence by sixo on Saturday September 02, 2023 @10:08PM (#37367110)
I think "information" is a better name than surprisal.

If your distribution has N equally-likely values, `p(x) = 1/N`, and information/surprisal `I(x) = log(N)`. In base 2, this is how many bits are required to specify exactly WHICH of the N values you're talking about.

If `x` is not a single one of the N states but an event consisting of `n(x)` states, then `I(x) = log(N) - log(n(x))`, suggesting it takes _somewhat less information_ to specify this particular state, and it correctly gives 0 if `n(x) = N`, i.e. there's only one state.

Exactly what this "less information" means is vague, but you might think of it in terms of compression: if you compress some stream of data which is sampled from `X` with probability `p(x)`, you could use use the shortest codes (0, 10, 11, etc) for the most common values with some "stop word" to say when the end of a datum is reached. `I(x)` captures this sense in general, but it might only become literally true in the limit of a very large stream of data with a very large dictionary.

Re:Kullback–Leibler Divergence by ramblenode on Tuesday August 22, 2023 @08:30PM (#37230414)
> If you have two independent events it makes sense that the total surprisal (information content) should be the sum of the surprisals of the two events.

I think the parent is also asking why we would expect surprisal to be additive rather than multiplicative like probabilities.

Re:Kullback–Leibler Divergence by Majromax on Tuesday August 22, 2023 @09:04PM (#37230649)
Because if two things happen – one totally expected and one very surprising – then on net you’re still surprised.
Re:Kullback–Leibler Divergence by ramblenode on Wednesday August 23, 2023 @12:37PM (#37238641)
That's also the case for probabilities.
Re:Kullback–Leibler Divergence by joelthelion on Wednesday August 23, 2023 @04:36AM (#37233311)
I think in part because it's really nice to measure information in bits rather than tiny probabilities. And it aligns well with how information is stored.
Re:Kullback–Leibler Divergence by KRAKRISMOTT on Tuesday August 22, 2023 @10:25PM (#37231192)
Convolution?
Re:Kullback–Leibler Divergence by beyondCritics on Thursday August 24, 2023 @11:53AM (#37250345)
> Negative log is the only decreasing function which satisfies f(x * y) = f(x) + f(y)

Times a constant!

Re:Kullback–Leibler Divergence by JonyEpsilon on Tuesday August 22, 2023 @07:44PM (#37230046)
It's not answering quite the question you asked, but Shannon - who invented much of this stuff - has some really nice practical arguments in the start of his amazing paper that introduced "information theory" [1]. It's a really readable paper, much less intimidating than you might think, and worth a look.

Hartley was (as far as I know) the first person to recognise the usefulness of log probabilities in the context of measuring information [2]. It's a really amazing paper, for several reasons ... but one thing that really strikes me about it is how it's aged: the first half has an essentially timeless presentation of the essence of information, and the second has a now-quite-irrelevant presentation of how to make a better TV. I guess that was the really interesting problem at the time!

[1] https://people.math.harvard.edu/~ctm/home/text/others/shanno...

[2] http://dotrose.com/etext/90_Miscellaneous/transmission_of_in...

Re:Kullback–Leibler Divergence by stygiansonic on Tuesday August 22, 2023 @09:44PM (#37230894)
There’s some information here about why this was chosen. Specifically it fulfills certain criteria: https://en.m.wikipedia.org/wiki/Entropy_(information_theory)...
Re:Kullback–Leibler Divergence by jwarden on Wednesday August 23, 2023 @06:43AM (#37234006)
> Why is surprisal defined as `log (1/p(X=x)`?

Good question. It's because -log P(X) is the number of bits of information I would need to eliminate any surprise about the value of X.

For example, let's say X is the value of an 8-bit register, and each of the 2^8=256 possible values has a probability of 1/256. I would need to learn all 8 bits to know the value of X: so Surprisal(X=x) = -log P(X=x) = -log 1/256 = 8.

Suppose I learned only that X is odd, which is equivalent to learning that the last bit of the register is equal to 1. The probability that X is 1 is 1/2, and so I would need to be provided with -log P(X is odd) = -log 1/2 = 1 bit of information to learn that X is odd.

Information can be thought of as simply the elimination of uncertainty. So Surprisal (uncertainty) is basically a measure of information, but instead of how much information you have, it's how much information you need to eliminate all uncertainty.