diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-21 10:30:55 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-21 10:30:55 +0200 |
commit | 197e88c3b56d28840217010a2871c6ea3a4dd1a4 (patch) | |
tree | 825be9d6c9d87d23f7e5497c0133d11d52c63535 /packages/gsl/src/Numeric/GSL/Random.hs | |
parent | e07c3dee7235496b71a89233106d93f6cc94ada1 (diff) |
update dependencies, move examples etc
Diffstat (limited to 'packages/gsl/src/Numeric/GSL/Random.hs')
-rw-r--r-- | packages/gsl/src/Numeric/GSL/Random.hs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/packages/gsl/src/Numeric/GSL/Random.hs b/packages/gsl/src/Numeric/GSL/Random.hs new file mode 100644 index 0000000..2872b17 --- /dev/null +++ b/packages/gsl/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 | |||
14 | module Numeric.GSL.Random ( | ||
15 | Seed, | ||
16 | RandDist(..), | ||
17 | randomVector, | ||
18 | gaussianSample, | ||
19 | uniformSample, | ||
20 | rand, randn | ||
21 | ) where | ||
22 | |||
23 | import Numeric.GSL.Vector | ||
24 | import Numeric.Container | ||
25 | import Numeric.LinearAlgebra(Seed,RandDist(..),cholSH) | ||
26 | import System.Random(randomIO) | ||
27 | |||
28 | |||
29 | |||
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 | |||