module Numeric.Probability.Example.Alarm where
import qualified Numeric.Probability.Distribution as Dist
import Numeric.Probability.Distribution ((??), (?=<<), )
type Probability = Rational
type Dist a = Dist.T Probability a
type PBool = Dist.T Probability Bool
flp :: Probability -> PBool
flp p = Dist.choose p True False
b :: PBool
b = flp 0.01
e :: PBool
e = flp 0.001
a :: Bool -> Bool -> PBool
a b0 e0 =
case (b0,e0) of
(False,False) -> flp 0.01
(False,True) -> flp 0.1
(True,False) -> flp 0.7
(True,True) -> flp 0.8
j :: Bool -> PBool
j a0 = if a0 then flp 0.8 else flp 0.05
m :: Bool -> PBool
m a0 = if a0 then flp 0.9 else flp 0.1
data Burglary = B { burglary :: Bool,
earthquake :: Bool,
alarm :: Bool,
john :: Bool,
mary :: Bool }
deriving (Eq, Ord, Show)
bJoint :: Dist Burglary
bJoint = do b' <- b
e' <- e
a' <- a b' e'
j' <- j a'
m' <- m a'
return (B b' e' a' j' m')
pmj :: Probability
pmj = mary ?? john ?=<< bJoint