random-fu-0.1.4: Random number generation




class Distribution d t whereSource

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.


rvar :: d t -> RVar tSource

Return a random variable with this distribution.

rvarT :: d t -> RVarT n tSource

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.


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 () 
Distribution Uniform Bool 
Distribution Uniform Char 
Distribution Uniform Double 
Distribution Uniform Float 
Distribution Uniform 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 () 
(Floating a, Distribution StdUniform a) => Distribution Weibull a 
(Floating a, Distribution StdUniform a) => Distribution Exponential a 
Distribution Normal Double 
Distribution Normal Float 
(Floating a, Ord a, Distribution Normal a, Distribution StdUniform a) => Distribution Gamma a 
Distribution Beta Double 
Distribution Beta Float 
(RealFloat a, Distribution StdUniform a) => Distribution Rayleigh a 
(RealFloat a, Ord a, Distribution StdUniform a) => Distribution Triangular a 
HasResolution r => Distribution StdUniform (Fixed r) 
HasResolution r => Distribution Uniform (Fixed r) 
(Fractional a, Distribution Gamma a) => Distribution Dirichlet [a] 
(Fractional b, Ord b, Distribution StdUniform b) => Distribution (Bernoulli b) Bool 
Distribution (Bernoulli b[a16gB]) Bool => Distribution (Bernoulli b[a16gB]) Word64 
Distribution (Bernoulli b[a16gx]) Bool => Distribution (Bernoulli b[a16gx]) Word32 
Distribution (Bernoulli b[a16gt]) Bool => Distribution (Bernoulli b[a16gt]) Word16 
Distribution (Bernoulli b[a16gp]) Bool => Distribution (Bernoulli b[a16gp]) Word8 
Distribution (Bernoulli b[a16gl]) Bool => Distribution (Bernoulli b[a16gl]) Word 
Distribution (Bernoulli b[a16gh]) Bool => Distribution (Bernoulli b[a16gh]) Int64 
Distribution (Bernoulli b[a16gd]) Bool => Distribution (Bernoulli b[a16gd]) Int32 
Distribution (Bernoulli b[a16g9]) Bool => Distribution (Bernoulli b[a16g9]) Int16 
Distribution (Bernoulli b[a16g5]) Bool => Distribution (Bernoulli b[a16g5]) Int8 
Distribution (Bernoulli b[a16g1]) Bool => Distribution (Bernoulli b[a16g1]) Int 
Distribution (Bernoulli b[a16fU]) Bool => Distribution (Bernoulli b[a16fU]) Integer 
Distribution (Bernoulli b[a16tQ]) Bool => Distribution (Bernoulli b[a16tQ]) Double 
Distribution (Bernoulli b[a16tM]) Bool => Distribution (Bernoulli b[a16tM]) Float 
(Fractional p, Ord p, Distribution StdUniform p) => Distribution (Categorical p) a 
(Num t, Ord t, Vector v t) => Distribution (Ziggurat v) t 
(Integral a, Floating b, Ord b, Distribution Normal b, Distribution StdUniform b) => Distribution (Erlang a) b 
(Floating b[a1ogO], Ord b[a1ogO], Distribution Beta b[a1ogO], Distribution StdUniform b[a1ogO]) => Distribution (Binomial b[a1ogO]) Word64 
(Floating b[a1ogI], Ord b[a1ogI], Distribution Beta b[a1ogI], Distribution StdUniform b[a1ogI]) => Distribution (Binomial b[a1ogI]) Word32 
(Floating b[a1ogC], Ord b[a1ogC], Distribution Beta b[a1ogC], Distribution StdUniform b[a1ogC]) => Distribution (Binomial b[a1ogC]) Word16 
(Floating b[a1ogw], Ord b[a1ogw], Distribution Beta b[a1ogw], Distribution StdUniform b[a1ogw]) => Distribution (Binomial b[a1ogw]) Word8 
(Floating b[a1ogq], Ord b[a1ogq], Distribution Beta b[a1ogq], Distribution StdUniform b[a1ogq]) => Distribution (Binomial b[a1ogq]) Word 
(Floating b[a1ogk], Ord b[a1ogk], Distribution Beta b[a1ogk], Distribution StdUniform b[a1ogk]) => Distribution (Binomial b[a1ogk]) Int64 
(Floating b[a1oge], Ord b[a1oge], Distribution Beta b[a1oge], Distribution StdUniform b[a1oge]) => Distribution (Binomial b[a1oge]) Int32 
(Floating b[a1og8], Ord b[a1og8], Distribution Beta b[a1og8], Distribution StdUniform b[a1og8]) => Distribution (Binomial b[a1og8]) Int16 
(Floating b[a1og2], Ord b[a1og2], Distribution Beta b[a1og2], Distribution StdUniform b[a1og2]) => Distribution (Binomial b[a1og2]) Int8 
(Floating b[a1ofW], Ord b[a1ofW], Distribution Beta b[a1ofW], Distribution StdUniform b[a1ofW]) => Distribution (Binomial b[a1ofW]) Int 
(Floating b[a1ofQ], Ord b[a1ofQ], Distribution Beta b[a1ofQ], Distribution StdUniform b[a1ofQ]) => Distribution (Binomial b[a1ofQ]) Integer 
Distribution (Binomial b[a1ous]) Integer => Distribution (Binomial b[a1ous]) Double 
Distribution (Binomial b[a1oum]) Integer => Distribution (Binomial b[a1oum]) Float 
(RealFloat b[a1u9B], Distribution StdUniform b[a1u9B], Distribution (Erlang Word64) b[a1u9B], Distribution (Binomial b[a1u9B]) Word64) => Distribution (Poisson b[a1u9B]) Word64 
(RealFloat b[a1u9x], Distribution StdUniform b[a1u9x], Distribution (Erlang Word32) b[a1u9x], Distribution (Binomial b[a1u9x]) Word32) => Distribution (Poisson b[a1u9x]) Word32 
(RealFloat b[a1u9t], Distribution StdUniform b[a1u9t], Distribution (Erlang Word16) b[a1u9t], Distribution (Binomial b[a1u9t]) Word16) => Distribution (Poisson b[a1u9t]) Word16 
(RealFloat b[a1u9p], Distribution StdUniform b[a1u9p], Distribution (Erlang Word8) b[a1u9p], Distribution (Binomial b[a1u9p]) Word8) => Distribution (Poisson b[a1u9p]) Word8 
(RealFloat b[a1u9l], Distribution StdUniform b[a1u9l], Distribution (Erlang Word) b[a1u9l], Distribution (Binomial b[a1u9l]) Word) => Distribution (Poisson b[a1u9l]) Word 
(RealFloat b[a1u9h], Distribution StdUniform b[a1u9h], Distribution (Erlang Int64) b[a1u9h], Distribution (Binomial b[a1u9h]) Int64) => Distribution (Poisson b[a1u9h]) Int64 
(RealFloat b[a1u9d], Distribution StdUniform b[a1u9d], Distribution (Erlang Int32) b[a1u9d], Distribution (Binomial b[a1u9d]) Int32) => Distribution (Poisson b[a1u9d]) Int32 
(RealFloat b[a1u99], Distribution StdUniform b[a1u99], Distribution (Erlang Int16) b[a1u99], Distribution (Binomial b[a1u99]) Int16) => Distribution (Poisson b[a1u99]) Int16 
(RealFloat b[a1u95], Distribution StdUniform b[a1u95], Distribution (Erlang Int8) b[a1u95], Distribution (Binomial b[a1u95]) Int8) => Distribution (Poisson b[a1u95]) Int8 
(RealFloat b[a1u91], Distribution StdUniform b[a1u91], Distribution (Erlang Int) b[a1u91], Distribution (Binomial b[a1u91]) Int) => Distribution (Poisson b[a1u91]) Int 
(RealFloat b[a1u8X], Distribution StdUniform b[a1u8X], Distribution (Erlang Integer) b[a1u8X], Distribution (Binomial b[a1u8X]) Integer) => Distribution (Poisson b[a1u8X]) Integer 
Distribution (Poisson b[a1umc]) Integer => Distribution (Poisson b[a1umc]) Double 
Distribution (Poisson b[a1um8]) Integer => Distribution (Poisson b[a1um8]) Float 
(Distribution (Bernoulli b) Bool, RealFloat a) => Distribution (Bernoulli b) (Complex a) 
(Distribution (Bernoulli b) Bool, Integral a) => Distribution (Bernoulli b) (Ratio a) 
(Num a, Fractional p, Distribution (Binomial p) a) => Distribution (Multinomial p) [a] 

