Skip to content

Instantly share code, notes, and snippets.

@yuanmai
Last active December 14, 2015 04:19
Show Gist options
  • Save yuanmai/5027048 to your computer and use it in GitHub Desktop.
Save yuanmai/5027048 to your computer and use it in GitHub Desktop.
Shannon Entropy
(defn shannon-entropy [col]
(let [xs (vals (frequencies col))
n (apply + xs)
ln2 (Math/log 2)]
(-
(apply +
(for [x xs]
(let [a (/ x n)]
(* a (/ (Math/log a) ln2))))))))
user> (shannon-entropy [1 2 3 4 5 6 7 8])
3.0
user> (shannon-entropy [1 2 3 4 5 6 7])
2.8073549220576046
user> (shannon-entropy [1 2 3 4 5 6 7 6 5 4 3 2 1])
2.7773627950641684
user> (shannon-entropy [1 2 3 4 5 6 7 6 5 4 3 2 1 99])
2.950212064914748
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment