diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-16 12:36:52 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-16 12:36:52 +0200 |
commit | a2d99e7d0e83fcedf3a856cdb927309e28a8eddd (patch) | |
tree | c6192d21e4b59e5527aac5d25d8b8baefc2b3052 /packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs | |
parent | 5d15d765c0204854b587b03232b20336ddf91ced (diff) |
container and algorithms moved to base
Diffstat (limited to 'packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs')
-rw-r--r-- | packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs b/packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs index fb2d54c..e4f21b0 100644 --- a/packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs +++ b/packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs | |||
@@ -27,6 +27,7 @@ module Numeric.LinearAlgebra.Util( | |||
27 | unitary, | 27 | unitary, |
28 | mt, | 28 | mt, |
29 | pairwiseD2, | 29 | pairwiseD2, |
30 | meanCov, | ||
30 | rowOuters, | 31 | rowOuters, |
31 | null1, | 32 | null1, |
32 | null1sym, | 33 | null1sym, |
@@ -55,6 +56,7 @@ module Numeric.LinearAlgebra.Util( | |||
55 | ) where | 56 | ) where |
56 | 57 | ||
57 | import Numeric.Container | 58 | import Numeric.Container |
59 | import Numeric.IO | ||
58 | import Numeric.LinearAlgebra.Algorithms hiding (i) | 60 | import Numeric.LinearAlgebra.Algorithms hiding (i) |
59 | import Numeric.Matrix() | 61 | import Numeric.Matrix() |
60 | import Numeric.Vector() | 62 | import Numeric.Vector() |
@@ -196,7 +198,27 @@ size m = (rows m, cols m) | |||
196 | mt :: Matrix Double -> Matrix Double | 198 | mt :: Matrix Double -> Matrix Double |
197 | mt = trans . inv | 199 | mt = trans . inv |
198 | 200 | ||
199 | ---------------------------------------------------------------------- | 201 | -------------------------------------------------------------------------------- |
202 | |||
203 | {- | Compute mean vector and covariance matrix of the rows of a matrix. | ||
204 | |||
205 | >>> meanCov $ gaussianSample 666 1000 (fromList[4,5]) (diagl[2,3]) | ||
206 | (fromList [4.010341078059521,5.0197204699640405], | ||
207 | (2><2) | ||
208 | [ 1.9862461923890056, -1.0127225830525157e-2 | ||
209 | , -1.0127225830525157e-2, 3.0373954915729318 ]) | ||
210 | |||
211 | -} | ||
212 | meanCov :: Matrix Double -> (Vector Double, Matrix Double) | ||
213 | meanCov x = (med,cov) where | ||
214 | r = rows x | ||
215 | k = 1 / fromIntegral r | ||
216 | med = konst k r `vXm` x | ||
217 | meds = konst 1 r `outer` med | ||
218 | xc = x `sub` meds | ||
219 | cov = scale (recip (fromIntegral (r-1))) (trans xc `mXm` xc) | ||
220 | |||
221 | -------------------------------------------------------------------------------- | ||
200 | 222 | ||
201 | -- | Matrix of pairwise squared distances of row vectors | 223 | -- | Matrix of pairwise squared distances of row vectors |
202 | -- (using the matrix product trick in blog.smola.org) | 224 | -- (using the matrix product trick in blog.smola.org) |