All formulas

Information theory

Jensen-Shannon divergence

info.js-divergenceAlso called js divergence, jensen shannon, jsd, js div

JS(p,q)=12∑i=1npiln⁡pimi+12∑i=1nqiln⁡qimi\mathrm{JS}(p, q) = \frac{1}{2}\sum_{i=1}^{n} p_i \ln\frac{p_i}{m_i} + \frac{1}{2}\sum_{i=1}^{n} q_i \ln\frac{q_i}{m_i}

wheremi=pi+qi2m_i = \frac{p_i + q_i}{2}

Symmetric divergence: the mean KL divergence of p and q from their midpoint m, in nats. It is bounded by ln 2.

Notation

pp

probability vector

p ∈ Δn: pi > 0, ∑pi = 1

qq

second probability vector

q ∈ Δn: qi > 0, ∑qi = 1

nn

number of outcomes

Size, at least 2

JS\mathrm{JS}

Result: the divergence, in nats

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    p=[0.25,0.75],q=[0.25,0.75]p = [0.25, 0.75], q = [0.25, 0.75]

    Result

    JS=0\displaystyle \mathrm{JS} = 0
  2. Given

    p=[0.25,0.75],q=[0.75,0.25]p = [0.25, 0.75], q = [0.75, 0.25]

    Result

    JS=−log⁡(2)4+3log⁡(32)4\displaystyle \mathrm{JS} = - \frac{\log{\left(2 \right)}}{4} + \frac{3 \log{\left(\frac{3}{2} \right)}}{4}

    ≈ 0.130812035941

  3. Given

    p=[1/3,2/3],q=[2/3,1/3]p = [1/3, 2/3], q = [2/3, 1/3]

    Result

    JS=log⁡(23)3+2log⁡(43)3\displaystyle \mathrm{JS} = \frac{\log{\left(\frac{2}{3} \right)}}{3} + \frac{2 \log{\left(\frac{4}{3} \right)}}{3}

    ≈ 0.0566330122651

  4. Given

    p=[0.5,0.5],q=[0.25,0.75]p = [0.5, 0.5], q = [0.25, 0.75]

    Result

    JS=log⁡(45)4+log⁡(23)8+3log⁡(65)8+log⁡(43)4\displaystyle \mathrm{JS} = \frac{\log{\left(\frac{4}{5} \right)}}{4} + \frac{\log{\left(\frac{2}{3} \right)}}{8} + \frac{3 \log{\left(\frac{6}{5} \right)}}{8} + \frac{\log{\left(\frac{4}{3} \right)}}{4}

    ≈ 0.0338220755686

Common mistakes

  • Returns the Jensen-Shannon distance

    The result is the square root of the divergence, which is what scipy.spatial.distance.jensenshannon returns.

    Fix: Square the distance, or compute the divergence directly.

  • Uses log base 2

    The result is in bits.

    Fix: Use the natural logarithm, or multiply by ln 2.

Source

  • Lin 1991

    Lin, Jianhua. Divergence measures based on the Shannon entropy. IEEE Transactions on Information Theory 37(1): 145-151, 1991. doi:10.1109/18.61115

    doi.org