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 This module aims at implementig distances of terms context by context is
11 the same referential of corpus.
14 Implementation use Accelerate library which enables GPU and CPU computation:
16 * Manuel M. T. Chakravarty, Gabriele Keller, Sean Lee, Trevor L. McDonell, and Vinod Grover.
17 [Accelerating Haskell Array Codes with Multicore GPUs][CKLM+11].
18 In _DAMP '11: Declarative Aspects of Multicore Programming_, ACM, 2011.
20 * Trevor L. McDonell, Manuel M. T. Chakravarty, Gabriele Keller, and Ben Lippmeier.
21 [Optimising Purely Functional GPU Programs][MCKL13].
22 In _ICFP '13: The 18th ACM SIGPLAN International Conference on Functional Programming_, ACM, 2013.
24 * Robert Clifton-Everest, Trevor L. McDonell, Manuel M. T. Chakravarty, and Gabriele Keller.
25 [Embedding Foreign Code][CMCK14].
26 In _PADL '14: The 16th International Symposium on Practical Aspects of Declarative Languages_, Springer-Verlag, LNCS, 2014.
28 * Trevor L. McDonell, Manuel M. T. Chakravarty, Vinod Grover, and Ryan R. Newton.
29 [Type-safe Runtime Code Generation: Accelerate to LLVM][MCGN15].
30 In _Haskell '15: The 8th ACM SIGPLAN Symposium on Haskell_, ACM, 2015.
34 {-# LANGUAGE NoImplicitPrelude #-}
35 {-# LANGUAGE FlexibleContexts #-}
36 {-# LANGUAGE TypeFamilies #-}
37 {-# LANGUAGE TypeOperators #-}
38 {-# LANGUAGE ScopedTypeVariables #-}
40 module Gargantext.Viz.Graph.Distances.Matrice
43 import Data.Array.Accelerate
44 import Data.Array.Accelerate.Interpreter (run)
46 import qualified Gargantext.Prelude as P
49 -----------------------------------------------------------------------
53 -- Vector (Z :. 3) [0,1,2]
54 vector :: Int -> (Array (Z :. Int) Int)
55 vector n = fromList (Z :. n) [0..n]
59 -- >>> matrix 3 ([1..] :: [Double])
60 -- Matrix (Z :. 3 :. 3)
64 matrix :: Elt c => Int -> [c] -> Matrix c
65 matrix n l = fromList (Z :. n :. n) l
67 -- | Two ways to get the rank (as documentation)
69 -- >>> rank (matrix 3 ([1..] :: [Int]))
71 rank :: (Matrix a) -> Int
72 rank m = arrayRank $ arrayShape m
74 -----------------------------------------------------------------------
75 -- | Dimension of a square Matrix
76 -- How to force use with SquareMatrix ?
79 -- | Get Dimension of a square Matrix
81 -- >>> dim (matrix 3 ([1..] :: [Int]))
83 dim :: Matrix a -> Dim
86 Z :. _ :. n = arrayShape m
87 -- indexTail (arrayShape m)
89 -----------------------------------------------------------------------
91 -- | Sum of a Matrix by Column
93 -- >>> run $ matSum 3 (use $ matrix 3 [1..])
94 -- Matrix (Z :. 3 :. 3)
95 -- [ 12.0, 15.0, 18.0,
98 matSum :: Dim -> Acc (Matrix Double) -> Acc (Matrix Double)
99 matSum r mat = replicate (constant (Z :. (r :: Int) :. All)) $ sum $ transpose mat
102 -- | Proba computes de probability matrix: all cells divided by thee sum of its column
103 -- if you need get the probability on the lines, just transpose it
105 -- >>> run $ matProba 3 (use $ matrix 3 [1..])
106 -- Matrix (Z :. 3 :. 3)
107 -- [ 8.333333333333333e-2, 0.13333333333333333, 0.16666666666666666,
108 -- 0.3333333333333333, 0.3333333333333333, 0.3333333333333333,
109 -- 0.5833333333333334, 0.5333333333333333, 0.5]
110 matProba :: Dim -> Acc (Matrix Double) -> Acc (Matrix Double)
111 matProba r mat = zipWith (/) mat (matSum r mat)
113 -- | Diagonal of the matrix
115 -- >>> run $ diag (use $ matrix 3 ([1..] :: [Int]))
116 -- Vector (Z :. 3) [1,5,9]
117 diag :: Elt e => Acc (Matrix e) -> Acc (Vector e)
118 diag m = backpermute (indexTail (shape m)) (lift1 (\(Z :. x) -> (Z :. x :. (x :: Exp Int)))) m
120 -- | Divide by the Diagonal of the matrix
122 -- >>> run $ divByDiag 3 (use $ matrix 3 ([1..] :: [Double]))
123 -- Matrix (Z :. 3 :. 3)
124 -- [ 1.0, 0.4, 0.3333333333333333,
125 -- 4.0, 1.0, 0.6666666666666666,
127 divByDiag :: Dim -> Acc (Matrix Double) -> Acc (Matrix Double)
128 divByDiag d mat = zipWith (/) mat (replicate (constant (Z :. (d :: Int) :. All)) $ diag mat)
130 -----------------------------------------------------------------------
131 -- | Filters the matrix with the minimum of maximums
133 -- >>> run $ matMiniMax $ use $ matrix 3 [1..]
134 -- Matrix (Z :. 3 :. 3)
138 matMiniMax :: Acc (Matrix Double) -> Acc (Matrix Double)
139 matMiniMax m = map (\x -> ifThenElse (x > miniMax') x 0) (transpose m)
141 miniMax' = (the $ minimum $ maximum m)
143 -- | Filters the matrix with a constant
145 -- >>> run $ matFilter 5 $ use $ matrix 3 [1..]
146 -- Matrix (Z :. 3 :. 3)
150 matFilter :: Double -> Acc (Matrix Double) -> Acc (Matrix Double)
151 matFilter t m = map (\x -> ifThenElse (x > (constant t)) x 0) (transpose m)
153 -----------------------------------------------------------------------
154 -- * Measures of proximity
155 -----------------------------------------------------------------------
156 -- ** Conditional distance
158 -- *** Conditional distance (basic)
160 -- | Conditional distance (basic version)
162 -- 2 main measures are actually implemented in order to compute the
163 -- proximity of two terms: conditional and distributional
165 -- Conditional measure is an absolute measure which reflects
166 -- interactions of 2 terms in the corpus.
167 measureConditional :: Matrix Int -> Matrix Double
168 --measureConditional m = run (matMiniMax $ matProba (dim m) $ map fromIntegral $ use m)
169 measureConditional m = run (matProba (dim m) $ map fromIntegral $ use m)
172 -- *** Conditional distance (advanced)
174 -- | Conditional distance (advanced version)
176 -- The conditional measure P(i|j) of 2 terms @i@ and @j@, also called
177 -- "confidence" , is the maximum probability between @i@ and @j@ to see
178 -- @i@ in the same context of @j@ knowing @j@.
180 -- If N(i) (resp. N(j)) is the number of occurrences of @i@ (resp. @j@)
181 -- in the corpus and _[n_{ij}\] the number of its occurrences we get:
183 -- \[P_c=max(\frac{n_i}{n_{ij}},\frac{n_j}{n_{ij}} )\]
184 conditional' :: Matrix Int -> (Matrix InclusionExclusion, Matrix SpecificityGenericity)
185 conditional' m = (run $ ie $ map fromIntegral $ use m, run $ sg $ map fromIntegral $ use m)
187 ie :: Acc (Matrix Double) -> Acc (Matrix Double)
188 ie mat = map (\x -> x / (2*n-1)) $ zipWith (+) (xs mat) (ys mat)
189 sg :: Acc (Matrix Double) -> Acc (Matrix Double)
190 sg mat = map (\x -> x / (2*n-1)) $ zipWith (-) (xs mat) (ys mat)
198 xs :: Acc (Matrix Double) -> Acc (Matrix Double)
199 xs mat = zipWith (-) (matSum r $ matProba r mat) (matProba r mat)
200 ys :: Acc (Matrix Double) -> Acc (Matrix Double)
201 ys mat = zipWith (-) (matSum r $ transpose $ matProba r mat) (matProba r mat)
203 -----------------------------------------------------------------------
204 -- ** Distributional Distance
206 -- | Distributional Distance Measure
208 -- Distributional measure is a relative measure which depends on the
209 -- selected list, it represents structural equivalence.
211 -- The distributional measure P(c) of @i@ and @j@ terms is: \[
212 -- S_{MI} = \frac {\sum_{k \neq i,j ; MI_{ik} >0}^{} \min(MI_{ik},
213 -- MI_{jk})}{\sum_{k \neq i,j ; MI_{ik}}^{}} \]
215 -- Mutual information
216 -- \[S_{MI}({i},{j}) = \log(\frac{C{ij}}{E{ij}})\]
218 -- Number of cooccurrences of @i@ and @j@ in the same context of text
221 -- The expected value of the cooccurrences @i@ and @j@ (given a map list of size @n@)
222 -- \[E_{ij}^{m} = \frac {S_{i} S_{j}} {N_{m}}\]
224 -- Total cooccurrences of term @i@ given a map list of size @m@
225 -- \[S_{i} = \sum_{j, j \neq i}^{m} S_{ij}\]
227 -- Total cooccurrences of terms given a map list of size @m@
228 -- \[N_{m} = \sum_{i,i \neq i}^{m} \sum_{j, j \neq j}^{m} S_{ij}\]
230 distributional :: Matrix Int -> Matrix Double
231 distributional m = run $ matMiniMax $ ri (map fromIntegral $ use m)
234 -- filter m = zipWith (\a b -> max a b) m (transpose m)
236 ri mat = zipWith (/) mat1 mat2
238 mat1 = matSum n $ zipWith min (s_mi mat) (s_mi $ transpose mat)
241 s_mi m' = zipWith (\a b -> log (a/b)) m'
242 $ zipWith (/) (crossProduct m') (total m')
244 total m'' = replicate (constant (Z :. n :. n)) $ fold (+) 0 $ fold (+) 0 m''
247 crossProduct m''' = zipWith (*) (cross m''' ) (cross (transpose m'''))
248 cross mat = zipWith (-) (matSum n mat) (mat)
250 -----------------------------------------------------------------------
251 -----------------------------------------------------------------------
252 -- * Specificity and Genericity
254 {- | Metric Specificity and genericity: select terms
256 - let N termes and occurrences of i \[N{i}\]
258 - Cooccurrences of i and j \[N{ij}\]
259 - Probability to get i given j : \[P(i|j)=N{ij}/N{j}\]
261 - Genericity of i \[Gen(i) = \frac{\sum_{j \neq i,j} P(i|j)}{N-1}\]
262 - Specificity of j \[Spec(i) = \frac{\sum_{j \neq i,j} P(j|i)}{N-1}\]
264 - \[Inclusion (i) = Gen(i) + Spec(i)\)
265 - \[GenericityScore = Gen(i)- Spec(i)\]
267 - References: Science mapping with asymmetrical paradigmatic proximity
268 Jean-Philippe Cointet (CREA, TSV), David Chavalarias (CREA) (Submitted
269 on 15 Mar 2008), Networks and Heterogeneous Media 3, 2 (2008) 267 - 276,
270 arXiv:0803.2315 [cs.OH]
272 type InclusionExclusion = Double
273 type SpecificityGenericity = Double
275 data SquareMatrix = SymetricMatrix | NonSymetricMatrix
276 type SymetricMatrix = Matrix
277 type NonSymetricMatrix = Matrix
280 incExcSpeGen :: Matrix Int -> (Vector InclusionExclusion, Vector SpecificityGenericity)
281 incExcSpeGen m = (run' inclusionExclusion m, run' specificityGenericity m)
283 run' fun mat = run $ fun $ map fromIntegral $ use mat
285 -- | Inclusion (i) = Gen(i)+Spec(i)
286 inclusionExclusion :: Acc (Matrix Double) -> Acc (Vector Double)
287 inclusionExclusion mat = zipWith (+) (pV mat) (pH mat)
289 -- | Genericity score = Gen(i)- Spec(i)
290 specificityGenericity :: Acc (Matrix Double) -> Acc (Vector Double)
291 specificityGenericity mat = zipWith (-) (pV mat) (pH mat)
293 -- | Gen(i) : 1/(N-1)*Sum(j!=i, P(i|j)) : Genericity of i
294 pV :: Acc (Matrix Double) -> Acc (Vector Double)
295 pV mat = map (\x -> (x-1)/(cardN-1)) $ sum $ p_ij mat
297 -- | Spec(i) : 1/(N-1)*Sum(j!=i, P(j|i)) : Specificity of j
298 pH :: Acc (Matrix Double) -> Acc (Vector Double)
299 pH mat = map (\x -> (x-1)/(cardN-1)) $ sum $ p_ji mat
302 cardN = constant (P.fromIntegral (dim m) :: Double)
305 -- | P(i|j) = Nij /N(jj) Probability to get i given j
306 --p_ij :: (Elt e, P.Fractional (Exp e)) => Acc (SymetricMatrix e) -> Acc (Matrix e)
307 p_ij :: (Elt e, P.Fractional (Exp e)) => Acc (Matrix e) -> Acc (Matrix e)
308 p_ij m = zipWith (/) m (n_jj m)
310 n_jj :: Elt e => Acc (SymetricMatrix e) -> Acc (Matrix e)
311 n_jj myMat' = backpermute (shape m)
312 (lift1 ( \(Z :. (_ :: Exp Int) :. (j:: Exp Int))
317 -- | P(j|i) = Nij /N(ii) Probability to get i given j
319 p_ji :: (Elt e, P.Fractional (Exp e)) => Acc (Array DIM2 e) -> Acc (Array DIM2 e)
320 p_ji = transpose . p_ij
323 -- | Step to ckeck the result in visual/qualitative tests
324 incExcSpeGen_proba :: Matrix Int -> Matrix Double
325 incExcSpeGen_proba m = run' pro m
327 run' fun mat = run $ fun $ map fromIntegral $ use mat
332 -- | Hypothesis to test maybe later (or not)
333 -- TODO ask accelerate for instances to ease such writtings:
334 p_ :: (Elt e, P.Fractional (Exp e)) => Acc (Array DIM2 e) -> Acc (Array DIM2 e)
335 p_ m = zipWith (/) m (n_ m)
337 n_ :: Elt e => Acc (SymetricMatrix e) -> Acc (Matrix e)
338 n_ m = backpermute (shape m)
339 (lift1 ( \(Z :. (i :: Exp Int) :. (j:: Exp Int))
340 -> (ifThenElse (i < j) (lift (Z :. j :. j)) (lift (Z :. i :. i)) :: Exp DIM2)
345 -- * For Tests (to be removed)
346 -- | Test perfermance with this matrix
347 -- TODO : add this in a benchmark folder
348 distriTest :: Matrix Double
349 distriTest = distributional $ matrix 100 [1..]
350 -----------------------------------------------------------------------