random-fu-0.2.7.0: Random number generation

Safe HaskellNone
LanguageHaskell98

Data.Random

Contents

Description

Flexible modeling and sampling of random variables.

The central abstraction in this library is the concept of a random variable. It is not fully formalized in the standard measure-theoretic language, but rather is informally defined as a "thing you can get random values out of". Different random variables may have different types of values they can return or the same types but different probabilities for each value they can return. The random values you get out of them are traditionally called "random variates".

Most imperative-language random number libraries are all about obtaining and manipulating random variates. This one is about defining, manipulating and sampling random variables. Computationally, the distinction is small and mostly just a matter of perspective, but from a program design perspective it provides both a powerfully composable abstraction and a very useful separation of concerns.

Abstract random variables as implemented by RVar are composable. They can be defined in a monadic / "imperative" style that amounts to manipulating variates, but with strict type-level isolation. Concrete random variables are also provided, but they do not compose as generically. The Distribution type class allows concrete random variables to "forget" their concreteness so that they can be composed. For examples of both, see the documentation for RVar and Distribution, as well as the code for any of the concrete distributions such as Uniform, Gamma, etc.

Both abstract and concrete random variables can be sampled (despite the types GHCi may list for the functions) by the functions in Data.Random.Sample.

Random variable sampling is done with regard to a generic basis of primitive random variables defined in Data.Random.Internal.Primitives. This basis is very low-level and the actual set of primitives is still fairly experimental, which is why it is in the "Internal" sub-heirarchy. User-defined variables should use the existing high-level variables such as Uniform and Normal rather than these basis variables. Data.Random.Source defines classes for entropy sources that provide implementations of these primitive variables. Several implementations are available in the Data.Random.Source.* modules.

Synopsis

Random variables

Abstract (RVar)

type RVar = RVarT Identity #

An opaque type modeling a "random variable" - a value which depends on the outcome of some random event. RVars can be conveniently defined by an imperative-looking style:

normalPair =  do
    u <- stdUniform
    t <- stdUniform
    let r = sqrt (-2 * log u)
        theta = (2 * pi) * t
        
        x = r * cos theta
        y = r * sin theta
    return (x,y)

OR by a more applicative style:

logNormal = exp <$> stdNormal

Once defined (in any style), there are several ways to sample RVars:

runRVar (uniform 1 100) DevRandom :: IO Int
sampleRVar (uniform 1 100) :: State PureMT Int
  • As a pure function transforming a functional RNG:
sampleState (uniform 1 100) :: StdGen -> (Int, StdGen)

(where sampleState = runState . sampleRVar)

data RVarT m a :: (* -> *) -> * -> * #

A random variable with access to operations in an underlying monad. Useful examples include any form of state for implementing random processes with hysteresis, or writer monads for implementing tracing of complicated algorithms.

For example, a simple random walk can be implemented as an RVarT IO value:

rwalkIO :: IO (RVarT IO Double)
rwalkIO d = do
    lastVal <- newIORef 0
    
    let x = do
            prev    <- lift (readIORef lastVal)
            change  <- rvarT StdNormal
            
            let new = prev + change
            lift (writeIORef lastVal new)
            return new
        
    return x

To run the random walk it must first be initialized, after which it can be sampled as usual:

do
    rw <- rwalkIO
    x <- sampleRVarT rw
    y <- sampleRVarT rw
    ...

The same random-walk process as above can be implemented using MTL types as follows (using import Control.Monad.Trans as MTL):

rwalkState :: RVarT (State Double) Double
rwalkState = do
    prev <- MTL.lift get
    change  <- rvarT StdNormal
    
    let new = prev + change
    MTL.lift (put new)
    return new

Invocation is straightforward (although a bit noisy) if you're used to MTL:

rwalk :: Int -> Double -> StdGen -> ([Double], StdGen)
rwalk count start gen = 
    flip evalState start .
        flip runStateT gen .
            sampleRVarTWith MTL.lift $
                replicateM count rwalkState

Instances

MonadTrans RVarT 

Methods

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

MonadPrompt Prim (RVarT n) 

Methods

prompt :: Prim a -> RVarT n a #

Monad (RVarT n) 

Methods

(>>=) :: RVarT n a -> (a -> RVarT n b) -> RVarT n b #

(>>) :: RVarT n a -> RVarT n b -> RVarT n b #

return :: a -> RVarT n a #

fail :: String -> RVarT n a #

Functor (RVarT n) 

Methods

fmap :: (a -> b) -> RVarT n a -> RVarT n b #

(<$) :: a -> RVarT n b -> RVarT n a #

Applicative (RVarT n) 

Methods

pure :: a -> RVarT n a #

(<*>) :: RVarT n (a -> b) -> RVarT n a -> RVarT n b #

(*>) :: RVarT n a -> RVarT n b -> RVarT n b #

(<*) :: RVarT n a -> RVarT n b -> RVarT n a #

MonadIO m => MonadIO (RVarT m) 

Methods

liftIO :: IO a -> RVarT m a #

MonadRandom (RVarT n) 
Lift m n => Sampleable (RVarT m) n t # 

Methods

sampleFrom :: RandomSource n s => s -> RVarT m t -> n t #

Lift (RVarT Identity) (RVarT m) # 

Methods

lift :: RVarT Identity a -> RVarT m a #

runRVar :: RandomSource m s => RVar a -> s -> m a #

"Run" an RVar - samples the random variable from the provided source of entropy.

runRVarT :: (Lift n m, RandomSource m s) => RVarT n a -> s -> m a #

Like runRVarTWith, but using an implicit lifting (provided by the Lift class)

