nlp-scores 0.6.2 → 0.7.0
raw patch · 2 files changed
+34/−4 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ NLP.Scores: countTotal :: Counts a k -> Count
+ NLP.Scores: logLikelihoodRatio :: (Ord a, Ord b) => Counts a b -> a -> b -> Double
Files
- NLP/Scores.hs +33/−3
- nlp-scores.cabal +1/−1
NLP/Scores.hs view
@@ -27,6 +27,8 @@ , ari , mi , vi+ -- * Strength of association+ , logLikelihoodRatio -- * Comparing probability distributions , kullbackLeibler , jensenShannon@@ -43,7 +45,8 @@ , countJoint , countFst , countSnd- -- * Extracting lists of values from 'Counts'+ , countTotal+ -- * Extracting lists of values from 'Counts' , fstElems , sndElems )@@ -115,6 +118,25 @@ vi cs@(Counts _ cx cy) = entropy (elems cx) + entropy (elems cy) - 2 * mi cs where elems = Map.elems ++-- | Log-likelihood ratio for two binomial distributions.+-- H_0: P(x|y) = p = P(x|~y)+-- H_1: P(x|y) = p1 =/= p2 = P(x|~y)+logLikelihoodRatio :: (Ord a, Ord b) => Counts a b -> a -> b -> Double+logLikelihoodRatio cs x y =+ let p = nx / n -- relative count of x+ p1 = nxy / ny -- relative count of xy among _y+ p2 = (nx - nxy) / (n - ny) -- relative count of xnoty among noty+ n = countTotal cs+ nx = countFst x cs+ ny = countSnd y cs+ nxy = countJoint x y cs+ b k n p = p**k * (1-p)**(n-k)+ {-# INLINE b #-}+ in log (b nxy nx p) + log (b (nx - nxy) (n - ny) p)+ - log (b nxy nx p1) - log (b (nx - nxy) (n - ny) p2)++ -- | Kullback-Leibler divergence: KL(X,Y) = SUM_i P(X=i) log_2(P(X=i)\/P(Y=i)). -- The distributions can be unnormalized. @@ -130,8 +152,10 @@ -- | Jensen-Shannon divergence: JS(X,Y) = 1\/2 KL(X,(X+Y)\/2) + 1\/2 KL(Y,(X+Y)\/2). -- The distributions can be unnormalized. jensenShannon :: (Eq a, Floating a, T.Traversable t, T.Traversable u) => t a -> u a -> a-jensenShannon xs ys = 0.5 * kullbackLeibler xs zs + 0.5 * kullbackLeibler ys zs- where zs = zipWithTF (+) xs ys+jensenShannon xs ys = 0.5 * kullbackLeibler xs' zs + 0.5 * kullbackLeibler ys' zs+ where zs = zipWithTF (+) xs' ys' + xs' = normalize xs+ ys' = normalize ys -- | Adjusted Rand Index: <http://en.wikipedia.org/wiki/Rand_index> ari :: (Ord a, Ord b) => Counts a b -> Double@@ -204,6 +228,9 @@ -- | Count of second element countSnd :: Ord k => k -> Counts a k -> Count countSnd y = Map.findWithDefault 0 y . marginalSnd+-- | Total element count+countTotal :: Counts a k -> Count+countTotal = F.sum . joint -- | List of values of first element fstElems :: Counts k b -> [k]@@ -219,3 +246,6 @@ zipWithTF h t f = snd . T.mapAccumL map_one (F.toList f) $ t where map_one (x:xs) y = (xs, h y x) +-- | @normalize xs@ divides each element of xs by the sum of xs.+normalize :: (Fractional b, Functor f, F.Foldable f) => f b -> f b +normalize xs = let s = sum xs in fmap (/s) xs
nlp-scores.cabal view
@@ -7,7 +7,7 @@ -- The package version. See the Haskell package versioning policy -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for -- standards guiding when and how versions should be incremented.-Version: 0.6.2+Version: 0.7.0 -- A short (one-line) description of the package. Synopsis: Scoring functions commonly used for evaluation in NLP and IR