probable 0.1.0.0 → 0.1.1
raw patch · 7 files changed
+38/−101 lines, 7 filesdep ~basedep ~mwc-randomdep ~vector
Dependency ranges changed: base, mwc-random, vector
Files
- README.md +9/−0
- bench/distrib.hs +0/−55
- bench/random.hs +13/−13
- examples/montyhall.hs +2/−12
- probable.cabal +4/−18
- src/Math/Probable/Distribution/Finite.hs +5/−1
- src/Math/Probable/Random.hs +5/−2
README.md view
@@ -1,6 +1,8 @@ probable ======== +[](http://travis-ci.org/alpmestan/probable)+ Simple random value generation for haskell, using an efficient random generator and minimizing system calls. But the library also lets you work with distributions over a finite set, adapting@@ -87,3 +89,10 @@ print $ exact twoBooks print $ exact oneInteresting ```++Contact+=======++This library is written and maintained by Alp Mestanogullari.++Feel free to contact me for any feedback, comment, suggestion, bug report and what not.
− bench/distrib.hs
@@ -1,55 +0,0 @@-module Main where--import Control.Applicative-import Control.Monad-import Control.Monad.Primitive-import Criterion.Main-import System.Random.MWC--import qualified Data.Vector as V-import qualified Data.Vector.Unboxed as U-import Math.Probable--n :: Int-n = 100000--data Fruit = Apple | Banana | Orange- deriving (Eq, Show)--fruitDist :: FromFinite d => d Fruit-fruitDist = weighted [ (Apple, 0.3)- , (Banana, 0.6)- , (Orange, 0.1)- ]-liftF' :: Fin a -> IO a-liftF' dist = do- d <- withSystemRandom . asGenIO $ uniform- pick (P d) (exact dist)--instance FromFinite IO where- weighted = liftF' . weighted--observe1 :: RandT IO Fruit -> IO (V.Vector Fruit)-observe1 = mwc . vectorOf5 n--observe2 :: RandT IO Fruit -> IO (V.Vector Fruit)-observe2 r = mwc . RandT $ \gen ->- V.replicateM n (runRandT r gen)--observe4 :: IO Fruit- -> IO (V.Vector Fruit)-observe4 fr = - V.replicateM n (fr gen)----- | Time to benchmark!-main :: IO ()-main = do - defaultMain - [ - bgroup "big vector of fruits"- [ bench "observe1" $ whnfIO (observe1 fruitDist)- , bench "observe2" $ whnfIO (observe2 fruitDist)- , bench "observe3" $ whnfIO (observe4 fruitDist)- ]- ]
bench/random.hs view
@@ -11,13 +11,13 @@ import qualified Data.Vector.Unboxed as U import Math.Probable -probable3 :: (U.Unbox a, Variate a) => Int -> IO (U.Vector a)-probable3 n = mwc (vectorOf3 n)-{-# INLINE probable3 #-}+probable1 :: (U.Unbox a, Variate a) => Int -> IO (U.Vector a)+probable1 n = mwc (vectorOfVariate n)+{-# INLINE probable1 #-} -probable5 :: U.Unbox a => Int -> RandT IO a -> IO (U.Vector a)-probable5 n gen = mwc (vectorOf5 n gen)-{-# INLINE probable5 #-}+probable2 :: U.Unbox a => RandT IO a -> Int -> IO (U.Vector a)+probable2 r n = mwc (vectorOf n r)+{-# INLINE probable2 #-} mwc1 :: (U.Unbox a, Variate a) => Int -> IO (U.Vector a) mwc1 n = @@ -28,7 +28,7 @@ mwc2 :: (U.Unbox a, Variate a) => Int -> IO (U.Vector a) mwc2 n = withSystemRandom . asGenIO $- \gen -> U.replicateM n (uniform gen)+ \gen -> U.replicateM n (System.Random.MWC.uniform gen) {-# INLINE mwc2 #-} mwcm :: (U.Unbox a, Variate a) => Int -> IO (U.Vector a)@@ -65,19 +65,19 @@ defaultMain [ bgroup "big vector of int"- [ bench "probable3" $ whnfIO (i $ probable3 n)- , bench "probable5" $ whnfIO (probable5 n int)+ [ bench "probable1" $ whnfIO (i $ probable1 n)+ , bench "probable2" $ whnfIO (i $ probable2 int n) , bench "mwc-random" $ whnfIO (i $ mwc1 n) , bench "mwc-random2" $ whnfIO (i $ mwc2 n)- , bench "mwc-random-monad2" $ whnfIO (i $ mwcm n)+ , bench "mwc-random-monad" $ whnfIO (i $ mwcm n) ], bgroup "big vector of double"- [ bench "probable3" $ whnfIO (d $ probable3 n)- , bench "probable5" $ whnfIO (probable5 n double)+ [ bench "probable1" $ whnfIO (d $ probable1 n)+ , bench "probable2" $ whnfIO (d $ probable2 double n) , bench "mwc-random" $ whnfIO (d $ mwc1 n) , bench "mwc-random2" $ whnfIO (d $ mwc2 n)- , bench "mwc-random-monad2" $ whnfIO (d $ mwcm n)+ , bench "mwc-random-monad" $ whnfIO (d $ mwcm n) ] ]
examples/montyhall.hs view
@@ -38,16 +38,6 @@ True -> Win False -> Lose --- | We run the distribution of results and group the 'Win's and the 'Lose's--- respective probabilities together--- maybe this should be in the library, with a more general type...-collect :: Fin Result -> (Event Result, Event Result)-collect = f . exact- where f = toPair . foldl' combine (0, 0)- combine (!winP, !loseP) (Event Win p) = (winP+p, loseP)- combine (!winP, !loseP) (Event Lose p) = (winP, loseP+p)- toPair (winP, loseP) = (Event Win winP, Event Lose loseP)- -- | Given a strategy to adopt, what's the distribution of Win/Lose ? result :: (Door -> Door -> Door) -> Fin Result@@ -97,11 +87,11 @@ main :: IO () main = do putStrLn $ "Using the conservative strategy: "- ++ show (collect $ result keep)+ ++ show (exact $ result keep) -- Using the conservative strategy: (Event Win 33.3%,Event Lose 66.7%) putStrLn $ "Switching: "- ++ show (collect $ result switch)+ ++ show (exact $ result switch) -- Switching: (Event Win 66.7%,Event Lose 33.3%) putStrLn "---"
probable.cabal view
@@ -1,5 +1,5 @@ name: probable-version: 0.1.0.0+version: 0.1.1 synopsis: Easy and reasonably efficient probabilistic programming and random generation description: Easy and reasonably efficient probabilistic programming and random generation .@@ -61,11 +61,11 @@ license-file: LICENSE author: Alp Mestanogullari maintainer: alpmestan@gmail.com-copyright: 2014 Alp Mestanogullari+copyright: 2014-2015 Alp Mestanogullari category: Math, Statistics build-type: Simple cabal-version: >=1.10-tested-with: GHC == 7.6.3, GHC == 7.8.2+tested-with: GHC == 7.6.3, GHC == 7.8.2, GHC == 7.10.1 extra-source-files: bench/*.hs, examples/*.hs, README.md@@ -81,7 +81,7 @@ Math.Probable.Distribution.Finite, Math.Probable.Random other-modules: - build-depends: base >=4.5 && <4.8,+ build-depends: base >=4.5 && <4.9, statistics >= 0.10, vector >= 0.10, mwc-random >= 0.10,@@ -104,18 +104,4 @@ probable, mwc-random, mwc-random-monad- default-language: Haskell2010--benchmark distrib- main-is: distrib.hs- hs-source-dirs: bench- ghc-options: -O2 -funbox-strict-fields- type: exitcode-stdio-1.0- build-depends: base >= 4 && < 5, - vector >= 0.7, - criterion, - probable,- mwc-random,- primitive- default-language: Haskell2010
src/Math/Probable/Distribution/Finite.hs view
@@ -1,10 +1,11 @@ {-# LANGUAGE GeneralizedNewtypeDeriving, + CPP, BangPatterns, TupleSections, FlexibleInstances, TypeSynonymInstances #-} -- |--- Module : Math.Probable+-- Module : Math.Probable.Distribution.Finite -- License : BSD3 -- Maintainer : alpmestan@gmail.com -- Stability : experimental@@ -149,7 +150,10 @@ , FinBayes, bayes, condition, onlyJust ) where +#if !MIN_VERSION_base(4, 8, 0) import Control.Applicative+#endif+ import Control.Monad import Control.Monad.Primitive import Control.Monad.Trans
src/Math/Probable/Random.hs view
@@ -1,8 +1,8 @@-{-# LANGUAGE BangPatterns,+{-# LANGUAGE CPP, BangPatterns, TypeFamilies #-} -- |--- Module : Math.Probable+-- Module : Math.Probable.Random -- Copyright : (c) 2014 Alp Mestanogullari -- License : BSD3 -- Maintainer : alpmestan@gmail.com@@ -111,7 +111,10 @@ listOf, vectorOf, vectorOfVariate ) where +#if !MIN_VERSION_base(4, 8, 0) import Control.Applicative+#endif+ import Control.Monad.Identity import Control.Monad.Primitive import Control.Monad.ST