commit 7a6e7cfd0a55d9b909b74a6ff3f398f536a8d044
parent 06aed8bb02c63a28e754399d8b4eef9352677840
Author: Marco Zocca <ocramz@users.noreply.github.com>
Date: Thu, 10 May 2018 08:16:21 +0200
Merge pull request #12 from ocramz/master
add inverse Gaussian
Diffstat:
7 files changed, 75 insertions(+), 7 deletions(-)
diff --git a/.travis.yml b/.travis.yml
@@ -8,6 +8,7 @@ addons:
env:
STACK_YAML: stack-travis.yaml
+ STACK_YAML: stack-travis-nightly-2018-05-09.yaml
before_install:
- mkdir -p ~/.local/bin
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,5 +1,8 @@
# Changelog
+ - 2.0.3 (2018-05-09)
+ * Add inverse Gaussian (Wald) distribution
+
- 2.0.2 (2018-01-30)
* Add negative binomial distribution
diff --git a/README.md b/README.md
@@ -13,21 +13,22 @@ This implementation is a thin layer over `mwc-random`, which handles RNG
state-passing automatically by using a `PrimMonad` like `IO` or `ST s` under
the hood.
+
Examples
--------
-Transform a distribution's support while leaving its density structure
+1. Transform a distribution's support while leaving its density structure
invariant:
- -- uniform over [0, 1] to uniform over [1, 2]
+ -- uniform over [0, 1] transformed to uniform over [1, 2]
succ <$> uniform
-Sequence distributions together using bind:
+2. Sequence distributions together using bind:
-- a beta-binomial composite distribution
beta 1 10 >>= binomial 10
-Use do-notation to build complex joint distributions from composable,
+3. Use do-notation to build complex joint distributions from composable,
local conditionals:
hierarchicalModel = do
@@ -38,3 +39,36 @@ local conditionals:
n <- uniformR (5, 10)
binomial n p
+
+
+Included probability distributions
+-------------
+
+## Continuous
+
+* Uniform
+* Normal
+* Log-Normal
+* Exponential
+* Inverse Gaussian
+* Laplace
+* Gamma
+* Inverse Gamma
+* Weibull
+* Chi-squared
+* Beta
+* Student t
+* Pareto
+* Dirichlet process
+* Symmetric Dirichlet process
+
+## Discrete
+
+* Discrete uniform
+* Zipf-Mandelbrot
+* Categorical
+* Bernoulli
+* Binomial
+* Negative Binomial
+* Multinomial
+* Poisson
+\ No newline at end of file
diff --git a/mwc-probability.cabal b/mwc-probability.cabal
@@ -1,5 +1,5 @@
name: mwc-probability
-version: 2.0.2
+version: 2.0.3
homepage: http://github.com/jtobin/mwc-probability
license: MIT
license-file: LICENSE
@@ -8,7 +8,7 @@ maintainer: jared@jtobin.ca, zocca.marco gmail
category: Math
build-type: Simple
cabal-version: >= 1.10
-tested-with: GHC == 8.0.2, GHC == 8.2.2
+tested-with: GHC == 8.0.2, GHC == 8.2.2 , GHC == 8.4.2
synopsis: Sampling function-based probability distributions.
description:
diff --git a/src/System/Random/MWC/Probability.hs b/src/System/Random/MWC/Probability.hs
@@ -73,6 +73,7 @@ module System.Random.MWC.Probability (
, isoNormal
, logNormal
, exponential
+ , inverseGaussian
, laplace
, gamma
, inverseGamma
@@ -334,6 +335,23 @@ isoNormal
isoNormal ms sd = traverse (`normal` sd) ms
{-# INLINABLE isoNormal #-}
+-- | The inverse Gaussian (also known as Wald) distribution.
+--
+-- Both the mean parameter 'mu' and the shape parameter 'lambda' must be positive.
+inverseGaussian :: PrimMonad m => Double -> Double -> Prob m Double
+inverseGaussian lambda mu = do
+ nu <- standardNormal
+ let y = nu ** 2
+ s = sqrt (4 * mu * lambda * y + mu ** 2 * y ** 2)
+ x = mu * (1 + 1 / (2 * lambda) * (mu * y - s))
+ thresh = mu / (mu + x)
+ z <- uniform
+ if z <= thresh
+ then return x
+ else return (mu ** 2 / x)
+{-# INLINABLE inverseGaussian #-}
+
+
-- | The Poisson distribution.
poisson :: PrimMonad m => Double -> Prob m Int
poisson l = Prob $ genFromTable table where
diff --git a/stack-travis-nightly-2018-05-09.yaml b/stack-travis-nightly-2018-05-09.yaml
@@ -0,0 +1,8 @@
+flags: {}
+packages:
+- '.'
+extra-deps: []
+resolver: nightly-2018-05-09
+compiler: ghc-8.4.2
+system-ghc: false
+install-ghc: true
+\ No newline at end of file
diff --git a/stack.yaml b/stack.yaml
@@ -2,5 +2,7 @@ flags: {}
packages:
- '.'
extra-deps: []
-resolver: lts-8.13
+# resolver: lts-8.13
+resolver: lts-11.8
+# resolver: nightly-2018-05-09