Six (and a half) intuitions for KL divergence

(perfectlynormal.co.uk)

59 points | by jxmorris12 1 day ago

4 comments

  • abetusk 37 minutes ago
    Here's my explanation:

    Let's say you're a company that's providing an internet connection to a business. The company trusts you, so there's only compression of bits over the wire, not encryption, and you're aware of the compression scheme the company is using to send their bits to you. You're charging the company a premium for using the line you manage but you also lease the line, so it's in your interest to compress what they give you as best as possible so as to make a profit.

    Say the companies compression scheme is imperfect. They have a Huffman coding of their (imperfect) model of tokens they send, call it q(x) (that is, they think token x shows up with probability q(x)). You've determined the true distribution, p(x) (token x shows up with actual probability p(x)).

    The business has tokens that show up with probability p(x) but they encode them with lg(q(x)) bits, giving an average token bit size of:

        -\sum _ x p(x) lg(q(x))
    
    If you then use an optimal Huffman encoding, you will send tokens with average bit length of:

        -\sum _ x p(x) lg(p(x))
    
    How many bits, on average, do you save? Just the difference:

        -\sum _ x p(x) lg(p(x)) - \sum _ x p(x) lg(q(x)) = -\sum _ x p(x) lg(p(x)/q(x))
    
    Which is the Kullback-Leibler divergence.

    To me, this is a much more intuitive explanation. I made a blog post about it [0], if anyone cares.

    [0] https://mechaelephant.com/dev/Kullback-Leibler-Divergence.ht...

  • ttul 2 hours ago
    This is great. I had only ever seen the expected surprise explanation. The others help to fill in the gaps.
  • RickHull 1 hour ago
    Is there a gentler intro to this topic?
    • jey 1 hour ago
      Try the textbook Elements of Information Theory by Cover and Thomas (2006)
  • darioterror 1 hour ago
    [dead]