random-fu-0.2: 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 Exponential a 
Distribution Normal Double 
Distribution Normal Float 
(Floating a, Ord a, Distribution Normal a, Distribution StdUniform a) => Distribution Gamma a 
(Fractional t, Distribution Gamma t) => Distribution ChiSquare t 
(RealFloat a, Distribution StdUniform a) => Distribution Rayleigh a 
(RealFloat a, Ord a, Distribution StdUniform a) => Distribution Triangular a 
(Floating a, Distribution StdUniform a) => Distribution Weibull a 
Distribution Beta Double 
Distribution Beta Float 
HasResolution r => Distribution StdUniform (Fixed r) 
HasResolution r => Distribution Uniform (Fixed r) 
(Fractional a, Distribution Gamma a) => Distribution Dirichlet [a] 
(Fractional p, Ord p, Distribution Uniform 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 
(Fractional b, Ord b, Distribution StdUniform b) => Distribution (Bernoulli b) Bool 
Distribution (Bernoulli b[aETu]) Bool => Distribution (Bernoulli b[aETu]) Word64 
Distribution (Bernoulli b[aETq]) Bool => Distribution (Bernoulli b[aETq]) Word32 
Distribution (Bernoulli b[aETm]) Bool => Distribution (Bernoulli b[aETm]) Word16 
Distribution (Bernoulli b[aETi]) Bool => Distribution (Bernoulli b[aETi]) Word8 
Distribution (Bernoulli b[aETe]) Bool => Distribution (Bernoulli b[aETe]) Word 
Distribution (Bernoulli b[aETa]) Bool => Distribution (Bernoulli b[aETa]) Int64 
Distribution (Bernoulli b[aET6]) Bool => Distribution (Bernoulli b[aET6]) Int32 
Distribution (Bernoulli b[aET2]) Bool => Distribution (Bernoulli b[aET2]) Int16 
Distribution (Bernoulli b[aESY]) Bool => Distribution (Bernoulli b[aESY]) Int8 
Distribution (Bernoulli b[aESU]) Bool => Distribution (Bernoulli b[aESU]) Int 
Distribution (Bernoulli b[aESN]) Bool => Distribution (Bernoulli b[aESN]) Integer 
Distribution (Bernoulli b[aF6J]) Bool => Distribution (Bernoulli b[aF6J]) Double 
Distribution (Bernoulli b[aF6F]) Bool => Distribution (Bernoulli b[aF6F]) Float 
(Floating b[aImY], Ord b[aImY], Distribution Beta b[aImY], Distribution StdUniform b[aImY]) => Distribution (Binomial b[aImY]) Word64 
(Floating b[aImS], Ord b[aImS], Distribution Beta b[aImS], Distribution StdUniform b[aImS]) => Distribution (Binomial b[aImS]) Word32 
(Floating b[aImM], Ord b[aImM], Distribution Beta b[aImM], Distribution StdUniform b[aImM]) => Distribution (Binomial b[aImM]) Word16 
(Floating b[aImG], Ord b[aImG], Distribution Beta b[aImG], Distribution StdUniform b[aImG]) => Distribution (Binomial b[aImG]) Word8 
(Floating b[aImA], Ord b[aImA], Distribution Beta b[aImA], Distribution StdUniform b[aImA]) => Distribution (Binomial b[aImA]) Word 
(Floating b[aImu], Ord b[aImu], Distribution Beta b[aImu], Distribution StdUniform b[aImu]) => Distribution (Binomial b[aImu]) Int64 
(Floating b[aImo], Ord b[aImo], Distribution Beta b[aImo], Distribution StdUniform b[aImo]) => Distribution (Binomial b[aImo]) Int32 
(Floating b[aImi], Ord b[aImi], Distribution Beta b[aImi], Distribution StdUniform b[aImi]) => Distribution (Binomial b[aImi]) Int16 
(Floating b[aImc], Ord b[aImc], Distribution Beta b[aImc], Distribution StdUniform b[aImc]) => Distribution (Binomial b[aImc]) Int8 
(Floating b[aIm6], Ord b[aIm6], Distribution Beta b[aIm6], Distribution StdUniform b[aIm6]) => Distribution (Binomial b[aIm6]) Int 
(Floating b[aIm0], Ord b[aIm0], Distribution Beta b[aIm0], Distribution StdUniform b[aIm0]) => Distribution (Binomial b[aIm0]) Integer 
Distribution (Binomial b[aIAC]) Integer => Distribution (Binomial b[aIAC]) Double 
Distribution (Binomial b[aIAw]) Integer => Distribution (Binomial b[aIAw]) Float 
(RealFloat b[aNMc], Distribution StdUniform b[aNMc], Distribution (Erlang Word64) b[aNMc], Distribution (Binomial b[aNMc]) Word64) => Distribution (Poisson b[aNMc]) Word64 
(RealFloat b[aNM8], Distribution StdUniform b[aNM8], Distribution (Erlang Word32) b[aNM8], Distribution (Binomial b[aNM8]) Word32) => Distribution (Poisson b[aNM8]) Word32 
(RealFloat b[aNM4], Distribution StdUniform b[aNM4], Distribution (Erlang Word16) b[aNM4], Distribution (Binomial b[aNM4]) Word16) => Distribution (Poisson b[aNM4]) Word16 
(RealFloat b[aNM0], Distribution StdUniform b[aNM0], Distribution (Erlang Word8) b[aNM0], Distribution (Binomial b[aNM0]) Word8) => Distribution (Poisson b[aNM0]) Word8 
(RealFloat b[aNLW], Distribution StdUniform b[aNLW], Distribution (Erlang Word) b[aNLW], Distribution (Binomial b[aNLW]) Word) => Distribution (Poisson b[aNLW]) Word 
(RealFloat b[aNLS], Distribution StdUniform b[aNLS], Distribution (Erlang Int64) b[aNLS], Distribution (Binomial b[aNLS]) Int64) => Distribution (Poisson b[aNLS]) Int64 
(RealFloat b[aNLO], Distribution StdUniform b[aNLO], Distribution (Erlang Int32) b[aNLO], Distribution (Binomial b[aNLO]) Int32) => Distribution (Poisson b[aNLO]) Int32 
(RealFloat b[aNLK], Distribution StdUniform b[aNLK], Distribution (Erlang Int16) b[aNLK], Distribution (Binomial b[aNLK]) Int16) => Distribution (Poisson b[aNLK]) Int16 
(RealFloat b[aNLG], Distribution StdUniform b[aNLG], Distribution (Erlang Int8) b[aNLG], Distribution (Binomial b[aNLG]) Int8) => Distribution (Poisson b[aNLG]) Int8 
(RealFloat b[aNLC], Distribution StdUniform b[aNLC], Distribution (Erlang Int) b[aNLC], Distribution (Binomial b[aNLC]) Int) => Distribution (Poisson b[aNLC]) Int 
(RealFloat b[aNLy], Distribution StdUniform b[aNLy], Distribution (Erlang Integer) b[aNLy], Distribution (Binomial b[aNLy]) Integer) => Distribution (Poisson b[aNLy]) Integer 
Distribution (Poisson b[aNYN]) Integer => Distribution (Poisson b[aNYN]) Double 
Distribution (Poisson b[aNYJ]) Integer => Distribution (Poisson b[aNYJ]) 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 Exponential a) => CDF Exponential a 
(Real a, Distribution Normal a) => CDF Normal a 
(Real a, Distribution Gamma a) => CDF Gamma a 
(Real t, Distribution ChiSquare t) => CDF ChiSquare t 
(Real a, Distribution Rayleigh a) => CDF Rayleigh a 
(RealFrac a, Distribution Triangular a) => CDF Triangular a 
(Real a, Distribution Weibull a) => CDF Weibull a 
HasResolution r => CDF StdUniform (Fixed r) 
HasResolution r => CDF Uniform (Fixed r) 
(Integral a, Real b, Distribution (Erlang a) b) => CDF (Erlang a) b 
(Distribution (Bernoulli b) Bool, Real b) => CDF (Bernoulli b) Bool 
CDF (Bernoulli b[aETw]) Bool => CDF (Bernoulli b[aETw]) Word64 
CDF (Bernoulli b[aETs]) Bool => CDF (Bernoulli b[aETs]) Word32 
CDF (Bernoulli b[aETo]) Bool => CDF (Bernoulli b[aETo]) Word16 
CDF (Bernoulli b[aETk]) Bool => CDF (Bernoulli b[aETk]) Word8 
CDF (Bernoulli b[aETg]) Bool => CDF (Bernoulli b[aETg]) Word 
CDF (Bernoulli b[aETc]) Bool => CDF (Bernoulli b[aETc]) Int64 
CDF (Bernoulli b[aET8]) Bool => CDF (Bernoulli b[aET8]) Int32 
CDF (Bernoulli b[aET4]) Bool => CDF (Bernoulli b[aET4]) Int16 
CDF (Bernoulli b[aET0]) Bool => CDF (Bernoulli b[aET0]) Int8 
CDF (Bernoulli b[aESW]) Bool => CDF (Bernoulli b[aESW]) Int 
CDF (Bernoulli b[aESP]) Bool => CDF (Bernoulli b[aESP]) Integer 
CDF (Bernoulli b[aF6L]) Bool => CDF (Bernoulli b[aF6L]) Double 
CDF (Bernoulli b[aF6H]) Bool => CDF (Bernoulli b[aF6H]) Float 
(Real b[aIn1], Distribution (Binomial b[aIn1]) Word64) => CDF (Binomial b[aIn1]) Word64 
(Real b[aImV], Distribution (Binomial b[aImV]) Word32) => CDF (Binomial b[aImV]) Word32 
(Real b[aImP], Distribution (Binomial b[aImP]) Word16) => CDF (Binomial b[aImP]) Word16 
(Real b[aImJ], Distribution (Binomial b[aImJ]) Word8) => CDF (Binomial b[aImJ]) Word8 
(Real b[aImD], Distribution (Binomial b[aImD]) Word) => CDF (Binomial b[aImD]) Word 
(Real b[aImx], Distribution (Binomial b[aImx]) Int64) => CDF (Binomial b[aImx]) Int64 
(Real b[aImr], Distribution (Binomial b[aImr]) Int32) => CDF (Binomial b[aImr]) Int32 
(Real b[aIml], Distribution (Binomial b[aIml]) Int16) => CDF (Binomial b[aIml]) Int16 
(Real b[aImf], Distribution (Binomial b[aImf]) Int8) => CDF (Binomial b[aImf]) Int8 
(Real b[aIm9], Distribution (Binomial b[aIm9]) Int) => CDF (Binomial b[aIm9]) Int 
(Real b[aIm3], Distribution (Binomial b[aIm3]) Integer) => CDF (Binomial b[aIm3]) Integer 
CDF (Binomial b[aIAF]) Integer => CDF (Binomial b[aIAF]) Double 
CDF (Binomial b[aIAz]) Integer => CDF (Binomial b[aIAz]) Float 
(Real b[aNMe], Distribution (Poisson b[aNMe]) Word64) => CDF (Poisson b[aNMe]) Word64 
(Real b[aNMa], Distribution (Poisson b[aNMa]) Word32) => CDF (Poisson b[aNMa]) Word32 
(Real b[aNM6], Distribution (Poisson b[aNM6]) Word16) => CDF (Poisson b[aNM6]) Word16 
(Real b[aNM2], Distribution (Poisson b[aNM2]) Word8) => CDF (Poisson b[aNM2]) Word8 
(Real b[aNLY], Distribution (Poisson b[aNLY]) Word) => CDF (Poisson b[aNLY]) Word 
(Real b[aNLU], Distribution (Poisson b[aNLU]) Int64) => CDF (Poisson b[aNLU]) Int64 
(Real b[aNLQ], Distribution (Poisson b[aNLQ]) Int32) => CDF (Poisson b[aNLQ]) Int32 
(Real b[aNLM], Distribution (Poisson b[aNLM]) Int16) => CDF (Poisson b[aNLM]) Int16 
(Real b[aNLI], Distribution (Poisson b[aNLI]) Int8) => CDF (Poisson b[aNLI]) Int8 
(Real b[aNLE], Distribution (Poisson b[aNLE]) Int) => CDF (Poisson b[aNLE]) Int 
(Real b[aNLA], Distribution (Poisson b[aNLA]) Integer) => CDF (Poisson b[aNLA]) Integer 
CDF (Poisson b[aNYP]) Integer => CDF (Poisson b[aNYP]) Double 
CDF (Poisson b[aNYL]) Integer => CDF (Poisson b[aNYL]) Float 
(CDF (Bernoulli b) Bool, RealFloat a) => CDF (Bernoulli b) (Complex a) 
(CDF (Bernoulli b) Bool, Integral a) => CDF (Bernoulli b) (Ratio a)