diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Data/Packed/Random.hs | 21 | ||||
-rw-r--r-- | lib/Numeric/Container.hs | 11 |
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 | ||
14 | module Data.Packed.Random ( | 14 | module 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 | ||
22 | import Numeric.GSL.Vector | 22 | import Numeric.GSL.Vector |
@@ -25,9 +25,11 @@ import Numeric.ContainerBoot | |||
25 | import Numeric.LinearAlgebra.Algorithms | 25 | import Numeric.LinearAlgebra.Algorithms |
26 | 26 | ||
27 | 27 | ||
28 | type 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. |
30 | gaussianSample :: Int -- ^ seed | 32 | gaussianSample :: 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. |
43 | uniformSample :: Int -- ^ seed | 45 | uniformSample :: 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. | ||
59 | meanCov :: Matrix Double -> (Vector Double, Matrix Double) | ||
60 | meanCov 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. | ||
137 | meanCov :: Matrix Double -> (Vector Double, Matrix Double) | ||
138 | meanCov 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 | |||