Bayes Theorem is a theorem about the conditional probability of
random events A and B. The probability of event A occurring under the
condition that event B has occurred has certain relationship with the
probability of event B occurring under the condition that event A has
occurred. Their ratio is Event A’s prior probability divided by Event
B’s prior probability.
Example: Suppose you’re
testing for a rare disease (1% prevalence). If a person tests positive,
it doesn’t mean they definitely have the disease. If the test is 99%
accurate, and you test positive, your actual chance of having the
disease is around 50%, not 99%, because of the rarity of the disease.
Bayes’ Theorem helps quantify that adjusted probability.
Assume:
P(A) = Probability of academic events = 0.35
P(B) =
Probability of sporting event = 0.2
P(C) = Probability of no event =
0.45
P(F) = Probability of full garage
As a result:
P(F|A) =
Probability of full garage with an academic event = 0.25
P(F|B) =
Probability of full garage given with sporting events = 0.7
P(F|C) =
Probability of full garage given with no events = 0.05
The question is what is the probability that there is a sporting
event if the garage is full? The probability equals to P(B|F). We can
know P(B|F) by using Bayes Theorem. \[P(B|F)=\frac{P(F|B)P(B)}{P(F)}\] We know
P(F|B) and P(B), now we only need to know P(F). \[\begin{align*}
P(F)&=P(A)P(F|A)+P(B)P(F|B)+P(C)P(F|C)\\
&=[(0.35)(0.25)]*[(0.2)(0.7)]*[(0.45)(0.05)]\\
&= 0.25
\end{align*}\] So P(B|F) = 0.56.
library(BiocManager)
library(Rgraphviz)
## Loading required package: graph
## Loading required package: BiocGenerics
##
## Attaching package: '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
## Loading required package: grid
# Probability of events
a <- .35
b <- .20
c <- .45
fa <- .25
fb <- .7
fc <- .05
# Calculate the rest of the values based upon the variables above
notfa <- 1 - fa
notfb <- 1 - fb
notfc <- 1 - fc
# Joint Probabilities
aANDfa <- a * fa
bANDfb <- b * fb
cANDfc <- c * fc
#############################################
## 3. START CODING ELEMENTS FOR TREE FOR GRAPH
#############################################
### NODES (labels)
# These are the labels of the nodes on the graph
# To signify "Not A" - we use A' or A prime
node1 <- "P"
node2 <- "a"
node3 <- "b"
node4 <- "c"
node5 <- "a&fa"
node6 <- "a¬fa"
node7 <- "b&fb"
node8 <- "b¬fb"
node9 <- "c&fc"
node10 <- "c¬fc"
nodeNames <- c(node1, node2, node3, node4, node5, node6, node7, node8, node9, node10)
rEG <- new("graphNEL",
nodes = nodeNames,
edgemode="directed"
)
### LINES
# Draw the "lines" or "branches" of the probability Tree
rEG <- addEdge (nodeNames[1], nodeNames[2], rEG, 1)
rEG <- addEdge (nodeNames[1], nodeNames[3], rEG, 1)
rEG <- addEdge (nodeNames[1], nodeNames[4], rEG, 1)
rEG <- addEdge (nodeNames[2], nodeNames[5], rEG, 1)
rEG <- addEdge (nodeNames[2], nodeNames[6], rEG, 1)
rEG <- addEdge (nodeNames[3], nodeNames[7], rEG, 1)
rEG <- addEdge (nodeNames[3], nodeNames[8], rEG, 1)
rEG <- addEdge (nodeNames[4], nodeNames[9], rEG, 1)
rEG <- addEdge (nodeNames[4], nodeNames[10], rEG, 10)
eAttrs <- list()
q <- edgeNames(rEG)
### PROBABILITY VALUES
# Add the probability values to the the branch lines
eAttrs$label <- c(toString(a), toString(b),
toString(c), toString(fa),
toString(notfa), toString(fb),
toString(notfb), toString(fc),
toString(notfc)
)
names(eAttrs$label) <- c( q[1], q[2], q[3], q[4], q[5], q[6], q[7], q[8], q[9])
edgeAttrs <- eAttrs
### COLOR
# Set the color, etc, of the tree
attributes <- list(node = list(label = "foo",
fillcolor = "lightgreen",
fontsize = "15"
),
edge = list(color = "red"),
graph = list(rankdir = "LR")
)
### PLOT
# Plot the probability tree using Rgraphvis
plot (rEG, edgeAttrs = eAttrs, attrs=attributes)
### PROBABILITY (labels)
text(578,390, aANDfa, cex = .8)
text(578,240, bANDfb, cex = .8)
text(578,100, cANDfc, cex = .8)
text(360,233, 'P(F|B):', cex = 1.1)
text(160,50, paste('P(B):', b), cex = 1.1)
text(160,30, paste('P(F):', aANDfa+bANDfb+cANDfc), cex = 1.1)
text(160,10, paste('P(B|F):', fb*b/(aANDfa+bANDfb+cANDfc)), cex = 1.1)