summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES2
-rw-r--r--lib/Data/Packed/Random.hs19
-rw-r--r--lib/Numeric/LinearAlgebra/Tests.hs9
3 files changed, 26 insertions, 4 deletions
diff --git a/CHANGES b/CHANGES
index c6297eb..af90180 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,7 +1,7 @@
16.0.0.0 16.0.0.0
2======= 2=======
3 3
4- added randomVector, gaussianSample, meanCov 4- added randomVector, gaussianSample, uniformSample, meanCov
5 5
6- added rankSVD, nullspaceSVD 6- added rankSVD, nullspaceSVD
7 7
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
24import Numeric.LinearAlgebra.Instances() 25import Numeric.LinearAlgebra.Instances()
25import Numeric.LinearAlgebra.Interface 26import 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.
29gaussianSample :: Int -- ^ seed 30gaussianSample :: 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.
45uniformSample :: Int -- ^ seed
46 -> Int -- ^ number of rows
47 -> [(Double,Double)] -- ^ ranges for each column
48 -> Matrix Double -- ^ result
49uniformSample 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.
diff --git a/lib/Numeric/LinearAlgebra/Tests.hs b/lib/Numeric/LinearAlgebra/Tests.hs
index 097756e..4495396 100644
--- a/lib/Numeric/LinearAlgebra/Tests.hs
+++ b/lib/Numeric/LinearAlgebra/Tests.hs
@@ -131,7 +131,7 @@ rootFindingTest = TestList [ utest "root Hybrids" (fst sol1 ~~ [1,1])
131 131
132--------------------------------------------------------------------- 132---------------------------------------------------------------------
133 133
134randomTest = c :~1~: snd (meanCov dat) where 134randomTestGaussian = c :~1~: snd (meanCov dat) where
135 a = (3><3) [1,2,3, 135 a = (3><3) [1,2,3,
136 2,4,0, 136 2,4,0,
137 -2,2,1] 137 -2,2,1]
@@ -139,6 +139,10 @@ randomTest = c :~1~: snd (meanCov dat) where
139 c = a <> trans a 139 c = a <> trans a
140 dat = gaussianSample 7 (10^6) m c 140 dat = gaussianSample 7 (10^6) m c
141 141
142randomTestUniform = c :~1~: snd (meanCov dat) where
143 c = diag $ 3 |> map ((/12).(^2)) [1,2,3]
144 dat = uniformSample 7 (10^6) [(0,1),(1,3),(3,6)]
145
142--------------------------------------------------------------------- 146---------------------------------------------------------------------
143 147
144rot :: Double -> Matrix Double 148rot :: Double -> Matrix Double
@@ -237,7 +241,8 @@ runTests n = do
237 , utest "polySolve" (polySolveProp [1,2,3,4]) 241 , utest "polySolve" (polySolveProp [1,2,3,4])
238 , minimizationTest 242 , minimizationTest
239 , rootFindingTest 243 , rootFindingTest
240 , utest "random" randomTest 244 , utest "randomGaussian" randomTestGaussian
245 , utest "randomUniform" randomTestUniform
241 ] 246 ]
242 return () 247 return ()
243 248