2 Module : Gargantext.Graph.Distances.Matrix
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 @Conditional@ distance.
12 Implementation use Accelerate library :
13 * Manuel M. T. Chakravarty, Gabriele Keller, Sean Lee, Trevor L. McDonell, and Vinod Grover.
14 [Accelerating Haskell Array Codes with Multicore GPUs][CKLM+11].
15 In _DAMP '11: Declarative Aspects of Multicore Programming_, ACM, 2011.
17 * Trevor L. McDonell, Manuel M. T. Chakravarty, Gabriele Keller, and Ben Lippmeier.
18 [Optimising Purely Functional GPU Programs][MCKL13].
19 In _ICFP '13: The 18th ACM SIGPLAN International Conference on Functional Programming_, ACM, 2013.
21 * Robert Clifton-Everest, Trevor L. McDonell, Manuel M. T. Chakravarty, and Gabriele Keller.
22 [Embedding Foreign Code][CMCK14].
23 In _PADL '14: The 16th International Symposium on Practical Aspects of Declarative Languages_, Springer-Verlag, LNCS, 2014.
25 * Trevor L. McDonell, Manuel M. T. Chakravarty, Vinod Grover, and Ryan R. Newton.
26 [Type-safe Runtime Code Generation: Accelerate to LLVM][MCGN15].
27 In _Haskell '15: The 8th ACM SIGPLAN Symposium on Haskell_, ACM, 2015.
31 {-# LANGUAGE NoImplicitPrelude #-}
32 {-# LANGUAGE FlexibleContexts #-}
33 {-# LANGUAGE TypeFamilies #-}
34 {-# LANGUAGE TypeOperators #-}
36 module Gargantext.Viz.Graph.Distances.Matrice
39 import Data.Array.Accelerate
40 import Data.Array.Accelerate.Interpreter (run)
41 import Data.Array.Accelerate.Smart
42 import Data.Array.Accelerate.Type
43 import Data.Array.Accelerate.Array.Sugar (fromArr, Array, Z)
45 import Data.Maybe (Maybe(Just))
46 import qualified Gargantext.Prelude as P
47 import qualified Data.Array.Accelerate.Array.Representation as Repr
49 import Gargantext.Text.Metrics.Count
52 -----------------------------------------------------------------------
54 distriTest = distributional $ myMat 100
55 -----------------------------------------------------------------------
57 vector :: Int -> (Array (Z :. Int) Int)
58 vector n = fromList (Z :. n) [0..n]
60 matrix :: Elt c => Int -> [c] -> Matrix c
61 matrix n l = fromList (Z :. n :. n) l
63 myMat :: Int -> Matrix Int
64 myMat n = matrix n [1..]
66 -- | Two ways to get the rank (as documentation)
67 rank :: (Matrix a) -> Int
68 rank m = arrayRank $ arrayShape m
70 -----------------------------------------------------------------------
71 -- | Dimension of a square Matrix
72 -- How to force use with SquareMatrix ?
75 dim :: (Matrix a) -> Dim
78 Z :. _ :. n = arrayShape m
79 -- == indexTail (arrayShape m)
81 proba :: Dim -> Acc (Matrix Double) -> Acc (Matrix Double)
82 proba r mat = zipWith (/) mat (mkSum r mat)
84 mkSum :: Dim -> Acc (Matrix Double) -> Acc (Matrix Double)
85 mkSum r mat = replicate (constant (Z :. (r :: Int) :. All)) $ sum mat
88 divByDiag :: Dim -> Acc (Matrix Double) -> Acc (Matrix Double)
89 divByDiag d mat = zipWith (/) mat (replicate (constant (Z :. (d :: Int) :. All)) $ diag mat)
91 diag :: Elt e => Acc (Matrix e) -> Acc (Vector e)
92 diag m = backpermute (indexTail (shape m)) (lift1 (\(Z :. x) -> (Z :. x :. (x :: Exp Int)))) m
94 -- | Conditional Distance
97 Metric Specificity and genericity: select terms
105 P(i|j)=Nij/Nj Probability to get i given j
107 Gen(i) : 1/(N-1)*Sum(j!=i, P(i|j)) : Genericity of i
109 Spec(i) : 1/(N-1)*Sum( j!=i, P(j|i)) : Specificity of j
111 Inclusion (i) = Gen(i)+Spec(i)
113 Genericity score = Gen(i)- Spec(i)
118 Compute genericity/specificity:
119 P(j|i) = N(ij) / N(ii)
120 P(i|j) = N(ij) / N(jj)
122 Gen(i) = sum P(i|j) | j /= i) / (N-1)
123 Spec(i) = sum P(j|i) | i /= j) / (N-1)
125 Genericity(i) = (Gen(i) - Spe(i)) / 2
126 Inclusion(i) = (Spec(i) + Gen(i)) / 2
131 data SquareMatrix = SymetricMatrix | NonSymetricMatrix
133 type SymetricMatrix = Matrix
134 type NonSymetricMatrix = Matrix
136 -- | Compute genericity/specificity:
137 ---- | P(i|j) = N(ij) / N(jj)
138 p_ij :: (Elt e, P.Fractional (Exp e)) => Acc (Array DIM2 e) -> Acc (Array DIM2 e)
139 p_ij m = zipWith (/) m (n_jj m)
141 n_jj :: Elt e => Acc (SymetricMatrix e) -> Acc (Matrix e)
142 n_jj m = backpermute (shape m)
143 (lift1 ( \(Z :. (i :: Exp Int) :. (j:: Exp Int))
144 -> ifThenElse (i < j) (Z :. j :. j) (Z :. i :. i)
148 -- P(j|i) = N(ij) / N(ii)
149 p_ji :: (Elt e, P.Fractional (Exp e)) => Acc (Array DIM2 e) -> Acc (Array DIM2 e)
150 p_ji m = zipWith (/) m (n_ii m)
152 n_ii :: Elt e => Acc (SymetricMatrix e) -> Acc (Matrix e)
153 n_ii m = backpermute (shape m)
154 (lift1 ( \(Z :. (i :: Exp Int) :. (j:: Exp Int))
160 type Matrix' a = Acc (Matrix a)
161 type InclusionExclusion = Double
162 type SpecificityGenericity = Double
165 miniMax :: Acc (Matrix Double) -> Acc (Matrix Double)
166 miniMax m = map (\x -> ifThenElse (x > miniMax') x 0) m
168 miniMax' = (the $ minimum $ maximum m)
170 -- | Conditional distance (basic version)
171 conditional :: Matrix Int -> Matrix Double
172 conditional m = run (miniMax $ proba (dim m) $ map fromIntegral $ use m)
175 -- | Conditional distance (advanced version)
176 conditional' :: Matrix Int -> (Matrix InclusionExclusion, Matrix SpecificityGenericity)
177 conditional' m = (run $ ie $ map fromIntegral $ use m, run $ sg $ map fromIntegral $ use m)
180 ie :: Matrix' Double -> Matrix' Double
181 ie mat = map (\x -> x / (2*n-1)) $ zipWith (+) (xs mat) (ys mat)
182 sg :: Acc (Matrix Double) -> Acc (Matrix Double)
183 sg mat = map (\x -> x / (2*n-1)) $ zipWith (-) (xs mat) (ys mat)
191 xs :: Matrix' Double -> Matrix' Double
192 xs mat = zipWith (-) (proba r mat) (mkSum r $ proba r mat)
193 ys :: Acc (Matrix Double) -> Acc (Matrix Double)
194 ys mat = zipWith (-) (proba r mat) (mkSum r $ transpose $ proba r mat)
196 -----------------------------------------------------------------------
198 -- | Distributional Distance
199 distributional :: Matrix Int -> Matrix Double
200 distributional m = run $ miniMax $ ri (map fromIntegral $ use m)
204 filter m = zipWith (\a b -> max a b) m (transpose m)
206 ri mat = zipWith (/) mat1 mat2
208 mat1 = mkSum n $ zipWith min (mi mat) (mi $ transpose mat)
211 mi m' = zipWith (\a b -> max (log $ a/b) 0) m'
212 $ zipWith (/) (crossProduct m') (total m')
214 total m'' = replicate (constant (Z :. n :. n)) $ fold (+) 0 $ fold (+) 0 m''
216 crossProduct m = zipWith (*) (cross m ) (cross (transpose m))
217 cross mat = zipWith (-) (mkSum n mat) (mat)
220 int2double :: Matrix Int -> Matrix Double
221 int2double m = run (map fromIntegral $ use m)
223 incExcSpeGen' :: Matrix Int -> (Vector Double, Vector Double)
224 incExcSpeGen' m = (run' ie m, run' sg m)
226 run' fun mat = run $ fun $ map fromIntegral $ use mat
228 ie :: Acc (Matrix Double) -> Acc (Vector Double)
229 ie mat = zipWith (-) (pV mat) (pH mat)
231 sg :: Acc (Matrix Double) -> Acc (Vector Double)
232 sg mat = zipWith (+) (pV mat) (pH mat)
235 n = constant (P.fromIntegral (dim m) :: Double)
237 pV :: Acc (Matrix Double) -> Acc (Vector Double)
238 pV mat = map (\x -> (x-1)/(n-1)) $ sum $ p_ij mat
240 pH :: Acc (Matrix Double) -> Acc (Vector Double)
241 pH mat = map (\x -> (x-1)/(n-1)) $ sum $ p_ji mat
244 incExcSpeGen_proba :: Matrix Int -> Matrix Double
245 incExcSpeGen_proba m = run' pro m
247 run' fun mat = run $ fun $ map fromIntegral $ use mat