-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBayesianNetwork.curry
59 lines (45 loc) · 1.59 KB
/
BayesianNetwork.curry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{-# OPTIONS_CYMAKE -X TypeClassExtensions #-}
module BayesianNetwork
( (=:), (|>), (<|), given, jointProbability, bernoulli, guard )
where
import List (sum)
import PFLP
import SetFunctions (set0, mapValues, foldValues)
import Distributions (bernoulli)
infixl 5 =:
infixl 4 |>
infixl 4 <|
(=:) :: Eq a => Dist a -> a -> Dist a
dA =: val = filterDist (== val) dA
guard :: Bool -> Dist ()
guard True = pure ()
given :: Eq a => (Dist a,a) -> [Dist a] -> Probability
given (dist,val) = uncurry sumCondDist . condProbability (\() -> dist,val)
(|>) :: Dist a -> (a -> Dist b) -> Dist b
dA |> f = f (value dA)
(<|) :: (a -> Dist b) -> Dist a -> Dist b
(<|) = flip (|>)
-- Auxiliary functions
jointProbability :: [Dist a] -> Dist [a]
jointProbability = sequenceA
condProbability :: Eq a => (() -> Dist a,a)
-> [Dist a]
-> ([Probability],[Probability])
condProbability (distA,valA) dAs = (probsN, probsD)
where
probsD = collectProbs (calc (distA ()))
probsN = collectProbs (calc distA')
distA' = distA () =: valA
dists' = jointProbability dAs
calc dist = concat <$>
jointProbability [(\x -> [x]) <$> dist, dists']
collectProbs :: Dist a -> [Probability]
collectProbs dist =
foldValues (++)
[]
(mapValues (flip (:) [] . probability) (set0 dist))
sumConditional :: Eq a => (Dist a,a) -> [Dist a] -> Probability
sumConditional (dist,val) dists =
uncurry sumCondDist (condProbability (\ () -> dist,val) dists)
sumCondDist :: [Probability] -> [Probability] -> Probability
sumCondDist probN probD = sum probN / sum probD