Bayes’ Theorem allows us to update our beliefs about the probability of an event (A) given the occurrence of another event (B), based on prior probabilities. The formula for Bayes’ Theorem is: \[ P(A|B) = \frac{P(B|A) \times P(A)}{P(B)} \]
Assume we want to calculate the updated probability of raining today, given that the sky is cloudy:
Calculated Bayes’ Theorem in R code:
P_A = 0.5
P_B_A = 0.8
P_B = 0.6
P_A_B = (P_B_A * P_A) / P_B
print(P_A_B)
## [1] 0.6666667
After applying Bayes’ Theorem to our example, we obtained the updated probability:
\[ P(A|B) = 0.6667 \]
This means, given the sky is cloudy (B), the probability of it raining today (A) is approximately 66.67%.
Which means:
# Initial probabilities
probAcademic <- 0.35
probSporting <- 0.20
probNoEvent <- 0.45
# Probabilities garage is full
probFullGivenAcademic <- 0.25
probFullGivenSporting <- 0.70
probFullGivenNoEvent <- 0.05
probNotAcademic <- 1 - probAcademic
probNotSporting <- 1 - probSporting
probFull <- (probAcademic * probFullGivenAcademic) +
(probSporting * probFullGivenSporting) +
(probNoEvent * probFullGivenNoEvent)
# probability of sporting event
probSportingGivenFull <- (probSporting * probFullGivenSporting) / probFull
print(probSportingGivenFull)
## [1] 0.56
Using Bayes’ theorem, the probability that there is a sporting event given that the garage is full is approximately 0.56. This means that if Jose finds the garage full upon arriving at the campus, there’s a 56% chance that the fullness is due to a sporting event.
# Node
node1 <- "Start"
node2 <- "Academic"
node3 <- "Sporting"
node4 <- "NoEvent"
node5 <- "FullAcademic" # Adjusted
node6 <- "NotFullAcademic" # Adjusted
node7 <- "FullSporting" # Adjusted
node8 <- "NotFullSporting" # Adjusted
node9 <- "FullNoEvent" # Adjusted
node10 <- "NotFullNoEvent" # Adjusted
nodeNames <- c(node1, node2, node3, node4, node5, node6, node7, node8, node9, node10)
rEG <- new("graphNEL", nodes = nodeNames, edgemode="directed")
rEG <- addEdge(node1, node2, rEG, 1)
rEG <- addEdge(node1, node3, rEG, 1)
rEG <- addEdge(node1, node4, rEG, 1)
rEG <- addEdge(node2, node5, rEG, 1)
rEG <- addEdge(node2, node6, rEG, 1)
rEG <- addEdge(node3, node7, rEG, 1)
rEG <- addEdge(node3, node8, rEG, 1)
rEG <- addEdge(node4, node9, rEG, 1)
rEG <- addEdge(node4, node10, rEG, 1)
eAttrs <- list()
eAttrs$label <- c(toString(probAcademic), toString(probSporting), toString(probNoEvent),
toString(probFullGivenAcademic), toString(1 - probFullGivenAcademic),
toString(probFullGivenSporting), toString(1 - probFullGivenSporting),
toString(probFullGivenNoEvent), toString(1 - probFullGivenNoEvent))
q <- edgeNames(rEG)
names(eAttrs$label) <- c(q[1], q[2], q[3], q[4], q[5], q[6], q[7], q[8], q[9])
# Tree Diagram
attributes <- list(node = list(fillcolor = "lightblue", fontsize = "12"),
edge = list(color = "blue"),
graph = list(rankdir = "LR"))
plot(rEG, edgeAttrs = eAttrs, attrs = attributes)