| |||||||||||||||||||||||||||||

| |||||||||||||||||||||||||||||

| |||||||||||||||||||||||||||||

Description | |||||||||||||||||||||||||||||

Common interface for probability distribution monads. Heavily inspired by
Martin Erwig's and Steve Kollmansberger's For background, see Michele Giry, | |||||||||||||||||||||||||||||

Synopsis | |||||||||||||||||||||||||||||

| |||||||||||||||||||||||||||||

Common interface | |||||||||||||||||||||||||||||

Common interfaces to probability monads. For example, if we assume that a family has two children, each a boy or a girl, we can build a probability distribution representing all such families. import Control.Monad.Distribution data Child = Girl | Boy deriving (Show, Eq, Ord) child = uniform [Girl, Boy] family = do child1 <- child child2 <- child return [child1, child2] The use of child :: (Dist d) => d Child child = uniform [Girl, Boy] family :: (Dist d) => d [Child] family = ... Unfortunately, using | |||||||||||||||||||||||||||||

class (Functor d, Monad d) => Dist d where | |||||||||||||||||||||||||||||

| |||||||||||||||||||||||||||||

weighted :: Dist d => [(a, Rational)] -> d a | |||||||||||||||||||||||||||||

Creates a new distribution from a weighted list of values. The individual weights must be non-negative, and they must sum to a positive number. | |||||||||||||||||||||||||||||

uniform :: Dist d => [a] -> d a | |||||||||||||||||||||||||||||

Creates a new distribution from a list of values, weighting it evenly. | |||||||||||||||||||||||||||||

Bayes' rule | |||||||||||||||||||||||||||||

Using import Control.Monad import Control.Monad.Distribution.Rational import Data.List data Coin = Heads | Tails deriving (Eq, Ord, Show) toss = uniform [Heads, Tails] tosses n = sequence (replicate n toss) tossesWithAtLeastOneHead n = do result <- tosses n guard (Heads `elem` result) return result In this example, we use | |||||||||||||||||||||||||||||

MonadPlus (mzero, mplus) | |||||||||||||||||||||||||||||

mzero | |||||||||||||||||||||||||||||

mplus | |||||||||||||||||||||||||||||

guard | |||||||||||||||||||||||||||||

Random sampling functions | |||||||||||||||||||||||||||||

Support for probability distributions represented by sampling functions. This API is heavily inspired by Sungwoo Park and colleagues' $lambda_{bigcirc}$ caculus http://citeseer.ist.psu.edu/752237.html. Two sampling-function monads are available: It's possible run code in the sampleIO family 3 -- [[Boy,Girl],[Boy,Girl],[Girl,Girl]] If the probability distribution uses sampleBayesIO (tossesWithAtLeastOneHead 2) 4 -- [[Tails,Heads],[Heads,Heads],[Tails,Heads]] | |||||||||||||||||||||||||||||

module Control.Monad.Random | |||||||||||||||||||||||||||||

sample :: MonadRandom m => m a -> Int -> m [a] | |||||||||||||||||||||||||||||

Take n samples from the distribution r.
| |||||||||||||||||||||||||||||

sampleIO :: Rand StdGen a -> Int -> IO [a] | |||||||||||||||||||||||||||||

Take n samples from the distribution r using the IO monad.
| |||||||||||||||||||||||||||||

type BRand g = MaybeT (Rand g) | |||||||||||||||||||||||||||||

A random distribution where some samples may be discarded. | |||||||||||||||||||||||||||||

sampleBayes :: MonadRandom m => MaybeT m a -> Int -> m [a] | |||||||||||||||||||||||||||||

Take n samples from the distribution r, and eliminate any samples
which fail a guard condition.
| |||||||||||||||||||||||||||||

sampleBayesIO :: BRand StdGen a -> Int -> IO [a] | |||||||||||||||||||||||||||||

Take n samples from the distribution r using the IO monad, and
eliminate any samples which fail a guard condition.
| |||||||||||||||||||||||||||||

Discrete, finite distributions | |||||||||||||||||||||||||||||

Using the ddist family -- [MV 0.25 [Girl,Girl], -- MV 0.25 [Girl,Boy], -- MV 0.25 [Boy,Girl], -- MV 0.25 [Boy,Boy]] If the probability distribution uses bddist (tossesWithAtLeastOneHead 2) -- Just [MV 1%3 [Heads,Heads], -- MV 1%3 [Heads,Tails], -- MV 1%3 [Tails,Heads]] Note that we see rational numbers in this second example, because we used
| |||||||||||||||||||||||||||||

bayes :: Probability p => MaybeT (MVT p []) a -> Maybe (MVT p [] a) | |||||||||||||||||||||||||||||

Apply Bayes' rule, discarding impossible outcomes and normalizing the probabilities that remain. TODO: It's entirely possible that this method should be moved to a type class. | |||||||||||||||||||||||||||||

Produced by Haddock version 0.8 |