From ca67378bef1b0e4e76b4421786a846956c47f380 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Sun, 15 Nov 2009 17:42:50 +0000 Subject: uniformSample --- lib/Data/Packed/Random.hs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'lib/Data/Packed/Random.hs') 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. -- cgit v1.2.3