diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-08 08:56:43 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-08 08:56:43 +0200 |
commit | d558f5165e7e7f4daffadae1197e53618727971d (patch) | |
tree | bfdb293eca4f2e7a8ae5e7230dd244975e3c3a27 /packages/hmatrix/src/Numeric | |
parent | 1925c123d7d8184a1d2ddc0a413e0fd2776e1083 (diff) |
moved Random
Diffstat (limited to 'packages/hmatrix/src/Numeric')
-rw-r--r-- | packages/hmatrix/src/Numeric/Container.hs | 2 | ||||
-rw-r--r-- | packages/hmatrix/src/Numeric/Random.hs | 57 |
2 files changed, 58 insertions, 1 deletions
diff --git a/packages/hmatrix/src/Numeric/Container.hs b/packages/hmatrix/src/Numeric/Container.hs index 7e46147..ff649b7 100644 --- a/packages/hmatrix/src/Numeric/Container.hs +++ b/packages/hmatrix/src/Numeric/Container.hs | |||
@@ -71,7 +71,7 @@ import Numeric.Chain | |||
71 | import Numeric.IO | 71 | import Numeric.IO |
72 | import Data.Complex | 72 | import Data.Complex |
73 | import Numeric.LinearAlgebra.Algorithms(Field,linearSolveSVD) | 73 | import Numeric.LinearAlgebra.Algorithms(Field,linearSolveSVD) |
74 | import Data.Packed.Random | 74 | import Numeric.Random |
75 | 75 | ||
76 | ------------------------------------------------------------------ | 76 | ------------------------------------------------------------------ |
77 | 77 | ||
diff --git a/packages/hmatrix/src/Numeric/Random.hs b/packages/hmatrix/src/Numeric/Random.hs new file mode 100644 index 0000000..936c777 --- /dev/null +++ b/packages/hmatrix/src/Numeric/Random.hs | |||
@@ -0,0 +1,57 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- | | ||
3 | -- Module : Numeric.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.Random ( | ||
15 | Seed, | ||
16 | RandDist(..), | ||
17 | randomVector, | ||
18 | gaussianSample, | ||
19 | uniformSample | ||
20 | ) where | ||
21 | |||
22 | import Numeric.GSL.Vector | ||
23 | import Data.Packed | ||
24 | import Numeric.ContainerBoot | ||
25 | import Numeric.LinearAlgebra.Algorithms | ||
26 | |||
27 | |||
28 | type Seed = Int | ||
29 | |||
30 | -- | Obtains a matrix whose rows are pseudorandom samples from a multivariate | ||
31 | -- Gaussian distribution. | ||
32 | gaussianSample :: Seed | ||
33 | -> Int -- ^ number of rows | ||
34 | -> Vector Double -- ^ mean vector | ||
35 | -> Matrix Double -- ^ covariance matrix | ||
36 | -> Matrix Double -- ^ result | ||
37 | gaussianSample seed n med cov = m where | ||
38 | c = dim med | ||
39 | meds = konst' 1 n `outer` med | ||
40 | rs = reshape c $ randomVector seed Gaussian (c * n) | ||
41 | m = rs `mXm` cholSH cov `add` meds | ||
42 | |||
43 | -- | Obtains a matrix whose rows are pseudorandom samples from a multivariate | ||
44 | -- uniform distribution. | ||
45 | uniformSample :: Seed | ||
46 | -> Int -- ^ number of rows | ||
47 | -> [(Double,Double)] -- ^ ranges for each column | ||
48 | -> Matrix Double -- ^ result | ||
49 | uniformSample seed n rgs = m where | ||
50 | (as,bs) = unzip rgs | ||
51 | a = fromList as | ||
52 | cs = zipWith subtract as bs | ||
53 | d = dim a | ||
54 | dat = toRows $ reshape n $ randomVector seed Uniform (n*d) | ||
55 | am = konst' 1 n `outer` a | ||
56 | m = fromColumns (zipWith scale cs dat) `add` am | ||
57 | |||