diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-16 20:10:57 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-16 20:10:57 +0200 |
commit | d4d9082a8d7d3eed6cb5f188fc3b476847dcac27 (patch) | |
tree | 0a19bc18be429454fd59c16fd46e29e1e7bae722 /packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs | |
parent | 51f4cc7b4b301142b8df73568ffaa448f9e6dd50 (diff) |
GSL.LinearAlgebra reorganized
Diffstat (limited to 'packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs')
-rw-r--r-- | packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs b/packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs new file mode 100644 index 0000000..e9ab2b7 --- /dev/null +++ b/packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs | |||
@@ -0,0 +1,84 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- | | ||
3 | -- Module : Numeric.LinearAlgebra.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 | |||
14 | module Numeric.LinearAlgebra.Random ( | ||
15 | Seed, | ||
16 | RandDist(..), | ||
17 | randomVector, | ||
18 | gaussianSample, | ||
19 | uniformSample, | ||
20 | rand, randn | ||
21 | ) where | ||
22 | |||
23 | import Numeric.GSL.LinearAlgebra | ||
24 | import Numeric.Container | ||
25 | import Numeric.LinearAlgebra.Algorithms | ||
26 | import System.Random(randomIO) | ||
27 | |||
28 | |||
29 | type Seed = Int | ||
30 | |||
31 | -- | Obtains a matrix whose rows are pseudorandom samples from a multivariate | ||
32 | -- Gaussian distribution. | ||
33 | gaussianSample :: Seed | ||
34 | -> Int -- ^ number of rows | ||
35 | -> Vector Double -- ^ mean vector | ||
36 | -> Matrix Double -- ^ covariance matrix | ||
37 | -> Matrix Double -- ^ result | ||
38 | gaussianSample 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. | ||
46 | uniformSample :: Seed | ||
47 | -> Int -- ^ number of rows | ||
48 | -> [(Double,Double)] -- ^ ranges for each column | ||
49 | -> Matrix Double -- ^ result | ||
50 | uniformSample 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 | ||
60 | randm :: RandDist | ||
61 | -> Int -- ^ rows | ||
62 | -> Int -- ^ columns | ||
63 | -> IO (Matrix Double) | ||
64 | randm 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 | ||
69 | rand :: Int -> Int -> IO (Matrix Double) | ||
70 | rand = randm Uniform | ||
71 | |||
72 | {- | pseudorandom matrix with normal elements | ||
73 | |||
74 | >>> x <- randn 3 5 | ||
75 | >>> disp 3 x | ||
76 | 3x5 | ||
77 | 0.386 -1.141 0.491 -0.510 1.512 | ||
78 | 0.069 -0.919 1.022 -0.181 0.745 | ||
79 | 0.313 -0.670 -0.097 -1.575 -0.583 | ||
80 | |||
81 | -} | ||
82 | randn :: Int -> Int -> IO (Matrix Double) | ||
83 | randn = randm Gaussian | ||
84 | |||