probability-0.1: Computations with discrete random variablesSource codeContentsIndex
Probability
Contents
Auxiliary definitions
Events
Probabilities
Monad composition
Deterministic and probabilistic values
Distributions
Auxiliary functions for constructing and working with distributions
Normalization = grouping
Operations on distributions
Spread: functions to convert a list of values into a distribution
Randomized values
R random value
RDist random distribution
Deterministic and probabilistic generators
Transitions
Spreading changes into transitions
Spreading transitions into transitions
Randomized generators
Randomized changes
Randomized transitions
Iteration and simulation
Tracing
Synopsis
type Event a = a -> Bool
oneOf :: Eq a => [a] -> Event a
just :: Eq a => a -> Event a
newtype Probability = P ProbRep
type ProbRep = Float
precision :: Int
showPfix :: ProbRep -> String
roundRel :: RealFrac a => Int -> a -> a
showP :: ProbRep -> String
errorMargin :: ProbRep
(>@>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
sequ :: Monad m => [a -> m a] -> a -> m a
newtype Dist a = D {
unD :: [(a, ProbRep)]
}
isZero :: Dist a -> Bool
onD :: ([(a, ProbRep)] -> [(a, ProbRep)]) -> Dist a -> Dist a
sizeD :: Dist a -> Int
checkD :: Dist a -> Dist a
mkD :: [(a, ProbRep)] -> Dist a
sumP :: [(a, ProbRep)] -> ProbRep
sortP :: [(a, ProbRep)] -> [(a, ProbRep)]
normBy :: Ord a => (a -> a -> Bool) -> Dist a -> Dist a
accumBy :: Num b => (a -> a -> Bool) -> [(a, b)] -> [(a, b)]
norm :: Ord a => Dist a -> Dist a
norm' :: Ord a => [(a, ProbRep)] -> [(a, ProbRep)]
joinWith :: (a -> b -> c) -> Dist a -> Dist b -> Dist c
prod :: Dist a -> Dist b -> Dist (a, b)
type Spread a = [a] -> Dist a
certainly :: Trans a
impossible :: Dist a
choose :: ProbRep -> a -> a -> Dist a
enum :: [ProbRep] -> Spread a
enumPC :: [ProbRep] -> Spread a
relative :: [Int] -> Spread a
shape :: (Float -> Float) -> Spread a
linear :: Float -> Spread a
uniform :: Spread a
negexp :: Spread a
normal :: Spread a
normalCurve :: Float -> Float -> Float -> Float
extract :: Dist a -> [a]
mapD :: (a -> b) -> Dist a -> Dist b
unfoldD :: Dist (Dist a) -> Dist a
cond :: Dist Bool -> Dist a -> Dist a -> Dist a
truth :: Dist Bool -> Probability
(|||) :: Dist a -> Event a -> Dist a
data Select a
= Case a
| Other
above :: Ord a => ProbRep -> Dist a -> Dist (Select a)
scale :: [(a, ProbRep)] -> Dist a
filterD :: (a -> Bool) -> Dist a -> Dist a
selectP :: Dist a -> ProbRep -> a
scanP :: ProbRep -> [(a, ProbRep)] -> a
(??) :: Event a -> Dist a -> Probability
class ToFloat a where
toFloat :: a -> Float
class FromFloat a where
fromFloat :: Float -> a
class Expected a where
expected :: a -> Float
variance :: Expected a => Dist a -> Float
stddev :: Expected a => Dist a -> Float
type R a = IO a
printR :: Show a => R a -> R ()
pick :: Dist a -> R a
type RDist a = R (Dist a)
rAbove :: Ord a => ProbRep -> RDist a -> RDist (Select a)
type Change a = a -> a
type Trans a = a -> Dist a
idT :: Trans a
mapT :: Change a -> Trans a -> Trans a
unfoldT :: Dist (Trans a) -> Trans a
type SpreadC a = [Change a] -> Trans a
certainlyT :: Change a -> Trans a
maybeT :: ProbRep -> Change a -> Trans a
liftC :: Spread a -> [Change a] -> Trans a
uniformT :: [Change a] -> Trans a
normalT :: [Change a] -> Trans a
linearT :: Float -> [Change a] -> Trans a
enumT :: [ProbRep] -> [Change a] -> Trans a
type SpreadT a = [Trans a] -> Trans a
liftT :: Spread (Trans a) -> [Trans a] -> Trans a
uniformTT :: [Trans a] -> Trans a
normalTT :: [Trans a] -> Trans a
linearTT :: Float -> [Trans a] -> Trans a
enumTT :: [ProbRep] -> [Trans a] -> Trans a
type RChange a = a -> R a
random :: Trans a -> RChange a
type RTrans a = a -> RDist a
type ApproxDist a = R [a]
rDist :: Ord a => [R a] -> RDist a
class Iterate c where
(*.) :: Int -> (a -> c a) -> (a -> c a)
while :: (a -> Bool) -> (a -> c a) -> (a -> c a)
until :: (a -> Bool) -> (a -> c a) -> (a -> c a)
class Sim c where
(~.) :: Ord a => Int -> (a -> c a) -> RTrans a
(~..) :: Ord a => (Int, Int) -> (a -> c a) -> RExpand a
(~*.) :: Ord a => (Int, Int) -> (a -> c a) -> RTrans a
type Trace a = [a]
type Space a = Trace (Dist a)
type Walk a = a -> Trace a
type Expand a = a -> Space a
(>>:) :: Trans a -> Expand a -> Expand a
walk :: Int -> Change a -> Walk a
(*..) :: Int -> Trans a -> Expand a
type RTrace a = R (Trace a)
type RSpace a = R (Space a)
type RWalk a = a -> RTrace a
type RExpand a = a -> RSpace a
composelR :: RChange a -> RWalk a -> RWalk a
rWalk :: Int -> RChange a -> RWalk a
mergeTraces :: Ord a => [RTrace a] -> RSpace a
Auxiliary definitions
Events
type Event a = a -> BoolSource
oneOf :: Eq a => [a] -> Event aSource
just :: Eq a => a -> Event aSource
Probabilities
newtype Probability Source
Constructors
P ProbRep
show/hide Instances
type ProbRep = FloatSource
precision :: IntSource
showPfix :: ProbRep -> StringSource
roundRel :: RealFrac a => Int -> a -> aSource
showP :: ProbRep -> StringSource
errorMargin :: ProbRepSource
Monad composition
(>@>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m cSource
binary composition
sequ :: Monad m => [a -> m a] -> a -> m aSource
composition of a list of monadic functions
Deterministic and probabilistic values
Distributions
newtype Dist a Source
probability disribution
Constructors
D
unD :: [(a, ProbRep)]
show/hide Instances
isZero :: Dist a -> BoolSource
Auxiliary functions for constructing and working with distributions
onD :: ([(a, ProbRep)] -> [(a, ProbRep)]) -> Dist a -> Dist aSource
sizeD :: Dist a -> IntSource
checkD :: Dist a -> Dist aSource
mkD :: [(a, ProbRep)] -> Dist aSource
sumP :: [(a, ProbRep)] -> ProbRepSource
sortP :: [(a, ProbRep)] -> [(a, ProbRep)]Source
Normalization = grouping
normBy :: Ord a => (a -> a -> Bool) -> Dist a -> Dist aSource
accumBy :: Num b => (a -> a -> Bool) -> [(a, b)] -> [(a, b)]Source
norm :: Ord a => Dist a -> Dist aSource
norm' :: Ord a => [(a, ProbRep)] -> [(a, ProbRep)]Source
Operations on distributions
joinWith :: (a -> b -> c) -> Dist a -> Dist b -> Dist cSource
product of independent distributions, identical to Monad.liftM2
prod :: Dist a -> Dist b -> Dist (a, b)Source
Spread: functions to convert a list of values into a distribution
type Spread a = [a] -> Dist aSource
distribution generators
certainly :: Trans aSource
impossible :: Dist aSource
choose :: ProbRep -> a -> a -> Dist aSource
enum :: [ProbRep] -> Spread aSource
enumPC :: [ProbRep] -> Spread aSource
relative :: [Int] -> Spread aSource
shape :: (Float -> Float) -> Spread aSource
linear :: Float -> Spread aSource
uniform :: Spread aSource
negexp :: Spread aSource
normal :: Spread aSource
normalCurve :: Float -> Float -> Float -> FloatSource
extract :: Dist a -> [a]Source
extracting and mapping the domain of a distribution
mapD :: (a -> b) -> Dist a -> Dist bSource
unfoldD :: Dist (Dist a) -> Dist aSource
unfold a distribution of distributions into one distribution
cond :: Dist Bool -> Dist a -> Dist a -> Dist aSource
conditional distribution
truth :: Dist Bool -> ProbabilitySource
(|||) :: Dist a -> Event a -> Dist aSource
conditional probability
data Select a Source
filtering distributions
Constructors
Case a
Other
show/hide Instances
Eq a => Eq (Select a)
Ord a => Ord (Select a)
Show a => Show (Select a)
above :: Ord a => ProbRep -> Dist a -> Dist (Select a)Source
scale :: [(a, ProbRep)] -> Dist aSource
filterD :: (a -> Bool) -> Dist a -> Dist aSource
selectP :: Dist a -> ProbRep -> aSource
selecting from distributions
scanP :: ProbRep -> [(a, ProbRep)] -> aSource
(??) :: Event a -> Dist a -> ProbabilitySource
class ToFloat a whereSource
Methods
toFloat :: a -> FloatSource
show/hide Instances
class FromFloat a whereSource
Methods
fromFloat :: Float -> aSource
show/hide Instances
class Expected a whereSource
Methods
expected :: a -> FloatSource
show/hide Instances
variance :: Expected a => Dist a -> FloatSource
statistical analyses
stddev :: Expected a => Dist a -> FloatSource
Randomized values
R random value
type R a = IO aSource
Random values
printR :: Show a => R a -> R ()Source
pick :: Dist a -> R aSource
RDist random distribution
type RDist a = R (Dist a)Source
Randomized distributions
rAbove :: Ord a => ProbRep -> RDist a -> RDist (Select a)Source
Deterministic and probabilistic generators
Transitions
type Change a = a -> aSource
deterministic generator
type Trans a = a -> Dist aSource
probabilistic generator
idT :: Trans aSource
mapT :: Change a -> Trans a -> Trans aSource
unfoldT :: Dist (Trans a) -> Trans aSource
Spreading changes into transitions
type SpreadC a = [Change a] -> Trans aSource
functions to convert a list of changes into a transition
certainlyT :: Change a -> Trans aSource
maybeT :: ProbRep -> Change a -> Trans aSource
liftC :: Spread a -> [Change a] -> Trans aSource
uniformT :: [Change a] -> Trans aSource
normalT :: [Change a] -> Trans aSource
linearT :: Float -> [Change a] -> Trans aSource
enumT :: [ProbRep] -> [Change a] -> Trans aSource
Spreading transitions into transitions
type SpreadT a = [Trans a] -> Trans aSource
functions to convert a list of transitions into a transition
liftT :: Spread (Trans a) -> [Trans a] -> Trans aSource
uniformTT :: [Trans a] -> Trans aSource
normalTT :: [Trans a] -> Trans aSource
linearTT :: Float -> [Trans a] -> Trans aSource
enumTT :: [ProbRep] -> [Trans a] -> Trans aSource
Randomized generators
Randomized changes
type RChange a = a -> R aSource
random change
random :: Trans a -> RChange aSource
Randomized transitions
type RTrans a = a -> RDist aSource
random transition
type ApproxDist a = R [a]Source
rDist :: Ord a => [R a] -> RDist aSource
rDist converts a list of randomly generated values into a distribution by taking equal weights for all values
Iteration and simulation
class Iterate c whereSource

Naming convention:

  • * takes n :: Int and a generator and iterates the generator n times
  • . produces a single result
  • .. produces a trace
  • ~ takes k :: Int [and n :: Int] and a generator and simulates the [n-fold repetition of the] generator k times

There are the following functions:

  • n *. t iterates t and produces a distribution
  • n *.. t iterates t and produces a trace
  • k ~. t simulates t and produces a distribution
  • (k,n) ~*. t simulates the n-fold repetition of t and produces a distribution
  • (k,n) ~.. t simulates the n-fold repetition of t and produces a trace

Iteration captures three iteration strategies: iter builds an n-fold composition of a (randomized) transition while and until implement conditional repetitions

The class Iterate allows the overloading of iteration for different kinds of generators, namely transitions and random changes:

  • Trans   a = a -> Dist a    ==>   c = Dist
  • RChange a = a -> R a       ==>   c = R = IO
Methods
(*.) :: Int -> (a -> c a) -> (a -> c a)Source
while :: (a -> Bool) -> (a -> c a) -> (a -> c a)Source
until :: (a -> Bool) -> (a -> c a) -> (a -> c a)Source
show/hide Instances
class Sim c whereSource

Simulation means to repeat a random chage many times and to accumulate all results into a distribution. Therefore, simulation can be regarded as an approximation of distributions through randomization.

The Sim class allows the overloading of simulation for different kinds of generators, namely transitions and random changes:

  • Trans   a = a -> Dist a   ==>   c = Dist
  • RChange a = a -> R a      ==>   c = R = IO
Methods
(~.) :: Ord a => Int -> (a -> c a) -> RTrans aSource
returns the final randomized transition
(~..) :: Ord a => (Int, Int) -> (a -> c a) -> RExpand aSource
returns the whole trace
(~*.) :: Ord a => (Int, Int) -> (a -> c a) -> RTrans aSource
show/hide Instances
Tracing
type Trace a = [a]Source
type Space a = Trace (Dist a)Source
type Walk a = a -> Trace aSource
type Expand a = a -> Space aSource
(>>:) :: Trans a -> Expand a -> Expand aSource

(>>:) composes the result of a transition with a space (transition is composed on the left)

(a -> m a) -> (a -> [m a]) -> (a -> [m a])
walk :: Int -> Change a -> Walk aSource
walk is a bounded version of the predefined function iterate
(*..) :: Int -> Trans a -> Expand aSource
(*..) is identical to (*.), but returns the list of all intermediate distributions
type RTrace a = R (Trace a)Source
type RSpace a = R (Space a)Source
type RWalk a = a -> RTrace aSource
type RExpand a = a -> RSpace aSource
composelR :: RChange a -> RWalk a -> RWalk aSource
rWalk :: Int -> RChange a -> RWalk aSource
rWalk computes a list of values by randomly selecting one value from a distribution in each step.
mergeTraces :: Ord a => [RTrace a] -> RSpace aSource
mergeTraces converts a list of RTraces into a list of randomized distributions, i.e., an RSpace, by creating a randomized distribution for each list position across all traces
Produced by Haddock version 2.1.0