Safe Haskell | None |
---|---|
Language | Haskell2010 |
Random samplers for few common distributions, with an interface similar to that of mwc-probability
.
Usage
Compose your random sampler out of simpler ones thanks to the Applicative and Monad interface, e.g. this is how you would declare and sample a binary mixture of Gaussian random variables:
import Control.Monad (replicateM) import System.Random.SplitMix.Distributions (Gen, sample, bernoulli, normal) process ::Gen
Double process = do coin <-bernoulli
0.7 if coin thennormal
0 2 else normal 3 1 dataset :: [Double] dataset =sample
1234 $ replicateM 20 process
and sample your data in a pure (sample
) or monadic (sampleT
) setting.
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.
Synopsis
- stdUniform :: Gen Double
- uniformR :: Double -> Double -> Gen Double
- exponential :: Double -> Gen Double
- stdNormal :: Gen Double
- normal :: Double -> Double -> Gen Double
- beta :: Double -> Double -> Gen Double
- gamma :: Double -> Double -> Gen Double
- bernoulli :: Double -> Gen Bool
- type Gen = GenT Identity
- sample :: Word64 -> Gen a -> a
- data GenT m a
- sampleT :: Monad m => Word64 -> GenT m a -> m a
- withGen :: Monad m => (SMGen -> (a, SMGen)) -> GenT m a
Distributions
Continuous
stdUniform :: Gen Double Source #
Uniform in [0, 1)
Normal distribution
Beta distribution, from two standard uniform samples
Gamma distribution, using Ahrens-Dieter accept-reject (algorithm GD):
Ahrens, J. H.; Dieter, U (January 1982). "Generating gamma variates by a modified rejection technique". Communications of the ACM. 25 (1): 47–54
Discrete
PRNG
Pure
Monadic
Random generator
wraps splitmix
state-passing inside a StateT
monad
useful for embedding random generation inside a larger effect stack
Instances
MonadTrans GenT Source # | |
Defined in System.Random.SplitMix.Distributions | |
Monad m => MonadState SMGen (GenT m) Source # | |
Monad m => Monad (GenT m) Source # | |
Functor m => Functor (GenT m) Source # | |
Monad m => Applicative (GenT m) Source # | |
MonadIO m => MonadIO (GenT m) Source # | |
Defined in System.Random.SplitMix.Distributions |