Created
March 10, 2017 16:29
-
-
Save agarman/dbb3a6649497c2eaa0353ada9f6639ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn log2 [n] (/ (Math/log n) (Math/log 2))) | |
(defn entropy | |
"Calculate Shannon entropy for xs. Higher value is greater entropy. | |
Values returned between 0.0 & 8.0" | |
[xs] | |
(let [freqs (vals (frequencies xs)) | |
cnt (reduce + freqs) | |
calc #(let [p (double (/ % cnt))] | |
(* p (log2 p)))] | |
(reduce - 0 (map calc freqs)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment