Skip to content
DeepForgeby svx
All problems
EasyInformation Theoryinfo-026

Bernoulli Entropy in Nats

Problem

Compute the entropy of a Bernoulli(p) distribution in nats:

H = -p * ln(p) - (1 - p) * ln(1 - p)

Return 0.0 for p = 0 or p = 1.

Hint

The same function in bits is just this value divided by ln(2).

Test cases

case 1
in: [0.5]
expected: 0.6931471805599453
case 2
in: [0]
expected: 0
case 3
in: [1]
expected: 0
case 4
in: [0.25]
expected: 0.5623351446188083
Starter codePython
import math
def bernoulli_entropy_nats(p):
    # Your code here
    pass

4 test cases · run your code against them in the editor