summaryrefslogtreecommitdiff
path: root/packages/base/src/Internal/Random.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2015-06-05 16:43:01 +0200
committerAlberto Ruiz <aruiz@um.es>2015-06-05 16:43:01 +0200
commit13856329ed09cd9f70f03363895545a6ca83374c (patch)
tree732d4b495e98e0d4914a4edae89fe8df190da881 /packages/base/src/Internal/Random.hs
parentf8420df8e9f70c77a708a1eceef7340d300d4595 (diff)
move random
Diffstat (limited to 'packages/base/src/Internal/Random.hs')
-rw-r--r--packages/base/src/Internal/Random.hs83
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
13module Internal.Random (
14 Seed,
15 RandDist(..),
16 randomVector,
17 gaussianSample,
18 uniformSample,
19 rand, randn
20) where
21
22import Internal.Vectorized
23import Internal.Vector
24import Internal.Matrix
25import Internal.Numeric
26import Internal.Algorithms
27import System.Random(randomIO)
28import Data.Vector.Storable(fromList)
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>>> disp 3 =<< randn 3 5
753x5
760.386 -1.141 0.491 -0.510 1.512
770.069 -0.919 1.022 -0.181 0.745
780.313 -0.670 -0.097 -1.575 -0.583
79
80-}
81randn :: Int -> Int -> IO (Matrix Double)
82randn = randm Gaussian
83