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?
Baye’s Theorem For any two events \(A\) and \(B\) \(P(A|B) = \frac{P(A)P(B|A)}{P(B)}\)
ontimeTrain <- 0.9 #probability train is ontime 90%
ontimeBus <- 0.5 #probability bus is ontime 50%
trainDays <- 20/30
busDays <- 10/30
busToday <- (ontimeBus*busDays) / (ontimeBus*busDays) + (ontimeTrain*trainDays)
busToday
## [1] 1.6
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 letter was good? In addition, now 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? You should use the gRain package in R to build your network and perform these cal- culations. You may need to install RBGL package from BioConductor in R to get gRain working.
#source("http://bioconductor.org/biocLite.R")
#biocLite(c("graph", "RBGL", "Rgraphviz"))
#biocLite("Rgraphviz")
##install.packages("gRain", dependencies=TRUE, #lib="/Library/Frameworks/R.framework/Versions/3.3/Resources/library")
library(gRain)
## Loading required package: gRbase
## Warning: package 'gRbase' was built under R version 3.3.2
yN <- c("yes", "no")
lH <- c("low", "high")
#Use cptable to create conditional probability tables
d <- cptable(~difficulty, values=c(7,3), levels=yN)
i <- cptable(~intelligence, values=c(8,2), levels=lH)
g <- cptable(~grade|difficulty:intelligence, values=c(8,2,6,4,1,9,1,99), levels=lH)
s <- cptable(~SAT|intelligence, values=c(9,1,2,8), levels=lH)
l <- cptable(~letter|grade, values=c(9,1,5,95), levels=lH)
#list conditional probablitiy table
conTbl <- grain(compileCPT(list(d,i,g,s,l)))
#likelyhood evidence
lklyEvd <- setEvidence(conTbl, nodes="letter", state="high")
#auery evidence
querygrain(lklyEvd, nodes="difficulty", type="marginal")
## $difficulty
## difficulty
## yes no
## 0.6268561 0.3731439
What is the probability of the Difficulty of Course now?
#Likelyhood evidence
lklyEvd2 <- setEvidence(lklyEvd, nodes="SAT", state="high")
querygrain(lklyEvd2, nodes="difficulty", type="marginal")
## $difficulty
## difficulty
## yes no
## 0.6676522 0.3323478
yn <- c("yes", "no")
a <- cptable(~asia, values=c(1,99), levels=yn)
t.a <- cptable(~tub|asia, values=c(5,95,1,99), levels=yn)
s <- cptable(~smoke, values=c(5,5), levels=yn)
l.s <- cptable(~lung|smoke, values=c(1,9,1,99), levels=yn)
b.s <- cptable(~bronc|smoke, values=c(6,4,3,7), levels=yn)
e.lt <- cptable(~either|lung:tub, values=c(1,0,1,0,1,0,0,1), levels=yn)
x.e <- cptable(~xray|either, values=c(98,2,5,95), levels=yn)
d.be <- cptable(~dysp|bronc:either, values=c(9,1,7,3,8,2,1,9), levels=yn)
plist <- compileCPT(list(a, t.a, s, l.s, b.s, e.lt, x.e, d.be))
conTbl <- grain(plist)
lklyEvd2 <- setEvidence(lklyEvd, nodes=c("asia", "dysp"), states=c("yes", "yes"))
lklyEvd2
## Independence network: Compiled: TRUE Propagated: TRUE
## Nodes: chr [1:5] "difficulty" "intelligence" "grade" "SAT" ...
## Evidence:
## nodes is.hard.evidence hard.state
## 1 letter TRUE high
## pEvidence: 0.434390
pEvidence(lklyEvd2)
## [1] 0.43439
querygrain(conTbl, nodes="asia", type="marginal")
## $asia
## asia
## yes no
## 0.01 0.99
querygrain(conTbl, nodes=c("asia", "tub"), type="joint")
## tub
## asia yes no
## yes 0.0005 0.0095
## no 0.0099 0.9801
querygrain(conTbl, nodes=c("asia", "dysp", type="joint"))
## $asia
## asia
## yes no
## 0.01 0.99
##
## $dysp
## dysp
## yes no
## 0.4359706 0.5640294