HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulerandom-fu-0.3.0.1Haskell2010

Data.Random

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.

  • 6 types
  • 6 classes
  • 20 values
  • Packagerandom-fu-0.3.0.1
  • Exports32
  • LanguageHaskell2010
  • LicenceLicenseRef-PublicDomain
  • SourceRandom.hs

Random variables

0 declarations

Abstract (RVar)

typetype 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:

  • Using an immutable pseudo-random number generator that has an instance for RandomGen with StateT monad:

Example4 expressions
import qualified Data.Random as Fu (uniform)import System.Random (mkStdGen)import Control.Monad.State (runState)runState (sampleStateRVar (Fu.uniform 1 (100 :: Integer))) (mkStdGen 2021)(79,StdGen {unStdGen = SMGen 4687568268719557181 4805600293067301895})
  • Using a mutable pseud-random number generator that has an instance for StatefulGen with ReaderT monad.

Example5 expressions
import qualified Data.Random as Fu (uniform)import System.Random.MWC (create)import Control.Monad.Reader (runReaderT)import qualified Data.Vector.Storable as VSinitialize (VS.singleton 2021) >>= runReaderT (sampleReaderRVar (uniform 1 (100 :: Integer)))8
newtypenewtype RVarT (m :: Type -> Type) 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
Instances9MonadTrans, StatefulGen, MonadPrompt, Monad, Functor, Applicative, …
valuerunRVar :: StatefulGen g m => RVar a -> g -> m a
#

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

valuerunRVarTWith
  1. :: StatefulGen g m
  2. => forall t. n t -> m t
  3. -> RVarT n a
  4. -> g
  5. -> 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)

classclass Distribution (d :: Type -> Type) 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.

Instances96Distribution, …
classclass Distribution d t => CDF (d :: Type -> Type) t where
#

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).

Instances88CDF, …
classclass Distribution d t => PDF (d :: Type -> Type) t where
#

Methods

Instances31PDF, …

Sampling random variables

4 declarations
classclass Sampleable (d :: Type -> Type) (m :: Type -> Type) 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.

Methods

  • sampleFrom :: StatefulGen g m => g -> d t -> m t

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

Instances2Sampleable
valuesampleState :: (Distribution d t, RandomGen g, MonadState g m) => d t -> m t
#

Sample a random variable in a "functional" style. Typical instantiations of s are System.Random.StdGen or System.Random.Mersenne.Pure64.PureMT. sample :: (Distribution d a, StatefulGen g m, MonadReader g m) => d t -> m t sample thing gen = runStateGen gen (stateGen -> sampleFrom stateGen thing)

valuesamplePure :: (Distribution d t, RandomGen g) => d t -> g -> (t, g)
#

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

A few very common distributions

14 declarations
datadata 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.

Instances36CDF, Distribution, …
datadata 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).

Instances36PDF, CDF, Distribution, …

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).

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).

datadata 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.

Instances4CDF, Distribution, PDF

Entropy Sources

2 declarations
classclass Monad m => StatefulGen g (m :: Type -> Type) where
#

StatefulGen is an interface to monadic pseudo-random number generators.

Instances8StatefulGen, …
classclass RandomGen g where
#

RandomGen is an interface to pure pseudo-random number generators.

StdGen is the standard RandomGen instance provided by this library.

Instances8RandomGen, …

Useful list-based operations

4 declarations
valuerandomElement :: [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."

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

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

valueshuffleN :: 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.

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

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