Five Nation Game
Purpose
Want to simulate and find the answer to the 5 nation problem mentioned in chapter 3 of the book.
> library(plyr) > x <- as.data.frame(matrix(data = NA, nrow = 10, ncol = 2)) > games <- c("AB", "AC", "AD", "AE", "BC", "BD", "BE", "CD", "CE", + "DE") > x[, 2] <- games > FiveNationResult <- function() { + res <- sample(c("10", "01"), 10, replace = T) + x[, 1] <- res + y <- as.data.frame(cbind(c(substr(x$V1, 1, 1), substr(x$V1, + 2, 2)), c(substr(x$V2, 1, 1), substr(x$V2, 2, 2)))) + y$score <- as.numeric(y[, 1]) - 1 + colnames(y) <- c("code", "player", "score") + score_count <- ddply(y, .(player), summarise, score_count = sum(score))$score_count + result <- identical(score_count, rep(2, 5)) + return(result) + } > N <- 5000 > set.seed(2011) > counter <- numeric(N) > for (i in 1:N) { + result <- FiveNationResult() + counter[i] <- result + } > print(sum(counter)/N) [1] 0.0232 |
I rechecked the calculation and got it right…The actual prob is
0.0234375 The simulated result is pretty close to the closed form solution mentioned in williams