From ca67378bef1b0e4e76b4421786a846956c47f380 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Sun, 15 Nov 2009 17:42:50 +0000 Subject: uniformSample --- CHANGES | 2 +- lib/Data/Packed/Random.hs | 19 ++++++++++++++++++- lib/Numeric/LinearAlgebra/Tests.hs | 9 +++++++-- 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 @@ 6.0.0.0 ======= -- added randomVector, gaussianSample, meanCov +- added randomVector, gaussianSample, uniformSample, meanCov - added rankSVD, nullspaceSVD 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 ( RandDist(..), randomVector, gaussianSample, + uniformSample, meanCov, ) where @@ -24,7 +25,7 @@ import Numeric.LinearAlgebra.Algorithms import Numeric.LinearAlgebra.Instances() import Numeric.LinearAlgebra.Interface --- | Obtains a matrix whose rows are pseudorandom samples from a multivariante +-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate -- Gaussian distribution. gaussianSample :: Int -- ^ seed -> Int -- ^ number of rows @@ -39,6 +40,22 @@ gaussianSample seed n med cov = m where ds = sqrt (abs l) m = rs <> (diag ds <> trans v) + meds +-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate +-- uniform distribution. +uniformSample :: Int -- ^ seed + -> Int -- ^ number of rows + -> [(Double,Double)] -- ^ ranges for each column + -> Matrix Double -- ^ result +uniformSample seed n rgs = m where + (as,bs) = unzip rgs + a = fromList as + b = fromList bs + cs = zipWith subtract as bs + d = dim a + dat = toRows $ reshape n $ randomVector seed Uniform (n*d) + am = constant 1 n `outer` a + m = fromColumns (zipWith (.*) cs dat) + am + ------------ utilities ------------------------------- -- | 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]) --------------------------------------------------------------------- -randomTest = c :~1~: snd (meanCov dat) where +randomTestGaussian = c :~1~: snd (meanCov dat) where a = (3><3) [1,2,3, 2,4,0, -2,2,1] @@ -139,6 +139,10 @@ randomTest = c :~1~: snd (meanCov dat) where c = a <> trans a dat = gaussianSample 7 (10^6) m c +randomTestUniform = c :~1~: snd (meanCov dat) where + c = diag $ 3 |> map ((/12).(^2)) [1,2,3] + dat = uniformSample 7 (10^6) [(0,1),(1,3),(3,6)] + --------------------------------------------------------------------- rot :: Double -> Matrix Double @@ -237,7 +241,8 @@ runTests n = do , utest "polySolve" (polySolveProp [1,2,3,4]) , minimizationTest , rootFindingTest - , utest "random" randomTest + , utest "randomGaussian" randomTestGaussian + , utest "randomUniform" randomTestUniform ] return () -- cgit v1.2.3