#Event
#Things that can happen when something happens by chance
#creating an urn :
#1 : We use the rep function to create an urn
beads <- rep(c("red", "blue"), times = c(2,3))
beads
## [1] "red" "red" "blue" "blue" "blue"
# we use sample() function to pick one at random
sample(beads,1)
## [1] "blue"
#sample With replacement
#replicate() function
#we use replicate function to repeat any experiment any number of times
B <- 10000
events <- replicate(B,sample(beads,1))
table(events)
## events
## blue red
## 5936 4064
prop.table(table(events))
## events
## blue red
## 0.5936 0.4064
#We don't need to use replicate function because the function sample()
#has an argument that permits us to pick more than one element from urn. However, this action occurs without replacement, by deafult i.e. after a bead is selected it is not put back in the urn.
#Sample with Replacement
events<-sample(beads, B, replace = TRUE)
prop.table(table(events))
## events
## blue red
## 0.6004 0.3996
#Independent Events
#2 events are independent events if the outcome of one does not affect the outcome of other. The classic example are coin tosses. Every time, we toss a fair coin, the probability of seeing heads is 0.5 regardless of what previous tosses have revealed.
#The same is true when we pick the beads from an urn with replacement.
#Dependent Events
#Many examples of events that are not independent comes from card Games.
#Let the Events
#A : getting a king from a deck of 52 cards
#B : second drawn card is a king given that the first was a king
#P(A) = 4/52 = 1/13
#P(B/A) = 3/51 = 1/17
#Multiplication Rule : BLACKJACK
#P(A and B) = P(A) * P(B/A)
#P(A and B and C) = P(A) * P(B/A) * P(C/A and B)
#A : getting an ace
#B : getting a face card or a 10
#P(A and B) = P(A) * P(B/A) = 4/52 * 16/51 = 16/663 = 0.02 approx.
#Multiplication rule for independent events
#P(A and B and C) = P(A) * P(B) * P(C)
#One ball will be drawn at random from a box containing: 3 cyan balls, 5 magenta balls, and 7 yellow balls.
#What is the probability that the ball will be cyan?
#Ans : 3/15 = 1/5
cyan <- 3
magneta <- 5
yellow<-7
p_1 = cyan/(cyan + magneta + yellow)
p_1
## [1] 0.2
#One ball will be drawn at random from a box containing: 3 cyan balls, 5 magenta balls, and 7 yellow balls.
#What is the probability that the ball will not be cyan?
#Ans : 12/15 = 4/5
p_2 = 1 - p_1
p_2
## [1] 0.8
#Instead of taking just one draw, consider taking two draws. You take the second draw without returning the first draw to the box. We call this sampling without replacement.
#What is the probability that the first draw is cyan and that the second draw is not cyan?
#Ans : Let events :
#A : the first draw is cyan
#B : the second draw is not cyan
#P(A) = 3/15 = 1/5
#P(B/A) = 12/14 = 6 / 7
#P(A and B ) = P(A) * P(B/A) = 1/5 * 6/7 = 6/35
cyan <- 3
magenta <- 5
yellow <- 7
# The variable `p_1` is the probability of choosing a cyan ball from the box on the first draw.
p_1 <- cyan / (cyan + magenta + yellow)
# Assign a variable `p_2` as the probability of not choosing a cyan ball on the second draw without replacement.
p_2 <- 1 - (cyan - 1) / (cyan + magenta + yellow - 1)
# Calculate the probability that the first draw is cyan and the second draw is not cyan.
p_1 * p_2
## [1] 0.1714286
#Now repeat the experiment, but this time, after taking the first draw and recording the color, return it back to the box and shake the box. We call this sampling with replacement.
#What is the probability that the first draw is cyan and that the second draw is not cyan?
#Ans : Let Events :
#A : first draw is cyan
#B : the second draw is not cyan
#P(A) = 1/5
#P(B) = 4/5
#P(A and B) = P(A) * P(B) = 1/5 * 4/5 = 4/25
cyan <- 3
magenta <- 5
yellow <- 7
# The variable 'p_1' is the probability of choosing a cyan ball from the box on the first draw.
p_1 <- cyan / (cyan + magenta + yellow)
# Assign a variable 'p_2' as the probability of not choosing a cyan ball on the second draw with replacement.
p_2 <- 1 - (cyan) / (cyan + magenta + yellow)
# Calculate the probability that the first draw is cyan and the second draw is not cyan using `p_1` and `p_2`.
p_1 * p_2
## [1] 0.16