Shannon Entropy and KL Divergence Calculator

Calculate the Shannon entropy of a distribution and its Kullback-Leibler divergence from another.

Runs in your browser. Your files never leave your device. See how to check

…

How to do it

  1. 1
    Step 1: Open Shannon Entropy and KL Divergence Calculator and add your input

    Type your numbers or text, add your file, or pick the options you need. Nothing is uploaded: everything stays in your browser.

  2. 2
    Step 2: What Shannon Entropy and KL Divergence Calculator does

    Calculate the Shannon entropy of a distribution and its Kullback-Leibler divergence from another.

  3. 3
    Step 3: Read the result and compare

    The result appears at once. Change one value to see how the answer moves, then keep the figure that fits your case.

  4. 4
    Step 4: Copy or save it

    Copy the result or download it. For an important decision, confirm it with the official source or an adviser.

About this tool

Type the probabilities or counts of a distribution P and optionally a second distribution Q with the same number of categories. The calculator normalizes them, gives the entropy in bits and nats, the maximum entropy, the efficiency and the KL divergence and cross-entropy.

Entropy

H(P) = −Σ p log₂ p bits, the average information of an outcome. It is highest, log₂ of the number of categories, when all outcomes are equally likely. In nats, multiply by ln 2.

KL divergence

D(P‖Q) = Σ p log₂(p ÷ q) measures how much information is lost when Q is used to approximate P. It is zero only when P and Q are the same and is not symmetric. Cross-entropy = H(P) + D(P‖Q).

Input

Enter probabilities that add up to 1 or raw counts: they are scaled to add to 1. A zero in Q where P is positive gives an infinite divergence.

Worked example

P = (0.5, 0.25, 0.125, 0.125) has an entropy of 1.75 bits against a maximum of 2 (87.5%). Compared with the uniform Q, the KL divergence is 0.25 bits and the cross-entropy 2 bits.

Tips for best results

Check every box before you rely on a result, because one wrong digit changes the whole answer. Use the exact figures from your payslip, bill, certificate or contract, and keep a note of the date you calculated. Rates, thresholds and rules change from time to time, so for tax, fees and legal matters treat the output as an estimate and confirm with the official source named on this page. You can run as many scenarios as you like; nothing is saved or sent, and refreshing the page clears the boxes.

Quick facts

Embed this calculator on your site

Copy this code to show the calculator on your own page. A link back to shobfree.com is included.

Frequently asked questions

Bits or nats?

Bits use log base 2, nats the natural log.

Is KL a distance?

No, it is not symmetric.

What if q is zero?

The divergence is infinite when p is positive there.

Can I enter counts?

Yes, they are normalized.

Guides for this tool

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages