I. Bayes Theorem : In probability & Statistics, Bayes theorem describes the probability of event, based on prior knowledge of conditions related to event.In simple terms, Bayes’ Theorem helps us adjust the probabilities when we receive new data. It provides a systematic way to reason and update our situation.
Formula : P(A|B) = P(B/A)P(A) / P(B)
For example, let’s say there is a medical test for a rare disease that is 99% accurate. If the prevalence of the disease is 1 in 1000 in the population, and a person tests positive, Bayes’ Theorem can help us calculate the probability of that person actually having the disease. By incorporating the accuracy of the test and the prevalence of the disease, Bayes’ Theorem allows us to make a more informed judgment about the likelihood of the person being affected.
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
#Sport event (A1) :
A1 = 20
#Academic event (A2) :
A2 = 35
#No events (A3) :
A3 = 45
#Find Probability of A1, A2, A3
#Probability of A1 :
PA1 = A1/100
print(PA1)
## [1] 0.2
nota1 = 1-PA1
#Probability of A2 :
PA2 = A2/100
print(PA2)
## [1] 0.35
nota2 = 1-PA2
#Probability of A3 :
PA3 = A3/100
print(PA3)
## [1] 0.45
nota3 = 1-PA3
#Find the probability of garage full for A1, A2, A3 :
b1 = 70
b2 = 25
b3 = 5
#Probability of (B/A1) :
B1A1 = b1/100
print(B1A1)
## [1] 0.7
#Probability of (B/A2) :
B2A2 = b2/100
print(B2A2)
## [1] 0.25
#Probability of (B/A3) :
B3A3 = b3/100
print(B3A3)
## [1] 0.05
#Not functions of B1A1, B2A2, B3A3 :
notb1a1 = 1-B1A1
notb2a2 = 1-B2A2
notb3a3 = 1-B3A3
#Find the probability with Bayes theorem
#The formula is P(A1/B1) = P(B1A1)*P(A1) / P(B1A1)*P(A1)+P(B2A2)*P(A2)+P(B3A3)*P(A3)
#After the apply all the values in the formula :
aAndb = B1A1*PA1
aAndnotb = notb1a1*PA1
a2Andb2 = B2A2*PA2
a2Andnotb2 = notb2a2*PA2
a3Andb3 = B3A3*PA3
a3Andnotb3 = notb3a3*PA3
Top = (B1A1) * (PA1)
print(Top)
## [1] 0.14
Bot1 = B2A2 * PA2
print(Bot1)
## [1] 0.0875
Bot2 = B3A3 * PA3
print(Bot2)
## [1] 0.0225
plus = Top + Bot1 + Bot2
print(plus)
## [1] 0.25
PA1B1 = Top / plus
print(PA1B1)
## [1] 0.56
# (0.7)(0.2) / (0.7)(0.2)(0.25)(0.35)+(0.05)(0.45) = 0.140 / .25 = 0.56.
# The probability of sporting event is 0.56.
# Creating Tree Diagram :
node1 = "P"
node2 = "A1"
node3 = "A2"
node4 = "A3"
node5 = "A1&B"
node6 = "A1&B'"
node7 = "A2&B"
node8 = "A2&B'"
node9 = "A3&B"
node10 = "A3&B'"
nodeNames = c(node1,node2,node3,node4, node5,node6, node7, node8, node9, node10)
rEG <- new("graphNEL", nodes=nodeNames, edgemode="directed")
# 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)
# Add the probability values to the the branch lines
eAttrs$label <- c(toString(A1),toString(A2),
toString(A3), toString(B1A1),
toString(notb1a1), toString(B2A2), toString(notb2a2), toString(B3A3), toString(notb3a3))
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 of tree
attributes<-list(node=list(label="foo", fillcolor="lightgreen", fontsize="15"),
edge=list(color="red"),graph=list(rankdir="LR"))
#Plot
plot(rEG, edgeAttrs=eAttrs, attrs=attributes)
nodes(rEG)
## [1] "P" "A1" "A2" "A3" "A1&B" "A1&B'" "A2&B" "A2&B'" "A3&B"
## [10] "A3&B'"
edges(rEG)
## $P
## [1] "A1" "A2" "A3"
##
## $A1
## [1] "A1&B" "A1&B'"
##
## $A2
## [1] "A2&B" "A2&B'"
##
## $A3
## [1] "A3&B" "A3&B'"
##
## $`A1&B`
## character(0)
##
## $`A1&B'`
## character(0)
##
## $`A2&B`
## character(0)
##
## $`A2&B'`
## character(0)
##
## $`A3&B`
## character(0)
##
## $`A3&B'`
## character(0)
text(570,390,aAndb, cex=.8)
text(570,320,aAndnotb,cex=.8)
text(570,245,a2Andb2,cex=.8)
text(570,170,a2Andnotb2,cex=.8)
text(570,95,a3Andb3,cex=.8)
text(570,30,a3Andnotb3,cex=.8)
text(350,385,"(B1 | A1)",cex=.8)
text(350,255,"(B2 | A2)",cex=.8)
text(350,120,"(B3 | A3)",cex=.8)
#Write a table in the lower left of the probablites of A and B
text(50,50,paste("P(A):",PA1),cex=.9, col="darkgreen")
text(50,20,paste("P(A'):",nota1),cex=.9, col="darkgreen")
text(110,50,paste("P(B):",round(PA2,digits=2)),cex=.9, col="darkgreen")
text(110,20,paste("P(B'):",round(nota2, 2)),cex=.9, col="darkgreen")
text(175,50,paste("P(C):",round(PA3,digits=2)),cex=.9, col="darkgreen")
text(175,20,paste("P(C'):",round(nota3, 2)),cex=.9, col="darkgreen")
text(80,420,paste("P(A|B): ",round(PA1B1,digits=2)),cex=.9,col="blue")