JB Value Calculator Formula Explained
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
The JB value calculator works out the Jensen–Bregman (JB) divergence between two probability distributions, a smoothed and symmetrized measure of how different they are. You enter two sets of values (or two probability vectors) and the calculator returns a single non-negative number: 0 means the distributions are identical, and larger values mean greater divergence. The formula below is the standard JB divergence, which is always finite and symmetric, making it a popular alternative to the Kullback–Leibler divergence in machine learning and image processing.
Formula
The Jensen–Bregman divergence is defined as:
JB(P, Q) = ½ · Σ pi · ln(2·pi / (pi + qi)) + ½ · Σ qi · ln(2·qi / (pi + qi))
Where:
- P = (p1, p2, …, pn) – first probability distribution, each pi ≥ 0 and Σ pi = 1
- Q = (q1, q2, …, qn) – second probability distribution, each qi ≥ 0 and Σ qi = 1
- ln – natural logarithm (base e)
- n – number of elements (must be the same for both lists)
The result is a non-negative number measured in nats (natural units of information). If you prefer bits, divide by ln(2).
Worked Example
Using the default values from the calculator: P = (0.1, 0.2, 0.3, 0.4) and Q = (0.4, 0.3, 0.2, 0.1).
- Compute pi + qi for each i: 0.5, 0.5, 0.5, 0.5
- Compute 2·pi/(pi+qi): 0.4, 0.8, 1.2, 1.6
- Compute pi · ln(that): 0.1·ln(0.4) + 0.2·ln(0.8) + 0.3·ln(1.2) + 0.4·ln(1.6) ≈ -0.0916 – 0.0446 + 0.0547 + 0.1880 = 0.1065
- Similarly for qi · ln(2·qi/(pi+qi)): 0.4·ln(1.6) + 0.3·ln(1.2) + 0.2·ln(0.8) + 0.1·ln(0.4) ≈ 0.1880 + 0.0547 – 0.0446 – 0.0916 = 0.1065
- Sum the two halves and multiply by ½: ½·(0.1065 + 0.1065) = 0.1065
The calculator returns 0.1065 (rounded to 4 decimals). Because the two distributions are mirror images, the two KL terms are equal, which is not generally the case.
When to Use the JB Divergence
The JB divergence is useful whenever you need a symmetric, finite measure of difference between two probability distributions. Common applications include:
- Comparing histograms in image processing (e.g., color or gradient histograms)
- Measuring the distance between topic distributions in natural language processing
- Evaluating how well a trained model’s output distribution matches a target distribution
- Clustering or classification tasks where you need a distance between probability vectors
Unlike the Kullback–Leibler divergence, JB is symmetric (JB(P,Q) = JB(Q,P)) and always finite, even when some qi = 0 (as long as pi = 0 whenever qi = 0). This makes it more convenient for many practical tasks.
How to Read the Result
The JB value is always ≥ 0. A value of 0 means the two distributions are identical. There is no universal “large” or “small” threshold – it depends on your data and the number of bins. In general, values closer to 0 indicate more similarity. To compare across different numbers of elements, you can normalize by dividing by ln(2) (to get bits) or by the maximum possible JB divergence for that n (which occurs when one distribution is a point mass and the other is uniform, but that maximum depends on n).
For the default example, 0.1065 nats indicates a moderate difference – the distributions are not identical but not completely opposite either.
Common Mistakes and Pitfalls
- Not normalizing the inputs: The formula assumes P and Q are probability distributions (sum to 1). If you enter raw counts or scores, the result is meaningless. Always divide by the sum first.
- Mismatched lengths: The two lists must have the same number of elements. If they don’t, the calculator will return an error or you must pad with zeros.
- Zero handling: If pi = 0 and qi = 0, the term 0·ln(0) is defined as 0. If pi > 0 and qi = 0, the term is infinite – the JB divergence is not defined for that case. Ensure your distributions have the same support.
- Confusing nats and bits: The calculator returns nats. To get bits, divide by ln(2) ≈ 0.6931. Some texts use log base 2, which changes the numerical value.
Limitations of the JB Divergence
While JB is symmetric and finite, it is not a true metric – it does not satisfy the triangle inequality. That means you cannot use it directly for algorithms that require a metric (e.g., some nearest-neighbor searches). Also, because it is based on the arithmetic mean of the two distributions, it can be less sensitive to differences in the tails than other divergences. For very high-dimensional distributions, the value tends to grow with the number of dimensions, so comparing across different dimensions requires care.
If you need a true metric, consider the Jensen–Shannon distance (the square root of the Jensen–Shannon divergence, which is a metric). If you need to handle zeros more gracefully, the Hellinger distance is another option.
Frequently Asked Questions
What is the difference between JB divergence and KL divergence?
The Kullback–Leibler divergence is not symmetric (KL(P||Q) ≠ KL(Q||P)) and can be infinite if Q has zeros where P does not. The JB divergence is symmetric and always finite when both distributions have the same support. JB can be seen as the average of the two KL divergences (KL(P||M) + KL(Q||M)) where M is the arithmetic mean of P and Q, but the JB formula is a slightly different smoothed version.
Can I use JB divergence for non-probability data?
Technically, the formula works for any non-negative vectors, but the result is only meaningful as a divergence if the vectors are normalized to sum to 1. If you have counts or scores, normalize first. If your data contains negative values, the logarithm is undefined – use a different measure.
What does a JB value of 0 mean?
It means the two distributions are identical – every pi equals qi. If you get 0, your inputs are exactly the same (after normalization).
How do I interpret a JB value like 0.5?
There is no absolute scale. A value of 0.5 nats is relatively large for a 4-bin distribution – it suggests a substantial difference. For a 100-bin distribution, 0.5 might be moderate. Compare JB values only when the number of bins is the same.
Why does the calculator output three values?
The first output is the JB divergence itself. The other two are the one-sided KL divergences (KL(P||Q) and KL(Q||P)), which are shown for reference. They help you see how asymmetric the difference is – if they are very different, the two distributions differ in a one-sided way.
What if my distributions have different lengths?
The JB divergence requires equal-length vectors. If one is shorter, pad it with zeros to match the length of the other, but be aware that adding zeros changes the distribution – you should renormalize after padding. Alternatively, use a different method that handles variable-length data, such as a kernel-based divergence.
Related
Formula used
in p | Distribution P (comma-separated) | - | 0.1, 0.2, 0.3, 0.4 | 0 in q | Distribution Q (comma-separated) | - | 0.4, 0.3, 0.2, 0.1 | 0 out jb | JB divergence | - | 0.5 * (sum(p * ln(2*p/(p+q))) + sum(q * ln(2*q/(p+q)))) | 4 out kl_pq | KL(P||Q) | - | sum(p * ln(p/q)) | 4 out kl_qp | KL(Q||P) | - | sum(q * ln(q/p)) | 4
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.