probability-0.2.3.1: Probabilistic Functional Programming

Numeric.Probability.Distribution

Contents

Description

Deterministic and probabilistic values

Synopsis

Events

type Event a = a -> Bool

oneOf :: Eq a => [a] -> Event a

just :: Eq a => a -> Event a

Distributions

newtype T prob a

Probability disribution

The underlying data structure is a list. Unfortunately we cannot use a more efficient data structure because the key type must be of class Ord, but the Monad class does not allow constraints for result types. The Monad instance is particularly useful because many generic monad functions make sense here, monad transformers can be used and the monadic design allows to simulate probabilistic games in an elegant manner.

We have the same problem like making Data.Set an instance of Monad, see http://www.randomhacks.net/articles/2007/03/15/data-set-monad-haskell-macros

If you need efficiency, you should remove redundant elements by norm. norm converts to Data.Map and back internally and you can hope that the compiler fuses the lists with the intermediate Map structure.

The defined monad is equivalent to WriterT (Product prob) [] a. See http://www.randomhacks.net/articles/2007/02/21/refactoring-probability-distributions.

Constructors

Cons 

Fields

decons :: [(a, prob)]
 

Instances

Fractional prob => C prob (T prob) 
Num prob => Monad (T prob) 
Functor (T prob) 
Num prob => Applicative (T prob) 
(Num prob, Ord prob, Random prob) => C (T prob) 
Eq (T prob a)

We would like to have an equality test of type

 (==) :: T prob a -> T prob a -> T prob Bool

that is consistent with the Num instance. We would certainly define

 x==y = norm (liftM2 (==) x y)   .

However the Eq class enforces the type

 T prob a -> T prob a -> Bool    .

We could implement this as check for equal distributions. This would be inconsistent with the Num instance because it compares entire distributions, not only individual outcomes. Thus we provide this function as equal.

I would prefer to omit the Eq instance completely, but unfortunately the Num instance requires Eq as superclass.

(Num prob, Ord prob, Ord a, Fractional a) => Fractional (T prob a) 
(Num prob, Ord prob, Ord a, Num a) => Num (T prob a) 
(Num prob, Ord prob, Show prob, Ord a, Show a) => Show (T prob a) 
(ToFloat prob, Expected a) => Expected (T prob a) 

certainly :: Num prob => a -> T prob a

errorMargin :: RealFloat prob => prob

approx :: (RealFloat prob, Ord a) => T prob a -> T prob a -> Bool

Check whether two distributions are equal when neglecting rounding errors. We do not want to put this into an Eq instance, since it is not exact equivalence and it seems to be too easy to mix it up with liftM2 (==) x y.

Auxiliary functions for constructing and working with distributions

lift :: Num prob => ([(a, prob)] -> [(a, prob)]) -> T prob a -> T prob a

size :: T prob a -> Int

check :: (RealFloat prob, Show prob) => T prob a -> T prob a

cons :: (RealFloat prob, Show prob) => [(a, prob)] -> T prob a

can fail because of rounding errors, better use fromFreqs

sumP :: Num prob => [(a, prob)] -> prob

sortP :: Ord prob => [(a, prob)] -> [(a, prob)]

sortElem :: Ord a => [(a, prob)] -> [(a, prob)]

Normalization = grouping

norm :: (Num prob, Ord a) => T prob a -> T prob a

norm' :: (Num prob, Ord a) => [(a, prob)] -> [(a, prob)]

norm'' :: (Num prob, Ord a) => [(a, prob)] -> [(a, prob)]

pretty :: (Ord a, Show a, Num prob, Ord prob) => (prob -> String) -> T prob a -> String

pretty printing

(//%) :: (Ord a, Show a) => T Rational a -> () -> IO ()

equal :: (Num prob, Eq prob, Ord a) => T prob a -> T prob a -> Bool

Spread: functions to convert a list of values into a distribution

type Spread prob a = [a] -> T prob a

distribution generators

choose :: Num prob => prob -> a -> a -> T prob a

enum :: Fractional prob => [Int] -> Spread prob a

relative :: Fractional prob => [prob] -> Spread prob a

Give a list of frequencies, they do not need to sum up to 1.

shape :: Fractional prob => (prob -> prob) -> Spread prob a

linear :: Fractional prob => Spread prob a

uniform :: Fractional prob => Spread prob a

negExp :: Floating prob => Spread prob a

normal :: Floating prob => Spread prob a

extract :: T prob a -> [a]

extracting and mapping the domain of a distribution

map :: (Num prob, Ord b) => (a -> b) -> T prob a -> T prob b

fmap with normalization

unfold :: (Num prob, Ord a) => T prob (T prob a) -> T prob a

unfold a distribution of distributions into one distribution, this is join with normalization.

cond

Arguments

:: Num prob 
=> T prob Bool 
-> T prob a

True

-> T prob a

False

-> T prob a 

conditional distribution

truth :: Num prob => T prob Bool -> prob

(?=<<) :: Fractional prob => (a -> Bool) -> T prob a -> T prob a

conditional probability, identical to Dist.filter

(>>=?) :: Fractional prob => T prob a -> (a -> Bool) -> T prob a

Dist.filter in infix form. Can be considered an additional monadic combinator, which can be used where you would want Control.Monad.guard otherwise.

data Select a

filtering distributions

Constructors

Case a 
Other 

Instances

Eq a => Eq (Select a) 
Ord a => Ord (Select a) 
Show a => Show (Select a) 

above :: (Num prob, Ord prob, Ord a) => prob -> T prob a -> T prob (Select a)

fromFreqs :: Fractional prob => [(a, prob)] -> T prob a

filter :: Fractional prob => (a -> Bool) -> T prob a -> T prob a

mapMaybe :: Fractional prob => (a -> Maybe b) -> T prob a -> T prob b

selectP :: (Num prob, Ord prob) => T prob a -> prob -> a

selecting from distributions

scanP :: (Num prob, Ord prob) => prob -> [(a, prob)] -> a

(??) :: Num prob => Event a -> T prob a -> prob

expected :: Num a => T a a -> a

expectation value

variance :: Num a => T a a -> a

statistical analyses

stdDev :: Floating a => T a a -> a