Problem Set 1

Substituting into Bayes’ Rule using for the probability that the colleague took the bus (\(x = b\)) and was on time (\(e = ot\)),

\[\begin{align} P(X = b|ot) &= \frac{P(ot|X=b) \times P(X=b)}{P(ot|X=b) \times P(X=b) + P(ot|X \neq b) \times P(X \neq b)} \\ &= \frac{0.5 \times \frac{10}{30}}{0.5 \times \frac{10}{30} + 0.9 \times \frac{20}{30}} \\ &= 0.2174 \end{align}\]

Problem Set 2

The network is first built using the gRain package:

library(gRain)
ny <- c('N', 'Y')
lh <- c('L', 'H')
D <- cptable(~difficulty, values = c(0.3, 0.7), levels = ny)
I <- cptable(~intelligence, values = c(0.8, 0.2), levels = lh)
S.I <- cptable(~sat|intelligence, values = c(0.9, 0.1, 0.2, 0.8), levels = lh)
G.DI <- cptable(~grade|intelligence:difficulty, values = c(0.6, 0.4, 0.01, 0.99, 0.8, 0.2, 0.1, 0.9), levels = lh)
L.G <- cptable(~letter|grade, values = c(0.9, 0.1, 0.05, 0.95), levels = lh)
plist <- compileCPT(list(D, I, S.I, G.DI, L.G))
grade_net <- grain(plist)

Evidence is then entered, and the probabilities Difficulty of Course are returned.

grade_net.letter <- setEvidence(grade_net, evidence = list(letter = 'H'))
querygrain(grade_net.letter, nodes = "difficulty")[[1]]
difficulty
        N         Y 
0.3731439 0.6268561 
grade_net.letter.sat <- setEvidence(grade_net, evidence = list(letter = 'H', sat = 'H'))
querygrain(grade_net.letter.sat, nodes = "difficulty")[[1]]
difficulty
        N         Y 
0.3323478 0.6676522