Commit e9d83efa authored by Nicola Botta's avatar Nicola Botta
Browse files

Added CP examples.

parent 13886a0d
......@@ -35,6 +35,19 @@ What is the probability of ~a~? (If ~a~ is not in the support of pd, we "round"
> lookup a [(_, p)] = p
> lookup a ((a', p):rest) = if a <= a' then p else lookup a rest
Probabilities:
> prob :: (a -> Bool) -> PD a -> Double
> prob e (PD aps) = sum [p | (a, p) <- aps, e a == True]
> lift :: (Bool -> Bool -> Bool) -> (a -> Bool) -> (a -> Bool) -> (a -> Bool)
> lift op f g = \ a -> op (f a) (g a)
> condProb :: (a -> Bool) -> (a -> Bool) -> PD a -> Maybe Double
> condProb e1 e2 pd = let pe2 = prob e2 pd in
> if pe2 == 0.0
> then Nothing
> else Just (prob (lift (&&) e1 e2) pd / pe2)
Cummulative probabilities:
......@@ -51,6 +64,7 @@ Expected value:
> expectation :: PD Double -> Double
> expectation (PD as) = sum (map (uncurry (*)) as)
Some distributions
> bernoulli :: Double -> PD Double
......@@ -195,19 +209,9 @@ What is the probability of the first flip being a head given that there are at l
> then return a
> else twoOutOfThree
> toss3 :: PD (Double, Double, Double)
> toss3 = PD [((x, y, z), p) | x <- s, y <- s, z <- s]
> where p = 0.5 * 0.5 * 0.5
> s = [0.0, 1.0]
> prob :: (a -> Bool) -> PD a -> Double
> prob e (PD aps) = sum [p | (a, p) <- aps, e a == True]
> cprob :: (a -> Bool) -> (a -> Bool) -> PD a -> Maybe Double
> cprob e1 e2 pd = let pe2 = prob e2 pd in
> if pe2 == 0.0
> then Nothing
> else Just (prob (\ a -> e1 a && e2 a) pd / pe2)
> fstHtwoHs1 :: Nat -> Double
> fstHtwoHs1 n = let pd = PD (get_pd n twoOutOfThree) in
> get_prob 1.0 pd
> fstH :: (Double, Double, Double) -> Bool
> fstH (x, _, _) = x == 1.0
......@@ -215,7 +219,28 @@ What is the probability of the first flip being a head given that there are at l
> twoHs :: (Double, Double, Double) -> Bool
> twoHs (x, y, z) = x + y + z >= 2.0
> lala = cprob fstH twoHs toss3
> toss3 :: RV (Double, Double, Double)
> toss3 = do a <- toss 0.5
> b <- toss 0.5
> c <- toss 0.5
> return (a, b, c)
> fstHtwoHs2 :: Nat -> Maybe Double
> fstHtwoHs2 n = let pd = PD (get_pd n toss3) in
> condProb fstH twoHs pd
> fstHtwoHs3 :: Nat -> Maybe Double
> fstHtwoHs3 n = let t1 = get_pd n (toss 0.5) in
> let pd = PD [((x, y, z), px * py * pz) | (x, px) <- t1, (y, py) <- t1, (z, pz) <- t1] in
> condProb fstH twoHs pd
> fstHtwoHs4 :: Maybe Double
> fstHtwoHs4 = let t1 = pd2List (bernoulli 0.5) in
> let pd = PD [((x, y, z), px * py * pz) | (x, px) <- t1, (y, py) <- t1, (z, pz) <- t1] in
> condProb fstH twoHs pd
3.2 Factor
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment