random-fu 0.2.7.4 → 0.2.7.6
raw patch · 3 files changed
+38/−36 lines, 3 filesdep +random
Dependencies added: random
Files
- random-fu.cabal +11/−9
- src/Data/Random.hs +22/−23
- src/Data/Random/Sample.hs +5/−4
random-fu.cabal view
@@ -1,8 +1,8 @@ name: random-fu-version: 0.2.7.4+version: 0.2.7.6 stability: provisional -cabal-version: >= 1.6+cabal-version: >= 1.10 build-type: Simple author: James Cook <mokus@deepbondi.net>@@ -12,21 +12,21 @@ category: Math synopsis: Random number generation-description: Random number generation based on modeling random +description: 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 + 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 + 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 + a fair bit slower than straight C implementations of the same algorithms. tested-with: GHC == 7.10.3@@ -47,6 +47,7 @@ Library ghc-options: -Wall -funbox-strict-fields hs-source-dirs: src+ default-language: Haskell2010 exposed-modules: Data.Random Data.Random.Distribution Data.Random.Distribution.Bernoulli@@ -82,15 +83,16 @@ else cpp-options: -Dold_Fixed build-depends: base >= 4 && <4.2- + if flag(mtl2) build-depends: mtl == 2.* cpp-options: -DMTL2 else build-depends: mtl == 1.*- + build-depends: math-functions, monad-loops >= 0.3.0.1,+ random >= 1.2.0, random-shuffle, random-source == 0.3.*, rvar == 0.2.*,@@ -99,7 +101,7 @@ transformers, vector >= 0.7, erf- + if impl(ghc == 7.2.1) -- Doesn't work under GHC 7.2.1 due to -- http://hackage.haskell.org/trac/ghc/ticket/5410
src/Data/Random.hs view
@@ -1,39 +1,39 @@ -- |Flexible modeling and sampling of random variables. ----- The central abstraction in this library is the concept of a random --- variable. It is not fully formalized in the standard measure-theoretic --- language, but rather is informally defined as a \"thing you can get random --- values out of\". Different random variables may have different types of +-- The central abstraction in this library is the concept of a random+-- variable. It is not fully formalized in the standard measure-theoretic+-- language, but rather is informally defined as a \"thing you can get random+-- values out of\". Different random variables may have different types of -- values they can return or the same types but different probabilities for -- each value they can return. The random values you get out of them are -- traditionally called \"random variates\".--- --- Most imperative-language random number libraries are all about obtaining --- and manipulating random variates. This one is about defining, manipulating --- and sampling random variables. Computationally, the distinction is small --- and mostly just a matter of perspective, but from a program design +--+-- Most imperative-language random number libraries are all about obtaining+-- and manipulating random variates. This one is about defining, manipulating+-- and sampling random variables. Computationally, the distinction is small+-- and mostly just a matter of perspective, but from a program design -- perspective it provides both a powerfully composable abstraction and a -- very useful separation of concerns.--- +-- -- Abstract random variables as implemented by 'RVar' are composable. They can -- be defined in a monadic / \"imperative\" style that amounts to manipulating -- variates, but with strict type-level isolation. Concrete random variables -- are also provided, but they do not compose as generically. The 'Distribution'--- type class allows concrete random variables to \"forget\" their concreteness --- so that they can be composed. For examples of both, see the documentation --- for 'RVar' and 'Distribution', as well as the code for any of the concrete +-- type class allows concrete random variables to \"forget\" their concreteness+-- so that they can be composed. For examples of both, see the documentation+-- for 'RVar' and 'Distribution', as well as the code for any of the concrete -- distributions such as 'Uniform', 'Gamma', etc.--- +-- -- Both abstract and concrete random variables can be sampled (despite the -- types GHCi may list for the functions) by the functions in "Data.Random.Sample".--- +-- -- Random variable sampling is done with regard to a generic basis of primitive--- random variables defined in "Data.Random.Internal.Primitives". This basis +-- random variables defined in "Data.Random.Internal.Primitives". This basis -- is very low-level and the actual set of primitives is still fairly experimental, -- which is why it is in the \"Internal\" sub-heirarchy. User-defined variables -- should use the existing high-level variables such as 'Uniform' and 'Normal' -- rather than these basis variables. "Data.Random.Source" defines classes for--- entropy sources that provide implementations of these primitive variables. +-- entropy sources that provide implementations of these primitive variables. -- Several implementations are available in the Data.Random.Source.* modules. module Data.Random ( -- * Random variables@@ -43,23 +43,23 @@ -- ** Concrete ('Distribution') Distribution(..), CDF(..), PDF(..),- + -- * Sampling random variables Sampleable(..), sample, sampleState, sampleStateT,- + -- * A few very common distributions Uniform(..), uniform, uniformT, StdUniform(..), stdUniform, stdUniformT, Normal(..), normal, stdNormal, normalT, stdNormalT, Gamma(..), gamma, gammaT,- + -- * Entropy Sources MonadRandom, RandomSource, StdRandom(..),- + -- * Useful list-based operations randomElement, shuffle, shuffleN, shuffleNofM- + ) where import Data.Random.Sample@@ -67,7 +67,6 @@ import Data.Random.Source.IO () import Data.Random.Source.MWC () import Data.Random.Source.StdGen ()-import Data.Random.Source.PureMT () import Data.Random.Source.Std import Data.Random.Distribution import Data.Random.Distribution.Gamma
src/Data/Random/Sample.hs view
@@ -1,6 +1,6 @@ {-# LANGUAGE MultiParamTypeClasses,- FlexibleInstances, FlexibleContexts, + FlexibleInstances, FlexibleContexts, IncoherentInstances #-} @@ -8,12 +8,13 @@ module Data.Random.Sample where -import Control.Monad.State +import Control.Monad.State import Data.Random.Distribution import Data.Random.Lift import Data.Random.RVar import Data.Random.Source import Data.Random.Source.Std+import System.Random (RandomGen) -- |A typeclass allowing 'Distribution's and 'RVar's to be sampled. Both may -- also be sampled via 'runRVar' or 'runRVarT', but I find it psychologically@@ -37,10 +38,10 @@ -- |Sample a random variable in a \"functional\" style. Typical instantiations -- of @s@ are @System.Random.StdGen@ or @System.Random.Mersenne.Pure64.PureMT@.-sampleState :: (Sampleable d (State s) t, MonadRandom (State s)) => d t -> s -> (t, s)+sampleState :: (RandomGen s, Sampleable d (State s) t, MonadRandom (State s)) => d t -> s -> (t, s) sampleState thing = runState (sample thing) -- |Sample a random variable in a \"semi-functional\" style. Typical instantiations -- of @s@ are @System.Random.StdGen@ or @System.Random.Mersenne.Pure64.PureMT@.-sampleStateT :: (Sampleable d (StateT s m) t, MonadRandom (StateT s m)) => d t -> s -> m (t, s)+sampleStateT :: (RandomGen s, Sampleable d (StateT s m) t, MonadRandom (StateT s m)) => d t -> s -> m (t, s) sampleStateT thing = runStateT (sample thing)