random-fu 0.2 → 0.2.1.0
raw patch · 2 files changed
+39/−12 lines, 2 filesdep ~base
Dependency ranges changed: base
Files
- random-fu.cabal +25/−10
- src/Data/Random/Distribution/Categorical.hs +14/−2
random-fu.cabal view
@@ -1,5 +1,5 @@ name: random-fu-version: 0.2+version: 0.2.1.0 stability: provisional cabal-version: >= 1.6@@ -18,24 +18,34 @@ 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.+ .+ Changes in 0.2.1.0: Exposed Categorical type (it+ had been hidden by accident a few version ago),+ gave it a Read instance, and dropped a + no-longer-necessary Ord context from 'fromWeightedList'.+ Thank you Antal Spector-Zabusky for catching these!+ .+ Changes in 0.2.0.2: None except setting + \"Buildable: False\" under GHC 7.2.1 (see + flexible-defaults 0.0.0.2 for more detailed+ explanation).+ .+ Changes in 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. tested-with: GHC == 6.10.4, GHC == 6.12.1, GHC == 6.12.3,- GHC == 7.0.1, GHC == 7.0.2+ GHC == 7.0.1, GHC == 7.0.2, GHC == 7.0.4 source-repository head type: git@@ -49,7 +59,7 @@ Description: mtl-2 has State, etc., as "type" rather than "newtype" Library- ghc-options: -Wall -funbox-strict-fields -fno-method-sharing+ ghc-options: -Wall -funbox-strict-fields hs-source-dirs: src exposed-modules: Data.Random Data.Random.Distribution@@ -103,3 +113,8 @@ build-depends: erf-native else build-depends: 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+ Buildable: False
src/Data/Random/Distribution/Categorical.hs view
@@ -4,7 +4,8 @@ #-} module Data.Random.Distribution.Categorical- ( categorical, categoricalT+ ( Categorical+ , categorical, categoricalT , fromList, toList , fromWeightedList, fromObservations , mapCategoricalPs, normalizeCategoricalPs@@ -53,7 +54,7 @@ -- |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+fromWeightedList :: Fractional p => [(p,a)] -> Categorical p a fromWeightedList = normalizeCategoricalPs . fromList -- |Construct a 'Categorical' distribution from a list of observed outcomes.@@ -63,6 +64,11 @@ fromObservations :: (Fractional p, Ord a) => [a] -> Categorical p a fromObservations = fromWeightedList . map (genericLength &&& head) . group . sort +-- The following description refers to the public interface. For those reading+-- the code, in the actual implementation Categorical is stored as a vector of+-- (cumulative-probability, value) pairs, so that sampling can take advantage of+-- binary search.+ -- |Categorical distribution; a list of events with corresponding probabilities. -- The sum of the probabilities must be 1, and no event should have a zero -- or negative probability (at least, at time of sampling; very clever users@@ -76,6 +82,12 @@ ( showString "fromList " . showsPrec 11 (toList cat) )++instance (Num p, Read p, Read a) => Read (Categorical p a) where+ readsPrec p = readParen (p > 10) $ \str -> do+ ("fromList", valStr) <- lex str+ (vals, rest) <- readsPrec 11 valStr+ return (fromList vals, rest) instance (Fractional p, Ord p, Distribution Uniform p) => Distribution (Categorical p) a where rvarT (Categorical ds)