Explore Library
Code QuizIntermediate

Entropy of a Distribution

Find the sign error that makes this entropy function return negative values.

Codepython
import math

def entropy(p):
    # Shannon entropy in nats
    return sum(pi * math.log(pi) for pi in p)

print(entropy([0.5, 0.5]))  # expected ~0.693

What is the bug in this entropy calculation?