summaryrefslogtreecommitdiff
path: root/lib/Data
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data')
-rw-r--r--lib/Data/Packed/Random.hs21
1 files changed, 6 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)