on master help sign in
Small fixes
commit62d60189f002372db25ffa1c1c1977699e81bf3b authorArmando Santos <armandoifsantos@gmail.com> dateSat 28 Dec 2019, 23:08 (7 years ago) parent9d59e46
2 files changed+34−4
app/Main.hs+24 src/Matrix/Type.hs+10−4
app/Main.hs+24
@@ -39,9 +39,33 @@ mulSSM3 = fromF' (mulSS (Nat 3))
3939die :: Dist (Natural 6) 6
4040die = uniform [nat @6 1 .. nat 6]
4141
42+-- Sprinkler
43+
44+rain :: Dist Bool 2
45+rain = choose 0.8
46+
47+sprinkler :: Matrix Double 2 2
48+sprinkler = fromLists [[0.6, 0.99], [0.4, 0.01]]
49+
50+grass :: Matrix Double 4 2
51+grass = fromLists [[1, 0.2, 0.1, 0.01], [0, 0.8, 0.9, 0.99]]
52+
53+state = khatri grass identity `comp` khatri sprinkler identity `comp` rain
54+
55+grass_wet = row [0,1] `comp` kp1 @Double @2 @4
56+
57+rainning = row [0,1] `comp` kp2 @Double @2 @2 `comp` kp2 @Double @2 @4
58+
4259main :: IO ()
4360main = do
61+ putStrLn "Monty Hall Problem solution:"
4462 prettyPrint (p1 @Double @1 `comp` secondChoice `comp` firstChoice)
63+ putStrLn "\n Sum of dices probability:"
4564 prettyPrint (sumSSM `comp` khatri die die)
65+ putStrLn "\n Checking that the last result is indeed a distribution: "
4666 prettyPrint (bang `comp` sumSSM `comp` khatri die die)
67+ putStrLn "\n Probability of grass being wet:"
68+ prettyPrint (grass_wet `comp` state)
69+ putStrLn "\n Probability of rain:"
70+ prettyPrint (rainning `comp` state)
4771
src/Matrix/Type.hs+10−4
@@ -166,20 +166,26 @@ instance Eq e => Eq (Matrix e cols rows) where
166166
167167instance Num e => Num (Matrix e cols rows) where
168168
169− Empty + Empty = Empty
170− (One a) + (One b) = One (a + b)
171− (Junc a b) + (Junc c d) = Junc (a + c) (b + d)
172− (Split a b) + (Split c d) = Split (a + c) (b + d)
169+ Empty + Empty = Empty
170+ (One a) + (One b) = One (a + b)
171+ (Junc a b) + (Junc c d) = Junc (a + c) (b + d)
172+ (Split a b) + (Split c d) = Split (a + c) (b + d)
173+ x@(Split a b) + y@(Junc c d) = x + abideJS y
174+ x@(Junc a b) + y@(Split c d) = abideJS x + y
173175
174176 Empty - Empty = Empty
175177 (One a) - (One b) = One (a - b)
176178 (Junc a b) - (Junc c d) = Junc (a - c) (b - d)
177179 (Split a b) - (Split c d) = Split (a - c) (b - d)
180+ x@(Split a b) - y@(Junc c d) = x - abideJS y
181+ x@(Junc a b) - y@(Split c d) = abideJS x - y
178182
179183 Empty * Empty = Empty
180184 (One a) * (One b) = One (a * b)
181185 (Junc a b) * (Junc c d) = Junc (a * c) (b * d)
182186 (Split a b) * (Split c d) = Split (a * c) (b * d)
187+ x@(Split a b) * y@(Junc c d) = x * abideJS y
188+ x@(Junc a b) * y@(Split c d) = abideJS x * y
183189
184190 abs Empty = Empty
185191 abs (One a) = One (abs a)