-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Random number generation -- -- Random number generation based on modeling random variables in two -- complementary ways: first, by the parameters of standard mathematical -- distributions and, second, by an abstract type (RVar) which can -- be composed and manipulated monadically and sampled in either monadic -- or "pure" styles. The primary purpose of this library is to support -- defining and sampling a wide variety of high quality random variables. -- Quality is prioritized over speed, but performance is an important -- goal too. In my testing, I have found it capable of speed comparable -- to other Haskell libraries, but still a fair bit slower than straight -- C implementations of the same algorithms. Warning to anyone upgrading -- from "< 0.2": The old random-fu package has been split into three -- parts: random-source, rvar, and this new random-fu. The end-user -- interface is mostly the same. @package random-fu @version 0.2 module Data.Random.Internal.Find findMax :: (Fractional a, Ord 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 -- | Given an upward-closed predicate on an ordered Fractional type, find -- the smallest value satisfying the predicate. Starts at the specified -- point with the specified stepsize, performs an exponential search out -- from there until it finds an interval bracketing the change-point of -- the predicate, and then performs a bisection search to isolate the -- change point. Note that infinitely-divisible domains such as -- Rational cannot be searched by this function because it does -- not terminate until it reaches a point where further subdivision of -- the interval has no effect. 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 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. -- -- This form of lift has an extremely general type and is used -- primarily to support sample. Its excessive generality is the -- main reason it's not exported from Data.Random. RVarT -- is, however, an instance of MonadTrans, which in most cases is -- the preferred way to do the lifting. class Lift m n lift :: Lift m n => m a -> n a instance [incoherent] Lift (RVarT Identity) (RVarT m) 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.RVar -- | An opaque type modeling a "random variable" - a value which depends on -- the outcome of some random event. RVars can be conveniently -- defined by an imperative-looking style: -- --
-- normalPair = do -- u <- stdUniform -- t <- stdUniform -- let r = sqrt (-2 * log u) -- theta = (2 * pi) * t -- -- x = r * cos theta -- y = r * sin theta -- return (x,y) ---- -- OR by a more applicative style: -- --
-- logNormal = exp <$> stdNormal ---- -- Once defined (in any style), there are several ways to sample -- RVars: -- --
-- runRVar (uniform 1 100) DevRandom :: IO Int ---- --
-- sampleRVar (uniform 1 100) :: State PureMT Int ---- --
-- sampleState (uniform 1 100) :: StdGen -> (Int, StdGen) ---- -- (where sampleState = runState . sampleRVar) type RVar = RVarT Identity -- | "Run" an RVar - samples the random variable from the provided -- source of entropy. 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. -- -- For example, a simple random walk can be implemented as an -- RVarT IO value: -- --
-- rwalkIO :: IO (RVarT IO Double) -- rwalkIO d = do -- lastVal <- newIORef 0 -- -- let x = do -- prev <- lift (readIORef lastVal) -- change <- rvarT StdNormal -- -- let new = prev + change -- lift (writeIORef lastVal new) -- return new -- -- return x ---- -- To run the random walk it must first be initialized, after which it -- can be sampled as usual: -- --
-- do -- rw <- rwalkIO -- x <- sampleRVarT rw -- y <- sampleRVarT rw -- ... ---- -- The same random-walk process as above can be implemented using MTL -- types as follows (using import Control.Monad.Trans as MTL): -- --
-- rwalkState :: RVarT (State Double) Double -- rwalkState = do -- prev <- MTL.lift get -- change <- rvarT StdNormal -- -- let new = prev + change -- MTL.lift (put new) -- return new ---- -- Invocation is straightforward (although a bit noisy) if you're used to -- MTL: -- --
-- rwalk :: Int -> Double -> StdGen -> ([Double], StdGen) -- rwalk count start gen = -- flip evalState start . -- flip runStateT gen . -- sampleRVarTWith MTL.lift $ -- replicateM count rwalkState --data RVarT m :: (* -> *) a :: (* -> *) -> * -> * -- | Like runRVarTWith, but using an implicit lifting (provided by -- the Lift class) runRVarT :: (Lift n m, RandomSource m s) => RVarT n a -> s -> m a -- | "Runs" an RVarT, sampling the random variable it defines. -- -- The first argument lifts the base monad into the sampling monad. This -- operation must obey the "monad transformer" laws: -- --
-- lift . return = return -- lift (x >>= f) = (lift x) >>= (lift . f) ---- -- One example of a useful non-standard lifting would be one that takes -- State s to another monad with a different state -- representation (such as IO with the state mapped to an -- IORef): -- --
-- embedState :: (Monad m) => m s -> (s -> m ()) -> State s a -> m a -- embedState get put = \m -> do -- s <- get -- (res,s) <- return (runState m s) -- put s -- return res ---- -- The ability to lift is very important - without it, every RVar -- would have to either be given access to the full capability of the -- monad in which it will eventually be sampled (which, incidentally, -- would also have to be monomorphic so you couldn't sample one -- RVar in more than one monad) or functions manipulating -- RVars would have to use higher-ranked types to enforce the same -- kind of isolation and polymorphism. runRVarTWith :: RandomSource m s => (forall t. n t -> m t) -> RVarT n a -> s -> m a module Data.Random.Distribution -- | 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: -- --
-- $( 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.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 uniformT :: Distribution Uniform a => a -> a -> RVarT m 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 variable. For integral types, -- this means uniformly distributed over the full range of the type -- (there is no support for Integer). For fractional types, this -- means uniformly distributed on the interval [0,1). stdUniform :: Distribution StdUniform a => RVar a -- | Get a "standard" uniformly distributed process. For integral types, -- this means uniformly distributed over the full range of the type -- (there is no support for Integer). For fractional types, this -- means uniformly distributed on the interval [0,1). stdUniformT :: Distribution StdUniform a => RVarT m a -- | Like stdUniform but only returns positive values. stdUniformPos :: (Distribution StdUniform a, Num a) => RVar a -- | Like stdUniform but only returns positive values. stdUniformPosT :: (Distribution StdUniform a, Num a) => RVarT m a -- | Compute a random Integral value between the 2 values provided -- (inclusive). integralUniform :: Integral a => a -> a -> RVarT m a -- | realFloatUniform a b computes a uniform random value in the -- range [a,b) for any RealFloat type realFloatUniform :: RealFloat a => a -> a -> RVarT m a -- | floatUniform a b computes a uniform random Float value -- in the range [a,b) floatUniform :: Float -> Float -> RVarT m Float -- | doubleUniform a b computes a uniform random Double -- value in the range [a,b) doubleUniform :: Double -> Double -> RVarT m 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 -> RVarT m (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) => RVarT m a -- | Compute a uniform random value in the range [0,1) for any -- RealFloat type realFloatStdUniform :: RealFloat a => RVarT m a -- | Compute a uniform random Fixed value in the range [0,1), with -- any desired precision. fixedStdUniform :: HasResolution r => RVarT m (Fixed r) -- | Compute a uniform random Float value in the range [0,1) floatStdUniform :: RVarT m Float -- | Compute a uniform random Double value in the range [0,1) doubleStdUniform :: RVarT m 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 Int instance CDF StdUniform Int64 instance CDF StdUniform Int32 instance CDF StdUniform Int16 instance CDF StdUniform Int8 instance CDF StdUniform Word instance CDF StdUniform Word64 instance CDF StdUniform Word32 instance CDF StdUniform Word16 instance CDF StdUniform Word8 instance Distribution StdUniform Word instance Distribution StdUniform Int instance Distribution StdUniform Int64 instance Distribution StdUniform Int32 instance Distribution StdUniform Int16 instance Distribution StdUniform Int8 instance Distribution StdUniform Word64 instance Distribution StdUniform Word32 instance Distribution StdUniform Word16 instance Distribution StdUniform Word8 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 Word instance Distribution Uniform Word 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 Int instance Distribution Uniform Int instance CDF Uniform Integer instance Distribution Uniform Integer 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 (or a list -- prefix of the specified 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. shuffleN :: Int -> [a] -> RVar [a] -- | A random variable that selects N arbitrary elements of a list of known -- length M. shuffleNofM :: Int -> Int -> [a] -> RVar [a] module Data.Random.Distribution.Categorical -- | Construct a Categorical random variable from a list of -- probabilities and categories, where the probabilities all sum to 1. categorical :: (Num p, Distribution (Categorical p) a) => [(p, a)] -> RVar a -- | Construct a Categorical random process from a list of -- probabilities and categories, where the probabilities all sum to 1. categoricalT :: (Num p, Distribution (Categorical p) a) => [(p, a)] -> RVarT m a -- | Construct a Categorical distribution from a list of weighted -- categories. fromList :: Num p => [(p, a)] -> Categorical p a toList :: Num p => Categorical p a -> [(p, a)] -- | Construct a Categorical distribution from a list of weighted -- categories, where the weights do not necessarily sum to 1. fromWeightedList :: (Fractional p, Ord a) => [(p, a)] -> Categorical p a -- | Construct a Categorical distribution from a list of observed -- outcomes. Equivalent events will be grouped and counted, and the -- probabilities of each event in the returned distribution will be -- proportional to the number of occurrences of that event. fromObservations :: (Fractional p, Ord a) => [a] -> Categorical p a -- | Like fmap, but for the probabilities of a categorical -- distribution. mapCategoricalPs :: (p -> q) -> Categorical p e -> Categorical q e -- | Adjust all the weights of a categorical distribution so that they sum -- to unity. normalizeCategoricalPs :: Fractional p => Categorical p e -> Categorical p e -- | Simplify a categorical distribution by combining equivalent categories -- (the new category will have a probability equal to the sum of all the -- originals). collectEvents :: (Ord e, Num p, Ord p) => Categorical p e -> Categorical p e -- | Simplify a categorical 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 by the -- provided probability and event aggregation function. collectEventsBy :: Num p => (e -> e -> Ordering) -> ([(p, e)] -> (p, e)) -> Categorical p e -> Categorical p e instance (Eq p, Eq a) => Eq (Categorical p a) instance Fractional p => Applicative (Categorical p) instance Num p => Monad (Categorical p) instance Traversable (Categorical p) instance Foldable (Categorical p) instance Functor (Categorical p) instance (Fractional p, Ord p, Distribution Uniform p) => Distribution (Categorical p) a instance (Num p, Show a) => Show (Categorical p a) module Data.Random.Distribution.Exponential data Exponential a Exp :: a -> Exponential a floatingExponential :: (Floating a, Distribution StdUniform a) => a -> RVarT m a floatingExponentialCDF :: Real a => a -> a -> Double exponential :: Distribution Exponential a => a -> RVar a exponentialT :: Distribution Exponential a => a -> RVarT m a instance (Real a, Distribution Exponential a) => CDF Exponential a instance (Floating a, Distribution StdUniform a) => Distribution Exponential 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 v t Ziggurat :: !v t -> !v t -> !v t -> !forall m. RVarT m (Int, t) -> (forall m. RVarT m t) -> !forall m. t -> t -> RVarT m t -> !t -> t -> !Bool -> Ziggurat v 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 v t -> !v t -- | The ratio of each bin's Y value to the next bin's Y value zTable_y_ratios :: Ziggurat v t -> !v t -- | The Y value (zFunc x) of each bin zTable_ys :: Ziggurat v t -> !v t -- | An RVar providing a random tuple consisting of: -- --
-- normalPair = do -- u <- stdUniform -- t <- stdUniform -- let r = sqrt (-2 * log u) -- theta = (2 * pi) * t -- -- x = r * cos theta -- y = r * sin theta -- return (x,y) ---- -- OR by a more applicative style: -- --
-- logNormal = exp <$> stdNormal ---- -- Once defined (in any style), there are several ways to sample -- RVars: -- --
-- runRVar (uniform 1 100) DevRandom :: IO Int ---- --
-- sampleRVar (uniform 1 100) :: State PureMT Int ---- --
-- sampleState (uniform 1 100) :: StdGen -> (Int, StdGen) ---- -- (where sampleState = runState . sampleRVar) type RVar = RVarT Identity -- | 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. -- -- For example, a simple random walk can be implemented as an -- RVarT IO value: -- --
-- rwalkIO :: IO (RVarT IO Double) -- rwalkIO d = do -- lastVal <- newIORef 0 -- -- let x = do -- prev <- lift (readIORef lastVal) -- change <- rvarT StdNormal -- -- let new = prev + change -- lift (writeIORef lastVal new) -- return new -- -- return x ---- -- To run the random walk it must first be initialized, after which it -- can be sampled as usual: -- --
-- do -- rw <- rwalkIO -- x <- sampleRVarT rw -- y <- sampleRVarT rw -- ... ---- -- The same random-walk process as above can be implemented using MTL -- types as follows (using import Control.Monad.Trans as MTL): -- --
-- rwalkState :: RVarT (State Double) Double -- rwalkState = do -- prev <- MTL.lift get -- change <- rvarT StdNormal -- -- let new = prev + change -- MTL.lift (put new) -- return new ---- -- Invocation is straightforward (although a bit noisy) if you're used to -- MTL: -- --
-- rwalk :: Int -> Double -> StdGen -> ([Double], StdGen) -- rwalk count start gen = -- flip evalState start . -- flip runStateT gen . -- sampleRVarTWith MTL.lift $ -- replicateM count rwalkState --data RVarT m :: (* -> *) a :: (* -> *) -> * -> * -- | "Run" an RVar - samples the random variable from the provided -- source of entropy. runRVar :: RandomSource m s => RVar a -> s -> m a -- | Like runRVarTWith, but using an implicit lifting (provided by -- the Lift class) runRVarT :: (Lift n m, RandomSource m s) => RVarT n a -> s -> m a -- | "Runs" an RVarT, sampling the random variable it defines. -- -- The first argument lifts the base monad into the sampling monad. This -- operation must obey the "monad transformer" laws: -- --
-- lift . return = return -- lift (x >>= f) = (lift x) >>= (lift . f) ---- -- One example of a useful non-standard lifting would be one that takes -- State s to another monad with a different state -- representation (such as IO with the state mapped to an -- IORef): -- --
-- embedState :: (Monad m) => m s -> (s -> m ()) -> State s a -> m a -- embedState get put = \m -> do -- s <- get -- (res,s) <- return (runState m s) -- put s -- return res ---- -- The ability to lift is very important - without it, every RVar -- would have to either be given access to the full capability of the -- monad in which it will eventually be sampled (which, incidentally, -- would also have to be monomorphic so you couldn't sample one -- RVar in more than one monad) or functions manipulating -- RVars would have to use higher-ranked types to enforce the same -- kind of isolation and polymorphism. runRVarTWith :: RandomSource m s => (forall t. n t -> m t) -> RVarT n a -> s -> m a -- | 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: -- --
-- $(monadRandom [d|
-- instance MonadRandom FooM where
-- getRandomDouble = return pi
-- getRandomWord16 = return 4
-- {- etc... -}
-- |])
--
class Monad m => MonadRandom m :: (* -> *)
-- | A source of entropy which can be used in the given monad.
--
-- See also MonadRandom.
--
-- Minimum implementation is either the internal getRandomPrimFrom
-- or all other functions. Additionally, this class's interface is
-- subject to extension at any time, so it is very, very strongly
-- recommended that the randomSource Template Haskell function
-- be used to implement this function rather than directly implementing
-- it. That function takes care of choosing default implementations for
-- any missing functions; as long as at least one function is
-- implemented, it will derive sensible implementations of all others.
--
-- To use randomSource, just wrap your instance declaration as
-- follows (and enable the TemplateHaskell, MultiParamTypeClasses and
-- GADTs language extensions, as well as any others required by your
-- instances, such as FlexibleInstances):
--
--
-- $(randomSource [d|
-- instance RandomSource FooM Bar where
-- {- at least one RandomSource function... -}
-- |])
--
class Monad m => RandomSource m :: (* -> *) s
-- | A token representing the "standard" entropy source in a
-- MonadRandom monad. Its sole purpose is to make the following
-- true (when the types check):
--
-- -- runRVar x StdRandom === sampleRVar --data StdRandom :: * StdRandom :: StdRandom -- | 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 (or a list -- prefix of the specified 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. shuffleN :: Int -> [a] -> RVar [a] -- | A random variable that selects N arbitrary elements of a list of known -- length M. shuffleNofM :: Int -> Int -> [a] -> RVar [a] module Data.Random.Distribution.ChiSquare chiSquare :: Distribution ChiSquare t => Integer -> RVar t chiSquareT :: Distribution ChiSquare t => Integer -> RVarT m t newtype ChiSquare b ChiSquare :: Integer -> ChiSquare b instance (Real t, Distribution ChiSquare t) => CDF ChiSquare t instance (Fractional t, Distribution Gamma t) => Distribution ChiSquare t module Data.Random.Distribution.Dirichlet fractionalDirichlet :: (Fractional a, Distribution Gamma a) => [a] -> RVarT m [a] dirichlet :: Distribution Dirichlet [a] => [a] -> RVar [a] dirichletT :: Distribution Dirichlet [a] => [a] -> RVarT m [a] newtype Dirichlet a Dirichlet :: a -> Dirichlet a instance Eq a => Eq (Dirichlet a) instance Show a => Show (Dirichlet a) instance (Fractional a, Distribution Gamma a) => Distribution Dirichlet [a] module Data.Random.Distribution.Rayleigh floatingRayleigh :: (Floating a, Distribution StdUniform a) => a -> RVarT m 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 rayleighT :: Distribution Rayleigh a => a -> RVarT m 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 Floating type. floatingTriangular :: (Floating a, Ord a, Distribution StdUniform a) => a -> a -> a -> RVarT m a -- | triangularCDF a b c is the CDF of realFloatTriangular a b -- c. triangularCDF :: 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 module Data.Random.Distribution.Weibull data Weibull a Weibull :: !a -> !a -> Weibull a weibullLambda :: Weibull a -> !a weibullK :: Weibull a -> !a instance Eq a => Eq (Weibull a) instance Show a => Show (Weibull a) instance (Real a, Distribution Weibull a) => CDF Weibull a instance (Floating a, Distribution StdUniform a) => Distribution Weibull a 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 -- | Generate a Bernoulli process 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. bernoulliT :: Distribution (Bernoulli b) a => b -> RVarT m 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 -> RVarT m 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 -> RVarT m 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[aF6L]) Bool => CDF (Bernoulli b[aF6L]) Double instance Distribution (Bernoulli b[aF6J]) Bool => Distribution (Bernoulli b[aF6J]) Double instance CDF (Bernoulli b[aF6H]) Bool => CDF (Bernoulli b[aF6H]) Float instance Distribution (Bernoulli b[aF6F]) Bool => Distribution (Bernoulli b[aF6F]) Float instance CDF (Bernoulli b[aETw]) Bool => CDF (Bernoulli b[aETw]) Word64 instance Distribution (Bernoulli b[aETu]) Bool => Distribution (Bernoulli b[aETu]) Word64 instance CDF (Bernoulli b[aETs]) Bool => CDF (Bernoulli b[aETs]) Word32 instance Distribution (Bernoulli b[aETq]) Bool => Distribution (Bernoulli b[aETq]) Word32 instance CDF (Bernoulli b[aETo]) Bool => CDF (Bernoulli b[aETo]) Word16 instance Distribution (Bernoulli b[aETm]) Bool => Distribution (Bernoulli b[aETm]) Word16 instance CDF (Bernoulli b[aETk]) Bool => CDF (Bernoulli b[aETk]) Word8 instance Distribution (Bernoulli b[aETi]) Bool => Distribution (Bernoulli b[aETi]) Word8 instance CDF (Bernoulli b[aETg]) Bool => CDF (Bernoulli b[aETg]) Word instance Distribution (Bernoulli b[aETe]) Bool => Distribution (Bernoulli b[aETe]) Word instance CDF (Bernoulli b[aETc]) Bool => CDF (Bernoulli b[aETc]) Int64 instance Distribution (Bernoulli b[aETa]) Bool => Distribution (Bernoulli b[aETa]) Int64 instance CDF (Bernoulli b[aET8]) Bool => CDF (Bernoulli b[aET8]) Int32 instance Distribution (Bernoulli b[aET6]) Bool => Distribution (Bernoulli b[aET6]) Int32 instance CDF (Bernoulli b[aET4]) Bool => CDF (Bernoulli b[aET4]) Int16 instance Distribution (Bernoulli b[aET2]) Bool => Distribution (Bernoulli b[aET2]) Int16 instance CDF (Bernoulli b[aET0]) Bool => CDF (Bernoulli b[aET0]) Int8 instance Distribution (Bernoulli b[aESY]) Bool => Distribution (Bernoulli b[aESY]) Int8 instance CDF (Bernoulli b[aESW]) Bool => CDF (Bernoulli b[aESW]) Int instance Distribution (Bernoulli b[aESU]) Bool => Distribution (Bernoulli b[aESU]) Int instance CDF (Bernoulli b[aESP]) Bool => CDF (Bernoulli b[aESP]) Integer instance Distribution (Bernoulli b[aESN]) Bool => Distribution (Bernoulli b[aESN]) Integer 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.Beta fractionalBeta :: (Fractional a, Distribution Gamma a, Distribution StdUniform a) => a -> a -> RVarT m a beta :: Distribution Beta a => a -> a -> RVar a betaT :: Distribution Beta a => a -> a -> RVarT m a data Beta a Beta :: a -> a -> Beta a instance Distribution Beta Double instance Distribution Beta Float module Data.Random.Distribution.Binomial integralBinomial :: (Integral a, Floating b, Ord b, Distribution Beta b, Distribution StdUniform b) => a -> b -> RVarT m 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 binomialT :: Distribution (Binomial b) a => a -> b -> RVarT m a data Binomial b a Binomial :: a -> b -> Binomial b a instance CDF (Binomial b[aIAF]) Integer => CDF (Binomial b[aIAF]) Double instance Distribution (Binomial b[aIAC]) Integer => Distribution (Binomial b[aIAC]) Double instance CDF (Binomial b[aIAz]) Integer => CDF (Binomial b[aIAz]) Float instance Distribution (Binomial b[aIAw]) Integer => Distribution (Binomial b[aIAw]) Float instance (Real b[aIn1], Distribution (Binomial b[aIn1]) Word64) => CDF (Binomial b[aIn1]) Word64 instance (Floating b[aImY], Ord b[aImY], Distribution Beta b[aImY], Distribution StdUniform b[aImY]) => Distribution (Binomial b[aImY]) Word64 instance (Real b[aImV], Distribution (Binomial b[aImV]) Word32) => CDF (Binomial b[aImV]) Word32 instance (Floating b[aImS], Ord b[aImS], Distribution Beta b[aImS], Distribution StdUniform b[aImS]) => Distribution (Binomial b[aImS]) Word32 instance (Real b[aImP], Distribution (Binomial b[aImP]) Word16) => CDF (Binomial b[aImP]) Word16 instance (Floating b[aImM], Ord b[aImM], Distribution Beta b[aImM], Distribution StdUniform b[aImM]) => Distribution (Binomial b[aImM]) Word16 instance (Real b[aImJ], Distribution (Binomial b[aImJ]) Word8) => CDF (Binomial b[aImJ]) Word8 instance (Floating b[aImG], Ord b[aImG], Distribution Beta b[aImG], Distribution StdUniform b[aImG]) => Distribution (Binomial b[aImG]) Word8 instance (Real b[aImD], Distribution (Binomial b[aImD]) Word) => CDF (Binomial b[aImD]) Word instance (Floating b[aImA], Ord b[aImA], Distribution Beta b[aImA], Distribution StdUniform b[aImA]) => Distribution (Binomial b[aImA]) Word instance (Real b[aImx], Distribution (Binomial b[aImx]) Int64) => CDF (Binomial b[aImx]) Int64 instance (Floating b[aImu], Ord b[aImu], Distribution Beta b[aImu], Distribution StdUniform b[aImu]) => Distribution (Binomial b[aImu]) Int64 instance (Real b[aImr], Distribution (Binomial b[aImr]) Int32) => CDF (Binomial b[aImr]) Int32 instance (Floating b[aImo], Ord b[aImo], Distribution Beta b[aImo], Distribution StdUniform b[aImo]) => Distribution (Binomial b[aImo]) Int32 instance (Real b[aIml], Distribution (Binomial b[aIml]) Int16) => CDF (Binomial b[aIml]) Int16 instance (Floating b[aImi], Ord b[aImi], Distribution Beta b[aImi], Distribution StdUniform b[aImi]) => Distribution (Binomial b[aImi]) Int16 instance (Real b[aImf], Distribution (Binomial b[aImf]) Int8) => CDF (Binomial b[aImf]) Int8 instance (Floating b[aImc], Ord b[aImc], Distribution Beta b[aImc], Distribution StdUniform b[aImc]) => Distribution (Binomial b[aImc]) Int8 instance (Real b[aIm9], Distribution (Binomial b[aIm9]) Int) => CDF (Binomial b[aIm9]) Int instance (Floating b[aIm6], Ord b[aIm6], Distribution Beta b[aIm6], Distribution StdUniform b[aIm6]) => Distribution (Binomial b[aIm6]) Int instance (Real b[aIm3], Distribution (Binomial b[aIm3]) Integer) => CDF (Binomial b[aIm3]) Integer instance (Floating b[aIm0], Ord b[aIm0], Distribution Beta b[aIm0], Distribution StdUniform b[aIm0]) => Distribution (Binomial b[aIm0]) Integer module Data.Random.Distribution.Multinomial multinomial :: Distribution (Multinomial p) [a] => [p] -> a -> RVar [a] multinomialT :: Distribution (Multinomial p) [a] => [p] -> a -> RVarT m [a] data Multinomial p a Multinomial :: [p] -> a -> Multinomial p [a] instance (Num a, Fractional p, Distribution (Binomial p) a) => Distribution (Multinomial p) [a] module Data.Random.Distribution.Poisson integralPoisson :: (Integral a, RealFloat b, Distribution StdUniform b, Distribution (Erlang a) b, Distribution (Binomial b) a) => b -> RVarT m a integralPoissonCDF :: (Integral a, Real b) => b -> a -> Double fractionalPoisson :: (Num a, Distribution (Poisson b) Integer) => b -> RVarT m a fractionalPoissonCDF :: (CDF (Poisson b) Integer, RealFrac a) => b -> a -> Double poisson :: Distribution (Poisson b) a => b -> RVar a poissonT :: Distribution (Poisson b) a => b -> RVarT m a data Poisson b a Poisson :: b -> Poisson b a instance CDF (Poisson b[aNYP]) Integer => CDF (Poisson b[aNYP]) Double instance Distribution (Poisson b[aNYN]) Integer => Distribution (Poisson b[aNYN]) Double instance CDF (Poisson b[aNYL]) Integer => CDF (Poisson b[aNYL]) Float instance Distribution (Poisson b[aNYJ]) Integer => Distribution (Poisson b[aNYJ]) Float instance (Real b[aNMe], Distribution (Poisson b[aNMe]) Word64) => CDF (Poisson b[aNMe]) Word64 instance (RealFloat b[aNMc], Distribution StdUniform b[aNMc], Distribution (Erlang Word64) b[aNMc], Distribution (Binomial b[aNMc]) Word64) => Distribution (Poisson b[aNMc]) Word64 instance (Real b[aNMa], Distribution (Poisson b[aNMa]) Word32) => CDF (Poisson b[aNMa]) Word32 instance (RealFloat b[aNM8], Distribution StdUniform b[aNM8], Distribution (Erlang Word32) b[aNM8], Distribution (Binomial b[aNM8]) Word32) => Distribution (Poisson b[aNM8]) Word32 instance (Real b[aNM6], Distribution (Poisson b[aNM6]) Word16) => CDF (Poisson b[aNM6]) Word16 instance (RealFloat b[aNM4], Distribution StdUniform b[aNM4], Distribution (Erlang Word16) b[aNM4], Distribution (Binomial b[aNM4]) Word16) => Distribution (Poisson b[aNM4]) Word16 instance (Real b[aNM2], Distribution (Poisson b[aNM2]) Word8) => CDF (Poisson b[aNM2]) Word8 instance (RealFloat b[aNM0], Distribution StdUniform b[aNM0], Distribution (Erlang Word8) b[aNM0], Distribution (Binomial b[aNM0]) Word8) => Distribution (Poisson b[aNM0]) Word8 instance (Real b[aNLY], Distribution (Poisson b[aNLY]) Word) => CDF (Poisson b[aNLY]) Word instance (RealFloat b[aNLW], Distribution StdUniform b[aNLW], Distribution (Erlang Word) b[aNLW], Distribution (Binomial b[aNLW]) Word) => Distribution (Poisson b[aNLW]) Word instance (Real b[aNLU], Distribution (Poisson b[aNLU]) Int64) => CDF (Poisson b[aNLU]) Int64 instance (RealFloat b[aNLS], Distribution StdUniform b[aNLS], Distribution (Erlang Int64) b[aNLS], Distribution (Binomial b[aNLS]) Int64) => Distribution (Poisson b[aNLS]) Int64 instance (Real b[aNLQ], Distribution (Poisson b[aNLQ]) Int32) => CDF (Poisson b[aNLQ]) Int32 instance (RealFloat b[aNLO], Distribution StdUniform b[aNLO], Distribution (Erlang Int32) b[aNLO], Distribution (Binomial b[aNLO]) Int32) => Distribution (Poisson b[aNLO]) Int32 instance (Real b[aNLM], Distribution (Poisson b[aNLM]) Int16) => CDF (Poisson b[aNLM]) Int16 instance (RealFloat b[aNLK], Distribution StdUniform b[aNLK], Distribution (Erlang Int16) b[aNLK], Distribution (Binomial b[aNLK]) Int16) => Distribution (Poisson b[aNLK]) Int16 instance (Real b[aNLI], Distribution (Poisson b[aNLI]) Int8) => CDF (Poisson b[aNLI]) Int8 instance (RealFloat b[aNLG], Distribution StdUniform b[aNLG], Distribution (Erlang Int8) b[aNLG], Distribution (Binomial b[aNLG]) Int8) => Distribution (Poisson b[aNLG]) Int8 instance (Real b[aNLE], Distribution (Poisson b[aNLE]) Int) => CDF (Poisson b[aNLE]) Int instance (RealFloat b[aNLC], Distribution StdUniform b[aNLC], Distribution (Erlang Int) b[aNLC], Distribution (Binomial b[aNLC]) Int) => Distribution (Poisson b[aNLC]) Int instance (Real b[aNLA], Distribution (Poisson b[aNLA]) Integer) => CDF (Poisson b[aNLA]) Integer instance (RealFloat b[aNLy], Distribution StdUniform b[aNLy], Distribution (Erlang Integer) b[aNLy], Distribution (Binomial b[aNLy]) Integer) => Distribution (Poisson b[aNLy]) Integer