-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Computations with discrete random variables -- -- The Library allows exact computation with discrete random variables in -- terms of their distributions by using a monad. The monad is similar to -- the List monad for non-deterministic computations, but extends the -- List monad by a measure of probability. Small interface to R plotting. @package probability @version 0.1 module Probability type Event a = a -> Bool oneOf :: (Eq a) => [a] -> Event a just :: (Eq a) => a -> Event a newtype Probability P :: ProbRep -> Probability type ProbRep = Float precision :: Int showPfix :: ProbRep -> String roundRel :: (RealFrac a) => Int -> a -> a showP :: ProbRep -> String errorMargin :: ProbRep -- | binary composition (>@>) :: (Monad m) => (a -> m b) -> (b -> m c) -> a -> m c -- | composition of a list of monadic functions sequ :: (Monad m) => [a -> m a] -> a -> m a -- | probability disribution newtype Dist a D :: [(a, ProbRep)] -> Dist a unD :: Dist a -> [(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)] -- | product of independent distributions, identical to -- Monad.liftM2 joinWith :: (a -> b -> c) -> Dist a -> Dist b -> Dist c prod :: Dist a -> Dist b -> Dist (a, b) -- | distribution generators 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 -- | extracting and mapping the domain of a distribution extract :: Dist a -> [a] mapD :: (a -> b) -> Dist a -> Dist b -- | unfold a distribution of distributions into one distribution unfoldD :: Dist (Dist a) -> Dist a -- | conditional distribution cond :: Dist Bool -> Dist a -> Dist a -> Dist a truth :: Dist Bool -> Probability -- | conditional probability (|||) :: Dist a -> Event a -> Dist a -- | filtering distributions data Select a Case :: a -> Select a Other :: Select a above :: (Ord a) => ProbRep -> Dist a -> Dist (Select a) scale :: [(a, ProbRep)] -> Dist a filterD :: (a -> Bool) -> Dist a -> Dist a -- | selecting from distributions selectP :: Dist a -> ProbRep -> a scanP :: ProbRep -> [(a, ProbRep)] -> a (??) :: Event a -> Dist a -> Probability class ToFloat a toFloat :: (ToFloat a) => a -> Float class FromFloat a fromFloat :: (FromFloat a) => Float -> a class Expected a expected :: (Expected a) => a -> Float -- | statistical analyses variance :: (Expected a) => Dist a -> Float stddev :: (Expected a) => Dist a -> Float -- | Random values type R a = IO a printR :: (Show a) => R a -> R () pick :: Dist a -> R a -- | Randomized distributions type RDist a = R (Dist a) rAbove :: (Ord a) => ProbRep -> RDist a -> RDist (Select a) -- | deterministic generator type Change a = a -> a -- | probabilistic generator type Trans a = a -> Dist a idT :: Trans a mapT :: Change a -> Trans a -> Trans a unfoldT :: Dist (Trans a) -> Trans a -- | functions to convert a list of changes into a transition 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 -- | functions to convert a list of transitions into a transition 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 -- | random change type RChange a = a -> R a random :: Trans a -> RChange a -- | random transition type RTrans a = a -> RDist a type ApproxDist a = R [a] -- | rDist converts a list of randomly generated values into a -- distribution by taking equal weights for all values rDist :: (Ord a) => [R a] -> RDist a -- | Naming convention: -- --
Trans a = a -> Dist a ==> c = Dist
RChange a = a -> R a ==> c = R = IO
Trans a = a -> Dist a ==> c = Dist
RChange a = a -> R a ==> c = R = IO
-- (a -> m a) -> (a -> [m a]) -> (a -> [m a]) --(>>:) :: Trans a -> Expand a -> Expand a -- | walk is a bounded version of the predefined function iterate walk :: Int -> Change a -> Walk a -- | (*..) is identical to (*.), but returns the list of -- all intermediate distributions (*..) :: 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 computes a list of values by randomly selecting one value -- from a distribution in each step. rWalk :: Int -> RChange a -> RWalk a -- | 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 mergeTraces :: (Ord a) => [RTrace a] -> RSpace a instance (Eq a) => Eq (Select a) instance (Ord a) => Ord (Select a) instance (Show a) => Show (Select a) instance Sim IO instance Sim Dist instance Iterate IO instance Iterate Dist instance (Expected a) => Expected (Dist a) instance (Expected a) => Expected [a] instance Expected Integer instance Expected Int instance Expected Float instance FromFloat Integer instance FromFloat Int instance FromFloat Float instance ToFloat Integer instance ToFloat Int instance ToFloat Float instance (Ord a, Show a) => Show (Dist a) instance (Ord a, Eq a) => Eq (Dist a) instance Functor Dist instance MonadPlus Dist instance Monad Dist instance Show Probability module Alarm type PBool = Dist Bool flp :: Float -> PBool -- | prior burglary 1% b :: PBool -- | prior earthquake 0.1% e :: PBool -- | conditional probability of alarm given burglary and earthquake a :: Bool -> Bool -> PBool -- | conditional probability of john calling given alarm j :: Bool -> PBool -- | conditional probability of mary calling given alarm m :: Bool -> PBool -- | calculate the full joint distribution data Burglary B :: Bool -> Bool -> Bool -> Bool -> Bool -> Burglary burglary :: Burglary -> Bool earthquake :: Burglary -> Bool alarm :: Burglary -> Bool john :: Burglary -> Bool mary :: Burglary -> Bool bJoint :: Dist Burglary -- | what is the probability that mary calls given that john calls? pmj :: Probability instance Eq Burglary instance Ord Burglary instance Show Burglary -- | Model: -- -- one server serving customers from one queue module Queuing type Time = Int -- | (servingTime, nextArrival) type Profile = (Time, Time) type Event a = (a, Profile) -- | customers and their individual serving times type Queue a = [(a, Time)] -- | (customers waiting,validity period of that queue) type State a = (Queue a, Time) type System a = [([a], Time)] type Events a = [Event a] event :: Time -> Events a -> Queue a -> [State a] system :: Events a -> System a -- | multiple servers mEvent :: Int -> Time -> Events a -> Queue a -> [State a] -- | decrease served customers remaining time by specified amount mServe :: Int -> Int -> Queue a -> Queue a -- | time until next completion mTimeStep :: Int -> Queue a -> Int mSystem :: Int -> Events a -> System a type RProfile = (Dist Time, Trans Time) type REvent a = (a, RProfile) type REvents a = [REvent a] rSystem :: Int -> REvents a -> R (System a) rBuildEvents :: REvents a -> R (Events a) rmSystem :: (Ord a) => Int -> Int -> REvents a -> RDist (System a) evalSystem :: (Ord a) => Int -> Int -> REvents a -> (System a -> b) -> RDist b unit :: b -> ((), b) maxQueue :: (Ord a) => System a -> Int allWaiting :: (Ord a) => Int -> System a -> [a] countWaiting :: (Ord a) => Int -> System a -> Int waiting :: Int -> System a -> Time inSystem :: System a -> Time total :: System a -> Time server :: Int -> System a -> Time idle :: Int -> System a -> Time idleAvgP :: Int -> System a -> Float module Barber custServ :: Dist Time nextCust :: Trans Time barbers :: Int customers :: Int runs :: Int barberEvent :: ((), (Dist Time, Time -> Dist Time)) barberEvents :: [((), (Dist Time, Time -> Dist Time))] barberSystem :: (System () -> b) -> RDist b data Category ThreeOrLess :: Category FourToTen :: Category MoreThanTen :: Category cat :: Time -> Category perc :: Float -> String -- | avg barber idle time barberIdle :: RDist String -- | avg customer waiting time (unserved customers) customerWait :: RDist Category instance Eq Category instance Ord Category instance Show Category module Bayesian type State a = [a] type PState a = Dist (State a) type STrans a = State a -> PState a type SPred a = a -> State a -> Bool event :: ProbRep -> a -> STrans a happens :: (Eq a) => SPred a network :: [STrans a] -> PState a source :: ProbRep -> a -> STrans a bin :: (Eq a) => a -> a -> ProbRep -> ProbRep -> ProbRep -> ProbRep -> a -> STrans a -- | Two possible causes for one effect data Nodes A :: Nodes B :: Nodes E :: Nodes g :: PState Nodes aE :: Probability bE :: Probability e :: Probability instance Eq Nodes instance Ord Nodes instance Show Nodes -- | Consider a family of two children. Given that there is a boy in the -- family, what is the probability that there are two boys in the family? module Boys data Child Boy :: Child Girl :: Child type Family = [Child] birth :: Trans Family family :: Dist Family boys :: Int -> Event Family existsBoy :: Event Family familyWithBoy :: Dist Family twoBoys :: Probability countBoys :: Family -> Int numBoys :: Dist Int instance Eq Child instance Ord Child instance Show Child module Collection type Collection a = [a] selectOne :: (Eq a) => Collection a -> Dist (a, Collection a) select1 :: (Eq a) => Collection a -> Dist a select2 :: (Eq a) => Collection a -> Dist (a, a) selectMany :: (Eq a) => Int -> Collection a -> Dist ([a], Collection a) select :: (Eq a) => Int -> Collection a -> Dist [a] data Marble R :: Marble G :: Marble B :: Marble bucket :: Collection Marble jar :: Collection Marble pRGB :: Probability pRG :: Probability data Suit Club :: Suit Spade :: Suit Heart :: Suit Diamond :: Suit data Rank Plain :: Int -> Rank Jack :: Rank Queen :: Rank King :: Rank Ace :: Rank type Card = (Rank, Suit) plains :: [Rank] faces :: [Rank] isFace :: Card -> Bool isPlain :: Card -> Bool ranks :: [Rank] suits :: [Suit] deck :: Collection Card -- | mini-blackjack: draw 2 cards, and if value is less than 14, continue -- drawing until value equals or exceeds 14. if values exceeds 21, you -- lose, otherwise you win. value :: Card -> Int draw :: ([Card], Collection Card) -> Dist ([Card], Collection Card) drawTo16 :: t -> IO ([Card], Collection Card) win :: ([Card], b) -> Bool chanceWin :: IO (Dist Bool) instance Eq Rank instance Ord Rank instance Show Rank instance Eq Suit instance Ord Suit instance Show Suit instance Enum Suit instance Eq Marble instance Ord Marble instance Show Marble module Dice type Die = Int die :: Dist Die twoDice :: Dist (Die, Die) dice :: Int -> Dist [Die] twoSixes :: Probability -- | sixes p n computes the probability of getting p sixes -- (>1, ==2, ...) when rolling n dice sixes :: (Int -> Bool) -> Int -> Probability droll :: Dist Die g3 :: Probability addTwo :: Dist Die module MontyHall data Door A :: Door B :: Door C :: Door doors :: [Door] data State Doors :: Door -> Door -> Door -> State prize :: State -> Door chosen :: State -> Door opened :: State -> Door -- | initial configuration of the game status start :: State -- | Steps of the game: -- --