Last active
May 7, 2016 18:37
-
-
Save ericnovik/d76864f4f0c4d8bca20350c243088b6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example 2.2.7 (A girl born in winter). Page 46. | |
# A family has two children. Find the probability that both children are girls, | |
# given that at least one of the two is a girl who was born in winter. Assume | |
# that the four seasons are equally likely and that gender is independent of season. | |
# Blitzstein, Joseph K.; Hwang, Jessica (2014-07-24). | |
# Introduction to Probability (Chapman & Hall/CRC Texts in Statistical Science) | |
library(stringr) | |
n <- 1000000 | |
# P(GG | At least one Gw [Gw = Winter Girl]) | |
# w: winter, s:spring, u: summer, f: fall | |
s <- c("Bw", "Bs", "Bu", "Bf", "Gw", "Gs", "Gu", "Gf") | |
ss <- as.character(outer(s, s, FUN = paste0)) | |
pop <- sample(ss, n, replace = TRUE) | |
# P(At least one Gw), which | |
# should be close to 1 - (7/8)^2 | |
sum(str_detect(pop, "Gw")) / n | |
1 - (7/8)^2 | |
# P(GG and At least one Gw) | |
# [`.` stands for any character, `|` means or] | |
# should be close to 1/4 * (1 - (3/4)^2) | |
sum(str_detect(pop, "GwG.|G.Gw")) / n | |
1/4 * (1 - (3/4)^2) | |
# P(GG | At leat one Gw) = P(GG, At least one Gw) / P(At least one Gw) | |
# should be close to 7/15 | |
sum(str_detect(pop, "GwG.|G.Gw")) / sum(str_detect(pop, "Gw")) | |
7/15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment