-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Mission409Solutions.Rmd
162 lines (128 loc) · 4.06 KB
/
Mission409Solutions.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
---
title: "Probability Fundamentals in R: Guided Project Solutions"
output: html_document
---
# Developing A Mobile App For Alleviating Lottery Addiction
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.
# Core Functions
```{r}
factorial <- function(n) {
product = 1
for (i in 1:n) {
product = product * i
}
return(product)
}
combinations <- function(n, k) {
numerator <- factorial(n)
denominator <- factorial(k) * factorial(n - k)
return(numerator / denominator)
}
```
# One-Ticket Probability
```{r}
one_ticket_probability <- function(nums) {
total_combinations <- combinations(49, 6)
prob <- (1 / total_combinations) * 100
pretty_prob <- sprintf("%1.9f", prob)
s <- paste("You have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
return(s)
}
one_ticket_probability(c(1, 2, 3, 4, 5, 6))
```
# Historical Data Check for Canada Lottery
```{r, message = FALSE, warning = FALSE}
library(tidyverse)
lottery649 <- read_csv("649.csv")
print(dim(lottery649))
```
```{r}
head(lottery649, 3)
```
```{r}
tail(lottery649, 3)
```
# A New Data Structure
```{r}
data1 <- c(1, 3, 5)
data2 <- c(2, 4, 6)
data3 <- c(8, 9, 7)
## Answer
unnamed_list <- list(data1, data2, data3)
first_vector <- unnamed_list[[1]]
named_list <-list(first = data1, second = data2, third = data3)
first_item_sum <- named_list$data1[1] + named_list$data2[1] + named_list$data3[1]
```
# Using pmap
```{r}
data1 <- c(1, 3, 5)
data2 <- c(2, 4, 6)
data3 <- c(8, 9, 7)
data_list <- list(data1, data2, data3)
## Answer
averages <- pmap(data_list, function(x, y, z) { (x + y + z) / 3 })
first_average <- unlist(averages)[1]
```
# Function for Historical Data Check
```{r}
historical_lots <- pmap(
list(
u <- lottery649$`NUMBER DRAWN 1`,
v <- lottery649$`NUMBER DRAWN 2`,
w <- lottery649$`NUMBER DRAWN 3`,
x <- lottery649$`NUMBER DRAWN 4`,
y <- lottery649$`NUMBER DRAWN 5`,
z <- lottery649$`NUMBER DRAWN 6`
),
.f <- function(u, v, w, x, y, z) { c(u, v, w, x, y, z) }
)
```
```{r}
library(sets)
check_historical_occurrences <- function(lot, hist_lots = historical_lots) {
historical_matches <- map(hist_lots, function(x) {setequal(x, lot)})
num_past_matches <- sum(unlist(historical_matches))
s <- paste("The combination you entered has appeared ",
num_past_matches,
" times in the past. ",
"Your chance of winning the big prize in the next drawing using this combination is 0.0000072%", sep = "")
return(s)
}
check_historical_occurrences(c(3, 12, 11, 14, 41, 43))
check_historical_occurrences(c(1, 2, 3, 4, 5, 6))
```
# Multi-ticket Probability
```{r}
multi_ticket_probability <- function(n) {
total_combinations <- combinations(49, 6)
prob <- (n / total_combinations) * 100
pretty_prob <- sprintf("%1.9f", prob)
s <- paste("you have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
return(s)
}
```
```{r}
test_amounts <- c(1, 10, 100, 10000, 1000000, 6991908, 13983816)
for (n in test_amounts) {
print(paste("For ", n, " tickets, ", multi_ticket_probability(n), sep = ""))
}
```
# Less Winning Numbers
```{r}
probability_less_6 <- function(n) {
n_combinations_ticket = combinations(6, n)
n_combinations_remaining = combinations(43, 6 - n)
successful_outcomes = n_combinations_ticket * n_combinations_remaining
n_combinations_total = combinations(49, 6)
prob = (successful_outcomes / n_combinations_total) * 100
pretty_prob <- sprintf("%1.9f", prob)
s <- paste("you have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
return(s)
}
```
```{r}
winning_nums <- c(3, 4, 5)
for (n in winning_nums) {
print(paste("For ", n, " tickets, ", probability_less_6(n), sep = ""))
}
```