]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Core/Methods/Similarities/Distributional.hs
[OPTIM] Distri Similarity
[gargantext.git] / src / Gargantext / Core / Methods / Similarities / Distributional.hs
1 {-|
2 Module : Gargantext.Core.Methods.Similarities.Distributional
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Motivation and definition of the @Distributional@ distance.
11 -}
12
13 {-# LANGUAGE BangPatterns #-}
14 {-# LANGUAGE Strict #-}
15
16
17 module Gargantext.Core.Methods.Similarities.Distributional
18 where
19
20 import Data.Matrix hiding (identity)
21 import qualified Data.Map as M
22 import Data.Vector (Vector)
23 import qualified Data.Vector as V
24 import Gargantext.Prelude
25 import Gargantext.Core.Viz.Graph.Utils
26
27
28 distributional' :: (Floating a, Ord a) => Matrix a -> [((Int, Int), a)]
29 distributional' m = filter (\((x,y), d) -> foldl' (&&) True (conditions x y d) ) distriList
30 where
31 conditions x y d = [ (x /= y)
32 , (d > miniMax')
33 , ((M.lookup (x,y) distriMap) > (M.lookup (y,x) distriMap))
34 ]
35 distriList = toListsWithIndex distriMatrix
36 distriMatrix = ri (mi m)
37
38 distriMap = M.fromList $ distriList
39 miniMax' = miniMax distriMatrix
40
41 ri :: (Ord a, Fractional a) => Matrix a -> Matrix a
42 ri m = matrix c r doRi
43 where
44 doRi (x,y) = doRi' x y m
45 doRi' x y mi'' = sumMin x y mi'' / (V.sum $ ax Col x y mi'')
46
47 sumMin x y mi' = V.sum $ V.map (\(a,b) -> min a b )
48 $ V.zip (ax Col x y mi') (ax Row x y mi')
49 (c,r) = (nOf Col m, nOf Row m)
50
51 mi :: (Ord a, Floating a) => Matrix a -> Matrix a
52 mi m = matrix c r createMat
53 where
54 (c,r) = (nOf Col m, nOf Row m)
55 createMat (x,y) = doMi x y m
56 doMi x y m' = if x == y then 0 else (max (log (doMi' x y m')) 0 )
57
58 doMi' x y m' = (getElem x y m) / ( cross x y m / total m' )
59
60 cross x y m' = (V.sum $ ax Col x y m) * (V.sum $ ax Row x y m')
61
62
63
64 ax :: Axis -> Int -> Int -> Matrix a -> Vector a
65 ax a i j m = dropAt j' $ axis a i' m
66 where
67 i' = div i c + 1
68 j' = mod r j + 1
69 (c,r) = (nOf Col m, nOf Row m)
70
71 miniMax :: (Ord a) => Matrix a -> a
72 miniMax m = V.minimum $ V.map (\c -> V.maximum $ getCol c m) (V.enumFromTo 1 (nOf Col m))
73
74