summaryrefslogtreecommitdiffhomepage
path: root/Entropy.hs
blob: 7a62c6824721452801b00b169b25f17360490877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{-# LANGUAGE FlexibleInstances #-}

{- Copyright 2016 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module Entropy where

import Data.List
import qualified Data.ByteString.UTF8 as B

class ToChars t where
	toChars :: t -> [Char]

instance ToChars String where
	toChars = id

instance ToChars B.ByteString where
	toChars = B.toString

shannonEntropy :: ToChars s => s -> Double
shannonEntropy = sum . map lg' . fq' . map (fromIntegral.length) . group . sort . toChars
  where
	lg' c = (c * ) . logBase 2 $ 1.0 / c
	fq' c = let sc = sum c in map (/ sc) c

totalEntropy :: ToChars s => s -> Double
totalEntropy s = shannonEntropy s * fromIntegral (length (toChars s))