Queer European MD passionate about IT

Mission409Solutions.Rmd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ---
  2. title: "Probability Fundamentals in R: Guided Project Solutions"
  3. output: html_document
  4. ---
  5. # Developing A Mobile App For Alleviating Lottery Addiction
  6. This RMarkdown file is intended to lay out the logic of a mobile app designed for those addicted to the lottery. By showing a user how to calculate the incredibly small probabilities of winning the lottery, we hope that the app will help them better grasp that buying multiple lottery tickets will do little to help them win. Through this understanding, they will hopefully stop purchasing lottery tickets in an unhealthy manner.
  7. # Core Functions
  8. ```{r}
  9. factorial <- function(n) {
  10. product = 1
  11. for (i in 1:n) {
  12. product = product * i
  13. }
  14. return(product)
  15. }
  16. combinations <- function(n, k) {
  17. numerator <- factorial(n)
  18. denominator <- factorial(k) * factorial(n - k)
  19. return(numerator / denominator)
  20. }
  21. ```
  22. # One-Ticket Probability
  23. ```{r}
  24. one_ticket_probability <- function(nums) {
  25. total_combinations <- combinations(49, 6)
  26. prob <- (1 / total_combinations) * 100
  27. pretty_prob <- sprintf("%1.9f", prob)
  28. s <- paste("You have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
  29. return(s)
  30. }
  31. one_ticket_probability(c(1, 2, 3, 4, 5, 6))
  32. ```
  33. # Historical Data Check for Canada Lottery
  34. ```{r, message = FALSE, warning = FALSE}
  35. library(tidyverse)
  36. lottery649 <- read_csv("649.csv")
  37. print(dim(lottery649))
  38. ```
  39. ```{r}
  40. head(lottery649, 3)
  41. ```
  42. ```{r}
  43. tail(lottery649, 3)
  44. ```
  45. # A New Data Structure
  46. ```{r}
  47. data1 <- c(1, 3, 5)
  48. data2 <- c(2, 4, 6)
  49. data3 <- c(8, 9, 7)
  50. ## Answer
  51. unnamed_list <- list(data1, data2, data3)
  52. first_vector <- unnamed_list[[1]]
  53. named_list <-list(first = data1, second = data2, third = data3)
  54. first_item_sum <- named_list$data1[1] + named_list$data2[1] + named_list$data3[1]
  55. ```
  56. # Using pmap
  57. ```{r}
  58. data1 <- c(1, 3, 5)
  59. data2 <- c(2, 4, 6)
  60. data3 <- c(8, 9, 7)
  61. data_list <- list(data1, data2, data3)
  62. ## Answer
  63. averages <- pmap(data_list, function(x, y, z) { (x + y + z) / 3 })
  64. first_average <- unlist(averages)[1]
  65. ```
  66. # Function for Historical Data Check
  67. ```{r}
  68. historical_lots <- pmap(
  69. list(
  70. u <- lottery649$`NUMBER DRAWN 1`,
  71. v <- lottery649$`NUMBER DRAWN 2`,
  72. w <- lottery649$`NUMBER DRAWN 3`,
  73. x <- lottery649$`NUMBER DRAWN 4`,
  74. y <- lottery649$`NUMBER DRAWN 5`,
  75. z <- lottery649$`NUMBER DRAWN 6`
  76. ),
  77. .f <- function(u, v, w, x, y, z) { c(u, v, w, x, y, z) }
  78. )
  79. ```
  80. ```{r}
  81. library(sets)
  82. check_historical_occurrences <- function(lot, hist_lots = historical_lots) {
  83. historical_matches <- map(hist_lots, function(x) {setequal(x, lot)})
  84. num_past_matches <- sum(unlist(historical_matches))
  85. s <- paste("The combination you entered has appeared ",
  86. num_past_matches,
  87. " times in the past. ",
  88. "Your chance of winning the big prize in the next drawing using this combination is 0.0000072%", sep = "")
  89. return(s)
  90. }
  91. check_historical_occurrences(c(3, 12, 11, 14, 41, 43))
  92. check_historical_occurrences(c(1, 2, 3, 4, 5, 6))
  93. ```
  94. # Multi-ticket Probability
  95. ```{r}
  96. multi_ticket_probability <- function(n) {
  97. total_combinations <- combinations(49, 6)
  98. prob <- (n / total_combinations) * 100
  99. pretty_prob <- sprintf("%1.9f", prob)
  100. s <- paste("you have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
  101. return(s)
  102. }
  103. ```
  104. ```{r}
  105. test_amounts <- c(1, 10, 100, 10000, 1000000, 6991908, 13983816)
  106. for (n in test_amounts) {
  107. print(paste("For ", n, " tickets, ", multi_ticket_probability(n), sep = ""))
  108. }
  109. ```
  110. # Less Winning Numbers
  111. ```{r}
  112. probability_less_6 <- function(n) {
  113. n_combinations_ticket = combinations(6, n)
  114. n_combinations_remaining = combinations(43, 6 - n)
  115. successful_outcomes = n_combinations_ticket * n_combinations_remaining
  116. n_combinations_total = combinations(49, 6)
  117. prob = (successful_outcomes / n_combinations_total) * 100
  118. pretty_prob <- sprintf("%1.9f", prob)
  119. s <- paste("you have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
  120. return(s)
  121. }
  122. ```
  123. ```{r}
  124. winning_nums <- c(3, 4, 5)
  125. for (n in winning_nums) {
  126. print(paste("For ", n, " tickets, ", probability_less_6(n), sep = ""))
  127. }
  128. ```