monte-carlo-0.2: A monad and transformer for Monte Carlo calculations.

Stabilityexperimental
MaintainerPatrick Perry <patperry@stanford.edu>

Control.Monad.MC.Class

Contents

Description

 

Synopsis

The Monte Carlo monad type class

class HasRNG m Source

Associated Types

type RNG m Source

The random number generator type for the monad.

Instances

HasRNG MC 
Monad m => HasRNG (MCT m) 

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

Methods

getRNG :: m (RNG m)Source

Get the current random number generator.

setRNG :: RNG m -> m ()Source

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.

Instances

MonadMC MC 
Monad m => MonadMC (MCT m) 

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 Ints

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 Doubles.

repeatMCWithSource

Arguments

:: 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

data Summary Source

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

summary :: SummarySource

Get an empty summary.

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.

sampleVar :: Summary -> DoubleSource

Get the sample variance.

sampleSD :: Summary -> DoubleSource

Get the sample standard deviation.

sampleSE :: Summary -> DoubleSource

Get the sample standard error.

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).

sampleMin :: Summary -> DoubleSource

Get the minimum of the sample.

sampleMax :: Summary -> DoubleSource

Get the maximum of the sample.

Interleaving computations