mwc-probability 2.0.4 → 2.1.0
raw patch · 3 files changed
+22/−11 lines, 3 files
Files
- CHANGELOG +4/−0
- mwc-probability.cabal +3/−3
- src/System/Random/MWC/Probability.hs +15/−8
CHANGELOG view
@@ -1,5 +1,9 @@ # Changelog + - 2.1.0 (2019-07-23)+ * Generalises 'categorical' and 'multinomial' to take things proportional to+ probabilities, rather than probabilities proper.+ - 2.0.4 (2018-06-30) * Clean up docs and add some additional usage information. * Split the existing Student t distribution into 'student' and its
mwc-probability.cabal view
@@ -1,5 +1,5 @@ name: mwc-probability-version: 2.0.4+version: 2.1.0 homepage: http://github.com/jtobin/mwc-probability license: MIT license-file: LICENSE@@ -8,7 +8,7 @@ category: Math build-type: Simple cabal-version: >= 1.10-tested-with: GHC == 8.0.2, GHC == 8.2.2 , GHC == 8.4.2+tested-with: GHC == 8.0.2, GHC == 8.2.2 , GHC == 8.4.2, GHC == 8.6.5 synopsis: Sampling function-based probability distributions. description: @@ -55,7 +55,7 @@ hs-source-dirs: src build-depends: base >= 4.8 && < 6- , mwc-random > 0.13 && < 0.14+ , mwc-random > 0.13 && < 0.15 , primitive >= 0.6 && < 1.0 , transformers >= 0.5 && < 1.0
src/System/Random/MWC/Probability.hs view
@@ -340,15 +340,21 @@ -- | The multinomial distribution of `n` trials and category probabilities -- `ps`. ----- Note that `ps` is a vector of probabilities and should sum to one.+-- Note that the supplied probability container should consist of non-negative+-- values but is not required to sum to one. multinomial :: (Foldable f, PrimMonad m) => Int -> f Double -> Prob m [Int] multinomial n ps = do- let cumulative = scanl1 (+) (F.toList ps)- replicateM n $ do- z <- uniform- case findIndex (> z) cumulative of- Just g -> return g- Nothing -> error "mwc-probability: invalid probability vector"+ let (cumulative, total) = runningTotals (F.toList ps)+ replicateM n $ do+ z <- uniformR (0, total)+ case findIndex (> z) cumulative of+ Just g -> return g+ Nothing -> error "mwc-probability: invalid probability vector"+ where+ -- Note: this is significantly faster than any+ -- of the recursions one might write by hand.+ runningTotals :: Num a => [a] -> ([a], a)+ runningTotals xs = let adds = scanl1 (+) xs in (adds, sum xs) {-# INLINABLE multinomial #-} -- | Generalized Student's t distribution with location parameter `m`, scale@@ -404,7 +410,8 @@ -- | A categorical distribution defined by the supplied probabilities. ----- Note that the supplied container of probabilities must sum to 1.+-- Note that the supplied probability container should consist of non-negative+-- values but is not required to sum to one. categorical :: (Foldable f, PrimMonad m) => f Double -> Prob m Int categorical ps = do xs <- multinomial 1 ps