# kldivergence() Function & Examples

Use the kldivergence() probability function in Calcul.io. Review its syntax, edit working examples, understand the result, and explore related math functions.

## kldivergence

#### kldivergence(x, y)

Try it yourself:

```calculio
kldivergence([0.7,0.5,0.4], [0.2,0.9,0.5])
```

---

## 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()](https://calcul.io/function/sum/index.md) and [division](https://calcul.io/function/divide/index.md).

## Related analysis

Use [variance()](https://calcul.io/function/variance/index.md) to quantify uncertainty within one distribution, [mean()](https://calcul.io/function/mean/index.md) for summary statistics, and [log()](https://calcul.io/function/log/index.md) for the logarithmic core of the formula. Check invalid numeric input with [isFinite()](https://calcul.io/function/isFinite/index.md).

[All functions](https://calcul.io/functions/index.md)
