Safe Haskell | None |
---|---|
Language | Haskell2010 |
The Random
effect provides access to uniformly distributed random values of
user-specified types or from well-known numerical distributions.
This is the “fancy” syntax that hides most details of randomness behind a nice API.
Synopsis
- data Random (m :: Type -> Type) k where
- uniform :: (Variate a, Has Random sig m) => m a
- uniformR :: (Variate a, Has Random sig m) => (a, a) -> m a
- normal :: Has Random sig m => Double -> Double -> m Double
- standard :: Has Random sig m => m Double
- exponential :: Has Random sig m => Double -> m Double
- truncatedExp :: Has Random sig m => Double -> (Double, Double) -> m Double
- gamma :: Has Random sig m => Double -> Double -> m Double
- chiSquare :: Has Random sig m => Int -> m Double
- beta :: Has Random sig m => Double -> Double -> m Double
- categorical :: (Has Random sig m, Vector v Double) => v Double -> m Int
- logCategorical :: (Has Random sig m, Vector v Double) => v Double -> m Int
- geometric0 :: Has Random sig m => Double -> m Int
- geometric1 :: Has Random sig m => Double -> m Int
- bernoulli :: Has Random sig m => Double -> m Bool
- dirichlet :: (Has Random sig m, Traversable t) => t Double -> m (t Double)
- uniformPermutation :: (Has Random sig m, Vector v Int) => Int -> m (v Int)
- uniformShuffle :: (Has Random sig m, Vector v a) => v a -> m (v a)
- save :: Has Random sig m => m Seed
- data Distrib a where
- Uniform :: Variate a => Distrib a
- UniformR :: Variate a => (a, a) -> Distrib a
- Normal :: Double -> Double -> Distrib Double
- Standard :: Distrib Double
- Exponential :: Double -> Distrib Double
- TruncatedExp :: Double -> (Double, Double) -> Distrib Double
- Gamma :: Double -> Double -> Distrib Double
- ChiSquare :: Int -> Distrib Double
- Beta :: Double -> Double -> Distrib Double
- Categorical :: Vector v Double => v Double -> Distrib Int
- LogCategorical :: Vector v Double => v Double -> Distrib Int
- Geometric0 :: Double -> Distrib Int
- Geometric1 :: Double -> Distrib Int
- Bernoulli :: Double -> Distrib Bool
- Dirichlet :: Traversable t => t Double -> Distrib (t Double)
- Permutation :: Vector v Int => Int -> Distrib (v Int)
- Shuffle :: Vector v a => v a -> Distrib (v a)
- class Variate a
- type Has (eff :: (Type -> Type) -> Type -> Type) (sig :: (Type -> Type) -> Type -> Type) (m :: Type -> Type) = (Members eff sig, Algebra sig m)
Documentation
Uniform distributions
uniform :: (Variate a, Has Random sig m) => m a Source #
Generate a single uniformly distributed random variate. The range of values produced varies by type:
- For fixed-width integral types, the type's entire range is used.
- For floating point numbers, the range (0,1] is used. Zero is
explicitly excluded, to allow variates to be used in
statistical calculations that require non-zero values
(e.g. uses of the
log
function).
To generate a Float
variate with a range of [0,1), subtract
2**(-33). To do the same with Double
variates, subtract
2**(-53).
uniformR :: (Variate a, Has Random sig m) => (a, a) -> m a Source #
Generate single uniformly distributed random variable in a given range.
- For integral types inclusive range is used.
- For floating point numbers range (a,b] is used if one ignores rounding errors.
Continuous distributions
Generate a normally distributed random variate with given mean and standard deviation.
standard :: Has Random sig m => m Double Source #
Generate a normally distributed random variate with zero mean and unit variance.
Generate an exponentially distributed random variate.
:: Has Random sig m | |
=> Double | Scale parameter |
-> (Double, Double) | Range to which distribution is truncated. Values may be negative. |
-> m Double |
Generate truncated exponentially distributed random variate.
Random variate generator for gamma distribution.
Random variate generator for the chi square distribution.
Random variate generator for Beta distribution
Discrete distributions
Random variate generator for categorical distribution.
Random variate generator for categorical distribution where the
weights are in the log domain. It's implemented in terms of
categorical
.
Random variate generator for the geometric distribution, computing the number of failures before success. Distribution's support is [0..].
Random variate generator for geometric distribution for number of
trials. Distribution's support is [1..] (i.e. just geometric0
shifted by 1).
Random variate generator for Bernoulli distribution
:: (Has Random sig m, Traversable t) | |
=> t Double | container of parameters |
-> m (t Double) |
Random variate generator for Dirichlet distribution
Permutations
uniformPermutation :: (Has Random sig m, Vector v Int) => Int -> m (v Int) Source #
Random variate generator for uniformly distributed permutations. It returns random permutation of vector [0 .. n-1]. This is the Fisher-Yates shuffle.
uniformShuffle :: (Has Random sig m, Vector v a) => v a -> m (v a) Source #
Random variate generator for a uniformly distributed shuffle (all shuffles are equiprobable) of a vector. It uses Fisher-Yates shuffle algorithm.
Implementation details prevent a native implementation of the uniformShuffleM
function. Use the native API if this is required.
Introspection
save :: Has Random sig m => m Seed Source #
Save the state of the random number generator to be used by subsequent carrier invocations.
GADT representing the functions provided by mwc-random.
Uniform :: Variate a => Distrib a | |
UniformR :: Variate a => (a, a) -> Distrib a | |
Normal :: Double -> Double -> Distrib Double | |
Standard :: Distrib Double | |
Exponential :: Double -> Distrib Double | |
TruncatedExp :: Double -> (Double, Double) -> Distrib Double | |
Gamma :: Double -> Double -> Distrib Double | |
ChiSquare :: Int -> Distrib Double | |
Beta :: Double -> Double -> Distrib Double | |
Categorical :: Vector v Double => v Double -> Distrib Int | |
LogCategorical :: Vector v Double => v Double -> Distrib Int | |
Geometric0 :: Double -> Distrib Int | |
Geometric1 :: Double -> Distrib Int | |
Bernoulli :: Double -> Distrib Bool | |
Dirichlet :: Traversable t => t Double -> Distrib (t Double) | |
Permutation :: Vector v Int => Int -> Distrib (v Int) | |
Shuffle :: Vector v a => v a -> Distrib (v a) |
Re-exports
The class of types for which we can generate uniformly distributed random variates.
The uniform PRNG uses Marsaglia's MWC256 (also known as MWC8222) multiply-with-carry generator, which has a period of 2^8222 and fares well in tests of randomness. It is also extremely fast, between 2 and 3 times faster than the Mersenne Twister.
Note: Marsaglia's PRNG is not known to be cryptographically secure, so you should not use it for cryptographic operations.
Instances
Variate Bool | |
Variate Double | |
Variate Float | |
Variate Int | |
Variate Int8 | |
Variate Int16 | |
Variate Int32 | |
Variate Int64 | |
Variate Word | |
Variate Word8 | |
Variate Word16 | |
Variate Word32 | |
Variate Word64 | |
(Variate a, Variate b) => Variate (a, b) | |
(Variate a, Variate b, Variate c) => Variate (a, b, c) | |
(Variate a, Variate b, Variate c, Variate d) => Variate (a, b, c, d) | |
type Has (eff :: (Type -> Type) -> Type -> Type) (sig :: (Type -> Type) -> Type -> Type) (m :: Type -> Type) = (Members eff sig, Algebra sig m) #
m
is a carrier for sig
containing eff
.
Note that if eff
is a sum, it will be decomposed into multiple Member
constraints. While this technically allows one to combine multiple unrelated effects into a single Has
constraint, doing so has two significant drawbacks:
- Due to a problem with recursive type families, this can lead to significantly slower compiles.
- It defeats
ghc
’s warnings for redundant constraints, and thus can lead to a proliferation of redundant constraints as code is changed.
Since: fused-effects-1.0.0.0