All formulas

Information theory

Shannon entropy

info.entropyAlso called entropy, shannon entropy, information entropy, h

H(p)=−∑i=1npiln⁡piH(p) = -\sum_{i=1}^{n} p_i \ln p_i

Expected information content of a discrete distribution, in nats. Zero for a point mass, ln n for the uniform distribution.

Notation

pp

probability vector

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

nn

number of outcomes

Size, at least 2

H(p)H(p)

Result: the entropy, in nats

scalar

Worked examples

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

  1. Given

    p=[0.5,0.5]p = [0.5, 0.5]

    Result

    H(p)=log⁡(2)\displaystyle H(p) = \log{\left(2 \right)}

    ≈ 0.69314718056

  2. Given

    p=[0.25,0.25,0.5]p = [0.25, 0.25, 0.5]

    Result

    H(p)=3log⁡(2)2\displaystyle H(p) = \frac{3 \log{\left(2 \right)}}{2}

    ≈ 1.03972077084

  3. Given

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

    Result

    H(p)=log⁡(3)\displaystyle H(p) = \log{\left(3 \right)}

    ≈ 1.09861228867

  4. Given

    p=[0.1,0.9]p = [0.1, 0.9]

    Result

    H(p)=−9log⁡(910)10+log⁡(10)10\displaystyle H(p) = - \frac{9 \log{\left(\frac{9}{10} \right)}}{10} + \frac{\log{\left(10 \right)}}{10}

    ≈ 0.325082973391

Common mistakes

  • Uses log base 2

    The result is in bits; entropy in nats is ln 2 times larger.

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

  • Uses log base 10

    The result is in hartleys (base-10 logarithm).

    Fix: Use the natural logarithm.

Sources

  • Shannon 1948

    Shannon, Claude E. A Mathematical Theory of Communication. Bell System Technical Journal 27(3): 379-423, 1948. doi:10.1002/j.1538-7305.1948.tb01338.x

    doi.org
  • Cover and Thomas 2006

    Cover, Thomas M.; Thomas, Joy A. Elements of Information Theory. 2nd ed. Wiley-Interscience, 2006. doi:10.1002/047174882X

    doi.org