runRVarTWith :: RandomSource m s => (forall t. n t -> m t) -> RVarT n a -> s -> m a #

"Runs" an RVarT, sampling the random variable it defines.

The first argument lifts the base monad into the sampling monad. This operation must obey the "monad transformer" laws:

lift . return = return
lift (x >>= f) = (lift x) >>= (lift . f)

One example of a useful non-standard lifting would be one that takes State s to another monad with a different state representation (such as IO with the state mapped to an IORef):

embedState :: (Monad m) => m s -> (s -> m ()) -> State s a -> m a
embedState get put = \m -> do
    s <- get
    (res,s) <- return (runState m s)
    put s
    return res

The ability to lift is very important - without it, every RVar would have to either be given access to the full capability of the monad in which it will eventually be sampled (which, incidentally, would also have to be monomorphic so you couldn't sample one RVar in more than one monad) or functions manipulating RVars would have to use higher-ranked types to enforce the same kind of isolation and polymorphism.

Concrete (Distribution)

class Distribution d t where #

A Distribution is a data representation of a random variable's probability structure. For example, in Data.Random.Distribution.Normal, the Normal distribution is defined as:

data Normal a
    = StdNormal
    | Normal a a

Where the two parameters of the Normal data constructor are the mean and standard deviation of the random variable, respectively. To make use of the Normal type, one can convert it to an rvar and manipulate it or sample it directly:

x <- sample (rvar (Normal 10 2))
x <- sample (Normal 10 2)

A Distribution is typically more transparent than an RVar but less composable (precisely because of that transparency). There are several practical uses for types implementing Distribution:

  • Typically, a Distribution will expose several parameters of a standard mathematical model of a probability distribution, such as mean and std deviation for the normal distribution. Thus, they can be manipulated analytically using mathematical insights about the distributions they represent. For example, a collection of bernoulli variables could be simplified into a (hopefully) smaller collection of binomial variables.
  • Because they are generally just containers for parameters, they can be easily serialized to persistent storage or read from user-supplied configurations (eg, initialization data for a simulation).
  • If a type additionally implements the CDF subclass, which extends Distribution with a cumulative density function, an arbitrary random variable x can be tested against the distribution by testing fmap (cdf dist) x for uniformity.

On the other hand, most Distributions will not be closed under all the same operations as RVar (which, being a monad, has a fully turing-complete internal computational model). The sum of two uniformly-distributed variables, for example, is not uniformly distributed. To support general composition, the Distribution class defines a function rvar to construct the more-abstract and more-composable RVar representation of a random variable.

Methods

rvar :: d t -> RVar t #

Return a random variable with this distribution.

rvarT :: d t -> RVarT n t #

Return a random variable with the given distribution, pre-lifted to an arbitrary RVarT. Any arbitrary RVar can also be converted to an 'RVarT m' for an arbitrary m, using either lift or sample.

Instances

Distribution StdUniform Bool # 
Distribution StdUniform Char # 
Distribution StdUniform Double # 
Distribution StdUniform Float # 
Distribution StdUniform Int # 
Distribution StdUniform Int8 # 
Distribution StdUniform Int16 # 
Distribution StdUniform Int32 # 
Distribution StdUniform Int64 # 
Distribution StdUniform Ordering # 
Distribution StdUniform Word # 
Distribution StdUniform Word8 # 
Distribution StdUniform Word16 # 
Distribution StdUniform Word32 # 
Distribution StdUniform Word64 # 
Distribution StdUniform () # 

Methods

rvar :: StdUniform () -> RVar () #

rvarT :: StdUniform () -> RVarT n () #

Distribution Uniform Bool # 
Distribution Uniform Char # 
Distribution Uniform Double # 
Distribution Uniform Float # 
Distribution Uniform Int # 

Methods

rvar :: Uniform Int -> RVar Int #

rvarT :: Uniform Int -> RVarT n Int #

Distribution Uniform Int8 # 
Distribution Uniform Int16 # 
Distribution Uniform Int32 # 
Distribution Uniform Int64 # 
Distribution Uniform Integer # 
Distribution Uniform Ordering # 
Distribution Uniform Word # 
Distribution Uniform Word8 # 
Distribution Uniform Word16 # 
Distribution Uniform Word32 # 
Distribution Uniform Word64 # 
Distribution Uniform () # 

Methods

rvar :: Uniform () -> RVar () #

rvarT :: Uniform () -> RVarT n () #

(Floating a, Distribution StdUniform a) => Distribution Exponential a # 

Methods

rvar :: Exponential a -> RVar a #

rvarT :: Exponential a -> RVarT n a #

(RealFloat a, Distribution StdUniform a) => Distribution Rayleigh a # 

Methods

rvar :: Rayleigh a -> RVar a #

rvarT :: Rayleigh a -> RVarT n a #

(Floating a, Distribution StdUniform a) => Distribution StretchedExponential a # 
(RealFloat a, Ord a, Distribution StdUniform a) => Distribution Triangular a # 

Methods

rvar :: Triangular a -> RVar a #

rvarT :: Triangular a -> RVarT n a #

(Floating a, Distribution StdUniform a) => Distribution Weibull a # 

Methods

rvar :: Weibull a -> RVar a #

rvarT :: Weibull a -> RVarT n a #

Distribution Normal Double # 
Distribution Normal Float # 
(Floating a, Ord a, Distribution Normal a, Distribution StdUniform a) => Distribution Gamma a # 

Methods

rvar :: Gamma a -> RVar a #

rvarT :: Gamma a -> RVarT n a #

Distribution Beta Double # 
Distribution Beta Float # 
(Fractional t, Distribution Gamma t) => Distribution ChiSquare t # 

Methods

rvar :: ChiSquare t -> RVar t #

rvarT :: ChiSquare t -> RVarT n t #

(Floating a, Distribution Normal a, Distribution ChiSquare a) => Distribution T a # 

Methods

rvar :: T a -> RVar a #

rvarT :: T a -> RVarT n a #

(Floating a, Distribution StdUniform a) => Distribution Pareto a # 

Methods

rvar :: Pareto a -> RVar a #

rvarT :: Pareto a -> RVarT n a #

HasResolution r => Distribution StdUniform (Fixed r) # 

Methods

rvar :: StdUniform (Fixed r) -> RVar (Fixed r) #

rvarT :: StdUniform (Fixed r) -> RVarT n (Fixed r) #

HasResolution r => Distribution Uniform (Fixed r) # 

Methods

rvar :: Uniform (Fixed r) -> RVar (Fixed r) #

rvarT :: Uniform (Fixed r) -> RVarT n (Fixed r) #

(Ord a, Fractional a, Distribution StdUniform a) => Distribution StdSimplex [a] # 

Methods

rvar :: StdSimplex [a] -> RVar [a] #

rvarT :: StdSimplex [a] -> RVarT n [a] #

(Fractional a, Distribution Gamma a) => Distribution Dirichlet [a] # 

Methods

rvar :: Dirichlet [a] -> RVar [a] #

rvarT :: Dirichlet [a] -> RVarT n [a] #

(Fractional b, Ord b, Distribution StdUniform b) => Distribution (Bernoulli b) Bool # 
Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Word64 # 
Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Word32 # 
Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Word16 # 
Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Word8 # 
Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Word # 

Methods

rvar :: Bernoulli b0 Word -> RVar Word #

rvarT :: Bernoulli b0 Word -> RVarT n Word #

Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Int64 # 
Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Int32 # 
Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Int16 # 
Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Int8 # 

Methods

rvar :: Bernoulli b0 Int8 -> RVar Int8 #

rvarT :: Bernoulli b0 Int8 -> RVarT n Int8 #

Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Int # 

Methods

rvar :: Bernoulli b0 Int -> RVar Int #

rvarT :: Bernoulli b0 Int -> RVarT n Int #

Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Integer # 
Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Double # 
Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Float # 
(Fractional p, Ord p, Distribution Uniform p) => Distribution (Categorical p) a # 

Methods

rvar :: Categorical p a -> RVar a #

rvarT :: Categorical p a -> RVarT n a #

(Num t, Ord t, Vector v t) => Distribution (Ziggurat v) t # 

Methods

rvar :: Ziggurat v t -> RVar t #

rvarT :: Ziggurat v t -> RVarT n t #

(Integral a, Floating b, Ord b, Distribution Normal b, Distribution StdUniform b) => Distribution (Erlang a) b # 

Methods

rvar :: Erlang a b -> RVar b #

rvarT :: Erlang a b -> RVarT n b #

(Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Word64 # 
(Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Word32 # 
(Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Word16 # 
(Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Word8 # 
(Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Word # 

Methods

rvar :: Binomial b0 Word -> RVar Word #

rvarT :: Binomial b0 Word -> RVarT n Word #

(Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Int64 # 
(Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Int32 # 
(Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Int16 # 
(Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Int8 # 

Methods

rvar :: Binomial b0 Int8 -> RVar Int8 #

rvarT :: Binomial b0 Int8 -> RVarT n Int8 #

(Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Int # 

Methods

rvar :: Binomial b0 Int -> RVar Int #

rvarT :: Binomial b0 Int -> RVarT n Int #

(Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Integer # 
Distribution (Binomial b0) Integer => Distribution (Binomial b0) Double # 
Distribution (Binomial b0) Integer => Distribution (Binomial b0) Float # 
(RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Word64) b0, Distribution (Binomial b0) Word64) => Distribution (Poisson b0) Word64 # 
(RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Word32) b0, Distribution (Binomial b0) Word32) => Distribution (Poisson b0) Word32 # 
(RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Word16) b0, Distribution (Binomial b0) Word16) => Distribution (Poisson b0) Word16 # 
(RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Word8) b0, Distribution (Binomial b0) Word8) => Distribution (Poisson b0) Word8 # 

Methods

rvar :: Poisson b0 Word8 -> RVar Word8 #

rvarT :: Poisson b0 Word8 -> RVarT n Word8 #

(RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Word) b0, Distribution (Binomial b0) Word) => Distribution (Poisson b0) Word # 

Methods

rvar :: Poisson b0 Word -> RVar Word #

rvarT :: Poisson b0 Word -> RVarT n Word #

(RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Int64) b0, Distribution (Binomial b0) Int64) => Distribution (Poisson b0) Int64 # 

Methods

rvar :: Poisson b0 Int64 -> RVar Int64 #

rvarT :: Poisson b0 Int64 -> RVarT n Int64 #

(RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Int32) b0, Distribution (Binomial b0) Int32) => Distribution (Poisson b0) Int32 # 

Methods

rvar :: Poisson b0 Int32 -> RVar Int32 #

rvarT :: Poisson b0 Int32 -> RVarT n Int32 #

(RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Int16) b0, Distribution (Binomial b0) Int16) => Distribution (Poisson b0) Int16 # 

Methods

rvar :: Poisson b0 Int16 -> RVar Int16 #

rvarT :: Poisson b0 Int16 -> RVarT n Int16 #

(RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Int8) b0, Distribution (Binomial b0) Int8) => Distribution (Poisson b0) Int8 # 

Methods

rvar :: Poisson b0 Int8 -> RVar Int8 #

rvarT :: Poisson b0 Int8 -> RVarT n Int8 #

(RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Int) b0, Distribution (Binomial b0) Int) => Distribution (Poisson b0) Int # 

Methods

rvar :: Poisson b0 Int -> RVar Int #

rvarT :: Poisson b0 Int -> RVarT n Int #

(RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Integer) b0, Distribution (Binomial b0) Integer) => Distribution (Poisson b0) Integer # 
Distribution (Poisson b0) Integer => Distribution (Poisson b0) Double # 
Distribution (Poisson b0) Integer => Distribution (Poisson b0) Float # 

Methods

rvar :: Poisson b0 Float -> RVar Float #

rvarT :: Poisson b0 Float -> RVarT n Float #

(Distribution (Bernoulli b) Bool, RealFloat a) => Distribution (Bernoulli b) (Complex a) # 

Methods

rvar :: Bernoulli b (Complex a) -> RVar (Complex a) #

rvarT :: Bernoulli b (Complex a) -> RVarT n (Complex a) #

(Distribution (Bernoulli b) Bool, Integral a) => Distribution (Bernoulli b) (Ratio a) # 

Methods

rvar :: Bernoulli b (Ratio a) -> RVar (Ratio a) #

rvarT :: Bernoulli b (Ratio a) -> RVarT n (Ratio a) #

(Num a, Eq a, Fractional p, Distribution (Binomial p) a) => Distribution (Multinomial p) [a] # 

Methods

rvar :: Multinomial p [a] -> RVar [a] #

rvarT :: Multinomial p [a] -> RVarT n [a] #

class Distribution d t => CDF d t where #

Minimal complete definition

cdf

Methods

cdf :: d t -> t -> Double #

Return the cumulative distribution function of this distribution. That is, a function taking x :: t to the probability that the next sample will return a value less than or equal to x, according to some order or partial order (not necessarily an obvious one).

In the case where t is an instance of Ord, cdf should correspond to the CDF with respect to that order.

In other cases, cdf is only required to satisfy the following law: fmap (cdf d) (rvar d) must be uniformly distributed over (0,1). Inclusion of either endpoint is optional, though the preferred range is (0,1].

Note that this definition requires that cdf for a product type should _not_ be a joint CDF as commonly defined, as that definition violates both conditions. Instead, it should be a univariate CDF over the product type. That is, it should represent the CDF with respect to the lexicographic order of the product.

The present specification is probably only really useful for testing conformance of a variable to its target distribution, and I am open to suggestions for more-useful specifications (especially with regard to the interaction with product types).

Instances

CDF StdUniform Bool # 

Methods

cdf :: StdUniform Bool -> Bool -> Double #

CDF StdUniform Char # 

Methods

cdf :: StdUniform Char -> Char -> Double #

CDF StdUniform Double # 
CDF StdUniform Float # 

Methods

cdf :: StdUniform Float -> Float -> Double #

CDF StdUniform Int # 

Methods

cdf :: StdUniform Int -> Int -> Double #

CDF StdUniform Int8 # 

Methods

cdf :: StdUniform Int8 -> Int8 -> Double #

CDF StdUniform Int16 # 

Methods

cdf :: StdUniform Int16 -> Int16 -> Double #

CDF StdUniform Int32 # 

Methods

cdf :: StdUniform Int32 -> Int32 -> Double #

CDF StdUniform Int64 # 

Methods

cdf :: StdUniform Int64 -> Int64 -> Double #

CDF StdUniform Ordering # 
CDF StdUniform Word # 

Methods

cdf :: StdUniform Word -> Word -> Double #

CDF StdUniform Word8 # 

Methods

cdf :: StdUniform Word8 -> Word8 -> Double #

CDF StdUniform Word16 # 
CDF StdUniform Word32 # 
CDF StdUniform Word64 # 
CDF StdUniform () # 

Methods

cdf :: StdUniform () -> () -> Double #

CDF Uniform Bool # 

Methods

cdf :: Uniform Bool -> Bool -> Double #

CDF Uniform Char # 

Methods

cdf :: Uniform Char -> Char -> Double #

CDF Uniform Double # 

Methods

cdf :: Uniform Double -> Double -> Double #

CDF Uniform Float # 

Methods

cdf :: Uniform Float -> Float -> Double #

CDF Uniform Int # 

Methods

cdf :: Uniform Int -> Int -> Double #

CDF Uniform Int8 # 

Methods

cdf :: Uniform Int8 -> Int8 -> Double #

CDF Uniform Int16 # 

Methods

cdf :: Uniform Int16 -> Int16 -> Double #

CDF Uniform Int32 # 

Methods

cdf :: Uniform Int32 -> Int32 -> Double #

CDF Uniform Int64 # 

Methods

cdf :: Uniform Int64 -> Int64 -> Double #

CDF Uniform Integer # 

Methods

cdf :: Uniform Integer -> Integer -> Double #

CDF Uniform Ordering # 
CDF Uniform Word # 

Methods

cdf :: Uniform Word -> Word -> Double #

CDF Uniform Word8 # 

Methods

cdf :: Uniform Word8 -> Word8 -> Double #

CDF Uniform Word16 # 

Methods

cdf :: Uniform Word16 -> Word16 -> Double #

CDF Uniform Word32 # 

Methods

cdf :: Uniform Word32 -> Word32 -> Double #

CDF Uniform Word64 # 

Methods

cdf :: Uniform Word64 -> Word64 -> Double #

CDF Uniform () # 

Methods

cdf :: Uniform () -> () -> Double #

(Real a, Distribution Exponential a) => CDF Exponential a # 

Methods

cdf :: Exponential a -> a -> Double #

(Real a, Distribution Rayleigh a) => CDF Rayleigh a # 

Methods

cdf :: Rayleigh a -> a -> Double #

(Real a, Distribution StretchedExponential a) => CDF StretchedExponential a # 

Methods

cdf :: StretchedExponential a -> a -> Double #

(RealFrac a, Distribution Triangular a) => CDF Triangular a # 

Methods

cdf :: Triangular a -> a -> Double #

(Real a, Distribution Weibull a) => CDF Weibull a # 

Methods

cdf :: Weibull a -> a -> Double #

(Real a, Distribution Normal a) => CDF Normal a # 

Methods

cdf :: Normal a -> a -> Double #

(Real a, Distribution Gamma a) => CDF Gamma a # 

Methods

cdf :: Gamma a -> a -> Double #

(Real t, Distribution ChiSquare t) => CDF ChiSquare t # 

Methods

cdf :: ChiSquare t -> t -> Double #

(Real a, Distribution T a) => CDF T a # 

Methods

cdf :: T a -> a -> Double #

(Real a, Distribution Pareto a) => CDF Pareto a # 

Methods

cdf :: Pareto a -> a -> Double #

HasResolution r => CDF StdUniform (Fixed r) # 

Methods

cdf :: StdUniform (Fixed r) -> Fixed r -> Double #

HasResolution r => CDF Uniform (Fixed r) # 

Methods

cdf :: Uniform (Fixed r) -> Fixed r -> Double #

(Distribution (Bernoulli b) Bool, Real b) => CDF (Bernoulli b) Bool # 

Methods

cdf :: Bernoulli b Bool -> Bool -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Word64 # 

Methods

cdf :: Bernoulli b0 Word64 -> Word64 -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Word32 # 

Methods

cdf :: Bernoulli b0 Word32 -> Word32 -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Word16 # 

Methods

cdf :: Bernoulli b0 Word16 -> Word16 -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Word8 # 

Methods

cdf :: Bernoulli b0 Word8 -> Word8 -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Word # 

Methods

cdf :: Bernoulli b0 Word -> Word -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Int64 # 

Methods

cdf :: Bernoulli b0 Int64 -> Int64 -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Int32 # 

Methods

cdf :: Bernoulli b0 Int32 -> Int32 -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Int16 # 

Methods

cdf :: Bernoulli b0 Int16 -> Int16 -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Int8 # 

Methods

cdf :: Bernoulli b0 Int8 -> Int8 -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Int # 

Methods

cdf :: Bernoulli b0 Int -> Int -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Integer # 

Methods

cdf :: Bernoulli b0 Integer -> Integer -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Double # 

Methods

cdf :: Bernoulli b0 Double -> Double -> Double #

CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Float # 

Methods

cdf :: Bernoulli b0 Float -> Float -> Double #

(Integral a, Real b, Distribution (Erlang a) b) => CDF (Erlang a) b # 

Methods

cdf :: Erlang a b -> b -> Double #

(Real b0, Distribution (Binomial b0) Word64) => CDF (Binomial b0) Word64 # 

Methods

cdf :: Binomial b0 Word64 -> Word64 -> Double #

(Real b0, Distribution (Binomial b0) Word32) => CDF (Binomial b0) Word32 # 

Methods

cdf :: Binomial b0 Word32 -> Word32 -> Double #

(Real b0, Distribution (Binomial b0) Word16) => CDF (Binomial b0) Word16 # 

Methods

cdf :: Binomial b0 Word16 -> Word16 -> Double #

(Real b0, Distribution (Binomial b0) Word8) => CDF (Binomial b0) Word8 # 

Methods

cdf :: Binomial b0 Word8 -> Word8 -> Double #

(Real b0, Distribution (Binomial b0) Word) => CDF (Binomial b0) Word # 

Methods

cdf :: Binomial b0 Word -> Word -> Double #

(Real b0, Distribution (Binomial b0) Int64) => CDF (Binomial b0) Int64 # 

Methods

cdf :: Binomial b0 Int64 -> Int64 -> Double #

(Real b0, Distribution (Binomial b0) Int32) => CDF (Binomial b0) Int32 # 

Methods

cdf :: Binomial b0 Int32 -> Int32 -> Double #

(Real b0, Distribution (Binomial b0) Int16) => CDF (Binomial b0) Int16 # 

Methods

cdf :: Binomial b0 Int16 -> Int16 -> Double #

(Real b0, Distribution (Binomial b0) Int8) => CDF (Binomial b0) Int8 # 

Methods

cdf :: Binomial b0 Int8 -> Int8 -> Double #

(Real b0, Distribution (Binomial b0) Int) => CDF (Binomial b0) Int # 

Methods

cdf :: Binomial b0 Int -> Int -> Double #

(Real b0, Distribution (Binomial b0) Integer) => CDF (Binomial b0) Integer # 

Methods

cdf :: Binomial b0 Integer -> Integer -> Double #

CDF (Binomial b0) Integer => CDF (Binomial b0) Double # 

Methods

cdf :: Binomial b0 Double -> Double -> Double #

CDF (Binomial b0) Integer => CDF (Binomial b0) Float # 

Methods

cdf :: Binomial b0 Float -> Float -> Double #

(Real b0, Distribution (Poisson b0) Word64) => CDF (Poisson b0) Word64 # 

Methods

cdf :: Poisson b0 Word64 -> Word64 -> Double #

(Real b0, Distribution (Poisson b0) Word32) => CDF (Poisson b0) Word32 # 

Methods

cdf :: Poisson b0 Word32 -> Word32 -> Double #

(Real b0, Distribution (Poisson b0) Word16) => CDF (Poisson b0) Word16 # 

Methods

cdf :: Poisson b0 Word16 -> Word16 -> Double #

(Real b0, Distribution (Poisson b0) Word8) => CDF (Poisson b0) Word8 # 

Methods

cdf :: Poisson b0 Word8 -> Word8 -> Double #

(Real b0, Distribution (Poisson b0) Word) => CDF (Poisson b0) Word # 

Methods

cdf :: Poisson b0 Word -> Word -> Double #

(Real b0, Distribution (Poisson b0) Int64) => CDF (Poisson b0) Int64 # 

Methods

cdf :: Poisson b0 Int64 -> Int64 -> Double #

(Real b0, Distribution (Poisson b0) Int32) => CDF (Poisson b0) Int32 # 

Methods

cdf :: Poisson b0 Int32 -> Int32 -> Double #

(Real b0, Distribution (Poisson b0) Int16) => CDF (Poisson b0) Int16 # 

Methods

cdf :: Poisson b0 Int16 -> Int16 -> Double #

(Real b0, Distribution (Poisson b0) Int8) => CDF (Poisson b0) Int8 # 

Methods

cdf :: Poisson b0 Int8 -> Int8 -> Double #

(Real b0, Distribution (Poisson b0) Int) => CDF (Poisson b0) Int # 

Methods

cdf :: Poisson b0 Int -> Int -> Double #

(Real b0, Distribution (Poisson b0) Integer) => CDF (Poisson b0) Integer # 

Methods

cdf :: Poisson b0 Integer -> Integer -> Double #

CDF (Poisson b0) Integer => CDF (Poisson b0) Double # 

Methods

cdf :: Poisson b0 Double -> Double -> Double #

CDF (Poisson b0) Integer => CDF (Poisson b0) Float # 

Methods

cdf :: Poisson b0 Float -> Float -> Double #

(CDF (Bernoulli b) Bool, RealFloat a) => CDF (Bernoulli b) (Complex a) # 

Methods

cdf :: Bernoulli b (Complex a) -> Complex a -> Double #

(CDF (Bernoulli b) Bool, Integral a) => CDF (Bernoulli b) (Ratio a) # 

Methods

cdf :: Bernoulli b (Ratio a) -> Ratio a -> Double #

class Distribution d t => PDF d t where #

Methods

pdf :: d t -> t -> Double #

logPdf :: d t -> t -> Double #

Instances

PDF StdUniform Double # 
PDF StdUniform Float # 
(Real a, Floating a, Distribution Normal a) => PDF Normal a # 

Methods

pdf :: Normal a -> a -> Double #

logPdf :: Normal a -> a -> Double #

PDF Beta Double # 
PDF Beta Float # 

Methods

pdf :: Beta Float -> Float -> Double #

logPdf :: Beta Float -> Float -> Double #

(Real b0, Distribution (Binomial b0) Word64) => PDF (Binomial b0) Word64 # 
(Real b0, Distribution (Binomial b0) Word32) => PDF (Binomial b0) Word32 # 
(Real b0, Distribution (Binomial b0) Word16) => PDF (Binomial b0) Word16 # 
(Real b0, Distribution (Binomial b0) Word8) => PDF (Binomial b0) Word8 # 

Methods

pdf :: Binomial b0 Word8 -> Word8 -> Double #

logPdf :: Binomial b0 Word8 -> Word8 -> Double #

(Real b0, Distribution (Binomial b0) Word) => PDF (Binomial b0) Word # 

Methods

pdf :: Binomial b0 Word -> Word -> Double #

logPdf :: Binomial b0 Word -> Word -> Double #

(Real b0, Distribution (Binomial b0) Int64) => PDF (Binomial b0) Int64 # 

Methods

pdf :: Binomial b0 Int64 -> Int64 -> Double #

logPdf :: Binomial b0 Int64 -> Int64 -> Double #

(Real b0, Distribution (Binomial b0) Int32) => PDF (Binomial b0) Int32 # 

Methods

pdf :: Binomial b0 Int32 -> Int32 -> Double #

logPdf :: Binomial b0 Int32 -> Int32 -> Double #

(Real b0, Distribution (Binomial b0) Int16) => PDF (Binomial b0) Int16 # 

Methods

pdf :: Binomial b0 Int16 -> Int16 -> Double #

logPdf :: Binomial b0 Int16 -> Int16 -> Double #

(Real b0, Distribution (Binomial b0) Int8) => PDF (Binomial b0) Int8 # 

Methods

pdf :: Binomial b0 Int8 -> Int8 -> Double #

logPdf :: Binomial b0 Int8 -> Int8 -> Double #

(Real b0, Distribution (Binomial b0) Int) => PDF (Binomial b0) Int # 

Methods

pdf :: Binomial b0 Int -> Int -> Double #

logPdf :: Binomial b0 Int -> Int -> Double #

(Real b0, Distribution (Binomial b0) Integer) => PDF (Binomial b0) Integer # 
PDF (Binomial b0) Integer => PDF (Binomial b0) Double # 
PDF (Binomial b0) Integer => PDF (Binomial b0) Float # 

Methods

pdf :: Binomial b0 Float -> Float -> Double #

logPdf :: Binomial b0 Float -> Float -> Double #

Sampling random variables

class Sampleable d m t where #

A typeclass allowing Distributions and RVars to be sampled. Both may also be sampled via runRVar or runRVarT, but I find it psychologically pleasing to be able to sample both using this function, as they are two separate abstractions for one base concept: a random variable.

Minimal complete definition

sampleFrom

Methods

sampleFrom :: RandomSource m s => s -> d t -> m t #

Directly sample from a distribution or random variable, using the given source of entropy.

Instances

Distribution d t => Sampleable d m t # 

Methods

sampleFrom :: RandomSource m s => s -> d t -> m t #

Lift m n => Sampleable (RVarT m) n t # 

Methods

sampleFrom :: RandomSource n s => s -> RVarT m t -> n t #

sample :: (Sampleable d m t, MonadRandom m) => d t -> m t #

Sample a random variable using the default source of entropy for the monad in which the sampling occurs.

sampleState :: (Sampleable d (State s) t, MonadRandom (State s)) => d t -> s -> (t, s) #

Sample a random variable in a "functional" style. Typical instantiations of s are System.Random.StdGen or System.Random.Mersenne.Pure64.PureMT.

sampleStateT :: (Sampleable d (StateT s m) t, MonadRandom (StateT s m)) => d t -> s -> m (t, s) #

Sample a random variable in a "semi-functional" style. Typical instantiations of s are System.Random.StdGen or System.Random.Mersenne.Pure64.PureMT.

A few very common distributions

data Uniform t #

A definition of a uniform distribution over the type t. See also uniform.

Constructors

Uniform !t !t

A uniform distribution defined by a lower and upper range bound. For Integral and Enum types, the range is inclusive. For Fractional types the range includes the lower bound but not the upper.

Instances

CDF Uniform Bool # 

Methods

cdf :: Uniform Bool -> Bool -> Double #

CDF Uniform Char # 

Methods

cdf :: Uniform Char -> Char -> Double #

CDF Uniform Double # 

Methods

cdf :: Uniform Double -> Double -> Double #

CDF Uniform Float # 

Methods

cdf :: Uniform Float -> Float -> Double #

CDF Uniform Int # 

Methods

cdf :: Uniform Int -> Int -> Double #

CDF Uniform Int8 # 

Methods

cdf :: Uniform Int8 -> Int8 -> Double #

CDF Uniform Int16 # 

Methods

cdf :: Uniform Int16 -> Int16 -> Double #

CDF Uniform Int32 # 

Methods

cdf :: Uniform Int32 -> Int32 -> Double #

CDF Uniform Int64 # 

Methods

cdf :: Uniform Int64 -> Int64 -> Double #

CDF Uniform Integer # 

Methods

cdf :: Uniform Integer -> Integer -> Double #

CDF Uniform Ordering # 
CDF Uniform Word # 

Methods

cdf :: Uniform Word -> Word -> Double #

CDF Uniform Word8 # 

Methods

cdf :: Uniform Word8 -> Word8 -> Double #

CDF Uniform Word16 # 

Methods

cdf :: Uniform Word16 -> Word16 -> Double #

CDF Uniform Word32 # 

Methods

cdf :: Uniform Word32 -> Word32 -> Double #

CDF Uniform Word64 # 

Methods

cdf :: Uniform Word64 -> Word64 -> Double #

CDF Uniform () # 

Methods

cdf :: Uniform () -> () -> Double #

Distribution Uniform Bool # 
Distribution Uniform Char # 
Distribution Uniform Double # 
Distribution Uniform Float # 
Distribution Uniform Int # 

Methods

rvar :: Uniform Int -> RVar Int #

rvarT :: Uniform Int -> RVarT n Int #

Distribution Uniform Int8 # 
Distribution Uniform Int16 # 
Distribution Uniform Int32 # 
Distribution Uniform Int64 # 
Distribution Uniform Integer # 
Distribution Uniform Ordering # 
Distribution Uniform Word # 
Distribution Uniform Word8 # 
Distribution Uniform Word16 # 
Distribution Uniform Word32 # 
Distribution Uniform Word64 # 
Distribution Uniform () # 

Methods

rvar :: Uniform () -> RVar () #

rvarT :: Uniform () -> RVarT n () #

HasResolution r => CDF Uniform (Fixed r) # 

Methods

cdf :: Uniform (Fixed r) -> Fixed r -> Double #

HasResolution r => Distribution Uniform (Fixed r) # 

Methods

rvar :: Uniform (Fixed r) -> RVar (Fixed r) #

rvarT :: Uniform (Fixed r) -> RVarT n (Fixed r) #

uniform :: Distribution Uniform a => a -> a -> RVar a #

uniformT :: Distribution Uniform a => a -> a -> RVarT m a #

data StdUniform t #

A name for the "standard" uniform distribution over the type t, if one exists. See also stdUniform.

For Integral and Enum types that are also Bounded, this is the uniform distribution over the full range of the type. For un-Bounded Integral types this is not defined. For Fractional types this is a random variable in the range [0,1) (that is, 0 to 1 including 0 but not including 1).

Constructors

StdUniform 

Instances

CDF StdUniform Bool # 

Methods

cdf :: StdUniform Bool -> Bool -> Double #

CDF StdUniform Char # 

Methods

cdf :: StdUniform Char -> Char -> Double #

CDF StdUniform Double # 
CDF StdUniform Float # 

Methods

cdf :: StdUniform Float -> Float -> Double #

CDF StdUniform Int # 

Methods

cdf :: StdUniform Int -> Int -> Double #

CDF StdUniform Int8 # 

Methods

cdf :: StdUniform Int8 -> Int8 -> Double #

CDF StdUniform Int16 # 

Methods

cdf :: StdUniform Int16 -> Int16 -> Double #

CDF StdUniform Int32 # 

Methods

cdf :: StdUniform Int32 -> Int32 -> Double #

CDF StdUniform Int64 # 

Methods

cdf :: StdUniform Int64 -> Int64 -> Double #

CDF StdUniform Ordering # 
CDF StdUniform Word # 

Methods

cdf :: StdUniform Word -> Word -> Double #

CDF StdUniform Word8 # 

Methods

cdf :: StdUniform Word8 -> Word8 -> Double #

CDF StdUniform Word16 # 
CDF StdUniform Word32 # 
CDF StdUniform Word64 # 
CDF StdUniform () # 

Methods

cdf :: StdUniform () -> () -> Double #

PDF StdUniform Double # 
PDF StdUniform Float # 
Distribution StdUniform Bool # 
Distribution StdUniform Char # 
Distribution StdUniform Double # 
Distribution StdUniform Float # 
Distribution StdUniform Int # 
Distribution StdUniform Int8 # 
Distribution StdUniform Int16 # 
Distribution StdUniform Int32 # 
Distribution StdUniform Int64 # 
Distribution StdUniform Ordering # 
Distribution StdUniform Word # 
Distribution StdUniform Word8 # 
Distribution StdUniform Word16 # 
Distribution StdUniform Word32 # 
Distribution StdUniform Word64 # 
Distribution StdUniform () # 

Methods

rvar :: StdUniform () -> RVar () #

rvarT :: StdUniform () -> RVarT n () #

HasResolution r => CDF StdUniform (Fixed r) # 

Methods

cdf :: StdUniform (Fixed r) -> Fixed r -> Double #

HasResolution r => Distribution StdUniform (Fixed r) # 

Methods

rvar :: StdUniform (Fixed r) -> RVar (Fixed r) #

rvarT :: StdUniform (Fixed r) -> RVarT n (Fixed r) #

stdUniform :: Distribution StdUniform a => RVar a #

Get a "standard" uniformly distributed variable. For integral types, this means uniformly distributed over the full range of the type (there is no support for Integer). For fractional types, this means uniformly distributed on the interval [0,1).

stdUniformT :: Distribution StdUniform a => RVarT m a #

Get a "standard" uniformly distributed process. For integral types, this means uniformly distributed over the full range of the type (there is no support for Integer). For fractional types, this means uniformly distributed on the interval [0,1).

data Normal a #

A specification of a normal distribution over the type a.

Constructors

StdNormal

The "standard" normal distribution - mean 0, stddev 1

Normal a a

Normal m s is a normal distribution with mean m and stddev sd.

Instances

normal :: Distribution Normal a => a -> a -> RVar a #

normal m s is a random variable with distribution Normal m s.

stdNormal :: Distribution Normal a => RVar a #

stdNormal is a normal variable with distribution StdNormal.

normalT :: Distribution Normal a => a -> a -> RVarT m a #

normalT m s is a random process with distribution Normal m s.

stdNormalT :: Distribution Normal a => RVarT m a #

stdNormalT is a normal process with distribution StdNormal.

data Gamma a #

Constructors

Gamma a a 

Instances

(Real a, Distribution Gamma a) => CDF Gamma a # 

Methods

cdf :: Gamma a -> a -> Double #

(Floating a, Ord a, Distribution Normal a, Distribution StdUniform a) => Distribution Gamma a # 

Methods

rvar :: Gamma a -> RVar a #

rvarT :: Gamma a -> RVarT n a #

gamma :: Distribution Gamma a => a -> a -> RVar a #

gammaT :: Distribution Gamma a => a -> a -> RVarT m a #

Entropy Sources

class Monad m => MonadRandom m #

A typeclass for monads with a chosen source of entropy. For example, RVar is such a monad - the source from which it is (eventually) sampled is the only source from which a random variable is permitted to draw, so when directly requesting entropy for a random variable these functions are used.

Minimum implementation is either the internal getRandomPrim or all other functions. Additionally, this class's interface is subject to extension at any time, so it is very, very strongly recommended that the monadRandom Template Haskell function be used to implement this function rather than directly implementing it. That function takes care of choosing default implementations for any missing functions; as long as at least one function is implemented, it will derive sensible implementations of all others.

To use monadRandom, just wrap your instance declaration as follows (and enable the TemplateHaskell and GADTs language extensions):

$(monadRandom [d|
        instance MonadRandom FooM where
            getRandomDouble = return pi
            getRandomWord16 = return 4
            {- etc... -}
    |])

class Monad m => RandomSource m s #

A source of entropy which can be used in the given monad.

See also MonadRandom.

Minimum implementation is either the internal getRandomPrimFrom or all other functions. Additionally, this class's interface is subject to extension at any time, so it is very, very strongly recommended that the randomSource Template Haskell function be used to implement this function rather than directly implementing it. That function takes care of choosing default implementations for any missing functions; as long as at least one function is implemented, it will derive sensible implementations of all others.

To use randomSource, just wrap your instance declaration as follows (and enable the TemplateHaskell, MultiParamTypeClasses and GADTs language extensions, as well as any others required by your instances, such as FlexibleInstances):

$(randomSource [d|
        instance RandomSource FooM Bar where
            {- at least one RandomSource function... -}
    |])

data StdRandom :: * #

A token representing the "standard" entropy source in a MonadRandom monad. Its sole purpose is to make the following true (when the types check):

runRVar x StdRandom === sampleRVar

Constructors

StdRandom 

Useful list-based operations

randomElement :: [a] -> RVar a #

A random variable returning an arbitrary element of the given list. Every element has equal probability of being chosen. Because it is a pure RVar it has no memory - that is, it "draws with replacement."

shuffle :: [a] -> RVar [a] #

A random variable that returns the given list in an arbitrary shuffled order. Every ordering of the list has equal probability.

shuffleN :: Int -> [a] -> RVar [a] #

A random variable that shuffles a list of a known length (or a list prefix of the specified length). Useful for shuffling large lists when the length is known in advance. Avoids needing to traverse the list to discover its length. Each ordering has equal probability.

shuffleNofM :: Int -> Int -> [a] -> RVar [a] #

A random variable that selects N arbitrary elements of a list of known length M.