mwc-random-monad 0.2 → 0.3
raw patch · 3 files changed
+115/−99 lines, 3 filesdep ~mwc-randomdep ~vectorPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: mwc-random, vector
API changes (from Hackage documentation)
- System.Random.MWC.Monad: choices :: PrimMonad m => [(Double, Rand m a)] -> Rand m a
- System.Random.MWC.Monad: equiprobable :: PrimMonad m => [Rand m a] -> Rand m a
- System.Random.MWC.Monad: normal :: PrimMonad m => Double -> Double -> Rand m Double
- System.Random.MWC.Monad: standard :: PrimMonad m => Rand m Double
+ System.Random.MWC.Distributions.Monad: chiSquare :: PrimMonad m => Int -> Rand m Double
+ System.Random.MWC.Distributions.Monad: exponential :: PrimMonad m => Double -> Rand m Double
+ System.Random.MWC.Distributions.Monad: gamma :: PrimMonad m => Double -> Double -> Rand m Double
+ System.Random.MWC.Distributions.Monad: normal :: PrimMonad m => Double -> Double -> Rand m Double
+ System.Random.MWC.Distributions.Monad: standard :: PrimMonad m => Rand m Double
+ System.Random.MWC.Monad: fromSeed :: Seed -> Vector Word32
+ System.Random.MWC.Monad: runWithCreate :: PrimMonad m => Rand m a -> m a
+ System.Random.MWC.Monad: toSeed :: Vector v Word32 => v Word32 -> Seed
Files
- System/Random/MWC/Distributions/Monad.hs +59/−0
- System/Random/MWC/Monad.hs +42/−94
- mwc-random-monad.cabal +14/−5
+ System/Random/MWC/Distributions/Monad.hs view
@@ -0,0 +1,59 @@+-- |+-- Module : System.Random.MWC.Monad+-- Copyright : (c) 2010-2012 Aleksey Khudyakov+-- License : BSD3+--+-- Maintainer : alexey.skladnoy@gmail.com+-- Stability : experimental+-- Portability : portable+--+-- Monadic wrapper for various distributions generators.+module System.Random.MWC.Distributions.Monad (+ normal+ , standard+ , exponential+ , gamma+ , chiSquare+ ) where++import Control.Monad.Primitive (PrimMonad)++import qualified System.Random.MWC.Distributions as MWC+import System.Random.MWC.Monad++++-- | Normally distributed variables with mean 0 and 1 standard deviation+standard :: PrimMonad m => Rand m Double+standard = toRand $ \g -> MWC.standard g+{-# INLINE normal #-}++-- | Normally distributed variable+normal :: PrimMonad m => + Double -- ^ Mean+ -> Double -- ^ Standard deviation+ -> Rand m Double+normal m s = toRand $ \g -> MWC.normal m s g+{-# INLINE standard #-}++-- | Generate exponentially distributed random variate. +exponential :: PrimMonad m =>+ Double -- ^ Scale parameter+ -> Rand m Double+exponential x = toRand $ \g -> MWC.exponential x g+{-# INLINE exponential #-}++-- | Random variate generator for gamma distribution.+gamma :: PrimMonad m+ => Double -- ^ Shape parameter+ -> Double -- ^ Scale parameter+ -> Rand m Double+gamma a b = toRand $ \g -> MWC.gamma a b g+{-# INLINE gamma #-}++-- | Random variate generator for chi square distribution.+chiSquare :: PrimMonad m+ => Int -- ^ Number of degrees of freedom+ -> Rand m Double+chiSquare n = toRand $ \g -> MWC.chiSquare n g+{-# INLINE chiSquare #-}
System/Random/MWC/Monad.hs view
@@ -1,43 +1,52 @@ {-# LANGUAGE FlexibleContexts #-}-module System.Random.MWC.Monad ( -- * Random monad- Rand- , liftR- , RandIO- , asRandIO- , RandST- , asRandST- , Seed- , runRand- , runWithSeed- , runWithVector- , runWithSystemRandom- -- * Random numbers generation- , toRand- , uniform- , uniformR- , standard- , normal- -- * Combining monads - , equiprobable- , choices- -- * Seed management- , save- ) where+-- |+-- Module : System.Random.MWC.Monad+-- Copyright : (c) 2010-2012 Aleksey Khudyakov+-- License : BSD3+--+-- Maintainer : alexey.skladnoy@gmail.com+-- Stability : experimental+-- Portability : portable+--+-- This module provide monadic interface for @mwc-random@+-- package. It's just a thin wrapper and all work is done+-- @mwc-random@.+module System.Random.MWC.Monad ( + -- * Random monad+ Rand+ , toRand+ , liftR+ -- ** Type syhnonims+ , RandIO+ , asRandIO+ , RandST+ , asRandST+ -- ** Running monad+ , runRand+ , runWithCreate+ , runWithSeed+ , runWithVector+ , runWithSystemRandom+ -- * Random numbers generation+ , uniform+ , uniformR+ -- * Seed management+ , Seed+ , toSeed+ , fromSeed+ , save+ ) where import Control.Applicative import Control.Monad (ap) import Control.Monad.ST (ST) import Control.Monad.Primitive (PrimMonad, PrimState) -import qualified Data.Vector as V-import qualified Data.Vector.Unboxed as U+import Data.Word (Word32) import qualified Data.Vector.Generic as G-import Data.Ord-import Data.Word (Word32)-import Debug.Trace import qualified System.Random.MWC as MWC-import System.Random.MWC (Gen,Variate,Seed)+import System.Random.MWC (Gen,Variate,Seed,toSeed,fromSeed) ---------------------------------------------------------------- @@ -93,7 +102,7 @@ {-# INLINE fmap #-} instance PrimMonad m => Monad (Rand m) where- return x = Rand (\_ -> return x)+ return = Rand . const . return (Rand rnd) >>= f = Rand $ \g -> (\x -> runRand (f x) g) =<< rnd g {-# INLINE return #-} {-# INLINE (>>=) #-}@@ -113,74 +122,13 @@ -- | Uniformly distributed values uniform :: (PrimMonad m, Variate a) => Rand m a-uniform = Rand MWC.uniform+uniform = Rand $ \g -> MWC.uniform g {-# INLINE uniform #-} -- | Uniformly distributed values in range uniformR :: (PrimMonad m, Variate a) => (a,a) -> Rand m a-uniformR rng = Rand (MWC.uniformR rng)+uniformR rng = Rand $ \g -> MWC.uniformR rng g {-# INLINE uniformR #-}---- | Normally distributed variables with mean 0 and 1 standard deviation-standard :: PrimMonad m => Rand m Double-standard = Rand MWC.normal-{-# INLINE normal #-}---- | Normally distributed variable-normal :: PrimMonad m => - Double -- Mean- -> Double -- Variance- -> Rand m Double-normal m s = (+ m) . (* s) <$> standard-{-# INLINE standard #-}---------------------------------------------------------------------- | Randomly select from list of equiprobable random sources. List must be non-empty-equiprobable :: PrimMonad m => [Rand m a] -> Rand m a-equiprobable [] = error "System.Random.MWC.Monad.equiprobable: list must be nonempty"-equiprobable xs = worker (V.fromList xs)- where- worker v = do- -- uniform - 2^(-53) lies in the [0,1) range- p <- uniform- v V.! truncate ((p - 2^^(-53)) * fromIntegral (V.length v) :: Double)- --- | Randomly select from list of weighted random sources. List must--- contain sources with positive weight. Elements with non-positive--- weight are discarded-choices :: PrimMonad m => [(Double,Rand m a)] -> Rand m a-choices xs- | null xs' = error "System.Random.MWC.Monad.choices: list must contain at least one nonnegative weight"- | otherwise = traceShow (ps, V.length vect) - $ worker vect ps- where- xs' = filter ((>0) . fst) xs- vect = V.fromList (map snd xs')- ps = U.init . U.scanl' (+) 0 $ U.map (/ U.sum q) q- where q = U.fromList (map fst xs')- worker vect probs = do- p <- uniform- let i = binary probs p- vect V.! (i - 1)---- Binary search /Copied from vector algorithms-binary :: (Ord a, U.Unbox a) => U.Vector a -> a -> Int-binary v x = binaryRange v x 0 (U.length v)-{-# INLINE binary #-}---- Binary search in range-binaryRange :: (Ord a, U.Unbox a) => U.Vector a -> a -> Int -> Int -> Int-binaryRange v x = loop - where- loop i j | i >= j = j- | otherwise = case compare (U.unsafeIndex v k) x of- LT -> loop (k+1) j- EQ -> k - GT -> loop i k- where k = (i + j) `div` 2-{-# INLINE binaryRange #-}- ----------------------------------------------------------------
mwc-random-monad.cabal view
@@ -1,23 +1,32 @@ Name: mwc-random-monad-Version: 0.2-Synopsis: Monadic interface for mwc-random+Version: 0.3 License: BSD3 License-file: LICENSE Author: Alexey Khudyakov <alexey.skladnoy@gmail.com> Maintainer: Alexey Khudyakov <alexey.skladnoy@gmail.com>+bug-reports: https://bitbucket.org/Shimuuar/mwc-random-monad/issues Category: Math, Statistics Build-type: Simple Cabal-version: >=1.6+Synopsis: Monadic interface for mwc-random Description: Simple monadit interface for mwc-random. Library build-depends: base >= 3 && < 5, primitive,- vector >= 0.6.0.2,- mwc-random >= 0.8+ vector >= 0.7,+ mwc-random >= 0.11 Exposed-modules: System.Random.MWC.Monad- + System.Random.MWC.Distributions.Monad+ Ghc-options: -O2 -Wall+ Ghc-prof-options: -auto-all+ source-repository head type: mercurial location: http://bitbucket.org/Shimuuar/mwc-random-monad++source-repository head+ type: git+ location: git://github.com/Shimuuar/mwc-random+