{
    "name": "KL Divergence Calculator",
    "url": "https://calculators.geekmidst.com/calculators/kl-divergence-calculator",
    "markdown": "https://calculators.geekmidst.com/calculators/kl-divergence-calculator.md",
    "category": "Statistics & probability",
    "description": "Calculate the Kullback–Leibler divergence D(P‖Q) and D(Q‖P) between two discrete distributions, plus cross-entropy and Jensen–Shannon divergence.",
    "summary": "KL divergence measures how much information is lost when Q is used to approximate P. It is zero only when the distributions match, and it is not symmetric.",
    "also_called": [
        "Kullback–Leibler divergence calculator",
        "Relative entropy calculator"
    ],
    "applies_to": "Worldwide",
    "last_updated": "2026-10-29",
    "computation": "Results are computed in the visitor’s browser on the HTML page. This API describes the calculator; it does not compute results.",
    "inputs": [
        {
            "name": "p",
            "label": "Distribution P (true / observed)",
            "type": "textarea",
            "default": "0.36, 0.48, 0.16"
        },
        {
            "name": "q",
            "label": "Distribution Q (model / reference)",
            "type": "textarea",
            "default": "1, 1, 1"
        },
        {
            "name": "base",
            "label": "Logarithm base",
            "type": "segmented",
            "default": "e",
            "options": {
                "e": "Nats (ln)",
                "2": "Bits (log₂)",
                "10": "Hartleys (log₁₀)"
            }
        },
        {
            "name": "smoothing",
            "label": "Pseudo-count added to every category (optional)",
            "type": "number",
            "default": "",
            "optional": true
        }
    ],
    "outputs": [
        {
            "key": "kl_pq",
            "label": "D(P ‖ Q)",
            "format": "number"
        },
        {
            "key": "kl_qp",
            "label": "D(Q ‖ P), reverse",
            "format": "text"
        },
        {
            "key": "js",
            "label": "Jensen–Shannon divergence",
            "format": "number"
        },
        {
            "key": "cross_entropy",
            "label": "Cross-entropy H(P, Q)",
            "format": "number"
        },
        {
            "key": "entropy_p",
            "label": "Entropy H(P)",
            "format": "number"
        },
        {
            "key": "unit",
            "label": "Unit",
            "format": "text"
        }
    ],
    "formula": {
        "text": "D(P‖Q) = Σ p_i log(p_i ÷ q_i), with 0·log(0/q) = 0 and p·log(p/0) = ∞;  H(P, Q) = H(P) + D(P‖Q);  JS = ½D(P‖M) + ½D(Q‖M), M = (P + Q) ÷ 2",
        "variables": {
            "p_i, q_i": "probabilities of category i under P and Q",
            "M": "the average of P and Q"
        }
    },
    "worked_example": {
        "inputs": {
            "p": "0.36, 0.48, 0.16",
            "q": "1, 1, 1",
            "base": "e",
            "smoothing": ""
        },
        "steps": [
            "Q normalises to ⅓ each.",
            "D(P‖Q) = 0.36 ln(1.08) + 0.48 ln(1.44) + 0.16 ln(0.48) = 0.0277 + 0.1750 − 0.1174 = 0.0853 nats.",
            "D(Q‖P) = ⅓[ln(⅓ ÷ 0.36) + ln(⅓ ÷ 0.48) + ln(⅓ ÷ 0.16)] = 0.0975 nats."
        ],
        "result": "Approximating the binomial(2, 0.4) distribution by a uniform one loses 0.0853 nats; the reverse direction gives 0.0975, showing KL is not symmetric."
    },
    "interpretation": "Use D(P‖Q) with P as the data and Q as the model. Jensen–Shannon divergence is symmetric and always finite (at most ln 2 nats or 1 bit), so it is easier to compare across pairs.",
    "assumptions": [
        "Both lists cover the same categories in the same order.",
        "Zero handling: categories where P is 0 add nothing; if Q is 0 where P is not, D(P‖Q) is infinite and the calculator asks for smoothing instead of returning a number."
    ],
    "limitations": [
        "Discrete distributions only.",
        "Smoothing changes the result; report the pseudo-count you used."
    ],
    "rules": null,
    "faqs": [
        {
            "question": "Why is KL divergence not a distance?",
            "answer": "It is not symmetric and does not satisfy the triangle inequality. Its square-root-of-JS cousin is a true metric."
        },
        {
            "question": "How does it relate to cross-entropy loss?",
            "answer": "Cross-entropy H(P, Q) = H(P) + D(P‖Q). With P fixed by the data, minimising cross-entropy is the same as minimising KL divergence."
        }
    ],
    "sources": [],
    "related": [
        {
            "name": "Shannon Entropy Calculator",
            "url": "https://calculators.geekmidst.com/calculators/entropy-shannon-calculator"
        },
        {
            "name": "Information Gain Calculator",
            "url": "https://calculators.geekmidst.com/calculators/information-gain-calculator"
        },
        {
            "name": "Probability Calculator",
            "url": "https://calculators.geekmidst.com/calculators/probability-calculator"
        }
    ],
    "disclaimer": null,
    "publisher": "GM¹X CalcVerse"
}