Copyright | (c) 2015-2017 Jared Tobin Marco Zocca |
---|---|

License | MIT |

Maintainer | Jared Tobin <jared@jtobin.ca>, Marco Zocca <zocca.marco gmail> |

Stability | unstable |

Portability | ghc |

Safe Haskell | None |

Language | Haskell2010 |

A probability monad based on sampling functions.

Probability distributions are abstract constructs that can be represented in a variety of ways. The sampling function representation is particularly useful - it's computationally efficient, and collections of samples are amenable to much practical work.

Probability monads propagate uncertainty under the hood. An expression like

corresponds to a
beta-binomial
distribution in which the uncertainty captured by `beta`

1 8 >>= `binomial`

10

has been
marginalized out.`beta`

1 8

The distribution resulting from a series of effects is called the
*predictive distribution* of the model described by the corresponding
expression. The monadic structure lets one piece together a hierarchical
structure from simpler, local conditionals:

hierarchicalModel = do [c, d, e, f] <- replicateM 4 $ uniformR (1, 10) a <- gamma c d b <- gamma e f p <- beta a b n <- uniformR (5, 10) binomial n p

The functor instance for a probability monad transforms the support of the
distribution while leaving its density structure invariant in some sense.
For example,

is a distribution over the 0-1 interval, but `uniform`

```
fmap
(+ 1) uniform
```

is the translated distribution over the 1-2 interval.

`>>>`

1.5480073474340754`create >>= sample (fmap (+ 1) uniform)`

## Running the examples

In the following we will assume an interactive GHCi session; the user should first declare a random number generator:

`>>>`

`gen <- create`

which will be reused throughout all examples.
Note: creating a random generator is an expensive operation, so it should be only performed once in the code (usually in the top-level IO action, e.g `main`

).

## References

