All problems
Entropy of a Zipf Source
Problem
Compute the entropy in bits of a Zipf distribution over n symbols, where the probability of rank i is proportional to 1/i. Return 0.0 for n <= 0.
Hint
Build the unnormalized weights 1/i, normalize by their sum, then take the Shannon entropy.
Test cases
case 1
in: [1]
expected: 0
case 2
in: [2]
expected: 0.9182958340544896
case 3
in: [4]
expected: 1.7924876891689534
case 4
in: [10]
expected: 2.876453681363911
case 5
in: [0]
expected: 0
Starter codePython
import math
def zipf_entropy(n):
# Your code here
pass5 test cases · run your code against them in the editor