Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell98 |
Deterministic and probabilistic values
- type Event a = a -> Bool
- oneOf :: Eq a => [a] -> Event a
- just :: Eq a => a -> Event a
- newtype T prob a = Cons {
- decons :: [(a, prob)]
- certainly :: Num prob => a -> T prob a
- errorMargin :: RealFloat prob => prob
- approx :: (RealFloat prob, Ord a) => T prob a -> T prob a -> Bool
- 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
- sumP :: Num prob => [(a, prob)] -> prob
- sortP :: Ord prob => [(a, prob)] -> [(a, prob)]
- sortPDesc :: Ord prob => [(a, prob)] -> [(a, prob)]
- sortElem :: Ord a => [(a, prob)] -> [(a, prob)]
- 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
- (//%) :: (Ord a, Show a) => T Rational a -> () -> IO ()
- equal :: (Num prob, Eq prob, Ord a) => T prob a -> T prob a -> Bool
- type Spread prob a = [a] -> T prob a
- choose :: Num prob => prob -> a -> a -> T prob a
- enum :: Fractional prob => [Int] -> Spread prob a
- relative :: Fractional prob => [prob] -> Spread prob a
- 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]
- map :: (Num prob, Ord b) => (a -> b) -> T prob a -> T prob b
- unfold :: (Num prob, Ord a) => T prob (T prob a) -> T prob a
- cond :: Num prob => T prob Bool -> T prob a -> T prob a -> T prob a
- truth :: Num prob => T prob Bool -> prob
- (?=<<) :: Fractional prob => (a -> Bool) -> T prob a -> T prob a
- (>>=?) :: Fractional prob => T prob a -> (a -> Bool) -> T prob a
- data Select a
- above :: (Num prob, Ord prob, Ord a) => prob -> T prob a -> T prob (Select a)
- below :: (Num prob, Ord prob, Ord a) => prob -> T prob a -> T prob (Select a)
- select :: (Num prob, Ord prob, Ord a) => (prob -> Bool) -> 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
- scanP :: (Num prob, Ord prob) => prob -> [(a, prob)] -> a
- (??) :: Num prob => Event a -> T prob a -> prob
- expected :: Num a => T a a -> a
- variance :: Num a => T a a -> a
- stdDev :: Floating a => T a a -> a
Events
Distributions
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 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.
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 x==y = norm (liftM2 (==) x y) . However the T prob a -> T prob a -> Bool . We could implement this as check for equal distributions.
This would be inconsistent with the I would prefer to omit the |
(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) |
errorMargin :: RealFloat prob => prob Source
approx :: (RealFloat prob, Ord a) => T prob a -> T prob a -> Bool Source
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
cons :: (RealFloat prob, Show prob) => [(a, prob)] -> T prob a Source
can fail because of rounding errors, better use fromFreqs
Normalization = grouping
pretty :: (Ord a, Show a, Num prob, Ord prob) => (prob -> String) -> T prob a -> String Source
pretty printing
Spread: functions to convert a list of values into a distribution
enum :: Fractional prob => [Int] -> Spread prob a Source
relative :: Fractional prob => [prob] -> Spread prob a Source
Give a list of frequencies, they do not need to sum up to 1.
shape :: Fractional prob => (prob -> prob) -> Spread prob a Source
linear :: Fractional prob => Spread prob a Source
uniform :: Fractional prob => Spread prob a Source
unfold :: (Num prob, Ord a) => T prob (T prob a) -> T prob a Source
unfold a distribution of distributions into one distribution,
this is join
with normalization.
conditional distribution
(?=<<) :: Fractional prob => (a -> Bool) -> T prob a -> T prob a infixr 1 Source
conditional probability, identical to filter
filtering distributions
fromFreqs :: Fractional prob => [(a, prob)] -> T prob a Source