diff options
author | Alberto Ruiz <aruiz@um.es> | 2015-06-05 16:43:01 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2015-06-05 16:43:01 +0200 |
commit | 13856329ed09cd9f70f03363895545a6ca83374c (patch) | |
tree | 732d4b495e98e0d4914a4edae89fe8df190da881 /packages/base/src/Internal | |
parent | f8420df8e9f70c77a708a1eceef7340d300d4595 (diff) |
move random
Diffstat (limited to 'packages/base/src/Internal')
-rw-r--r-- | packages/base/src/Internal/Random.hs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/packages/base/src/Internal/Random.hs b/packages/base/src/Internal/Random.hs new file mode 100644 index 0000000..c44c272 --- /dev/null +++ b/packages/base/src/Internal/Random.hs | |||
@@ -0,0 +1,83 @@ | |||
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 Internal.Random ( | ||
14 | Seed, | ||
15 | RandDist(..), | ||
16 | randomVector, | ||
17 | gaussianSample, | ||
18 | uniformSample, | ||
19 | rand, randn | ||
20 | ) where | ||
21 | |||
22 | import Internal.Vectorized | ||
23 | import Internal.Vector | ||
24 | import Internal.Matrix | ||
25 | import Internal.Numeric | ||
26 | import Internal.Algorithms | ||
27 | import System.Random(randomIO) | ||
28 | import Data.Vector.Storable(fromList) | ||
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 | >>> disp 3 =<< randn 3 5 | ||
75 | 3x5 | ||
76 | 0.386 -1.141 0.491 -0.510 1.512 | ||
77 | 0.069 -0.919 1.022 -0.181 0.745 | ||
78 | 0.313 -0.670 -0.097 -1.575 -0.583 | ||
79 | |||
80 | -} | ||
81 | randn :: Int -> Int -> IO (Matrix Double) | ||
82 | randn = randm Gaussian | ||
83 | |||