Skip to content

Instantly share code, notes, and snippets.

@PoisonAlien
Last active April 17, 2020 08:29
Show Gist options
  • Save PoisonAlien/967fc9c636b556b3757b56043e9ae551 to your computer and use it in GitHub Desktop.
Save PoisonAlien/967fc9c636b556b3757b56043e9ae551 to your computer and use it in GitHub Desktop.
Answers in R to frequently discussed statistical questions. Mostly derived from Coursera `An Intuitive Introduction to Probability`
#In a room of n people what is the probability of someone having a bday today
#This can be solved by 1 - prob of someone not havng bday today
# prob of someone not havng bday today = 364/365
# for n people this probabilty is same everyone. Hence these probability gets multiplied.
# 1 - above probability would the answer
#For n=253, thes probability reaches 50%
birthday_today = function(n){
1 - ((364/365)^n)
}
#In a room of n people, prob of two (or more) people having a same bday
#If some one has a bday then other person having a different bday is 364/365
#For the next person it would be 363/365
#For the third person it would be 362/364 and so on..
#Tese probs gets multiplied which gives us the total prob of of n people having different bday
# 1-above probability would the answer
# For n=23 prob of two people in a room of 23 people having same bday reaches 50%
same_bday = function(n){
prob = 1 #bday for first person (it can be anyday hence it would be one 365/365)
for(i in 2:n){
prob = prob * ((366-i)/365)
}
1-prob #contains prob of two people having same bday
}
#Above thing can be simulated randomly
birthday_prob = function(n = 23, times = 1000){
sim = lapply(1:times, function(i){
length(which(duplicated(sample(x = 1:365, size = n, replace = TRUE))))
})
hist(unlist(sim))
}
#Monty Hall problem
#As Karl says during the lecture, it's an infamous (or famous) one named after geme show host Monty Hall
#Lets simmulate
monty_hall= function(user_switches = TRUE){
doors = c("D1", "D2", "D3")
outcome = NULL
user_door = sample(doors, size = 1) #Contestsent chooses a door
#message("Contestent choice: ",user_door)
correct_door = sample(doors, size = 1) #Correct door with a car in it
#message("Correct door: ",correct_door)
#Now monty opens a false door (He is not allowed to open the correct one)
monty_opens = doors[!doors %in% c(user_door, correct_door)]
if(length(monty_opens) > 1){
monty_opens = sample(x = monty_opens, size = 1)
}
#message("Monty opens: ",monty_opens)
remaining_doors = doors[!doors %in% monty_opens]
#message("Remaining doors: ", paste(remaining_doors, collapse = ", "))
#user_switches = sample(x = c(TRUE, FALSE), size = 1)
if(user_switches){
user_door = remaining_doors[!remaining_doors %in% user_door]
#message("Contestent wishes to swicth. His new door: ", user_door)
}else{
#message("Contestent does not wishes to swicth. He sticks to ", user_door)
}
if(user_door == correct_door){
outcome = "WON"
}else{
outcome = "LOST"
}
#message("Outcome: ", outcome)
outcome
}
#Lets simulate above game for 1000 times
game_outcomes_when_door_swicthed = c()
for(i in 1:1000){
game_outcomes_when_door_swicthed = c(game_outcomes, monty_hall(user_switches = TRUE))
}
print(prop.table(table(game_outcomes_when_door_swicthed)))
game_outcomes_when_door_not_swicthed = c()
for(i in 1:1000){
game_outcomes_when_door_not_swicthed = c(game_outcomes_when_door_not_swicthed, monty_hall(user_switches = FALSE))
}
print(prop.table(table(game_outcomes_when_door_not_swicthed)))
#It seems ~57% of time contenstent WON the game when he chose to switch the door, whereas he WON only ~32% of the time when did not swicth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment