splitmix-distributions-0.1.0.0: Random samplers for some common distributions, based on splitmix.
Safe HaskellNone
LanguageHaskell2010

System.Random.SplitMix.Distributions

Description

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
    then
      normal 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

Distributions

Continuous

stdUniform :: Gen Double Source #

Uniform in [0, 1)

uniformR Source #

Arguments

:: Double

low

-> Double

high

-> Gen Double 

Uniform between two values

exponential Source #

Arguments

:: Double

rate parameter \( \lambda > 0 \)

-> Gen Double 

Exponential distribution

stdNormal :: Gen Double Source #

Standard normal

normal Source #

Arguments

:: Double

mean

-> Double

standard deviation \( \sigma \gt 0 \)

-> Gen Double 

Normal distribution

beta Source #

Arguments

:: Double

shape parameter \( \alpha \gt 0 \)

-> Double

shape parameter \( \beta \gt 0 \)

-> Gen Double 

Beta distribution, from two standard uniform samples

gamma Source #

Arguments

:: Double

shape parameter \( k \gt 0 \)

-> Double

scale parameter \( \theta \gt 0 \)

-> Gen Double 

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

bernoulli Source #

Arguments

:: Double

bias parameter \( 0 \lt p \lt 1 \)

-> Gen Bool 

Bernoulli trial

PRNG

Pure

type Gen = GenT Identity Source #

Pure random generation

sample Source #

Arguments

:: Word64

random seed

-> Gen a 
-> a 

Pure evaluation

Monadic

data GenT m a Source #

Random generator

wraps splitmix state-passing inside a StateT monad

useful for embedding random generation inside a larger effect stack

Instances

Instances details
MonadTrans GenT Source # 
Instance details

Defined in System.Random.SplitMix.Distributions

Methods

lift :: Monad m => m a -> GenT m a #

Monad m => MonadState SMGen (GenT m) Source # 
Instance details

Defined in System.Random.SplitMix.Distributions

Methods

get :: GenT m SMGen #

put :: SMGen -> GenT m () #

state :: (SMGen -> (a, SMGen)) -> GenT m a #

Monad m => Monad (GenT m) Source # 
Instance details

Defined in System.Random.SplitMix.Distributions

Methods

(>>=) :: GenT m a -> (a -> GenT m b) -> GenT m b #

(>>) :: GenT m a -> GenT m b -> GenT m b #

return :: a -> GenT m a #

Functor m => Functor (GenT m) Source # 
Instance details

Defined in System.Random.SplitMix.Distributions

Methods

fmap :: (a -> b) -> GenT m a -> GenT m b #

(<$) :: a -> GenT m b -> GenT m a #

Monad m => Applicative (GenT m) Source # 
Instance details

Defined in System.Random.SplitMix.Distributions

Methods

pure :: a -> GenT m a #

(<*>) :: GenT m (a -> b) -> GenT m a -> GenT m b #

liftA2 :: (a -> b -> c) -> GenT m a -> GenT m b -> GenT m c #

(*>) :: GenT m a -> GenT m b -> GenT m b #

(<*) :: GenT m a -> GenT m b -> GenT m a #

MonadIO m => MonadIO (GenT m) Source # 
Instance details

Defined in System.Random.SplitMix.Distributions

Methods

liftIO :: IO a -> GenT m a #

sampleT Source #

Arguments

:: Monad m 
=> Word64

random seed

-> GenT m a 
-> m a 

Monadic evaluation

withGen Source #

Arguments

:: Monad m 
=> (SMGen -> (a, SMGen))

explicit generator passing (e.g. nextDouble)

-> GenT m a 

Wrap a splitmix PRNG function