Which distribution weights the comparison?
Understand the calculation
- Choose P as reference and Q as comparison model. Direction is part of the definition.
- For each event compare pi with qi through log2(pi/qi).
- Weight by pi, not qi. Zero mass in P contributes zero; pi>0,qi=0 gives infinite divergence.
- Sum over the common event set. The information inequality gives D≥0, with equality only for identical distributions.
The calculator’s two-outcome model[1]
Visualize the learning idea
Swapping changes the curve
Free with a Quanta account. Your download stays ready after registration.
Two fully checked examples
Same events, different direction
- Given
- P=(3/4,1/4),Q=(1/2,1/2), logarithm base 2.
- Find
- Calculate both directed divergences.
Weight the first direction by P.
After swapping, both weights and ratios change.
Result: Both values are nonnegative but different. KL is not a symmetric distance.
Check: H(P)≈0.8112781245; cross entropy against uniform Q is 1 bit. Their difference is 0.1887218755 bits.
A certain reference event
- Given
- P=(1,0),Q=(1/4,3/4).
- Find
- Handle a zero term and finite divergence.
The second event has no mass in P and contributes zero.
Check what would happen with q=0 instead of q=1/4.
Result: For Q=(1/4,3/4), divergence is 2 bits. A model assigning zero probability to the certain reference event has infinite divergence.
Check: −log2(1/4)=2 verifies the result. Treat the second term by its limiting convention, not a numerical logarithm of zero.
Recall the method later with the formula’s flashcards.
10 flashcards · English materials
Already have an account? Sign in for flashcardsAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Interpret the quantities
Shannon entropy and cross entropy need the same base for H(P,Q)−H(P)=D(P||Q). Mutual information is a particular KL comparison: joint distribution against product marginals.
Five solved exercises
Calculate · Foundation
P=Q=(0.4,0.6): calculate D(P||Q).
Show hint
Each ratio equals one.
Learning goal: Recognize identical distributions.
Show solution and calculation
D=0.4·log21+0.6·log21=0 bits.
Rearrange · Application
P=(1,0) and D(P||Q)=3 bits. Find q for Q=(q,1−q).
Show hint
Then D=−log2q.
Learning goal: Rearrange a logarithm into model probability.
Show solution and calculation
q=2−3=1/8.
Check: log28=3.
Interpret · Foundation
Can one term be negative although KL is nonnegative?
Show hint
Consider an event with pi<qi.
Learning goal: Do not transfer a global property to every term.
Show solution and calculation
Yes.
Then log(pi/qi)<0.
Nonnegativity applies to the correctly weighted total for valid distributions, not each local contribution.
Error analysis · Application
Someone claims D(P||Q)=D(Q||P) universally.
Show hint
Compare the first worked example.
Learning goal: Check direction and metric properties.
Show solution and calculation
False: the first example gives about 0.188722 and 0.207519 bits.
A symmetric alternative needs a separately specified definition.
Transfer · Challenge
P=(0.2,0.8),Q=(0,1): may q be replaced by a tiny positive value without changing the model?
Show hint
P assigns positive mass to an event excluded by Q.
Learning goal: Do not silently smooth support mismatch.
Show solution and calculation
No.
Original divergence is infinite.
Smoothing creates a different model with a finite divergence depending on the smoothing choice.
Check your understanding with the formula’s reference test.
8 test questions · English materials
Already have an account? Sign in for the testAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Limits and connections
KL is generally neither symmetric nor a metric. The calculator does not smooth zeros or normalize arbitrary vectors. A finite Bernoulli comparison does not assess whether a chosen distribution fits observations or the intended application.
Sources for this explanation
Quanta · Updated November 9, 2026
- SciPy scipy.stats.entropy: relative entropy and logarithm base
- MIT 6.441 Chapter 2: Information Inequality
Editorial responsibility: Quanta Study editorial team
Review scope
This explanation, 2 worked examples and 5 exercises are available in German and English. Structure, source references and mathematical notation undergo automated checks. Calculations, model assumptions and both language versions receive a separate AI review.
An independent, named human subject review has not yet been documented. Our exercise numbers are worked examples, rather than measurements published by the cited sources.
Editorial responsibility, sources and methodsChange history
- Expanded directed Bernoulli comparison in bits, zero cases and an original asymmetry plot.
Cite this reference page
Quanta. Kullback-Leibler divergence (relative entropy). Updated November 9, 2026. https://quanta-study.de/en/formula-sheet/kullback-leibler-divergenz
When citing a specific scientific statement, also cite the specialist source linked to that statement.