Published October 29, 2026 ยท Independent educational guide
Read Kullback-Leibler Divergence Through Direction, Support and Category Alignment
Read Kullback-Leibler Divergence Through Direction, Support and Category Alignment
Two probability tables can look close while describing different categories, different populations or different normalization rules. A divergence value does not repair those differences. Before asking AI to explain Kullback-Leibler divergence, identify the two distributions, the role of each and the correspondence between their categories. Then state the direction of the calculation. A number that is meaningful for the supplied labeled vectors can become misleading when a summary changes the labels, reverses the roles or silently fills an unknown count.
This guide concerns finite discrete probability distributions and uses six original fictional teaching cases. Every vector, count and identifier was deliberately authored. No model, embedding system, retrieval index, production process or client evaluation was run. Local checks reproduce supplied arithmetic under declared conventions. They do not validate an empirical probability estimate, establish factual accuracy or recommend a deployment. The aim is to make the comparison question precise enough that a reader can inspect both the result and its limits.
Identify the distribution roles before computing
Call the first distribution P and the second Q, and explain why you are comparing them in that order. In the original exercises, those are simply names for authored vectors. They are not labels for a proven true population and a measured model. A real report should likewise avoid calling P truth unless the evidence establishes that role. An observed sample proportion, an expert-supplied target and an invented scenario have different provenance even when each is stored in a probability table.
Keep the category set explicit. If Cedar and Maple are the two categories, every probability in both vectors must refer to the intended label. Equal vector length does not establish alignment. An incoming table could list Maple first, omit a category or use a different category definition. Resolve that correspondence before interpreting a divergence. Sorting numerical values independently would destroy the label relationship while still producing a calculation that looks technically complete.
Also identify the comparison population. Two distributions can come from different selected periods or task groups. A divergence then describes the supplied vectors, but it does not by itself explain whether the difference comes from changed behavior, changed membership or changed labeling. Preserve each inclusion rule. A report can state the numerical difference without inventing a cause. An AI explanation should list unresolved provenance questions rather than turn familiar category names into an assumed experimental design.
Use the declared discrete formula and zero convention
The official SciPy entropy documentation distinguishes Shannon entropy from relative entropy. For normalized positive entries, the latter uses the sum of p times log of p divided by q. The separate rel_entr documentation makes its elementwise zero and infinity conventions explicit. This guide uses base-two logarithms and reports bits. An entry with p equal to zero contributes zero under the stated convention; positive p paired with zero q gives an infinite divergence. No SciPy function is executed here.
Those conventions require interpretation at the correct level. Zero probability is a specified numerical value, while a missing value is unresolved. Replacing an unknown with zero can create an infinite result that was never established by the source. Replacing a specified zero with a small positive value can produce a finite result for a changed distribution. Neither operation should be hidden as formatting or routine cleanup. Retain the original inputs and describe any deliberate transformation separately.
The calculation is directed. Do not call it an ordinary symmetric distance merely because it compares two vectors. Swapping P and Q generally changes the value. Keep the notation P relative to Q next to the result, alongside the logarithm base and category order. A phrase such as distribution difference can be a useful heading, but it should not replace the direction and assumptions needed to reproduce the number.
Case A: a directed comparison has its own contributions
Case A supplies P as one half for Cedar and one half for Maple. Q assigns three quarters to Cedar and one quarter to Maple. In the declared P-relative-to-Q calculation, Cedar contributes one half times log base two of two thirds, approximately minus 0.292481 bits. Maple contributes one half times log base two of two, which is one half bit. Their sum is approximately 0.207519 bits. The negative first contribution and positive second contribution belong to one complete sum.
Do not report only the positive contribution or replace the negative contribution with zero. Either change would create a different calculation. A category contribution can be negative even though the full KL divergence for valid normalized distributions is nonnegative. The local example makes this visible without needing to inspect a large probability table. Show both terms when explaining the result so that a reader can see how the final sum was obtained.
The value is not 20.7519 percent error, a count of wrong predictions or a proportion of rejected work. It has the unit associated with the logarithm base in the declared formula. The example supplies no predictions or correctness reference at all. A suitable explanation names the two authored distributions and their directed comparison. It leaves actual model quality, user experience and business consequences unresolved rather than translating the scalar into an invented operational score.
Case B: reversing the distributions changes the result
Case B reverses the roles of the vectors in Case A. The first distribution now assigns three quarters and one quarter, while the second assigns equal halves. The Cedar contribution is three quarters times log base two of three halves. The Maple contribution is one quarter times log base two of one half, or minus one quarter. The total is approximately 0.188722 bits, which differs from the earlier approximately 0.207519 bits. Both calculations use the same two labeled vectors, but in different roles.
A summary that sorts the values or names the larger number as the answer without direction loses the question. Keep a small comparison table with the two role assignments if both directions are relevant. The table should not imply that the two results are competing estimates of one symmetric quantity. They are different directed calculations. Choosing which direction serves a real analysis requires a stated purpose and appropriate source data, neither of which is supplied by the fictional numbers alone.
This distinction also limits before-and-after language. A lower divergence in one directed comparison does not establish an improved model or a better business process. The target, category definition, population and task criterion matter. An AI assistant may propose a plausible story about a distribution moving closer to a goal, but that story needs the actual goal and evaluation evidence. The local arithmetic establishes no goal, training change, causal effect or recommended release threshold.
Retain normalization as a visible preparation step
A probability vector has a declared total of one. When the starting data are nonnegative counts, normalize each collection by its own positive total and show that total. Do not divide both collections by one convenient shared denominator unless that is the intended construction and its meaning is explained. The authored cases concern distributions, not a comparison of raw collection sizes. A reader should be able to recover the original counts and see what information normalization retained.
This is especially useful when the collections have unequal sizes. A large count total and a small count total can produce the same proportions. Their directed divergence can therefore be zero even though their totals differ substantially. That does not establish equal evidence, independent sampling or equal uncertainty about a broader population. Keep collection size alongside the probability comparison. A normalized summary should not erase evidence about how much data was actually included.
Avoid treating every numerical table as eligible for normalization. Negative values, unknown values and all-zero totals raise different questions. If weights represent something other than event frequency, state that role before using them to construct a distribution. Successful arithmetic does not justify the representation. A report that explains why an input is unresolved can be more useful than one that forces every table into a finite divergence value.
Case C: label alignment can change a positive result to zero
Case C supplies P in label order Cedar then Maple, with probabilities three quarters and one quarter. An incoming Q list contains one quarter followed by three quarters, but its declared labels are Maple then Cedar. Positional comparison constructs Q as if Cedar had one quarter and Maple three quarters. That constructed comparison produces one half times log base two of three, approximately 0.792481 bits. It is an arithmetic result for the positional pairing, not evidence that the intended labeled distributions differ.
Align Q using the supplied labels and it becomes three quarters for Cedar and one quarter for Maple. The aligned vectors are equal, so every p-to-q ratio on positive support is one and the divergence is zero. This is a narrow example of a supplied correspondence. The identifiers do not prove that two real categories are semantically equivalent, nor do they establish the correctness of a production join. They make the authored mapping explicit enough to reproduce.
Ask an assistant to show the labeled rows it used before interpreting a result. If it cannot explain which Q entry corresponds to each P category, leave the comparison unresolved. An apparent improvement after alignment is a correction to the constructed input relationship, not a measured improvement in a model. Preserve the unaligned and aligned versions as an explanation of what changed. That record is more informative than presenting the zero without the mapping that produced it.
Case D: zero support and smoothing are different inputs
Case D assigns P probabilities one and zero, while Q assigns zero and one. The positive P mass for the first category is paired with zero Q probability. Under the stated convention, the divergence is infinite. This is not an unusually large finite error rate. It is a consequence of the specified support mismatch in the authored vectors. An unknown Q value would not establish the same conclusion, because missing information is different from the explicitly supplied zero.
Now consider two separately labeled hypothetical changes to Q. The first replaces Q with probabilities 0.01 and 0.99; the second uses 0.001 and 0.999. With P unchanged, the resulting finite divergences are approximately 6.643856 bits and 9.965784 bits respectively. These values come from minus the base-two logarithm of the first Q probability. They do not reproduce the original zero-support comparison. They are answers for two deliberately changed distributions, neither of which is an observed model output or a recommended smoothing rule.
If a practical system uses smoothing, clipping or another transformation, preserve that rule with the report and distinguish the original from the transformed inputs. Do not adopt either invented value here as a universal library default or operating standard. A finite calculation after replacing zero does not establish that the original support problem has been resolved in the application. It establishes that a different supplied vector permits a different calculation.
Keep a distribution comparison separate from a correctness evaluation
A distribution over output categories can remain the same while every individual output is wrong under a task-specific reference. Conversely, a changed distribution may reflect a changed mix of legitimate tasks. KL divergence alone does not distinguish those situations. The supplied category probabilities contain no case-level references, error severities or accepted-work criteria. Retain those separate evidence needs when a report moves from describing distributions toward deciding what to do.
The same caution applies to a proposed monitoring threshold. The exercise supplies no historical baseline variability, alert cost or operational review process. A numerical cutoff should not be invented merely because the formula returns values with several decimal places. If an assistant drafts a monitoring plan, ask it to identify which threshold inputs are supplied and which require evidence. Keep a proposed decision separate from the observed numerical comparison until its basis is established.
For reports about AI, be explicit about the source of probabilities. A normalized count of labels assigned to recorded responses is different from a model's internal probability vector. Neither should be presented as the other. This guide accesses neither source. Its invented cases teach input and interpretation distinctions; they are not a benchmark of any model's ability to handle those distinctions. Actual evaluation would need observed outputs, an assessment rule and retained evidence.
Case E: independently scaled counts can describe unchanged proportions
Case E starts with P counts one and one and Q counts three and one. Normalizing them separately gives P equal halves and Q three quarters, one quarter, reproducing the approximately 0.207519-bit directed comparison from Case A. A second construction multiplies both P counts by ten and both Q counts by one hundred. The resulting counts ten and ten versus three hundred and one hundred have different totals, but their separately normalized probability vectors are unchanged.
The divergence is therefore unchanged as well. This establishes scale invariance for these authored common-factor count changes after the stated normalization. It does not establish that larger collections are redundant, that collection size is irrelevant to inference or that a smaller dataset is adequate. No actual sampling or uncertainty calculation is performed. Preserve the counts and totals precisely because the normalized comparison does not retain all their information.
When comparing reports, check whether normalization was performed before or after categories were filtered. Removing categories and renormalizing changes the comparison population within the category space. A value for the retained subset should be labeled as a subset result. It should not be silently substituted for the original full-vector comparison. The numerical workflow and its scope need to travel together so that a later reader can tell which question was answered.
Case F: an unknown count can affect normalization even outside positive P support
Case F supplies P counts three, one and zero, and Q counts one, one and an unknown value for the third category. P normalizes to three quarters, one quarter and zero. Q's full total is unresolved. Two of its three count fields are available, a field-availability fraction of two thirds. That fraction is not a divergence, a probability estimate for the missing category or semantic coverage. The full directed comparison remains unresolved because Q cannot yet be normalized as a complete supplied collection.
Under a hypothetical missing count of zero, Q would normalize to equal halves and zero. The directed divergence would be approximately 0.188722 bits. Under a separately hypothetical missing count of two, Q would normalize to one quarter, one quarter and one half, giving approximately 1.188722 bits. The third P category contributes zero in both calculations, but Q's changed total alters the probabilities assigned to the first two categories. A zero P contribution therefore does not make an unknown raw Q count irrelevant to normalization.
Neither completion is a prediction or an observed value. Do not choose the one that gives the more convenient result, and do not describe the two examples as a proved range over every possible completion. They show two different outcomes from two explicit assumptions. Keep the original unknown and the full unresolved state beside the hypothetical calculations. An assistant's confidence cannot supply the missing count or establish its appropriate value.
Write a review brief that can be corrected
Include the original labeled tables, population descriptions, normalization rules, direction, base and zero convention. Ask for the exact prepared vectors and individual contributions before the final interpretation. For missing entries, request an unresolved result plus any explicitly requested hypothetical scenarios. For category mismatches, request a correspondence question rather than an invented alignment. These instructions define a bounded numerical review task; they do not certify a general-purpose statistical implementation.
When a correction changes the result, record the changed field and its dependent conclusions. A label-order correction differs from a new observation, and a changed smoothing assumption differs from a revised empirical count. Keep those reasons visible in the report. The comparison becomes easier to trust when its preparation can be followed, even if the appropriate final conclusion is that the available evidence does not determine a full result.
Frequently asked questions
Is KL divergence symmetric?
Not generally. The authored equal-halves versus three-quarter/one-quarter vectors give different values when their roles reverse. State P relative to Q and the logarithm base beside the result. Do not replace the directed notation with an ordinary symmetric-distance interpretation.
Can an individual category contribution be negative?
Yes. In Case A the first contribution is negative while the complete sum is positive. Preserve every term in the stated calculation. Dropping negative contributions or replacing them with zero creates a different quantity and no longer reproduces the supplied directed divergence.
Does zero divergence prove two systems behave identically?
No. Equal supplied distributions establish equality at that representation level under the stated calculation. They do not reconstruct individual responses, category semantics, collection sizes or task correctness. The alignment example also relies on explicitly supplied labels rather than proving real-world category identity.
Why can a support mismatch produce infinity?
The declared convention assigns an infinite result when positive P probability is paired with zero Q probability. Missing Q information is different from a specified zero. Replacing zero with a small positive value changes the input distribution and should be reported as a separate transformed comparison.
Can I normalize the known counts and call that the full result?
Only a known-subset result is established when required counts remain unresolved. An unknown count can change the normalization of other Q entries, even where the corresponding P probability is zero. Keep hypothetical completions separately labeled and preserve the original unresolved full-vector state.
Do the calculations measure an actual AI improvement?
No. The cases are authored finite inputs checked with elementary arithmetic and logarithms. No model, SciPy function or production evaluation was executed. Correctness, calibration, monitoring thresholds, business value and release decisions need separate actual evidence and task definitions.
A final comparison note should also retain displayed precision. The rounded values here are explanatory displays of the authored calculations. Equality or inequality should be checked against the stated numerical rule rather than inferred from a shortened decimal string. Two displayed results can round to the same digits while differing before rounding. That observation supplies no uncertainty interval; it simply distinguishes the calculated value from its presentation and keeps a later formatting change from being mistaken for new evidence.
Original fictional practice data
Original fictional teaching cases, not actual client records, reservations, model outputs, professional review or operating approval. No executable utility is supplied.
| fictional case | authored inputs | bounded result |
|---|---|---|
| A | {"p": [0.5, 0.5], "q": [0.75, 0.25]} | Terms-0.292481/+0.5bits; D0.207519 rounded not error percentage |
| B | {"p": [0.75, 0.25], "q": [0.5, 0.5]} | Reverse D0.188722 rounded; not symmetric |
| C | {"labels": ["Cedar", "Maple"], "p": [0.75, 0.25], "incomingLabels": ["Maple", "Cedar"], "incomingQ": [0.25, 0.75]} | PositionalD0.792481 versus supplied label alignment0; no real semantic identity |
| D | {"p": [1, 0], "q": [0, 1], "hypotheticalQ1": [0.01, 0.99], "hypotheticalQ2": [0.001, 0.999]} | Positive p/zero q infinite; changed q gives6.643856/9.965784 rounded; no universal smoothing rule |
| E | {"pCounts": [1, 1], "qCounts": [3, 1], "scaledP": [10, 10], "scaledQ": [300, 100]} | Separate normalization unchanged; D0.207519 not equal evidence |
| F | {"pCounts": [3, 1, 0], "qCounts": [1, 1, null], "hypotheticalZero": [1, 1, 0], "hypotheticalTwo": [1, 1, 2]} | Q2/3 fields known full unresolved; hypotheticalD0.188722/1.188722 rounded |
Primary sources and editorial scope
- Official directed relative-entropy formula context; no function execution
- Official elementwise zero/infinity convention read October29; no SciPy execution
AI-assisted website editorial. Sources read October 29, 2026 America/New_York. Original fictional teaching inputs and planning records only; no actual ticket, reservation, visit, inquiry, artwork/material observation, experiment, route inspection, individual access assessment, professional review or institutional endorsement. Original examples checked independently. No record change, actual client dataset, observed model output, professional review, security certification or operating approval is asserted.