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