-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Random number generation -- -- Random number generation based on orthogonal typeclasses for entropy -- sources and random variable distributions, all served up on a monadic -- platter. Aspires to be useful in an idiomatic way in both "pure" and -- "impure" styles, as well as reasonably fast. @package random-fu @version 0.0.3.2 module Data.Random.Internal.Find findMax :: (Fractional a, Ord a) => (a -> Bool) -> a findMaxFrom :: (Fractional a, Ord a) => a -> a -> (a -> Bool) -> a -- | Given an upward-closed predicate on an ordered Fractional type, find -- the smallest value satisfying the predicate. findMin :: (Fractional a, Ord a) => (a -> Bool) -> a findMinFrom :: (Fractional a, Ord a) => a -> a -> (a -> Bool) -> a module Data.Random.Internal.Fixed resolutionOf :: (HasResolution r) => f r -> Integer resolutionOf2 :: (HasResolution r) => f (g r) -> Integer -- | The Fixed type doesn't expose its constructors, but I need a -- way to convert them to and from their raw representation in order to -- sample them. As long as Fixed is a newtype wrapping -- Integer, mkFixed and unMkFixed as defined here -- will work. Both are implemented using unsafeCoerce. mkFixed :: Integer -> Fixed r unMkFixed :: Fixed r -> Integer -- | A few little functions I found myself writing inline over and over -- again. module Data.Random.Internal.Words -- | Build a word out of 8 bytes. No promises are made regarding the order -- in which the bytes are stuffed. Note that this means that a -- RandomSource or MonadRandom making use of the -- default definition of getRandomWord, etc., may return -- different random values on different platforms when started with the -- same seed, depending on the platform's endianness. buildWord :: Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word64 -- | Pack the low 23 bits from a Word64 into a Float in the -- range [0,1). Used to convert a stdUniform Word64 to a -- stdUniform Double. wordToFloat :: Word64 -> Float -- | Same as wordToFloat, but also return the unused bits (as the 41 least -- significant bits of a Word64) wordToFloatWithExcess :: Word64 -> (Float, Word64) -- | Pack the low 52 bits from a Word64 into a Double in the -- range [0,1). Used to convert a stdUniform Word64 to a -- stdUniform Double. wordToDouble :: Word64 -> Double -- | Same as wordToDouble, but also return the unused bits (as the 12 least -- significant bits of a Word64) wordToDoubleWithExcess :: Word64 -> (Double, Word64) -- | Template Haskell utility code to replicate instance declarations to -- cover large numbers of types. I'm doing that rather than using class -- contexts because most Distribution instances need to cover multiple -- classes (such as Enum, Integral and Fractional) and that can't be done -- easily because of overlap. -- -- I experimented a bit with a convoluted type-level classification -- scheme, but I think this is simpler and easier to understand. It makes -- the haddock docs more cluttered because of the combinatorial explosion -- of instances, but overall I think it's just more sane than anything -- else I've come up with yet. module Data.Random.Internal.TH -- | replicateInstances standin types decls will take the -- template-haskell Decs in decls and substitute every -- instance of the Name standin with each Name in -- types, producing one copy of the Decs in -- decls for every Name in types. -- -- For example, Data.Random.Distribution.Uniform has the -- following bit of TH code: -- --
--   $( replicateInstances ''Int integralTypes [d|
--   
-- --
--   instance Distribution Uniform Int   where rvar (Uniform a b) = integralUniform a b
--   
-- --
--   instance CDF Uniform Int            where cdf  (Uniform a b) = integralUniformCDF a b
--   
-- --
--   |])
--   
-- -- This code takes those 2 instance declarations and creates identical -- ones for every type named in integralTypes. replicateInstances :: (Monad m, Data t) => Name -> [Name] -> m [t] -> m [t] -- | Names of standard Integral types integralTypes :: [Name] -- | Names of standard RealFloat types realFloatTypes :: [Name] module Data.Random.Lift -- | A class for "liftable" data structures. Conceptually an extension of -- MonadTrans to allow deep lifting, but lifting need not be done -- between monads only. Eg lifting between Applicatives is -- allowed. -- -- For instances where m and n have 'return'/'pure' -- defined, these instances must satisfy lift (return x) == return -- x. class Lift m n lift :: (Lift m n) => m a -> n a instance [incoherent] (Monad m) => Lift Identity m instance [incoherent] Lift m m instance [incoherent] (Monad m, MonadTrans t) => Lift m (t m) module Data.Random.Source -- | A typeclass for monads with a chosen source of entropy. For example, -- RVar is such a monad - the source from which it is -- (eventually) sampled is the only source from which a random variable -- is permitted to draw, so when directly requesting entropy for a random -- variable these functions are used. -- -- The minimal definition is either getRandomByte or -- getRandomWord. getRandomDouble is defaulted in terms of -- getRandomWord. class (Monad m) => MonadRandom m getRandomByte :: (MonadRandom m) => m Word8 getRandomWord :: (MonadRandom m) => m Word64 getRandomDouble :: (MonadRandom m) => m Double -- | A source of entropy which can be used in the given monad. -- -- The minimal definition is either getRandomByteFrom or -- getRandomWordFrom. getRandomDoubleFrom is defaulted in -- terms of getRandomWordFrom class (Monad m) => RandomSource m s getRandomByteFrom :: (RandomSource m s) => s -> m Word8 getRandomWordFrom :: (RandomSource m s) => s -> m Word64 getRandomDoubleFrom :: (RandomSource m s) => s -> m Double instance (Monad m) => RandomSource m (m Word64) instance (Monad m) => RandomSource m (m Word8) module Data.Random.Source.DevRandom -- | On systems that have it, /dev/random is a handy-dandy ready-to-use -- source of nonsense. Keep in mind that on some systems, Linux included, -- /dev/random collects "real" entropy, and if you don't have a good -- source of it, such as special hardware for the purpose or a *lot* of -- network traffic, it's pretty easy to suck the entropy pool dry with -- entropy-intensive applications. For many purposes other than -- cryptography, /dev/urandom is preferable because when it runs out of -- real entropy it'll still churn out pseudorandom data. data DevRandom DevRandom :: DevRandom DevURandom :: DevRandom instance RandomSource IO DevRandom -- | This module provides functions useful for implementing new -- MonadRandom and RandomSource instances for -- state-abstractions containing StdGen values (the pure -- pseudorandom generator provided by the System.Random module in the -- "random" package), as well as instances for some common cases. module Data.Random.Source.StdGen getRandomByteFromStdGenIO :: IO Word8 getRandomWordFromStdGenIO :: IO Word64 getRandomDoubleFromStdGenIO :: IO Double -- | Given a mutable reference to a RandomGen generator, we can make -- a RandomSource usable in any monad in which the reference can -- be modified. -- -- For example, if x :: TVar StdGen, -- getRandomByteFromRandomGenRef x can be used as a -- RandomSource in IO, STM, or any monad which is an -- instance of MonadIO. It's generally probably better to use -- getRandomWordFromRandomGenRef though, as this one is likely to -- throw away a lot of perfectly good entropy. Better still is to use -- these 3 functions together to create a RandomSource instance -- for the reference you're using, if one does not already exist. getRandomByteFromRandomGenRef :: (Monad m, ModifyRef sr m g, RandomGen g) => sr -> m Word8 getRandomWordFromRandomGenRef :: (Monad m, ModifyRef sr m g, RandomGen g) => sr -> m Word64 getRandomDoubleFromRandomGenRef :: (Monad m, ModifyRef sr m g, RandomGen g) => sr -> m Double -- | Similarly, getRandomWordFromRandomGenState x can be used in -- any "state" monad in the mtl sense whose state is a RandomGen -- generator. Additionally, the standard mtl state monads have -- MonadRandom instances which do precisely that, allowing an easy -- conversion of RVars and other Distribution instances -- to "pure" random variables. getRandomByteFromRandomGenState :: (RandomGen g, MonadState g m) => m Word8 getRandomWordFromRandomGenState :: (RandomGen g, MonadState g m) => m Word64 getRandomDoubleFromRandomGenState :: (RandomGen g, MonadState g m) => m Double instance (Monad m) => MonadRandom (StateT StdGen m) instance MonadRandom (State StdGen) instance (Monad m) => MonadRandom (StateT StdGen m) instance MonadRandom (State StdGen) instance (Monad m, ModifyRef (STRef s StdGen) m StdGen) => RandomSource m (STRef s StdGen) instance (Monad m, ModifyRef (TVar StdGen) m StdGen) => RandomSource m (TVar StdGen) instance (Monad m, ModifyRef (IORef StdGen) m StdGen) => RandomSource m (IORef StdGen) instance (Monad m1, ModifyRef (Ref m2 StdGen) m1 StdGen) => RandomSource m1 (Ref m2 StdGen) -- | This module provides functions useful for implementing new -- MonadRandom and RandomSource instances for -- state-abstractions containing PureMT values (the pure -- pseudorandom generator provided by the mersenne-random-pure64 -- package), as well as instances for some common cases. module Data.Random.Source.PureMT -- | Given a mutable reference to a PureMT generator, we can make a -- RandomSource usable in any monad in which the reference can be -- modified. -- -- For example, if x :: TVar PureMT, getRandomWordFromMTRef -- x can be used as a RandomSource in IO, STM, -- or any monad which is an instance of MonadIO. These functions -- can also be used to implement additional RandomSource instances -- for mutable references to PureMT states. getRandomWordFromMTRef :: (ModifyRef sr m PureMT) => sr -> m Word64 getRandomByteFromMTRef :: (Monad m, ModifyRef sr m PureMT) => sr -> m Word8 getRandomDoubleFromMTRef :: (Monad m, ModifyRef sr m PureMT) => sr -> m Double -- | Similarly, getRandomWordFromMTState x can be used in any -- "state" monad in the mtl sense whose state is a PureMT -- generator. Additionally, the standard mtl state monads have -- MonadRandom instances which do precisely that, allowing an easy -- conversion of RVars and other Distribution instances -- to "pure" random variables (e.g., by runState . sample :: -- Distribution d t => d t -> PureMT -> (t, PureMT). -- PureMT in the type there can be replaced by StdGen or -- anything else satisfying MonadRandom (State s) => s). getRandomWordFromMTState :: (MonadState PureMT m) => m Word64 getRandomByteFromMTState :: (MonadState PureMT m) => m Word8 getRandomDoubleFromMTState :: (MonadState PureMT m) => m Double instance (Monad m, ModifyRef (TVar PureMT) m PureMT) => RandomSource m (TVar PureMT) instance (Monad m, ModifyRef (STRef s PureMT) m PureMT) => RandomSource m (STRef s PureMT) instance (Monad m, ModifyRef (IORef PureMT) m PureMT) => RandomSource m (IORef PureMT) instance (Monad m) => MonadRandom (StateT PureMT m) instance (Monad m) => MonadRandom (StateT PureMT m) instance (Monad m1, ModifyRef (Ref m2 PureMT) m1 PureMT) => RandomSource m1 (Ref m2 PureMT) instance MonadRandom (State PureMT) instance MonadRandom (State PureMT) module Data.Random.Source.Std -- | A token representing the "standard" entropy source in a -- MonadRandom monad. Its sole purpose is to make the following -- true (when the types check): -- --
--   sampleFrom StdRandom === sample
--   
data StdRandom StdRandom :: StdRandom instance (MonadRandom m) => RandomSource m StdRandom -- | Random variables. An RVar is a sampleable random variable. -- Because probability distributions form a monad, they are quite easy to -- work with in the standard Haskell monadic styles. For examples, see -- the source for any of the Distribution instances - they all -- are defined in terms of RVars. module Data.Random.RVar -- | An opaque type containing a "random variable" - a value which depends -- on the outcome of some random process. type RVar = RVarT Identity runRVar :: (RandomSource m s) => RVar a -> s -> m a -- | A random variable with access to operations in an underlying monad. -- Useful examples include any form of state for implementing random -- processes with hysteresis, or writer monads for implementing tracing -- of complicated algorithms. data RVarT n a -- | "Runs" the monad. runRVarT :: (Lift n m, RandomSource m s) => RVarT n a -> s -> m a -- | A random variable evenly distributed over all unsigned integers from 0 -- to 2^(8*n)-1, inclusive. nByteInteger :: Int -> RVarT m Integer -- | A random variable evenly distributed over all unsigned integers from 0 -- to 2^n-1, inclusive. nBitInteger :: Int -> RVarT m Integer instance MonadRandom (RVarT n) instance (MonadIO m) => MonadIO (RVarT m) instance Lift (RVarT Identity) (RVarT m) instance MonadTrans RVarT instance Applicative (RVarT n) instance Monad (RVarT n) instance Functor (RVarT n) module Data.Random.Distribution -- | A definition of a random variable's distribution. From the -- distribution an RVar can be created, or the distribution can be -- directly sampled using sampleFrom or sample. class Distribution d t rvar :: (Distribution d t) => d t -> RVar t class (Distribution d t) => CDF d t cdf :: (CDF d t) => d t -> t -> Double -- | 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. rvarT :: (Distribution d t) => d t -> RVarT n t module Data.Random.Distribution.Uniform -- | A definition of a uniform distribution over the type t. See -- also uniform. data Uniform t -- | A uniform distribution defined by a lower and upper range bound. For -- Integral and Enum types, the range is inclusive. For -- Fractional types the range includes the lower bound but not the -- upper. Uniform :: !t -> !t -> Uniform t uniform :: (Distribution Uniform a) => a -> a -> RVar a -- | A name for the "standard" uniform distribution over the type -- t, if one exists. See also stdUniform. -- -- For Integral and Enum types that are also -- Bounded, this is the uniform distribution over the full range -- of the type. For un-Bounded Integral types this is not -- defined. For Fractional types this is a random variable in the -- range [0,1) (that is, 0 to 1 including 0 but not including 1). data StdUniform t StdUniform :: StdUniform t -- | Get a "standard" uniformly distributed value. For integral types, this -- means uniformly distributed over the full range of the type (and hence -- there is no support for Integer). For fractional types, this means -- uniformly distributed on the interval [0,1). stdUniform :: (Distribution StdUniform a) => RVar a -- | Like stdUniform, but uses abs to return only positive or -- zero values. stdUniformNonneg :: (Distribution StdUniform a, Num a) => RVar a -- | Like stdUniform but only returns positive values. stdUniformPos :: (Distribution StdUniform a, Num a) => RVar a -- | Compute a random Integral value between the 2 values provided -- (inclusive). integralUniform :: (Integral a) => a -> a -> RVar a -- | realFloatUniform a b computes a uniform random value in the -- range [a,b) for any RealFloat type realFloatUniform :: (RealFloat a) => a -> a -> RVar a -- | floatUniform a b computes a uniform random Float value -- in the range [a,b) floatUniform :: Float -> Float -> RVar Float -- | doubleUniform a b computes a uniform random Double -- value in the range [a,b) doubleUniform :: Double -> Double -> RVar Double -- | fixedUniform a b computes a uniform random Fixed value -- in the range [a,b), with any desired precision. fixedUniform :: (HasResolution r) => Fixed r -> Fixed r -> RVar (Fixed r) -- | Compute a random value for a Bounded type, between -- minBound and maxBound (inclusive for Integral or -- Enum types, in [minBound, maxBound) for -- Fractional types.) boundedStdUniform :: (Distribution Uniform a, Bounded a) => RVar a -- | Compute a random value for a Bounded Enum type, between -- minBound and maxBound (inclusive) boundedEnumStdUniform :: (Enum a, Bounded a) => RVar a -- | Compute a uniform random value in the range [0,1) for any -- RealFloat type realFloatStdUniform :: (RealFloat a) => RVar a -- | Compute a uniform random Fixed value in the range [0,1), with -- any desired precision. fixedStdUniform :: (HasResolution r) => RVar (Fixed r) -- | Compute a uniform random Float value in the range [0,1) floatStdUniform :: RVar Float -- | Compute a uniform random Double value in the range [0,1) doubleStdUniform :: RVar Double -- | The CDF of the random variable realFloatStdUniform. realStdUniformCDF :: (Real a) => a -> Double -- | realUniformCDF a b is the CDF of the random variable -- realFloatUniform a b. realUniformCDF :: (RealFrac a) => a -> a -> a -> Double instance CDF StdUniform Ordering instance Distribution StdUniform Ordering instance CDF StdUniform Char instance Distribution StdUniform Char instance CDF StdUniform Bool instance Distribution StdUniform Bool instance CDF StdUniform () instance Distribution StdUniform () instance CDF Uniform Ordering instance Distribution Uniform Ordering instance CDF Uniform Bool instance Distribution Uniform Bool instance CDF Uniform Char instance Distribution Uniform Char instance CDF Uniform () instance Distribution Uniform () instance (HasResolution r) => CDF StdUniform (Fixed r) instance (HasResolution r) => Distribution StdUniform (Fixed r) instance (HasResolution r) => CDF Uniform (Fixed r) instance (HasResolution r) => Distribution Uniform (Fixed r) instance CDF StdUniform Double instance CDF StdUniform Float instance Distribution StdUniform Double instance Distribution StdUniform Float instance CDF Uniform Double instance CDF Uniform Float instance Distribution Uniform Double instance Distribution Uniform Float instance CDF StdUniform Word64 instance CDF StdUniform Word32 instance CDF StdUniform Word16 instance CDF StdUniform Word8 instance CDF StdUniform Int64 instance CDF StdUniform Int32 instance CDF StdUniform Int16 instance CDF StdUniform Int8 instance CDF StdUniform Int instance Distribution StdUniform Word32 instance Distribution StdUniform Word16 instance Distribution StdUniform Int64 instance Distribution StdUniform Int32 instance Distribution StdUniform Int16 instance Distribution StdUniform Int instance Distribution StdUniform Word64 instance Distribution StdUniform Word8 instance Distribution StdUniform Int8 instance CDF Uniform Word64 instance Distribution Uniform Word64 instance CDF Uniform Word32 instance Distribution Uniform Word32 instance CDF Uniform Word16 instance Distribution Uniform Word16 instance CDF Uniform Word8 instance Distribution Uniform Word8 instance CDF Uniform Int64 instance Distribution Uniform Int64 instance CDF Uniform Int32 instance Distribution Uniform Int32 instance CDF Uniform Int16 instance Distribution Uniform Int16 instance CDF Uniform Int8 instance Distribution Uniform Int8 instance CDF Uniform Integer instance Distribution Uniform Integer instance CDF Uniform Int instance Distribution Uniform Int module Data.Random.Distribution.Bernoulli -- | Generate a Bernoulli variate with the given probability. For -- Bool results, bernoulli p will return True (p*100)% -- of the time and False otherwise. For numerical types, True is replaced -- by 1 and False by 0. bernoulli :: (Distribution (Bernoulli b) a) => b -> RVar a -- | A random variable whose value is True the given fraction of the -- time and False the rest. boolBernoulli :: (Fractional a, Ord a, Distribution StdUniform a) => a -> RVar Bool boolBernoulliCDF :: (Real a) => a -> Bool -> Double -- | generalBernoulli t f p generates a random variable whose -- value is t with probability p and f with -- probability 1-p. generalBernoulli :: (Distribution (Bernoulli b) Bool) => a -> a -> b -> RVar a generalBernoulliCDF :: (CDF (Bernoulli b) Bool) => (a -> a -> Bool) -> a -> a -> b -> a -> Double data Bernoulli b a Bernoulli :: b -> Bernoulli b a instance (CDF (Bernoulli b) Bool, RealFloat a) => CDF (Bernoulli b) (Complex a) instance (Distribution (Bernoulli b) Bool, RealFloat a) => Distribution (Bernoulli b) (Complex a) instance (CDF (Bernoulli b) Bool, Integral a) => CDF (Bernoulli b) (Ratio a) instance (Distribution (Bernoulli b) Bool, Integral a) => Distribution (Bernoulli b) (Ratio a) instance (CDF (Bernoulli b[ar4r]) Bool) => CDF (Bernoulli b[ar4r]) Double instance (Distribution (Bernoulli b[ar4p]) Bool) => Distribution (Bernoulli b[ar4p]) Double instance (CDF (Bernoulli b[ar4r]) Bool) => CDF (Bernoulli b[ar4r]) Float instance (Distribution (Bernoulli b[ar4p]) Bool) => Distribution (Bernoulli b[ar4p]) Float instance (CDF (Bernoulli b[aqNC]) Bool) => CDF (Bernoulli b[aqNC]) Word64 instance (Distribution (Bernoulli b[aqNA]) Bool) => Distribution (Bernoulli b[aqNA]) Word64 instance (CDF (Bernoulli b[aqNC]) Bool) => CDF (Bernoulli b[aqNC]) Word32 instance (Distribution (Bernoulli b[aqNA]) Bool) => Distribution (Bernoulli b[aqNA]) Word32 instance (CDF (Bernoulli b[aqNC]) Bool) => CDF (Bernoulli b[aqNC]) Word16 instance (Distribution (Bernoulli b[aqNA]) Bool) => Distribution (Bernoulli b[aqNA]) Word16 instance (CDF (Bernoulli b[aqNC]) Bool) => CDF (Bernoulli b[aqNC]) Word8 instance (Distribution (Bernoulli b[aqNA]) Bool) => Distribution (Bernoulli b[aqNA]) Word8 instance (CDF (Bernoulli b[aqNC]) Bool) => CDF (Bernoulli b[aqNC]) Int64 instance (Distribution (Bernoulli b[aqNA]) Bool) => Distribution (Bernoulli b[aqNA]) Int64 instance (CDF (Bernoulli b[aqNC]) Bool) => CDF (Bernoulli b[aqNC]) Int32 instance (Distribution (Bernoulli b[aqNA]) Bool) => Distribution (Bernoulli b[aqNA]) Int32 instance (CDF (Bernoulli b[aqNC]) Bool) => CDF (Bernoulli b[aqNC]) Int16 instance (Distribution (Bernoulli b[aqNA]) Bool) => Distribution (Bernoulli b[aqNA]) Int16 instance (CDF (Bernoulli b[aqNC]) Bool) => CDF (Bernoulli b[aqNC]) Int8 instance (Distribution (Bernoulli b[aqNA]) Bool) => Distribution (Bernoulli b[aqNA]) Int8 instance (CDF (Bernoulli b[aqNC]) Bool) => CDF (Bernoulli b[aqNC]) Integer instance (Distribution (Bernoulli b[aqNA]) Bool) => Distribution (Bernoulli b[aqNA]) Integer instance (CDF (Bernoulli b[aqNC]) Bool) => CDF (Bernoulli b[aqNC]) Int instance (Distribution (Bernoulli b[aqNA]) Bool) => Distribution (Bernoulli b[aqNA]) Int instance (Distribution (Bernoulli b) Bool, Real b) => CDF (Bernoulli b) Bool instance (Fractional b, Ord b, Distribution StdUniform b) => Distribution (Bernoulli b) Bool module Data.Random.Distribution.Exponential data Exponential a Exp :: a -> Exponential a floatingExponential :: (Floating a, Distribution StdUniform a) => a -> RVar a floatingExponentialCDF :: (Real a) => a -> a -> Double exponential :: (Distribution Exponential a) => a -> RVar a instance (Real a, Distribution Exponential a) => CDF Exponential a instance (Floating a, Distribution StdUniform a) => Distribution Exponential a module Data.Random.Distribution.Rayleigh floatingRayleigh :: (Floating a, Distribution StdUniform a) => a -> RVar a -- | The rayleigh distribution with a specified mode ("sigma") parameter. -- Its mean will be sigma*sqrt(pi2)@ and its variance will be -- @sigma^2*(4-pi)2 -- -- (therefore if you want one with a particular mean m, -- sigma should be m*sqrt(2/pi)) newtype Rayleigh a Rayleigh :: a -> Rayleigh a rayleigh :: (Distribution Rayleigh a) => a -> RVar a rayleighCDF :: (Real a) => a -> a -> Double instance (Real a, Distribution Rayleigh a) => CDF Rayleigh a instance (RealFloat a, Distribution StdUniform a) => Distribution Rayleigh a module Data.Random.Distribution.Triangular -- | A description of a triangular distribution - a distribution whose PDF -- is a triangle ramping up from a lower bound to a specified midpoint -- and back down to the upper bound. This is a very simple distribution -- that does not generally occur naturally but is used sometimes as an -- estimate of a true distribution when only the range of the values and -- an approximate mode of the true distribution are known. data Triangular a Triangular :: a -> a -> a -> Triangular a -- | The lower bound of the triangle in the PDF (the smallest number the -- distribution can generate) triLower :: Triangular a -> a -- | The midpoint of the triangle (also the mode of the distribution) triMid :: Triangular a -> a -- | The upper bound of the triangle (and the largest number the -- distribution can generate) triUpper :: Triangular a -> a -- | Compute a triangular distribution for a Fractional type. The -- name is a historical accident and may change in the future. realFloatTriangular :: (Floating a, Ord a, Distribution StdUniform a) => a -> a -> a -> RVar a -- | realFloatTriangularCDF a b c is the CDF of -- realFloatTriangular a b c. realFloatTriangularCDF :: (RealFrac a) => a -> a -> a -> a -> Double instance (Eq a) => Eq (Triangular a) instance (Show a) => Show (Triangular a) instance (RealFrac a, Distribution Triangular a) => CDF Triangular a instance (RealFloat a, Ord a, Distribution StdUniform a) => Distribution Triangular a -- | A generic "ziggurat algorithm" implementation. Fairly rough right now. -- -- There is a lot of room for improvement in findBin0 especially. -- It needs a fair amount of cleanup and elimination of redundant -- calculation, as well as either a justification for using the simple -- findMinFrom or a proper root-finding algorithm. -- -- It would also be nice to add (preferably by pulling in an external -- package) support for numerical integration and differentiation, so -- that tables can be derived from only a PDF (if the end user is willing -- to take the performance and accuracy hit for the convenience). module Data.Random.Distribution.Ziggurat -- | A data structure containing all the data that is needed to implement -- Marsaglia & Tang's "ziggurat" algorithm for sampling certain kinds -- of random distributions. -- -- The documentation here is probably not sufficient to tell a user -- exactly how to build one of these from scratch, but it is not really -- intended to be. There are several helper functions that will build -- Ziggurats. The pathologically curious may wish to read the -- runZiggurat source. That is the ultimate specification of the -- semantics of all these fields. data Ziggurat t Ziggurat :: Vector t -> Vector t -> Vector t -> RVar (Int, t) -> RVar t -> (t -> t -> RVar t) -> (t -> t) -> Bool -> Ziggurat t -- | The X locations of each bin in the distribution. Bin 0 is the -- infinite one. -- -- In the case of bin 0, the value given is sort of magical - x[0] is -- defined to be V/f(R). It's not actually the location of any bin, but a -- value computed to make the algorithm more concise and slightly faster -- by not needing to specially-handle bin 0 quite as often. If you really -- need to know why it works, see the runZiggurat source or "the -- literature" - it's a fairly standard setup. zTable_xs :: Ziggurat t -> Vector t -- | The ratio of each bin's Y value to the next bin's Y value zTable_x_ratios :: Ziggurat t -> Vector t -- | The Y value (zFunc x) of each bin zTable_ys :: Ziggurat t -> Vector t -- | An RVar providing a random tuple consisting of: -- -- -- -- This is provided as a single RVar because it can be implemented -- more efficiently than naively sampling 2 separate values - a single -- random word (64 bits) can be efficiently converted to a double (using -- 52 bits) and a bin number (using up to 12 bits), for example. zGetIU :: Ziggurat t -> RVar (Int, t) -- | The distribution for the final "virtual" bin (the ziggurat algorithm -- does not handle distributions that wander off to infinity, so another -- distribution is needed to handle the last "bin" that stretches to -- infinity) zTailDist :: Ziggurat t -> RVar t -- | A copy of the uniform RVar generator for the base type, so that -- Distribution Uniform t is not needed when sampling from a -- Ziggurat (makes it a bit more self-contained). zUniform :: Ziggurat t -> t -> t -> RVar t -- | The (one-sided antitone) PDF, not necessarily normalized zFunc :: Ziggurat t -> t -> t -- | A flag indicating whether the distribution should be mirrored about -- the origin (the ziggurat algorithm it its native form only samples -- from one-sided distributions. By mirroring, we can extend it to -- symmetric distributions such as the normal distribution) zMirror :: Ziggurat t -> Bool -- | Build a lazy recursive ziggurat. Uses a lazily-constructed ziggurat as -- its tail distribution (with another as its tail, ad nauseum). -- -- Arguments: -- -- mkZigguratRec :: (RealFloat t, Storable t, Distribution Uniform t) => Bool -> (t -> t) -> (t -> t) -> (t -> t) -> t -> Int -> RVar (Int, t) -> Ziggurat t -- | Build the tables to implement the "ziggurat algorithm" devised by -- Marsaglia & Tang, attempting to automatically compute the R and V -- values. -- -- Arguments are the same as for mkZigguratRec, with an additional -- argument for the tail distribution as a function of the selected R -- value. mkZiggurat :: (RealFloat t, Storable t, Distribution Uniform t) => Bool -> (t -> t) -> (t -> t) -> (t -> t) -> t -> Int -> RVar (Int, t) -> (t -> RVar t) -> Ziggurat t -- | Build the tables to implement the "ziggurat algorithm" devised by -- Marsaglia & Tang, attempting to automatically compute the R and V -- values. -- -- Arguments: -- -- mkZiggurat_ :: (RealFloat t, Storable t, Distribution Uniform t) => Bool -> (t -> t) -> (t -> t) -> Int -> t -> t -> RVar (Int, t) -> RVar t -> Ziggurat t -- | I suspect this isn't completely right, but it works well so far. -- Search the distribution for an appropriate R and V. -- -- Arguments: -- -- -- -- Result: (R,V) findBin0 :: (RealFloat b) => Int -> (b -> b) -> (b -> b) -> (b -> b) -> b -> (b, b) -- | Sample from the distribution encoded in a Ziggurat data -- structure. runZiggurat :: (Num a, Ord a, Storable a) => Ziggurat a -> RVar a instance (Num t, Ord t, Storable t) => Distribution Ziggurat t module Data.Random.Distribution.Normal -- | A specification of a normal distribution over the type a. data Normal a -- | The "standard" normal distribution - mean 0, stddev 1 StdNormal :: Normal a -- | Normal m s is a normal distribution with mean m and -- stddev s. Normal :: a -> a -> Normal a -- | normal m s is a random variable with distribution -- Normal m s. normal :: (Distribution Normal a) => a -> a -> RVar a -- | stdNormal is a normal variable with distribution -- StdNormal. stdNormal :: (Distribution Normal a) => RVar a -- | A random variable sampling from the standard normal distribution over -- the Double type. doubleStdNormal :: RVar Double -- | A random variable sampling from the standard normal distribution over -- the Float type. floatStdNormal :: RVar Float -- | A random variable sampling from the standard normal distribution over -- any RealFloat type (subject to the rest of the constraints - it -- builds and uses a Ziggurat internally, which requires the -- Erf and Storable classes). -- -- Because it computes a Ziggurat, it is very expensive to use for -- just one evaluation, or even for multiple evaluations if not used and -- reused monomorphically (to enable the ziggurat table to be let-floated -- out). If you don't know whether your use case fits this description -- then you're probably better off using a different algorithm, such as -- boxMullerNormalPair or knuthPolarNormalPair. And of -- course if you don't need the full generality of this definition then -- you're much better off using doubleStdNormal or -- floatStdNormal. -- -- As far as I know, this should be safe to use in any monomorphic -- Distribution Normal instance declaration. realFloatStdNormal :: (RealFloat a, Erf a, Storable a, Distribution Uniform a) => RVar a -- | Draw from the tail of a normal distribution (the region beyond the -- provided value) normalTail :: (Distribution StdUniform a, Floating a, Ord a) => a -> RVar a -- | A random variable that produces a pair of independent -- normally-distributed values. normalPair :: (Floating a, Distribution StdUniform a) => RVar (a, a) -- | A random variable that produces a pair of independent -- normally-distributed values, computed using the Box-Muller method. -- This algorithm is slightly slower than Knuth's method but using a -- constant amount of entropy (Knuth's method is a rejection method). It -- is also slightly more general (Knuth's method require an Ord -- instance). boxMullerNormalPair :: (Floating a, Distribution StdUniform a) => RVar (a, a) -- | A random variable that produces a pair of independent -- normally-distributed values, computed using Knuth's polar method. -- Slightly faster than boxMullerNormalPair when it accepts on the -- first try, but does not always do so. knuthPolarNormalPair :: (Floating a, Ord a, Distribution Uniform a) => RVar (a, a) instance (Real a, Distribution Normal a) => CDF Normal a instance Distribution Normal Float instance Distribution Normal Double module Data.Random.Distribution.Gamma realFloatGamma :: (Floating a, Ord a, Distribution Normal a, Distribution StdUniform a) => a -> a -> RVar a realFloatErlang :: (Integral a, Floating b, Ord b, Distribution Normal b, Distribution StdUniform b) => a -> RVar b gamma :: (Distribution Gamma a) => a -> a -> RVar a erlang :: (Distribution (Erlang a) b) => a -> RVar b data Gamma a Gamma :: a -> a -> Gamma a data Erlang a b Erlang :: a -> Erlang a b instance (Integral a, Floating b, Ord b, Distribution Normal b, Distribution StdUniform b) => Distribution (Erlang a) b instance (Floating a, Ord a, Distribution Normal a, Distribution StdUniform a) => Distribution Gamma a module Data.Random.Distribution.Beta fractionalBeta :: (Fractional a, Distribution Gamma a, Distribution StdUniform a) => a -> a -> RVar a fractionalBetaFromIntegral :: (Fractional c, Distribution (Erlang a) c, Distribution (Erlang b) c) => a -> b -> RVar c beta :: (Distribution Beta a) => a -> a -> RVar a data Beta a Beta :: a -> a -> Beta a instance (Fractional a, Distribution Gamma a, Distribution StdUniform a) => Distribution Beta a module Data.Random.Distribution.Binomial integralBinomial :: (Integral a, Floating b, Ord b, Distribution Beta b, Distribution StdUniform b) => a -> b -> RVar a integralBinomialCDF :: (Integral a, Real b) => a -> b -> a -> Double floatingBinomial :: (RealFrac a, Distribution (Binomial b) Integer) => a -> b -> RVar a floatingBinomialCDF :: (CDF (Binomial b) Integer, RealFrac a) => a -> b -> a -> Double binomial :: (Distribution (Binomial b) a) => a -> b -> RVar a data Binomial b a Binomial :: a -> b -> Binomial b a instance (CDF (Binomial b[aFwO]) Integer) => CDF (Binomial b[aFwO]) Double instance (Distribution (Binomial b[aFwL]) Integer) => Distribution (Binomial b[aFwL]) Double instance (CDF (Binomial b[aFwO]) Integer) => CDF (Binomial b[aFwO]) Float instance (Distribution (Binomial b[aFwL]) Integer) => Distribution (Binomial b[aFwL]) Float instance (Real b[aFcB], Distribution (Binomial b[aFcB]) Word64) => CDF (Binomial b[aFcB]) Word64 instance (Floating b[aFcy], Ord b[aFcy], Distribution Beta b[aFcy], Distribution StdUniform b[aFcy]) => Distribution (Binomial b[aFcy]) Word64 instance (Real b[aFcB], Distribution (Binomial b[aFcB]) Word32) => CDF (Binomial b[aFcB]) Word32 instance (Floating b[aFcy], Ord b[aFcy], Distribution Beta b[aFcy], Distribution StdUniform b[aFcy]) => Distribution (Binomial b[aFcy]) Word32 instance (Real b[aFcB], Distribution (Binomial b[aFcB]) Word16) => CDF (Binomial b[aFcB]) Word16 instance (Floating b[aFcy], Ord b[aFcy], Distribution Beta b[aFcy], Distribution StdUniform b[aFcy]) => Distribution (Binomial b[aFcy]) Word16 instance (Real b[aFcB], Distribution (Binomial b[aFcB]) Word8) => CDF (Binomial b[aFcB]) Word8 instance (Floating b[aFcy], Ord b[aFcy], Distribution Beta b[aFcy], Distribution StdUniform b[aFcy]) => Distribution (Binomial b[aFcy]) Word8 instance (Real b[aFcB], Distribution (Binomial b[aFcB]) Int64) => CDF (Binomial b[aFcB]) Int64 instance (Floating b[aFcy], Ord b[aFcy], Distribution Beta b[aFcy], Distribution StdUniform b[aFcy]) => Distribution (Binomial b[aFcy]) Int64 instance (Real b[aFcB], Distribution (Binomial b[aFcB]) Int32) => CDF (Binomial b[aFcB]) Int32 instance (Floating b[aFcy], Ord b[aFcy], Distribution Beta b[aFcy], Distribution StdUniform b[aFcy]) => Distribution (Binomial b[aFcy]) Int32 instance (Real b[aFcB], Distribution (Binomial b[aFcB]) Int16) => CDF (Binomial b[aFcB]) Int16 instance (Floating b[aFcy], Ord b[aFcy], Distribution Beta b[aFcy], Distribution StdUniform b[aFcy]) => Distribution (Binomial b[aFcy]) Int16 instance (Real b[aFcB], Distribution (Binomial b[aFcB]) Int8) => CDF (Binomial b[aFcB]) Int8 instance (Floating b[aFcy], Ord b[aFcy], Distribution Beta b[aFcy], Distribution StdUniform b[aFcy]) => Distribution (Binomial b[aFcy]) Int8 instance (Real b[aFcB], Distribution (Binomial b[aFcB]) Integer) => CDF (Binomial b[aFcB]) Integer instance (Floating b[aFcy], Ord b[aFcy], Distribution Beta b[aFcy], Distribution StdUniform b[aFcy]) => Distribution (Binomial b[aFcy]) Integer instance (Real b[aFcB], Distribution (Binomial b[aFcB]) Int) => CDF (Binomial b[aFcB]) Int instance (Floating b[aFcy], Ord b[aFcy], Distribution Beta b[aFcy], Distribution StdUniform b[aFcy]) => Distribution (Binomial b[aFcy]) Int module Data.Random.Distribution.Poisson integralPoisson :: (Integral a, RealFloat b, Distribution StdUniform b, Distribution (Erlang a) b, Distribution (Binomial b) a) => b -> RVar a integralPoissonCDF :: (Integral a, Real b) => b -> a -> Double fractionalPoisson :: (Num a, Distribution (Poisson b) Integer) => b -> RVar a fractionalPoissonCDF :: (CDF (Poisson b) Integer, RealFrac a) => b -> a -> Double poisson :: (Distribution (Poisson b) a) => b -> RVar a data Poisson b a Poisson :: b -> Poisson b a instance (CDF (Poisson b[aIn2]) Integer) => CDF (Poisson b[aIn2]) Double instance (Distribution (Poisson b[aIn0]) Integer) => Distribution (Poisson b[aIn0]) Double instance (CDF (Poisson b[aIn2]) Integer) => CDF (Poisson b[aIn2]) Float instance (Distribution (Poisson b[aIn0]) Integer) => Distribution (Poisson b[aIn0]) Float instance (Real b[aHYv], Distribution (Poisson b[aHYv]) Word64) => CDF (Poisson b[aHYv]) Word64 instance (RealFloat b[aHYt], Distribution StdUniform b[aHYt], Distribution (Erlang Word64) b[aHYt], Distribution (Binomial b[aHYt]) Word64) => Distribution (Poisson b[aHYt]) Word64 instance (Real b[aHYv], Distribution (Poisson b[aHYv]) Word32) => CDF (Poisson b[aHYv]) Word32 instance (RealFloat b[aHYt], Distribution StdUniform b[aHYt], Distribution (Erlang Word32) b[aHYt], Distribution (Binomial b[aHYt]) Word32) => Distribution (Poisson b[aHYt]) Word32 instance (Real b[aHYv], Distribution (Poisson b[aHYv]) Word16) => CDF (Poisson b[aHYv]) Word16 instance (RealFloat b[aHYt], Distribution StdUniform b[aHYt], Distribution (Erlang Word16) b[aHYt], Distribution (Binomial b[aHYt]) Word16) => Distribution (Poisson b[aHYt]) Word16 instance (Real b[aHYv], Distribution (Poisson b[aHYv]) Word8) => CDF (Poisson b[aHYv]) Word8 instance (RealFloat b[aHYt], Distribution StdUniform b[aHYt], Distribution (Erlang Word8) b[aHYt], Distribution (Binomial b[aHYt]) Word8) => Distribution (Poisson b[aHYt]) Word8 instance (Real b[aHYv], Distribution (Poisson b[aHYv]) Int64) => CDF (Poisson b[aHYv]) Int64 instance (RealFloat b[aHYt], Distribution StdUniform b[aHYt], Distribution (Erlang Int64) b[aHYt], Distribution (Binomial b[aHYt]) Int64) => Distribution (Poisson b[aHYt]) Int64 instance (Real b[aHYv], Distribution (Poisson b[aHYv]) Int32) => CDF (Poisson b[aHYv]) Int32 instance (RealFloat b[aHYt], Distribution StdUniform b[aHYt], Distribution (Erlang Int32) b[aHYt], Distribution (Binomial b[aHYt]) Int32) => Distribution (Poisson b[aHYt]) Int32 instance (Real b[aHYv], Distribution (Poisson b[aHYv]) Int16) => CDF (Poisson b[aHYv]) Int16 instance (RealFloat b[aHYt], Distribution StdUniform b[aHYt], Distribution (Erlang Int16) b[aHYt], Distribution (Binomial b[aHYt]) Int16) => Distribution (Poisson b[aHYt]) Int16 instance (Real b[aHYv], Distribution (Poisson b[aHYv]) Int8) => CDF (Poisson b[aHYv]) Int8 instance (RealFloat b[aHYt], Distribution StdUniform b[aHYt], Distribution (Erlang Int8) b[aHYt], Distribution (Binomial b[aHYt]) Int8) => Distribution (Poisson b[aHYt]) Int8 instance (Real b[aHYv], Distribution (Poisson b[aHYv]) Integer) => CDF (Poisson b[aHYv]) Integer instance (RealFloat b[aHYt], Distribution StdUniform b[aHYt], Distribution (Erlang Integer) b[aHYt], Distribution (Binomial b[aHYt]) Integer) => Distribution (Poisson b[aHYt]) Integer instance (Real b[aHYv], Distribution (Poisson b[aHYv]) Int) => CDF (Poisson b[aHYv]) Int instance (RealFloat b[aHYt], Distribution StdUniform b[aHYt], Distribution (Erlang Int) b[aHYt], Distribution (Binomial b[aHYt]) Int) => Distribution (Poisson b[aHYt]) Int module Data.Random.List -- | A random variable returning an arbitrary element of the given list. -- Every element has equal probability of being chosen. Because it is a -- pure RVar it has no memory - that is, it "draws with -- replacement." randomElement :: [a] -> RVar a -- | A random variable that returns the given list in an arbitrary shuffled -- order. Every ordering of the list has equal probability. shuffle :: [a] -> RVar [a] -- | A random variable that shuffles a list of a known length. Useful for -- shuffling large lists when the length is known in advance. Avoids -- needing to traverse the list to discover its length. Each ordering has -- equal probability. -- -- Throws an error the list is not exactly as long as claimed. shuffleN :: Int -> [a] -> RVar [a] module Data.Random.Distribution.Discrete discrete :: (Distribution (Discrete p) a) => [(p, a)] -> RVar a empirical :: (Num p, Ord a) => [a] -> Discrete p a newtype Discrete p a Discrete :: [(p, a)] -> Discrete p a -- | Like fmap, but for the weights of a discrete distribution. mapDiscreteWeights :: (p -> q) -> Discrete p e -> Discrete q e -- | Adjust all the weights of a discrete distribution so that they sum to -- unity. If not possible, returns the original distribution unchanged. normalizeDiscreteWeights :: (Fractional p) => Discrete p e -> Discrete p e -- | Simplify a discrete distribution by combining equivalent events (the -- new event will have a weight equal to the sum of all the originals). collectDiscreteEvents :: (Ord e, Num p, Ord p) => Discrete p e -> Discrete p e -- | Simplify a discrete distribution by combining equivalent events (the -- new event will have a weight equal to the sum of all the originals). -- The comparator function is used to identify events to combine. Once -- chosen, the events and their weights are combined (independently) by -- the provided weight and event aggregation functions. collectDiscreteEventsBy :: (e -> e -> Ordering) -> ([p] -> p) -> ([e] -> e) -> Discrete p e -> Discrete p e instance (Eq p, Eq a) => Eq (Discrete p a) instance (Show p, Show a) => Show (Discrete p a) instance (Num p) => Applicative (Discrete p) instance (Num p) => Monad (Discrete p) instance Traversable (Discrete p) instance Foldable (Discrete p) instance Functor (Discrete p) instance (Num p, Ord p, Distribution Uniform p) => Distribution (Discrete p) a module Data.Random.Sample -- | A typeclass allowing Distributions and RVars to be -- sampled. Both may also be sampled via runRVar or -- runRVarT, but I find it psychologically pleasing to be able to -- sample both using this function. class Sampleable d m t sampleFrom :: (Sampleable d m t, RandomSource m s) => s -> d t -> m t -- | Sample a distribution using the default source of entropy for the -- monad in which the sampling occurs. sample :: (Sampleable d m t, MonadRandom m) => d t -> m t instance [incoherent] (Lift m n) => Sampleable (RVarT m) n t instance [incoherent] (Distribution d t) => Sampleable d m t -- | Random numbers and stuff... -- -- Data.Random.Source exports the typeclasses for entropy sources, -- and Data.Random.Source.* export various instances and/or functions -- with which instances can be defined. -- -- Data.Random.Distribution exports the typeclasses for sampling -- distributions, and Data.Random.Distribution.* export various specific -- distributions. -- -- Data.Random.RVar exports the RVar type, which is a -- probability distribution monad that allows for concise definitions of -- random variables, as well as a couple handy RVars. module Data.Random