packages feed

splitmix-distributions 0.6.0.0 → 0.7.0.0

raw patch · 3 files changed

+34/−3 lines, 3 filesdep ~containersPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: containers

API changes (from Hackage documentation)

+ System.Random.SplitMix.Distributions: sampleIO :: MonadIO m => GenT m b -> m b
+ System.Random.SplitMix.Distributions: samplesIO :: MonadIO m => Int -> GenT m a -> m [a]

Files

ChangeLog.md view
@@ -1,3 +1,8 @@+0.7 :++- add sampleIO, samplesIO+- clarify pure vs IO-based sampling in the docs+ 0.6 :  - add Chinese Restaurant Process
splitmix-distributions.cabal view
@@ -1,5 +1,5 @@ name:           splitmix-distributions-version:        0.6.0.0+version:        0.7.0.0 description:    Random samplers for some common distributions, as well as a convenient interface for composing them, based on splitmix. Please see the README on GitHub at <https://github.com/ocramz/splitmix-distributions#readme> homepage:       https://github.com/ocramz/splitmix-distributions#readme bug-reports:    https://github.com/ocramz/splitmix-distributions/issues
src/System/Random/SplitMix/Distributions.hs view
@@ -26,9 +26,11 @@  and sample your data in a pure (`sample`) or monadic (`sampleT`) setting. +Initializing the PRNG with a fixed seed makes all results fully reproducible across runs. If this behavior is not desired, one can sample the random seed itself from an IO-based entropy pool, and run the samplers with `sampleIO` and `samplesIO`.+ == Implementation details -The library is built on top of @splitmix@, so the caveats on safety and performance that apply there are relevant here as well.+The library is built on top of @splitmix@ ( https://hackage.haskell.org/package/splitmix ), which provides fast pseudorandom number generation utilities.   -}@@ -57,6 +59,9 @@   Gen, sample, samples,   -- ** Monadic   GenT, sampleT, samplesT,+  -- ** IO-based+  sampleIO, samplesIO,+  -- ** splitmix utilities   withGen                                             ) where @@ -76,7 +81,7 @@ import Control.Monad.Trans.Class (MonadTrans(..)) import Control.Monad.State (MonadState(..), modify) -- splitmix-import System.Random.SplitMix (SMGen, mkSMGen, splitSMGen, nextInt, nextInteger, nextDouble)+import System.Random.SplitMix (SMGen, mkSMGen, initSMGen, unseedSMGen, splitSMGen, nextInt, nextInteger, nextDouble) -- transformers import Control.Monad.Trans.State (StateT(..), runStateT, evalStateT, State, runState, evalState) @@ -97,6 +102,12 @@         -> m a sampleT seed gg = evalStateT (unGen gg) (mkSMGen seed) +-- | Initialize a splitmix random generator from system entropy (current time etc.) and sample from the PRNG.+sampleIO :: MonadIO m => GenT m b -> m b+sampleIO gg = do+  (s, _) <- unseedSMGen <$> liftIO initSMGen+  sampleT s gg+ -- | Sample a batch samplesT :: Monad m =>             Int -- ^ size of sample@@ -105,6 +116,12 @@          -> m [a] samplesT n seed gg = sampleT seed (replicateM n gg) +-- | Initialize a splitmix random generator from system entropy (current time etc.) and sample n times from the PRNG.+samplesIO :: MonadIO m => Int -> GenT m a -> m [a]+samplesIO n gg = do+  (s, _) <- unseedSMGen <$> liftIO initSMGen+  samplesT n s gg+ -- | Pure sampling sample :: Word64 -- ^ random seed         -> Gen a@@ -202,6 +219,15 @@   -- | Chinese restaurant process+--+-- >>> sample 1234 $ crp 1.02 50+-- [24,18,7,1]+--+-- >>> sample 1234 $ crp 2 50+-- [17,8,13,3,3,3,2,1]+--+-- >>> sample 1234 $ crp 10 50+-- [5,7,1,6,1,3,5,1,1,3,1,1,1,4,3,1,3,1,1,1] crp :: Monad m =>        Double -- ^ concentration parameter \( \alpha \gt 1 \)     -> Int -- ^ number of customers \( n > 0 \)