diff options
Diffstat (limited to 'lib/Data/Packed/Random.hs')
-rw-r--r-- | lib/Data/Packed/Random.hs | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/Data/Packed/Random.hs b/lib/Data/Packed/Random.hs index 56d038e..a6145be 100644 --- a/lib/Data/Packed/Random.hs +++ b/lib/Data/Packed/Random.hs | |||
@@ -15,6 +15,7 @@ module Data.Packed.Random ( | |||
15 | RandDist(..), | 15 | RandDist(..), |
16 | randomVector, | 16 | randomVector, |
17 | gaussianSample, | 17 | gaussianSample, |
18 | uniformSample, | ||
18 | meanCov, | 19 | meanCov, |
19 | ) where | 20 | ) where |
20 | 21 | ||
@@ -24,7 +25,7 @@ import Numeric.LinearAlgebra.Algorithms | |||
24 | import Numeric.LinearAlgebra.Instances() | 25 | import Numeric.LinearAlgebra.Instances() |
25 | import Numeric.LinearAlgebra.Interface | 26 | import Numeric.LinearAlgebra.Interface |
26 | 27 | ||
27 | -- | Obtains a matrix whose rows are pseudorandom samples from a multivariante | 28 | -- | Obtains a matrix whose rows are pseudorandom samples from a multivariate |
28 | -- Gaussian distribution. | 29 | -- Gaussian distribution. |
29 | gaussianSample :: Int -- ^ seed | 30 | gaussianSample :: Int -- ^ seed |
30 | -> Int -- ^ number of rows | 31 | -> Int -- ^ number of rows |
@@ -39,6 +40,22 @@ gaussianSample seed n med cov = m where | |||
39 | ds = sqrt (abs l) | 40 | ds = sqrt (abs l) |
40 | m = rs <> (diag ds <> trans v) + meds | 41 | m = rs <> (diag ds <> trans v) + meds |
41 | 42 | ||
43 | -- | Obtains a matrix whose rows are pseudorandom samples from a multivariate | ||
44 | -- uniform distribution. | ||
45 | uniformSample :: Int -- ^ 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 | b = fromList bs | ||
53 | cs = zipWith subtract as bs | ||
54 | d = dim a | ||
55 | dat = toRows $ reshape n $ randomVector seed Uniform (n*d) | ||
56 | am = constant 1 n `outer` a | ||
57 | m = fromColumns (zipWith (.*) cs dat) + am | ||
58 | |||
42 | ------------ utilities ------------------------------- | 59 | ------------ utilities ------------------------------- |
43 | 60 | ||
44 | -- | Compute mean vector and covariance matrix of the rows of a matrix. | 61 | -- | Compute mean vector and covariance matrix of the rows of a matrix. |