2 Module : Gargantext.Graph.Distances.Distributional
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Motivation and definition of the @Distributional@ distance.
13 {-# LANGUAGE BangPatterns #-}
14 {-# LANGUAGE Strict #-}
17 module Gargantext.Viz.Graph.Distances.Distributional
20 import Data.Matrix hiding (identity)
22 import qualified Data.Map as M
24 import Data.Vector (Vector)
25 import qualified Data.Vector as V
27 import Gargantext.Prelude
28 import Gargantext.Viz.Graph.Utils
31 distributional :: (Floating a, Ord a) => Matrix a -> [((Int, Int), a)]
32 distributional m = filter (\((x,y), d) -> foldl' (&&) True (conditions x y d) ) distriList
34 conditions x y d = [ (x /= y)
36 , ((M.lookup (x,y) distriMap) > (M.lookup (y,x) distriMap))
38 distriList = toListsWithIndex distriMatrix
39 distriMatrix = ri (mi m)
41 distriMap = M.fromList $ distriList
42 miniMax' = miniMax distriMatrix
44 ri :: (Ord a, Fractional a) => Matrix a -> Matrix a
45 ri m = matrix c r doRi
47 doRi (x,y) = doRi' x y m
48 doRi' x y mi'' = sumMin x y mi'' / (V.sum $ ax Col x y mi'')
50 sumMin x y mi' = V.sum $ V.map (\(a,b) -> min a b )
51 $ V.zip (ax Col x y mi') (ax Row x y mi')
52 (c,r) = (nOf Col m, nOf Row m)
55 mi :: (Ord a, Floating a) => Matrix a -> Matrix a
56 mi m = matrix c r createMat
58 (c,r) = (nOf Col m, nOf Row m)
59 createMat (x,y) = doMi x y m
60 doMi x y m' = if x == y then 0 else (max (log (doMi' x y m')) 0 )
62 doMi' x y m' = (getElem x y m) / ( cross x y m / total m' )
64 cross x y m' = (V.sum $ ax Col x y m) * (V.sum $ ax Row x y m')
68 ax :: Axis -> Int -> Int -> Matrix a -> Vector a
69 ax a i j m = dropAt j' $ axis a i' m
73 (c,r) = (nOf Col m, nOf Row m)
75 miniMax :: (Ord a) => Matrix a -> a
76 miniMax m = V.minimum $ V.map (\c -> V.maximum $ getCol c m) (V.enumFromTo 1 (nOf Col m))