mwc-probability

Sampling function-based probability distributions.
Log | Files | Refs | README | LICENSE

commit 01e0a01083a3fe4c936401a57b511bb3bf544ffd
parent e721dccc449a83f17a2867fbd97786ff0c6c4b1c
Author: Marco Zocca <marco.zocca@recordunion.com>
Date:   Mon, 29 Jan 2018 12:58:34 +0100

add Zipf, add Haddock sections, upd changelog, version bump

Diffstat:
MCHANGELOG | 9+++++++--
Mmwc-probability.cabal | 4+++-
Msrc/System/Random/MWC/Probability.hs | 53++++++++++++++++++++++++++++++++++++++---------------
3 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,6 +1,11 @@ # Changelog -- 1.3.0 (2016-12-04) - * Generalize a couple of samplers to use Traversable rather than lists. + - 1.4.0 (2018-01-29) + * Add Laplace and Zipf distribution + * Divide Haddock in sections + * Rename `isoGauss` to `isoNormal` and `standard` to `standardNormal` to uniform naming scheme + + - 1.3.0 (2016-12-04) + * Generalize a couple of samplers to use Traversable rather than lists. diff --git a/mwc-probability.cabal b/mwc-probability.cabal @@ -1,5 +1,5 @@ name: mwc-probability -version: 1.3.1 +version: 1.4.0 homepage: http://github.com/jtobin/mwc-probability license: MIT license-file: LICENSE @@ -41,6 +41,8 @@ description: > p <- beta a b > n <- uniformR (5, 10) > binomial n p +extra-source-files: README.md + CHANGELOG Source-repository head Type: git diff --git a/src/System/Random/MWC/Probability.hs b/src/System/Random/MWC/Probability.hs @@ -50,12 +50,13 @@ module System.Random.MWC.Probability ( , Prob(..) , samples + -- * Distributions + -- ** Continuous-valued , uniform , uniformR - , discreteUniform - , categorical - , standard , normal + , standardNormal + , isoNormal , logNormal , exponential , laplace @@ -63,14 +64,20 @@ module System.Random.MWC.Probability ( , inverseGamma , chiSquare , beta - , dirichlet - , symmetricDirichlet + , student + + -- ** Discrete-valued + , discreteUniform + , zipf + , categorical , bernoulli , binomial , multinomial - , student - , isoGauss - , poisson + , poisson + -- ** Dirichlet process + , dirichlet + , symmetricDirichlet + ) where import Control.Applicative @@ -169,9 +176,9 @@ discreteUniform cs = do -- | The standard normal or Gaussian distribution (with mean 0 and standard -- deviation 1). -standard :: PrimMonad m => Prob m Double -standard = Prob MWC.Dist.standard -{-# INLINABLE standard #-} +standardNormal :: PrimMonad m => Prob m Double +standardNormal = Prob MWC.Dist.standard +{-# INLINABLE standardNormal #-} -- | The normal or Gaussian distribution with a specified mean and standard -- deviation. @@ -267,11 +274,11 @@ student m s k = do normal m sd {-# INLINABLE student #-} --- | An isotropic or spherical Gaussian distribution. -isoGauss +-- | An isotropic or spherical Gaussian distribution with specified mean vector and scalar standard deviation parameter. +isoNormal :: (Traversable f, PrimMonad m) => f Double -> Double -> Prob m (f Double) -isoGauss ms sd = traverse (`normal` sd) ms -{-# INLINABLE isoGauss #-} +isoNormal ms sd = traverse (`normal` sd) ms +{-# INLINABLE isoNormal #-} -- | The Poisson distribution. poisson :: PrimMonad m => Double -> Prob m Int @@ -288,3 +295,19 @@ categorical ps = do _ -> error "categorical: invalid return value" {-# INLINABLE categorical #-} + +-- | The Zipf distribution, generated with the rejection sampling algorithm X.6.1 shown in L.Devroye, Non-Uniform Random Variate Generation. +zipf :: (Floating b, PrimMonad m, Variate b, Integral a, RealFrac b) => a -> Prob m b +zipf a' = do + let + a = fromIntegral a' + b = 2**(a - 1) + go = do + u <- uniform + v <- uniform + let x = fromIntegral (floor (u**(-1/(a-1))) :: Int) + t = (1 + 1/x)**(a-1) + if v*x*(t-1)/(b-1) <= t/b + then return x else go + go +{-# INLINABLE zipf #-}