Stability | experimental |
---|---|

Maintainer | Patrick Perry <patperry@stanford.edu> |

- class HasRNG m where
- type RNG m

- class (Monad m, HasRNG m) => MonadMC m where
- sample :: MonadMC m => Int -> [a] -> m a
- sampleWithWeights :: MonadMC m => [Double] -> Int -> [a] -> m a
- sampleSubset :: MonadMC m => Int -> Int -> [a] -> m [a]
- sampleInt :: MonadMC m => Int -> m Int
- sampleIntWithWeights :: MonadMC m => [Double] -> Int -> m Int
- sampleIntSubset :: MonadMC m => Int -> Int -> m [Int]
- shuffle :: MonadMC m => Int -> [a] -> m [a]
- shuffleInt :: MonadMC m => Int -> m [(Int, Int)]
- repeatMC :: MonadMC m => Int -> m Double -> m Summary
- repeatMCWith :: MonadMC m => (a -> b -> a) -> a -> Int -> m b -> m a
- data Summary
- summary :: Summary
- update :: Summary -> Double -> Summary
- sampleSize :: Summary -> Int
- sampleMean :: Summary -> Double
- sampleVar :: Summary -> Double
- sampleSD :: Summary -> Double
- sampleSE :: Summary -> Double
- sampleCI :: Double -> Summary -> (Double, Double)
- sampleMin :: Summary -> Double
- sampleMax :: Summary -> Double

# The Monte Carlo monad type class

class (Monad m, HasRNG m) => MonadMC m whereSource

Get the current random number generator.

Set the current random number generator.

uniform :: Double -> Double -> m DoubleSource

`uniform a b`

generates a value uniformly distributed in `[a,b)`

.

uniformInt :: Int -> m IntSource

`uniformInt n`

generates an integer uniformly in the range `[0,n-1]`

.
It is an error to call this function with a non-positive value.

normal :: Double -> Double -> m DoubleSource

`normal mu sigma`

generates a Normal random variable with mean
`mu`

and standard deviation `sigma`

.

poisson :: Double -> m IntSource

`poisson mu`

generates a Poisson random variable with mean `mu`

.

unsafeInterleaveMC :: m a -> m aSource

Get the baton from the Monte Carlo monad without performing any computations. Useful but dangerous.

# Getting and setting the generator

# Random distributions

# Sampling from lists

sample :: MonadMC m => Int -> [a] -> m aSource

`sample n xs`

samples a value uniformly from `take n xs`

. The results
are undefined if `length xs`

is less than `n`

.

sampleWithWeights :: MonadMC m => [Double] -> Int -> [a] -> m aSource

`sampleWithWeights ws n xs`

samples a value from `take n xs`

, putting
weight `ws !! i`

on element `xs !! i`

. The results
are undefined if `length xs`

or `length ws`

is less than `n`

.

sampleSubset :: MonadMC m => Int -> Int -> [a] -> m [a]Source

`sampleSubset k n xs`

samples a subset of size `k`

from `take n xs`

by
sampling without replacement. The return value is a list of length `k`

with the elements in the subset in the order that they were sampled. Note
also that the elements are lazily generated. The results are undefined
if `k > n`

or if `length xs < n`

.

# Sampling `Int`

s

sampleInt :: MonadMC m => Int -> m IntSource

`sampleInt n`

samples integers uniformly from `[ 0..n-1 ]`

. It is an
error to call this function with a non-positive `n`

.

sampleIntWithWeights :: MonadMC m => [Double] -> Int -> m IntSource

`sampleIntWithWeights ws n`

samples integers from `[ 0..n-1 ]`

with the
probability of choosing `i`

proportional to `ws !! i`

. The list `ws`

must
have length equal to `n`

. Also, the elements of `ws`

must be non-negative
with at least one nonzero entry.

sampleIntSubset :: MonadMC m => Int -> Int -> m [Int]Source

`sampleIntSubset k n`

samples a subset of size `k`

by sampling without
replacement from the integers `{ 0, ..., n-1 }`

. The return value is a
list of length `k`

with the elements in the subset in the order that they
were sampled. Note also that the elements are lazily generated.

# Shuffling

shuffle :: MonadMC m => Int -> [a] -> m [a]Source

`shuffle n xs`

randomly permutes the list `take n xs`

and returns
the result. All permutations of the elements of `xs`

are equally
likely. The results are undefined if `length xs`

is less than `n`

.

shuffleInt :: MonadMC m => Int -> m [(Int, Int)]Source

`shuffleInt n`

generates a sequence of swaps equivalent to a
uniformly-chosen random permutatation of the integers `{0, ..., n-1}`

.
For an input of `n`

, there are `n-1`

swaps, which are lazily generated.

# Averaging functions

repeatMC :: MonadMC m => Int -> m Double -> m SummarySource

Repeat a Monte Carlo generator the given number of times and return
the sample summary statistics. Note that this only works with
`Double`

s.

:: MonadMC m | |

=> (a -> b -> a) | accumulator |

-> a | initial value |

-> Int | number of repetitions |

-> m b | generator |

-> m a |

Generalized version of `repeatMC`

. Run a Monte Carlo generator
the given number of times and accumulate the results. The accumulator
is strictly evaluated.

# Summary statistics

## The `Summary`

data type

A type for storing summary statistics for a data set including sample size, min and max values, and first and second moments.

update :: Summary -> Double -> SummarySource

Update the summary with a data point. Running mean and variance computed as in Knuth, Vol 2, page 232, 3rd edition, see http:www.johndcook.com/standard_deviation.html for a description.

`Summary`

properties

sampleSize :: Summary -> IntSource

Get the sample size.

sampleMean :: Summary -> DoubleSource

Get the sample mean.

sampleCI :: Double -> Summary -> (Double, Double)Source

Get a Central Limit Theorem-based confidence interval for the mean
with the specified coverage level. The level must be in the range `(0,1)`

.