Skip to content

Instantly share code, notes, and snippets.

@danbst
Created February 8, 2016 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danbst/65bd624d31fcf1449a79 to your computer and use it in GitHub Desktop.
Save danbst/65bd624d31fcf1449a79 to your computer and use it in GitHub Desktop.
Entropy of a list
import Data.Char
import Data.Map (Map)
import qualified Data.Map as Map
entropy :: (Ord a) => [a] -> Float
entropy dta =
let freqmap = Map.fromListWith (+) $ zip dta (repeat 1)
modifier = 1.0 / fromIntegral (length dta)
freqs = Map.map (\v -> fromIntegral v * modifier) freqmap
log2 n = log n / log 2.0
in Map.fold (\p acc -> acc - p * log2 p) 0.0 freqs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment