kldivergence

kldivergence(x, y)

Try it yourself:


kldivergence(): compare probability distributions

kldivergence(p, q) calculates Kullback–Leibler divergence, a measure of how one probability distribution P differs from a reference distribution Q. It is widely used in information theory, Bayesian statistics, and machine learning.

Interpret the result

KL divergence is non-negative and is zero when the distributions match, but it is not symmetric: KL(P‖Q) usually differs from KL(Q‖P). It is therefore a directed discrepancy, not an ordinary distance.

Input requirements

Inputs must be compatible probability distributions: non-negative entries with totals of one. If P assigns positive probability where Q assigns zero, the divergence is infinite or undefined. Normalize counts first with sum() and division.

Related analysis

Use variance() to quantify uncertainty within one distribution, mean() for summary statistics, and log() for the logarithmic core of the formula. Check invalid numeric input with isFinite().

Try Kldivergence in Calcul.io

Start with one of the editable examples above, then replace its arguments with your own values. Keeping the function on a separate calculator line makes the input and result easy to compare. For a longer workflow, assign the result to a variable or reference that line in the next expression.

Check the shown signature before adding optional arguments, and use the related-function links to compare operations with similar purposes.

All functions