Skip to content

Instantly share code, notes, and snippets.

@nilspore
nilspore / entropy.clj
Created August 30, 2022 05:29 — forked from kyptin/entropy.clj
Shannon's entropy in Clojure
(defn entropy
"Calculate the Shannon entropy, a.k.a. amount of information, in a
distribution. Uses the log-base-2 version, so the result can be thought of as
the number of bits of information. Accepts either a seq of numbers or a map of
freqencies like that returned by the `clojure.core/freqencies` function.
Ex:
(entropy [1]) ; => 0.0
(entropy [0 1 0 1]) ; => 1.0
(entropy [0 1 2 3]) ; => 2.0
(entropy {0 2, 1 2}) ; => 1.0