-- 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: -- -- -- -- There are the following functions: -- -- -- -- 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: -- -- class Iterate c (*.) :: (Iterate c) => Int -> (a -> c a) -> (a -> c a) while :: (Iterate c) => (a -> Bool) -> (a -> c a) -> (a -> c a) until :: (Iterate c) => (a -> Bool) -> (a -> c a) -> (a -> c a) -- | 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: -- -- class Sim c (~.) :: (Sim c, Ord a) => Int -> (a -> c a) -> RTrans a (~..) :: (Sim c, Ord a) => (Int, Int) -> (a -> c a) -> RExpand a (~*.) :: (Sim c, 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 -- | (>>:) composes the result of a transition with a space -- (transition is composed on the left) -- --
--   (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: -- --
    --
  1. hide the prize
  2. --
  3. choose a door
  4. --
  5. open a non-open door, not revealing the prize
  6. --
  7. apply strategy: switch or stay
  8. --
hide :: Trans State choose :: Trans State open :: Trans State type Strategy = Trans State switch :: Strategy stay :: Strategy game :: Strategy -> Trans State data Outcome Win :: Outcome Lose :: Outcome result :: State -> Outcome eval :: Strategy -> Dist Outcome simEval :: Int -> Strategy -> RDist Outcome firstChoice :: Dist Outcome switch' :: Trans Outcome instance Eq Outcome instance Ord Outcome instance Show Outcome instance Eq State instance Ord State instance Show State instance Eq Door instance Ord Door instance Show Door -- | Ceneralization of Boys -- -- Consider a family of n children. Given that there are k boys in the -- family, what is the probability that there are m boys in the family? module NBoys data Child Boy :: Child Girl :: Child type Family = [Child] birth :: Trans Family family :: Int -> Dist Family countBoys :: Family -> Int boys :: Int -> Event Family nBoys :: Int -> Int -> Int -> Probability numBoys :: Int -> Int -> Dist Int onlyBoys1 :: Int -> Probability instance Eq Child instance Ord Child instance Show Child module Visualize -- | global settings for one figure data FigureEnv FE :: String -> String -> String -> String -> FigureEnv fileName :: FigureEnv -> String title :: FigureEnv -> String xLabel :: FigureEnv -> String yLabel :: FigureEnv -> String -- | default settings for figure environment figure :: FigureEnv data Color Black :: Color Blue :: Color Green :: Color Red :: Color Brown :: Color Gray :: Color Purple :: Color DarkGray :: Color Cyan :: Color LightGreen :: Color Magenta :: Color Orange :: Color Yellow :: Color White :: Color Custom :: Int -> Int -> Int -> Color data LineStyle Solid :: LineStyle Dashed :: LineStyle Dotted :: LineStyle DotDash :: LineStyle LongDash :: LineStyle TwoDash :: LineStyle type PlotFun = Float -> Float -- | settings for individual plots data Plot Plot :: [Float] -> [Float] -> Color -> LineStyle -> Int -> String -> Plot ys :: Plot -> [Float] xs :: Plot -> [Float] color :: Plot -> Color lineStyle :: Plot -> LineStyle lineWidth :: Plot -> Int label :: Plot -> String -- | default plotting environment plot :: Plot colors :: [Color] setColor :: Plot -> Color -> Plot autoColor :: [Plot] -> [Plot] -- | create a plot from a distribution plotD :: (ToFloat a) => Dist a -> Plot plotRD :: (ToFloat a) => RDist a -> IO Plot -- | create a plot from a function plotF :: (FromFloat a, ToFloat b) => (Float, Float, Float) -> (a -> b) -> Plot -- | create a plot from a list plotL :: (ToFloat a) => [a] -> Plot plotRL :: (ToFloat a) => R [a] -> IO Plot yls :: [Float] -> Plot -> Plot metaTuple :: [Float] -> [(Float, Float)] -> [(Float, Float)] -- | we want to increase the bounds absolutely, account for negative -- numbers decr :: (Ord a, Fractional a) => a -> a incr :: (Ord a, Fractional a) => a -> a -- | Visualization output type Vis = IO () fig :: [Plot] -> Vis figP :: FigureEnv -> [Plot] -> Vis showParams :: (Show a) => [a] -> [String] -> String legend :: Float -> Float -> [Plot] -> String drawy :: (ToFloat a) => Int -> Plot -> [a] -> String vec :: (Show a) => [a] -> String out0 :: String -> String -> IO () out1 :: String -> String -> IO () instance Eq LineStyle instance Eq Color instance Show FigureEnv instance Show LineStyle instance Show Color -- | Lotka-Volterra predator-prey model -- -- parameters -- -- module Predator d :: Float s :: Float e :: Float g :: Float v0 :: Float p0 :: Float dv :: (Float, Float) -> Float dp :: (Float, Float) -> Float dvp :: (Float, Float) -> (Float, Float) vp :: [(Float, Float)] vs :: [Float] ps :: [Float] fig1 :: Int -> Vis module TreeGrowth type Height = Int data Tree Alive :: Height -> Tree Hit :: Height -> Tree Fallen :: Tree grow :: Trans Tree hit :: Trans Tree fall :: Trans Tree evolve :: Trans Tree -- | tree growth simulation: start with seed and run for n generations seed :: Tree -- | tree n : tree distribution after n generations tree :: Int -> Tree -> Dist Tree -- | hist n : history of tree distributions for n generations hist :: Int -> Expand Tree -- | Since '(*.)' is overloaded for Trans and RChange, we can run the -- simulation ~. directly to n *. live. simTree :: Int -> Int -> RTrans Tree simHist :: Int -> Int -> RExpand Tree t2 :: Dist Tree h2 :: Space Tree st2 :: R () sh2 :: R () height :: Tree -> Int p2 :: Vis p3 :: Vis p4 :: Vis p5 :: Vis p6 :: Vis p1 :: Vis heightAtTime :: Int -> Plot heightCurve :: (Int, Color) -> Plot done :: Tree -> Bool ev5 :: Tree -> Dist Tree instance Ord Tree instance Eq Tree instance Show Tree