random-fu-0.2.1.1: Random number generation

Data.Random.Distribution

Synopsis

Documentation

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.

Methods

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.

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 () 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 b0) Bool => Distribution (Bernoulli b0) Integer Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Int Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Int8 Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Int16 Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Int32 Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Int64 Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Word Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Word8 Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Word16 Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Word32 Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Word64 Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Float Distribution (Bernoulli b0) Bool => Distribution (Bernoulli b0) Double (Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Integer (Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Int (Floating b0, Ord b0, Distribution Beta b0, Distribution StdUniform b0) => Distribution (Binomial b0) Int8 (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) Int32 (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) Word (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) Word16 (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) Word64 Distribution (Binomial b0) Integer => Distribution (Binomial b0) Float Distribution (Binomial b0) Integer => Distribution (Binomial b0) Double (RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Integer) b0, Distribution (Binomial b0) Integer) => Distribution (Poisson b0) Integer (RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Int) b0, Distribution (Binomial b0) Int) => Distribution (Poisson b0) Int (RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Int8) b0, Distribution (Binomial b0) Int8) => Distribution (Poisson b0) Int8 (RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Int16) b0, Distribution (Binomial b0) Int16) => Distribution (Poisson b0) Int16 (RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Int32) b0, Distribution (Binomial b0) Int32) => Distribution (Poisson b0) Int32 (RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Int64) b0, Distribution (Binomial b0) Int64) => Distribution (Poisson b0) Int64 (RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Word) b0, Distribution (Binomial b0) Word) => Distribution (Poisson b0) Word (RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Word8) b0, Distribution (Binomial b0) Word8) => Distribution (Poisson b0) Word8 (RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Word16) b0, Distribution (Binomial b0) Word16) => Distribution (Poisson b0) Word16 (RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Word32) b0, Distribution (Binomial b0) Word32) => Distribution (Poisson b0) Word32 (RealFloat b0, Distribution StdUniform b0, Distribution (Erlang Word64) b0, Distribution (Binomial b0) Word64) => Distribution (Poisson b0) Word64 Distribution (Poisson b0) Integer => Distribution (Poisson b0) Float Distribution (Poisson b0) Integer => Distribution (Poisson b0) Double (Distribution (Bernoulli b) Bool, RealFloat a) => Distribution (Bernoulli b) (Complex a) (Distribution (Bernoulli b) Bool, Integral a) => Distribution (Bernoulli b) (Ratio a) (Num a, Eq a, Fractional p, Distribution (Binomial p) a) => Distribution (Multinomial p) [a]

class Distribution d t => CDF d t whereSource

Methods

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

Instances

 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 b0) Bool => CDF (Bernoulli b0) Integer CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Int CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Int8 CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Int16 CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Int32 CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Int64 CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Word CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Word8 CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Word16 CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Word32 CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Word64 CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Float CDF (Bernoulli b0) Bool => CDF (Bernoulli b0) Double (Real b0, Distribution (Binomial b0) Integer) => CDF (Binomial b0) Integer (Real b0, Distribution (Binomial b0) Int) => CDF (Binomial b0) Int (Real b0, Distribution (Binomial b0) Int8) => CDF (Binomial b0) Int8 (Real b0, Distribution (Binomial b0) Int16) => CDF (Binomial b0) Int16 (Real b0, Distribution (Binomial b0) Int32) => CDF (Binomial b0) Int32 (Real b0, Distribution (Binomial b0) Int64) => CDF (Binomial b0) Int64 (Real b0, Distribution (Binomial b0) Word) => CDF (Binomial b0) Word (Real b0, Distribution (Binomial b0) Word8) => CDF (Binomial b0) Word8 (Real b0, Distribution (Binomial b0) Word16) => CDF (Binomial b0) Word16 (Real b0, Distribution (Binomial b0) Word32) => CDF (Binomial b0) Word32 (Real b0, Distribution (Binomial b0) Word64) => CDF (Binomial b0) Word64 CDF (Binomial b0) Integer => CDF (Binomial b0) Float CDF (Binomial b0) Integer => CDF (Binomial b0) Double (Real b0, Distribution (Poisson b0) Integer) => CDF (Poisson b0) Integer (Real b0, Distribution (Poisson b0) Int) => CDF (Poisson b0) Int (Real b0, Distribution (Poisson b0) Int8) => CDF (Poisson b0) Int8 (Real b0, Distribution (Poisson b0) Int16) => CDF (Poisson b0) Int16 (Real b0, Distribution (Poisson b0) Int32) => CDF (Poisson b0) Int32 (Real b0, Distribution (Poisson b0) Int64) => CDF (Poisson b0) Int64 (Real b0, Distribution (Poisson b0) Word) => CDF (Poisson b0) Word (Real b0, Distribution (Poisson b0) Word8) => CDF (Poisson b0) Word8 (Real b0, Distribution (Poisson b0) Word16) => CDF (Poisson b0) Word16 (Real b0, Distribution (Poisson b0) Word32) => CDF (Poisson b0) Word32 (Real b0, Distribution (Poisson b0) Word64) => CDF (Poisson b0) Word64 CDF (Poisson b0) Integer => CDF (Poisson b0) Float CDF (Poisson b0) Integer => CDF (Poisson b0) Double (CDF (Bernoulli b) Bool, RealFloat a) => CDF (Bernoulli b) (Complex a) (CDF (Bernoulli b) Bool, Integral a) => CDF (Bernoulli b) (Ratio a)