summaryrefslogtreecommitdiff
path: root/lib/Data
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2009-11-14 12:57:52 +0000
committerAlberto Ruiz <aruiz@um.es>2009-11-14 12:57:52 +0000
commitdc39c11d2d2e75428e6394cf8542c8c3ff2cd887 (patch)
tree24c5bac27258b5752691316e4d6736a5e0c1d2e5 /lib/Data
parent6bdf5355a26da547b775f29926c131d539e86e7c (diff)
added module Random.hs
Diffstat (limited to 'lib/Data')
-rw-r--r--lib/Data/Packed/Random.hs52
-rw-r--r--lib/Data/Packed/Vector.hs3
2 files changed, 53 insertions, 2 deletions
diff --git a/lib/Data/Packed/Random.hs b/lib/Data/Packed/Random.hs
new file mode 100644
index 0000000..56d038e
--- /dev/null
+++ b/lib/Data/Packed/Random.hs
@@ -0,0 +1,52 @@
1-----------------------------------------------------------------------------
2-- |
3-- Module : Data.Packed.Vector
4-- Copyright : (c) Alberto Ruiz 2009
5-- License : GPL
6--
7-- Maintainer : Alberto Ruiz <aruiz@um.es>
8-- Stability : provisional
9--
10-- Random vectors and matrices.
11--
12-----------------------------------------------------------------------------
13
14module Data.Packed.Random (
15 RandDist(..),
16 randomVector,
17 gaussianSample,
18 meanCov,
19) where
20
21import Numeric.GSL.Vector
22import Data.Packed
23import Numeric.LinearAlgebra.Algorithms
24import Numeric.LinearAlgebra.Instances()
25import Numeric.LinearAlgebra.Interface
26
27-- | Obtains a matrix whose rows are pseudorandom samples from a multivariante
28-- Gaussian distribution.
29gaussianSample :: Int -- ^ seed
30 -> Int -- ^ number of rows
31 -> Vector Double -- ^ mean vector
32 -> Matrix Double -- ^ covariance matrix
33 -> Matrix Double -- ^ result
34gaussianSample seed n med cov = m where
35 (l,v) = eigSH' cov
36 c = dim l
37 meds = constant 1 n `outer` med
38 rs = reshape c $ randomVector seed Gaussian (c * n)
39 ds = sqrt (abs l)
40 m = rs <> (diag ds <> trans v) + meds
41
42------------ utilities -------------------------------
43
44-- | Compute mean vector and covariance matrix of the rows of a matrix.
45meanCov :: Matrix Double -> (Vector Double, Matrix Double)
46meanCov x = (med,cov) where
47 r = rows x
48 k = 1 / fromIntegral r
49 med = constant k r <> x
50 meds = constant 1 r `outer` med
51 xc = x - meds
52 cov = (trans xc <> xc) / fromIntegral (r-1)
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs
index 836f11a..21f51e5 100644
--- a/lib/Data/Packed/Vector.hs
+++ b/lib/Data/Packed/Vector.hs
@@ -21,8 +21,7 @@ module Data.Packed.Vector (
21 vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, 21 vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex,
22 mapVector, zipVector, 22 mapVector, zipVector,
23 fscanfVector, fprintfVector, freadVector, fwriteVector, 23 fscanfVector, fprintfVector, freadVector, fwriteVector,
24 foldLoop, foldVector, foldVectorG, 24 foldLoop, foldVector, foldVectorG
25 RandDist(..), randomVector
26) where 25) where
27 26
28import Data.Packed.Internal 27import Data.Packed.Internal