class Distribution d t => CDF d t whereSource


cdf :: d t -> t -> DoubleSource

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


CDF StdUniform Bool 
CDF StdUniform Char 
CDF StdUniform Double 
CDF StdUniform Float 
CDF StdUniform Int 
CDF StdUniform Int8 
CDF StdUniform Int16 
CDF StdUniform Int32 
CDF StdUniform Int64 
CDF StdUniform Ordering 
CDF StdUniform Word 
CDF StdUniform Word8 
CDF StdUniform Word16 
CDF StdUniform Word32 
CDF StdUniform Word64 
CDF StdUniform () 
CDF Uniform Bool 
CDF Uniform Char 
CDF Uniform Double 
CDF Uniform Float 
CDF Uniform Int 
CDF Uniform Int8 
CDF Uniform Int16 
CDF Uniform Int32 
CDF Uniform Int64 
CDF Uniform Integer 
CDF Uniform Ordering 
CDF Uniform Word 
CDF Uniform Word8 
CDF Uniform Word16 
CDF Uniform Word32 
CDF Uniform Word64 
CDF Uniform () 
(Real a, Distribution Weibull a) => CDF Weibull a 
(Real a, Distribution Exponential a) => CDF Exponential a 
(Real a, Distribution Normal a) => CDF Normal a 
(Real a, Distribution Rayleigh a) => CDF Rayleigh a 
(RealFrac a, Distribution Triangular a) => CDF Triangular a 
HasResolution r => CDF StdUniform (Fixed r) 
HasResolution r => CDF Uniform (Fixed r) 
(Distribution (Bernoulli b) Bool, Real b) => CDF (Bernoulli b) Bool 
CDF (Bernoulli b[a16gD]) Bool => CDF (Bernoulli b[a16gD]) Word64 
CDF (Bernoulli b[a16gz]) Bool => CDF (Bernoulli b[a16gz]) Word32 
CDF (Bernoulli b[a16gv]) Bool => CDF (Bernoulli b[a16gv]) Word16 
CDF (Bernoulli b[a16gr]) Bool => CDF (Bernoulli b[a16gr]) Word8 
CDF (Bernoulli b[a16gn]) Bool => CDF (Bernoulli b[a16gn]) Word 
CDF (Bernoulli b[a16gj]) Bool => CDF (Bernoulli b[a16gj]) Int64 
CDF (Bernoulli b[a16gf]) Bool => CDF (Bernoulli b[a16gf]) Int32 
CDF (Bernoulli b[a16gb]) Bool => CDF (Bernoulli b[a16gb]) Int16 
CDF (Bernoulli b[a16g7]) Bool => CDF (Bernoulli b[a16g7]) Int8 
CDF (Bernoulli b[a16g3]) Bool => CDF (Bernoulli b[a16g3]) Int 
CDF (Bernoulli b[a16fW]) Bool => CDF (Bernoulli b[a16fW]) Integer 
CDF (Bernoulli b[a16tS]) Bool => CDF (Bernoulli b[a16tS]) Double 
CDF (Bernoulli b[a16tO]) Bool => CDF (Bernoulli b[a16tO]) Float 
(Real b[a1ogR], Distribution (Binomial b[a1ogR]) Word64) => CDF (Binomial b[a1ogR]) Word64 
(Real b[a1ogL], Distribution (Binomial b[a1ogL]) Word32) => CDF (Binomial b[a1ogL]) Word32 
(Real b[a1ogF], Distribution (Binomial b[a1ogF]) Word16) => CDF (Binomial b[a1ogF]) Word16 
(Real b[a1ogz], Distribution (Binomial b[a1ogz]) Word8) => CDF (Binomial b[a1ogz]) Word8 
(Real b[a1ogt], Distribution (Binomial b[a1ogt]) Word) => CDF (Binomial b[a1ogt]) Word 
(Real b[a1ogn], Distribution (Binomial b[a1ogn]) Int64) => CDF (Binomial b[a1ogn]) Int64 
(Real b[a1ogh], Distribution (Binomial b[a1ogh]) Int32) => CDF (Binomial b[a1ogh]) Int32 
(Real b[a1ogb], Distribution (Binomial b[a1ogb]) Int16) => CDF (Binomial b[a1ogb]) Int16 
(Real b[a1og5], Distribution (Binomial b[a1og5]) Int8) => CDF (Binomial b[a1og5]) Int8 
(Real b[a1ofZ], Distribution (Binomial b[a1ofZ]) Int) => CDF (Binomial b[a1ofZ]) Int 
(Real b[a1ofT], Distribution (Binomial b[a1ofT]) Integer) => CDF (Binomial b[a1ofT]) Integer 
CDF (Binomial b[a1ouv]) Integer => CDF (Binomial b[a1ouv]) Double 
CDF (Binomial b[a1oup]) Integer => CDF (Binomial b[a1oup]) Float 
(Real b[a1u9D], Distribution (Poisson b[a1u9D]) Word64) => CDF (Poisson b[a1u9D]) Word64 
(Real b[a1u9z], Distribution (Poisson b[a1u9z]) Word32) => CDF (Poisson b[a1u9z]) Word32 
(Real b[a1u9v], Distribution (Poisson b[a1u9v]) Word16) => CDF (Poisson b[a1u9v]) Word16 
(Real b[a1u9r], Distribution (Poisson b[a1u9r]) Word8) => CDF (Poisson b[a1u9r]) Word8 
(Real b[a1u9n], Distribution (Poisson b[a1u9n]) Word) => CDF (Poisson b[a1u9n]) Word 
(Real b[a1u9j], Distribution (Poisson b[a1u9j]) Int64) => CDF (Poisson b[a1u9j]) Int64 
(Real b[a1u9f], Distribution (Poisson b[a1u9f]) Int32) => CDF (Poisson b[a1u9f]) Int32 
(Real b[a1u9b], Distribution (Poisson b[a1u9b]) Int16) => CDF (Poisson b[a1u9b]) Int16 
(Real b[a1u97], Distribution (Poisson b[a1u97]) Int8) => CDF (Poisson b[a1u97]) Int8 
(Real b[a1u93], Distribution (Poisson b[a1u93]) Int) => CDF (Poisson b[a1u93]) Int 
(Real b[a1u8Z], Distribution (Poisson b[a1u8Z]) Integer) => CDF (Poisson b[a1u8Z]) Integer 
CDF (Poisson b[a1ume]) Integer => CDF (Poisson b[a1ume]) Double 
CDF (Poisson b[a1uma]) Integer => CDF (Poisson b[a1uma]) Float 
(CDF (Bernoulli b) Bool, RealFloat a) => CDF (Bernoulli b) (Complex a) 
(CDF (Bernoulli b) Bool, Integral a) => CDF (Bernoulli b) (Ratio a)