summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2013-05-21 10:10:04 +0200
committerAlberto Ruiz <aruiz@um.es>2013-05-21 10:10:04 +0200
commit6b2f191ea8d6665996070bf6b3e5b4109584dcab (patch)
treeffa6780f355f0923b01505834bc486c6bcc41af3
parent4574d43ba8710b0425bcdad79744166ff3bf1b0c (diff)
meanCov moved to Container
-rw-r--r--lib/Data/Packed/Random.hs21
-rw-r--r--lib/Numeric/Container.hs11
2 files changed, 17 insertions, 15 deletions
diff --git a/lib/Data/Packed/Random.hs b/lib/Data/Packed/Random.hs
index 4b229f0..dabb17d 100644
--- a/lib/Data/Packed/Random.hs
+++ b/lib/Data/Packed/Random.hs
@@ -12,11 +12,11 @@
12----------------------------------------------------------------------------- 12-----------------------------------------------------------------------------
13 13
14module Data.Packed.Random ( 14module Data.Packed.Random (
15 Seed,
15 RandDist(..), 16 RandDist(..),
16 randomVector, 17 randomVector,
17 gaussianSample, 18 gaussianSample,
18 uniformSample, 19 uniformSample
19 meanCov,
20) where 20) where
21 21
22import Numeric.GSL.Vector 22import Numeric.GSL.Vector
@@ -25,9 +25,11 @@ import Numeric.ContainerBoot
25import Numeric.LinearAlgebra.Algorithms 25import Numeric.LinearAlgebra.Algorithms
26 26
27 27
28type Seed = Int
29
28-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate 30-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate
29-- Gaussian distribution. 31-- Gaussian distribution.
30gaussianSample :: Int -- ^ seed 32gaussianSample :: Seed
31 -> Int -- ^ number of rows 33 -> Int -- ^ number of rows
32 -> Vector Double -- ^ mean vector 34 -> Vector Double -- ^ mean vector
33 -> Matrix Double -- ^ covariance matrix 35 -> Matrix Double -- ^ covariance matrix
@@ -40,7 +42,7 @@ gaussianSample seed n med cov = m where
40 42
41-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate 43-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate
42-- uniform distribution. 44-- uniform distribution.
43uniformSample :: Int -- ^ seed 45uniformSample :: Seed
44 -> Int -- ^ number of rows 46 -> Int -- ^ number of rows
45 -> [(Double,Double)] -- ^ ranges for each column 47 -> [(Double,Double)] -- ^ ranges for each column
46 -> Matrix Double -- ^ result 48 -> Matrix Double -- ^ result
@@ -53,14 +55,3 @@ uniformSample seed n rgs = m where
53 am = konst 1 n `outer` a 55 am = konst 1 n `outer` a
54 m = fromColumns (zipWith scale cs dat) `add` am 56 m = fromColumns (zipWith scale cs dat) `add` am
55 57
56------------ utilities -------------------------------
57
58-- | Compute mean vector and covariance matrix of the rows of a matrix.
59meanCov :: Matrix Double -> (Vector Double, Matrix Double)
60meanCov x = (med,cov) where
61 r = rows x
62 k = 1 / fromIntegral r
63 med = konst k r `vXm` x
64 meds = konst 1 r `outer` med
65 xc = x `sub` meds
66 cov = scale (recip (fromIntegral (r-1))) (trans xc `mXm` xc)
diff --git a/lib/Numeric/Container.hs b/lib/Numeric/Container.hs
index 661e5b4..345c1f1 100644
--- a/lib/Numeric/Container.hs
+++ b/lib/Numeric/Container.hs
@@ -132,3 +132,14 @@ instance LSDiv Matrix Matrix where
132 (<\>) = linearSolveSVD 132 (<\>) = linearSolveSVD
133 133
134-------------------------------------------------------- 134--------------------------------------------------------
135
136-- | Compute mean vector and covariance matrix of the rows of a matrix.
137meanCov :: Matrix Double -> (Vector Double, Matrix Double)
138meanCov x = (med,cov) where
139 r = rows x
140 k = 1 / fromIntegral r
141 med = konst k r `vXm` x
142 meds = konst 1 r `outer` med
143 xc = x `sub` meds
144 cov = scale (recip (fromIntegral (r-1))) (trans xc `mXm` xc)
145