Compute the histogram entropy estimation of a string
Python 3.3+, 64 bytes
import math
lambda s:sum(math.log2(len(s)/s.count(c))for c in s)
Got math.log2
from mbomb007's solution.
Jelly, 11 8 bytes
ċЀ÷Ll.S
Try it online!
APL, 18 14 bytes
+/2⍟≢÷(+/∘.=⍨)
This is an unnamed, monadic function train that accepts a string on the right and returns a real.
Like all good things in life, this uses xnor's formula. We get a matrix of booleans corresponding to the occurrences of each character in the string using ∘.=⍨
, sum this along the first axis (+/
) to get the number of occurrences of each character, divide the length of the string by each, then take log base 2 (2⍟
) and sum.
Try it here
Saved 4 bytes thanks to Dennis!