1) L.Devroye, Non-Uniform Random Variate Generation, Springer-Verlag, New York, 1986. (Made freely available by the author: http://www.nrbook.com/devroye )

- newtype Prob m a = Prob {}
- samples :: PrimMonad m => Int -> Prob m a -> Gen (PrimState m) -> m [a]
- uniform :: (PrimMonad m, Variate a) => Prob m a
- uniformR :: (PrimMonad m, Variate a) => (a, a) -> Prob m a
- normal :: PrimMonad m => Double -> Double -> Prob m Double
- standardNormal :: PrimMonad m => Prob m Double
- isoNormal :: (Traversable f, PrimMonad m) => f Double -> Double -> Prob m (f Double)
- logNormal :: PrimMonad m => Double -> Double -> Prob m Double
- exponential :: PrimMonad m => Double -> Prob m Double
- laplace :: (Floating a, Variate a, PrimMonad m) => a -> a -> Prob m a
- gamma :: PrimMonad m => Double -> Double -> Prob m Double
- inverseGamma :: PrimMonad m => Double -> Double -> Prob m Double
- normalGamma :: PrimMonad m => Double -> Double -> Double -> Double -> Prob m Double
- weibull :: (Floating a, Variate a, PrimMonad m) => a -> a -> Prob m a
- chiSquare :: PrimMonad m => Int -> Prob m Double
- beta :: PrimMonad m => Double -> Double -> Prob m Double
- student :: PrimMonad m => Double -> Double -> Double -> Prob m Double
- pareto :: PrimMonad m => Double -> Double -> Prob m Double
- dirichlet :: (Traversable f, PrimMonad m) => f Double -> Prob m (f Double)
- symmetricDirichlet :: PrimMonad m => Int -> Double -> Prob m [Double]
- discreteUniform :: (PrimMonad m, Foldable f) => f a -> Prob m a
- zipf :: (PrimMonad m, Integral b) => Double -> Prob m b
- categorical :: (Foldable f, PrimMonad m) => f Double -> Prob m Int
- bernoulli :: PrimMonad m => Double -> Prob m Bool
- binomial :: PrimMonad m => Int -> Double -> Prob m Int
- negativeBinomial :: (PrimMonad m, Integral a) => a -> Double -> Prob m Int
- multinomial :: (Foldable f, PrimMonad m) => Int -> f Double -> Prob m [Int]
- poisson :: PrimMonad m => Double -> Prob m Int

# Documentation

A probability distribution characterized by a sampling function.

`>>>`

0.4208881170464097`sample uniform gen`

samples :: PrimMonad m => Int -> Prob m a -> Gen (PrimState m) -> m [a] Source #

Sample from a model `n`

times.

`>>>`

[0.6738707766845254,0.9730405951541817]`samples 2 uniform gen`

# Distributions

## Continuous-valued

uniform :: (PrimMonad m, Variate a) => Prob m a Source #

The uniform distribution over a type.

`>>>`

0.29308497534914946`sample uniform gen :: IO Double`

`>>>`

False`sample uniform gen :: IO Bool`

uniformR :: (PrimMonad m, Variate a) => (a, a) -> Prob m a Source #

The uniform distribution over the provided interval.

`>>>`

0.44984153252922365`sample (uniformR (0, 1)) gen`

normal :: PrimMonad m => Double -> Double -> Prob m Double Source #

The normal or Gaussian distribution with a specified mean and standard deviation.

standardNormal :: PrimMonad m => Prob m Double Source #

The standard normal or Gaussian distribution (with mean 0 and standard deviation 1).

isoNormal :: (Traversable f, PrimMonad m) => f Double -> Double -> Prob m (f Double) Source #

An isotropic or spherical Gaussian distribution with specified mean vector and scalar standard deviation parameter.

logNormal :: PrimMonad m => Double -> Double -> Prob m Double Source #

The log-normal distribution with specified mean and standard deviation.

exponential :: PrimMonad m => Double -> Prob m Double Source #

The exponential distribution with provided rate parameter.

laplace :: (Floating a, Variate a, PrimMonad m) => a -> a -> Prob m a Source #

The Laplace distribution with provided location and scale parameters.

gamma :: PrimMonad m => Double -> Double -> Prob m Double Source #

The gamma distribution with shape parameter a and scale parameter b.

This is the parameterization used more traditionally in frequentist statistics. It has the following corresponding probability density function:

f(x; a, b) = 1 * (Gamma(a) * b ^ a) x ^ (a - 1) e ^ (- x * b)

inverseGamma :: PrimMonad m => Double -> Double -> Prob m Double Source #

The inverse-gamma distribution.

normalGamma :: PrimMonad m => Double -> Double -> Double -> Double -> Prob m Double Source #

The Normal-Gamma distribution of parameters mu, lambda, a, b

weibull :: (Floating a, Variate a, PrimMonad m) => a -> a -> Prob m a Source #

The Weibull distribution with provided shape and scale parameters.

student :: PrimMonad m => Double -> Double -> Double -> Prob m Double Source #

Student's t distribution.

pareto :: PrimMonad m => Double -> Double -> Prob m Double Source #

The Pareto distribution with specified index `a`

and minimum `xmin`

parameters.

Both `a`

and `xmin`

must be positive.

### Dirichlet process

dirichlet :: (Traversable f, PrimMonad m) => f Double -> Prob m (f Double) Source #

The Dirichlet distribution.

symmetricDirichlet :: PrimMonad m => Int -> Double -> Prob m [Double] Source #

The symmetric Dirichlet distribution of dimension n.

## Discrete-valued

discreteUniform :: (PrimMonad m, Foldable f) => f a -> Prob m a Source #

The discrete uniform distribution.

`>>>`

6`sample (discreteUniform [0..10]) gen`

`>>>`

'a'`sample (discreteUniform "abcdefghijklmnopqrstuvwxyz") gen`

zipf :: (PrimMonad m, Integral b) => Double -> Prob m b Source #

The Zipf-Mandelbrot distribution, generated with the rejection sampling algorithm X.6.1 shown in [1].

The parameter should be positive, but values close to 1 should be avoided as they are very computationally intensive. The following code illustrates this behaviour.

`>>>`

[11315371987423520,2746946,653,609,2,13,85,4,256184577853,50]`samples 10 (zipf 1.1) gen`

`>>>`

[19,3,3,1,1,2,1,191,2,1]`samples 10 (zipf 1.5) gen`

categorical :: (Foldable f, PrimMonad m) => f Double -> Prob m Int Source #

A categorical distribution defined by the supplied list of probabilities.

negativeBinomial :: (PrimMonad m, Integral a) => a -> Double -> Prob m Int Source #

The negative binomial distribution with `n`

trials each with "success" probability `p`

.
Example X.1.5 in [1].

Note: `n`

must be larger than 1 and `p`

included between 0 and 1.