Bayes’ Theorem is a fundamental concept in probability theory that allows us to update our beliefs or probabilities based on new evidence. It provides a way to calculate the probability of a hypothesis or event given prior knowledge and new information. The theorem is stated as: \[P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)}\] where:
\(P(A|B)\) represents the probability of event A given event B.
\(P(B|A)\) is the probability of event B given event A.
\(P(A)\) is the prior probability of event A.
\(P(B)\) is the prior probability of event B.
An example of Bayes’ Theorem in action is medical diagnosis. Let’s say we want to determine the probability of a person having a certain disease given that they exhibit a specific set of symptoms. We have the prior probability of the disease occurring in the general population \(P(A)\), the probability of observing those symptoms in people with the disease \(P(B|A)\), and the probability of observing those symptoms in the general population \(P(B)\). By applying Bayes’ Theorem, we can calculate the updated probability \(P(A|B)\) of the person having the disease based on the symptoms they exhibit.
library(graph)
## 载入需要的程辑包:BiocGenerics
##
## 载入程辑包:'BiocGenerics'
## The following objects are masked from 'package:stats':
##
## IQR, mad, sd, var, xtabs
## The following objects are masked from 'package:base':
##
## anyDuplicated, aperm, append, as.data.frame, basename, cbind,
## colnames, dirname, do.call, duplicated, eval, evalq, Filter, Find,
## get, grep, grepl, intersect, is.unsorted, lapply, Map, mapply,
## match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
## Position, rank, rbind, Reduce, rownames, sapply, setdiff, sort,
## table, tapply, union, unique, unsplit, which.max, which.min
library(Rgraphviz)
## 载入需要的程辑包:grid
library(graph)
library(Rgraphviz)
# Probability values
probAcademic <- 0.35
probSporting <- 0.20
probNoEvent <- 0.45
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 given full
probSportingGivenFull <- (probSporting * probFullGivenSporting) / probFull
print(probSportingGivenFull)
## [1] 0.56
# Node names
node1 <- "Start"
node2 <- "Academic"
node3 <- "Sporting"
node4 <- "NoEvent"
node5 <- "FullGivenAcademic"
node6 <- "NotFullGivenAcademic"
node7 <- "FullGivenSporting"
node8 <- "NotFullGivenSporting"
node9 <- "FullGivenNoEvent"
node10 <- "NotFullGivenNoEvent"
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 = "red", fontsize = "20"),
edge = list(color = "black"),
graph = list(rankdir = "LR")
)
plot(rEG, edgeAttrs = eAttrs, attrs = attributes)