summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src/Data/Packed/Random.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-05-08 08:48:12 +0200
committerAlberto Ruiz <aruiz@um.es>2014-05-08 08:48:12 +0200
commit1925c123d7d8184a1d2ddc0a413e0fd2776e1083 (patch)
treefad79f909d9c3be53d68e6ebd67202650536d387 /packages/hmatrix/src/Data/Packed/Random.hs
parenteb3f702d065a4a967bb754977233e6eec408fd1f (diff)
empty hmatrix-base
Diffstat (limited to 'packages/hmatrix/src/Data/Packed/Random.hs')
-rw-r--r--packages/hmatrix/src/Data/Packed/Random.hs57
1 files changed, 57 insertions, 0 deletions
diff --git a/packages/hmatrix/src/Data/Packed/Random.hs b/packages/hmatrix/src/Data/Packed/Random.hs
new file mode 100644
index 0000000..e8b0268
--- /dev/null
+++ b/packages/hmatrix/src/Data/Packed/Random.hs
@@ -0,0 +1,57 @@
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 Seed,
16 RandDist(..),
17 randomVector,
18 gaussianSample,
19 uniformSample
20) where
21
22import Numeric.GSL.Vector
23import Data.Packed
24import Numeric.ContainerBoot
25import Numeric.LinearAlgebra.Algorithms
26
27
28type Seed = Int
29
30-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate
31-- Gaussian distribution.
32gaussianSample :: Seed
33 -> Int -- ^ number of rows
34 -> Vector Double -- ^ mean vector
35 -> Matrix Double -- ^ covariance matrix
36 -> Matrix Double -- ^ result
37gaussianSample seed n med cov = m where
38 c = dim med
39 meds = konst' 1 n `outer` med
40 rs = reshape c $ randomVector seed Gaussian (c * n)
41 m = rs `mXm` cholSH cov `add` meds
42
43-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate
44-- uniform distribution.
45uniformSample :: Seed
46 -> Int -- ^ number of rows
47 -> [(Double,Double)] -- ^ ranges for each column
48 -> Matrix Double -- ^ result
49uniformSample seed n rgs = m where
50 (as,bs) = unzip rgs
51 a = fromList as
52 cs = zipWith subtract as bs
53 d = dim a
54 dat = toRows $ reshape n $ randomVector seed Uniform (n*d)
55 am = konst' 1 n `outer` a
56 m = fromColumns (zipWith scale cs dat) `add` am
57