random-fu- 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[a137h]) Bool => Distribution (Bernoulli b[a137h]) Word64 
Distribution (Bernoulli b[a137d]) Bool => Distribution (Bernoulli b[a137d]) Word32 
Distribution (Bernoulli b[a1379]) Bool => Distribution (Bernoulli b[a1379]) Word16 
Distribution (Bernoulli b[a1375]) Bool => Distribution (Bernoulli b[a1375]) Word8 
Distribution (Bernoulli b[a1371]) Bool => Distribution (Bernoulli b[a1371]) Word 
Distribution (Bernoulli b[a136X]) Bool => Distribution (Bernoulli b[a136X]) Int64 
Distribution (Bernoulli b[a136T]) Bool => Distribution (Bernoulli b[a136T]) Int32 
Distribution (Bernoulli b[a136P]) Bool => Distribution (Bernoulli b[a136P]) Int16 
Distribution (Bernoulli b[a136L]) Bool => Distribution (Bernoulli b[a136L]) Int8 
Distribution (Bernoulli b[a136H]) Bool => Distribution (Bernoulli b[a136H]) Int 
Distribution (Bernoulli b[a136A]) Bool => Distribution (Bernoulli b[a136A]) Integer 
Distribution (Bernoulli b[a13ou]) Bool => Distribution (Bernoulli b[a13ou]) Double 
Distribution (Bernoulli b[a13oq]) Bool => Distribution (Bernoulli b[a13oq]) 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[a1n1b], Ord b[a1n1b], Distribution Beta b[a1n1b], Distribution StdUniform b[a1n1b]) => Distribution (Binomial b[a1n1b]) Word64 
(Floating b[a1n15], Ord b[a1n15], Distribution Beta b[a1n15], Distribution StdUniform b[a1n15]) => Distribution (Binomial b[a1n15]) Word32 
(Floating b[a1n0Z], Ord b[a1n0Z], Distribution Beta b[a1n0Z], Distribution StdUniform b[a1n0Z]) => Distribution (Binomial b[a1n0Z]) Word16 
(Floating b[a1n0T], Ord b[a1n0T], Distribution Beta b[a1n0T], Distribution StdUniform b[a1n0T]) => Distribution (Binomial b[a1n0T]) Word8 
(Floating b[a1n0N], Ord b[a1n0N], Distribution Beta b[a1n0N], Distribution StdUniform b[a1n0N]) => Distribution (Binomial b[a1n0N]) Word 
(Floating b[a1n0H], Ord b[a1n0H], Distribution Beta b[a1n0H], Distribution StdUniform b[a1n0H]) => Distribution (Binomial b[a1n0H]) Int64 
(Floating b[a1n0B], Ord b[a1n0B], Distribution Beta b[a1n0B], Distribution StdUniform b[a1n0B]) => Distribution (Binomial b[a1n0B]) Int32 
(Floating b[a1n0v], Ord b[a1n0v], Distribution Beta b[a1n0v], Distribution StdUniform b[a1n0v]) => Distribution (Binomial b[a1n0v]) Int16 
(Floating b[a1n0p], Ord b[a1n0p], Distribution Beta b[a1n0p], Distribution StdUniform b[a1n0p]) => Distribution (Binomial b[a1n0p]) Int8 
(Floating b[a1n0j], Ord b[a1n0j], Distribution Beta b[a1n0j], Distribution StdUniform b[a1n0j]) => Distribution (Binomial b[a1n0j]) Int 
(Floating b[a1n0d], Ord b[a1n0d], Distribution Beta b[a1n0d], Distribution StdUniform b[a1n0d]) => Distribution (Binomial b[a1n0d]) Integer 
Distribution (Binomial b[a1nlK]) Integer => Distribution (Binomial b[a1nlK]) Double 
Distribution (Binomial b[a1nlE]) Integer => Distribution (Binomial b[a1nlE]) Float 
(RealFloat b[a1soP], Distribution StdUniform b[a1soP], Distribution (Erlang Word64) b[a1soP], Distribution (Binomial b[a1soP]) Word64) => Distribution (Poisson b[a1soP]) Word64 
(RealFloat b[a1soL], Distribution StdUniform b[a1soL], Distribution (Erlang Word32) b[a1soL], Distribution (Binomial b[a1soL]) Word32) => Distribution (Poisson b[a1soL]) Word32 
(RealFloat b[a1soH], Distribution StdUniform b[a1soH], Distribution (Erlang Word16) b[a1soH], Distribution (Binomial b[a1soH]) Word16) => Distribution (Poisson b[a1soH]) Word16 
(RealFloat b[a1soD], Distribution StdUniform b[a1soD], Distribution (Erlang Word8) b[a1soD], Distribution (Binomial b[a1soD]) Word8) => Distribution (Poisson b[a1soD]) Word8 
(RealFloat b[a1soz], Distribution StdUniform b[a1soz], Distribution (Erlang Word) b[a1soz], Distribution (Binomial b[a1soz]) Word) => Distribution (Poisson b[a1soz]) Word 
(RealFloat b[a1sov], Distribution StdUniform b[a1sov], Distribution (Erlang Int64) b[a1sov], Distribution (Binomial b[a1sov]) Int64) => Distribution (Poisson b[a1sov]) Int64 
(RealFloat b[a1sor], Distribution StdUniform b[a1sor], Distribution (Erlang Int32) b[a1sor], Distribution (Binomial b[a1sor]) Int32) => Distribution (Poisson b[a1sor]) Int32 
(RealFloat b[a1son], Distribution StdUniform b[a1son], Distribution (Erlang Int16) b[a1son], Distribution (Binomial b[a1son]) Int16) => Distribution (Poisson b[a1son]) Int16 
(RealFloat b[a1soj], Distribution StdUniform b[a1soj], Distribution (Erlang Int8) b[a1soj], Distribution (Binomial b[a1soj]) Int8) => Distribution (Poisson b[a1soj]) Int8 
(RealFloat b[a1sof], Distribution StdUniform b[a1sof], Distribution (Erlang Int) b[a1sof], Distribution (Binomial b[a1sof]) Int) => Distribution (Poisson b[a1sof]) Int 
(RealFloat b[a1sob], Distribution StdUniform b[a1sob], Distribution (Erlang Integer) b[a1sob], Distribution (Binomial b[a1sob]) Integer) => Distribution (Poisson b[a1sob]) Integer 
Distribution (Poisson b[a1sPg]) Integer => Distribution (Poisson b[a1sPg]) Double 
Distribution (Poisson b[a1sPc]) Integer => Distribution (Poisson b[a1sPc]) 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[a137j]) Bool => CDF (Bernoulli b[a137j]) Word64 
CDF (Bernoulli b[a137f]) Bool => CDF (Bernoulli b[a137f]) Word32 
CDF (Bernoulli b[a137b]) Bool => CDF (Bernoulli b[a137b]) Word16 
CDF (Bernoulli b[a1377]) Bool => CDF (Bernoulli b[a1377]) Word8 
CDF (Bernoulli b[a1373]) Bool => CDF (Bernoulli b[a1373]) Word 
CDF (Bernoulli b[a136Z]) Bool => CDF (Bernoulli b[a136Z]) Int64 
CDF (Bernoulli b[a136V]) Bool => CDF (Bernoulli b[a136V]) Int32 
CDF (Bernoulli b[a136R]) Bool => CDF (Bernoulli b[a136R]) Int16 
CDF (Bernoulli b[a136N]) Bool => CDF (Bernoulli b[a136N]) Int8 
CDF (Bernoulli b[a136J]) Bool => CDF (Bernoulli b[a136J]) Int 
CDF (Bernoulli b[a136C]) Bool => CDF (Bernoulli b[a136C]) Integer 
CDF (Bernoulli b[a13ow]) Bool => CDF (Bernoulli b[a13ow]) Double 
CDF (Bernoulli b[a13os]) Bool => CDF (Bernoulli b[a13os]) Float 
(Real b[a1n1e], Distribution (Binomial b[a1n1e]) Word64) => CDF (Binomial b[a1n1e]) Word64 
(Real b[a1n18], Distribution (Binomial b[a1n18]) Word32) => CDF (Binomial b[a1n18]) Word32 
(Real b[a1n12], Distribution (Binomial b[a1n12]) Word16) => CDF (Binomial b[a1n12]) Word16 
(Real b[a1n0W], Distribution (Binomial b[a1n0W]) Word8) => CDF (Binomial b[a1n0W]) Word8 
(Real b[a1n0Q], Distribution (Binomial b[a1n0Q]) Word) => CDF (Binomial b[a1n0Q]) Word 
(Real b[a1n0K], Distribution (Binomial b[a1n0K]) Int64) => CDF (Binomial b[a1n0K]) Int64 
(Real b[a1n0E], Distribution (Binomial b[a1n0E]) Int32) => CDF (Binomial b[a1n0E]) Int32 
(Real b[a1n0y], Distribution (Binomial b[a1n0y]) Int16) => CDF (Binomial b[a1n0y]) Int16 
(Real b[a1n0s], Distribution (Binomial b[a1n0s]) Int8) => CDF (Binomial b[a1n0s]) Int8 
(Real b[a1n0m], Distribution (Binomial b[a1n0m]) Int) => CDF (Binomial b[a1n0m]) Int 
(Real b[a1n0g], Distribution (Binomial b[a1n0g]) Integer) => CDF (Binomial b[a1n0g]) Integer 
CDF (Binomial b[a1nlN]) Integer => CDF (Binomial b[a1nlN]) Double 
CDF (Binomial b[a1nlH]) Integer => CDF (Binomial b[a1nlH]) Float 
(Real b[a1soR], Distribution (Poisson b[a1soR]) Word64) => CDF (Poisson b[a1soR]) Word64 
(Real b[a1soN], Distribution (Poisson b[a1soN]) Word32) => CDF (Poisson b[a1soN]) Word32 
(Real b[a1soJ], Distribution (Poisson b[a1soJ]) Word16) => CDF (Poisson b[a1soJ]) Word16 
(Real b[a1soF], Distribution (Poisson b[a1soF]) Word8) => CDF (Poisson b[a1soF]) Word8 
(Real b[a1soB], Distribution (Poisson b[a1soB]) Word) => CDF (Poisson b[a1soB]) Word 
(Real b[a1sox], Distribution (Poisson b[a1sox]) Int64) => CDF (Poisson b[a1sox]) Int64 
(Real b[a1sot], Distribution (Poisson b[a1sot]) Int32) => CDF (Poisson b[a1sot]) Int32 
(Real b[a1sop], Distribution (Poisson b[a1sop]) Int16) => CDF (Poisson b[a1sop]) Int16 
(Real b[a1sol], Distribution (Poisson b[a1sol]) Int8) => CDF (Poisson b[a1sol]) Int8 
(Real b[a1soh], Distribution (Poisson b[a1soh]) Int) => CDF (Poisson b[a1soh]) Int 
(Real b[a1sod], Distribution (Poisson b[a1sod]) Integer) => CDF (Poisson b[a1sod]) Integer 
CDF (Poisson b[a1sPi]) Integer => CDF (Poisson b[a1sPi]) Double 
CDF (Poisson b[a1sPe]) Integer => CDF (Poisson b[a1sPe]) Float 
(CDF (Bernoulli b) Bool, RealFloat a) => CDF (Bernoulli b) (Complex a) 
(CDF (Bernoulli b) Bool, Integral a) => CDF (Bernoulli b) (Ratio a)