{-# LANGUAGE CPP #-}
module Numeric.GSL.Random (
Seed,
RandDist(..),
randomVector,
gaussianSample,
uniformSample,
rand, randn
) where
import Numeric.GSL.Vector
import Numeric.LinearAlgebra.HMatrix hiding (
randomVector,
gaussianSample,
uniformSample,
Seed,
rand,
randn
)
import System.Random(randomIO)
#if MIN_VERSION_base(4,11,0)
import Prelude hiding ((<>))
#endif
type Seed = Int
gaussianSample :: Seed
-> Int
-> Vector Double
-> Herm Double
-> Matrix Double
gaussianSample seed n med cov = m where
c = size med
meds = konst 1 n `outer` med
rs = reshape c $ randomVector seed Gaussian (c * n)
m = rs <> chol cov + meds
uniformSample :: Seed
-> Int
-> [(Double,Double)]
-> Matrix Double
uniformSample seed n rgs = m where
(as,bs) = unzip rgs
a = fromList as
cs = zipWith subtract as bs
d = size a
dat = toRows $ reshape n $ randomVector seed Uniform (n*d)
am = konst 1 n `outer` a
m = fromColumns (zipWith scale cs dat) + am
randm :: RandDist
-> Int
-> Int
-> IO (Matrix Double)
randm d r c = do
seed <- randomIO
return (reshape c $ randomVector seed d (r*c))
rand :: Int -> Int -> IO (Matrix Double)
rand = randm Uniform
randn :: Int -> Int -> IO (Matrix Double)
randn = randm Gaussian