1. Problem Set 1 • Your colleague either commutes by train or by the bus. 20 days of the month, she takes the train and the remaining 10 days she takes the bus. If she takes the train, she reaches work on time with a probability of 0.9. If she takes the bus, she frequently gets stuck in traffic and reaches work on time with a probability of 0.5. Given that she was on time today, what is the probability that she took the bus to work today?

Given P(onTime|Bus) = 0.5 P(Bus) = 1/3 P(onTime|Train) = 0.9 P(Train) = 2/3 P(Bus|onTime) = P(onTime|Bus)P(Bus)/(P(onTime|Bus)P(Bus)+P(onTime|Train)*P(Train)) P(Bus|onTime) = 0.2174

(0.5*0.3333333)/((0.5*0.3333333) + (0.6666667*0.9))
## [1] 0.2173913
  1. Problem Set 2 In the Grade Network that we looked at in the notes, what happens to the probability of Difficulty of Course when you present the evidence that the received recommendation letterwas good?
source("https://bioconductor.org/biocLite.R")
## Bioconductor version 3.4 (BiocInstaller 1.24.0), ?biocLite for help
biocLite("RBGL")
## BioC_mirror: https://bioconductor.org
## Using Bioconductor 3.4 (BiocInstaller 1.24.0), R 3.3.3 (2017-03-06).
## Installing package(s) 'RBGL'
## 
## The downloaded binary packages are in
##  /var/folders/0r/pq_1j2356k56gtfhfc_gcytw0000gp/T//RtmpOPnW82/downloaded_packages
library(gRain)
## Loading required package: gRbase
library(gRbase)
no_yes <- c("no", "yes")
low_high <- c("low", "high")
d <- cptable(~difficulty, values=c(0.3,0.7), levels = no_yes)
i <- cptable(~intelligence, values = c(0.8, 0.2), levels = low_high)
s_i <- cptable(~sat|intelligence, values = c(0.9,0.1,0.2,0.8), levels = low_high)
g_d_i <- cptable(~grade|difficulty:intelligence, values=c(0.6,0.4,0.8,0.2,0.1,0.99,0.1,0.9), levels = low_high)
l_g <- cptable(~letter|grade, values=c(0.9,0.1,0.5,0.95), levels = low_high)

li <- compileCPT(list(d, i, s_i, g_d_i, l_g))
p <- grain(li)

(querygrain(p, nodes=c("difficulty", "intelligence", "sat", "grade", "letter"), type="marginal"))
## $difficulty
## difficulty
##  no yes 
## 0.3 0.7 
## 
## $intelligence
## intelligence
##  low high 
##  0.8  0.2 
## 
## $grade
## grade
##       low      high 
## 0.6115046 0.3884954 
## 
## $sat
## sat
##  low high 
## 0.76 0.24 
## 
## $letter
## letter
##       low      high 
## 0.6843181 0.3156819
# What happens to the probability of Difficulty of Course when you present the evidence that the received recommendation letter was good?
letter_good <- setFinding(p, nodes = c("letter"), states=c("high"))
(querygrain(letter_good, nodes=c("difficulty"), type="marginal"))
## $difficulty
## difficulty
##        no       yes 
## 0.3597003 0.6402997
# Present the evidence that both SAT scores were good and the letter of recommendation was good, What is the probability of the Difficulty of Course now?
sat_good <- setFinding(letter_good, nodes=c("sat"), states=c("high"))
(querygrain(sat_good, nodes=c("difficulty"), type="marginal"))
## $difficulty
## difficulty
##        no       yes 
## 0.3174519 0.6825481