summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src/Numeric/GSL/Random.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-05-21 09:57:03 +0200
committerAlberto Ruiz <aruiz@um.es>2014-05-21 09:57:03 +0200
commite07c3dee7235496b71a89233106d93f6cc94ada1 (patch)
tree1ad29c3fc93ee076ad68e3ee759c9a3357f9cd5b /packages/hmatrix/src/Numeric/GSL/Random.hs
parent92de588b82945bb251a056c34a8ef0c00cb00e5a (diff)
Numeric.Container and Numeric.LinearAlgebra moved to base
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/Random.hs')
-rw-r--r--packages/hmatrix/src/Numeric/GSL/Random.hs84
1 files changed, 84 insertions, 0 deletions
diff --git a/packages/hmatrix/src/Numeric/GSL/Random.hs b/packages/hmatrix/src/Numeric/GSL/Random.hs
new file mode 100644
index 0000000..2872b17
--- /dev/null
+++ b/packages/hmatrix/src/Numeric/GSL/Random.hs
@@ -0,0 +1,84 @@
1-----------------------------------------------------------------------------
2-- |
3-- Module : Numeric.GSL.Random
4-- Copyright : (c) Alberto Ruiz 2009-14
5-- License : GPL
6--
7-- Maintainer : Alberto Ruiz
8-- Stability : provisional
9--
10-- Random vectors and matrices.
11--
12-----------------------------------------------------------------------------
13
14module Numeric.GSL.Random (
15 Seed,
16 RandDist(..),
17 randomVector,
18 gaussianSample,
19 uniformSample,
20 rand, randn
21) where
22
23import Numeric.GSL.Vector
24import Numeric.Container
25import Numeric.LinearAlgebra(Seed,RandDist(..),cholSH)
26import System.Random(randomIO)
27
28
29
30
31-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate
32-- Gaussian distribution.
33gaussianSample :: Seed
34 -> Int -- ^ number of rows
35 -> Vector Double -- ^ mean vector
36 -> Matrix Double -- ^ covariance matrix
37 -> Matrix Double -- ^ result
38gaussianSample seed n med cov = m where
39 c = dim med
40 meds = konst' 1 n `outer` med
41 rs = reshape c $ randomVector seed Gaussian (c * n)
42 m = rs `mXm` cholSH cov `add` meds
43
44-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate
45-- uniform distribution.
46uniformSample :: Seed
47 -> Int -- ^ number of rows
48 -> [(Double,Double)] -- ^ ranges for each column
49 -> Matrix Double -- ^ result
50uniformSample seed n rgs = m where
51 (as,bs) = unzip rgs
52 a = fromList as
53 cs = zipWith subtract as bs
54 d = dim a
55 dat = toRows $ reshape n $ randomVector seed Uniform (n*d)
56 am = konst' 1 n `outer` a
57 m = fromColumns (zipWith scale cs dat) `add` am
58
59-- | pseudorandom matrix with uniform elements between 0 and 1
60randm :: RandDist
61 -> Int -- ^ rows
62 -> Int -- ^ columns
63 -> IO (Matrix Double)
64randm d r c = do
65 seed <- randomIO
66 return (reshape c $ randomVector seed d (r*c))
67
68-- | pseudorandom matrix with uniform elements between 0 and 1
69rand :: Int -> Int -> IO (Matrix Double)
70rand = randm Uniform
71
72{- | pseudorandom matrix with normal elements
73
74>>> x <- randn 3 5
75>>> disp 3 x
763x5
770.386 -1.141 0.491 -0.510 1.512
780.069 -0.919 1.022 -0.181 0.745
790.313 -0.670 -0.097 -1.575 -0.583
80
81-}
82randn :: Int -> Int -> IO (Matrix Double)
83randn = randm Gaussian
84