-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monte Carlo Sim R32.R
564 lines (468 loc) · 13.7 KB
/
Monte Carlo Sim R32.R
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
###################################
### Monte Carlo Simulation ####
###################################
## Run before Round 2 games
# This script will generate probabilities for each team to advance to each round of the NCAA Tourney.
###########################
## Set working directory ##
###########################
##############################
## Load necessary libraries ## --------------------------------------------------------------------------
##############################
library("here")
library("PlayerRatings")
library("knitr")
library("tidyverse")
library("tictoc")
##############################
## Load necessary Datafiles ## --------------------------------------------------------------------------
##############################
#####################################
##### #####
##### Read and Process the data ##### ---------------------------------------------------------------
##### #####
#####################################
# gets seeding info
seeds <- read_csv('Data/MNCAATourneySeeds.csv') %>% # data from https://www.kaggle.com/c/mens-march-mania-2022/data
filter(Season == 2022) %>%
select(TeamID, Season, Seed) %>%
mutate(
seed_n = str_sub(Seed, 2, -1),
seed_playin = str_sub(Seed, 4),
seed_n = as.numeric(str_replace_all(seed_n, "[a-z]", "")),
seed_region = str_sub(Seed, 1, 1),
Seed = str_sub(Seed, 1, 3)
)
# gets team info
teams <- read_csv('Data/MTeams.csv') %>% # data from https://www.kaggle.com/c/mens-march-mania-2022/data
select(TeamID, TeamName) %>%
inner_join(seeds, by = "TeamID") %>%
select(TeamID, TeamName, Seed)
# gets the predicted results for previous project
MarchMadness <- read_csv('Data/MarchMadness2022.csv') # data from A.I. Sports and https://www.kaggle.com/c/mens-march-mania-2022/data
#############################
##### #####
##### SET EVERYTHING UP ##### ---------------------------------------------------------------------------
##### #####
#############################
set.seed(1234)
simulation.results <- c()
# Set number of simulations at 15,000
num_sims = 2000
i = 1
###################################
##### #####
##### ALL NECESSARY FUNCTIONS ##### ---------------------------------------------------------------------
##### #####
###################################
## simulate play in game
simulate.playin.game <- function(team1, team2){
if(team1 > team2){
tmp <- team1
team1 <- team2
team2 <- tmp
}
# Extract Probabilities for each team in the matchup
p.1.2 <- MarchMadness %>%
filter(Team1 == team1,
Team2 == team2) %>%
pull(pred)
p.2.1 <- 1 - p.1.2
# simulate Game
game.result <- sample(c(team1, team2), size = 1, prob = c(p.1.2, p.2.1), replace=TRUE)
if (game.result == team1){
loser <- team2
} else {
loser <- team1
}
# return the winner
loser
}
## simulate regular game
simulate.game <- function(team1seed, team2seed){
team1 <- tourney.seeds %>%
filter(Seed == team1seed) %>%
pull(TeamID)
team2 <- tourney.seeds %>%
filter(Seed == team2seed) %>%
pull(TeamID)
if(team1 > team2){
tmp <- team1
team1 <- team2
team2 <- tmp
}
# Extract Probabilities for each team in the matchup
p.1.2 <- MarchMadness %>%
filter(Team1 == team1,
Team2 == team2) %>%
pull(pred)
p.2.1 <- 1 - p.1.2
# simulate Game
game.result <- sample(c(team1, team2), size = 1, prob = c(p.1.2, p.2.1), replace=TRUE)
if (game.result == team1){
winner <- tourney.seeds %>%
filter(TeamID == team1) %>%
pull("Seed")
} else {
winner <- tourney.seeds %>%
filter(TeamID == team2) %>%
pull("Seed")
}
# return the winner
winner
}
## chance.df
chance.df <- function(series){
tbl <- table(sim.results.df[ , series])
df <- data.frame(team = names(tbl), chance = as.numeric(tbl)/sum(tbl))
df <- df[order(df$chance, decreasing=TRUE), ]
df
}
###############################
##### #####
##### Run the Simulations ##### ------------------------------------------------------------------------
##### #####
###############################
tictoc::tic()
while (i <= num_sims) {
tourney.seeds <- seeds
play.teams <- seeds %>% filter(seed_playin == "a" | seed_playin == "b")
play.seeds <- unique(play.teams$Seed)
# play in games
for(seeding in play.seeds) {
play.1.2 <- play.teams %>% filter(Seed == seeding) %>% select(TeamID)
team1 <- play.1.2$TeamID[1]
team2 <- play.1.2$TeamID[2]
loser <- simulate.playin.game(team1, team2)
tourney.seeds <- tourney.seeds %>%
filter(TeamID != loser)
}
##### East games Round 1
# Top Round of 64
R32.1.w <- "W01"
R32.2.w <- "W08"
R32.3.w <- "W05"
R32.4.w <- "W04"
# Bottom Round of 64
R32.5.w <- "W06"
R32.6.w <- "W03"
R32.7.w <- "W07"
R32.8.w <- "W15"
# Round of 32
S16.1.w <- simulate.game(R32.1.w, R32.2.w)
S16.2.w <- simulate.game(R32.3.w, R32.4.w)
S16.3.w <- simulate.game(R32.5.w, R32.6.w)
S16.4.w <- simulate.game(R32.7.w, R32.8.w)
# Sweet 16
E8.1.w <- simulate.game(S16.1.w, S16.2.w)
E8.2.w <- simulate.game(S16.3.w, S16.4.w)
# Elite 8
F4.w <- simulate.game(E8.1.w, E8.2.w)
##### WEST games Round 1
# Top Round of 64
R32.1.x <- "X01"
R32.2.x <- "X09"
R32.3.x <- "X12"
R32.4.x <- "X04"
# Bottom Round of 64
R32.5.x <- "X11"
R32.6.x <- "X03"
R32.7.x <- "X07"
R32.8.x <- "X02"
# Round of 32
S16.1.x <- simulate.game(R32.1.x, R32.2.x)
S16.2.x <- simulate.game(R32.3.x, R32.4.x)
S16.3.x <- simulate.game(R32.5.x, R32.6.x)
S16.4.x <- simulate.game(R32.7.x, R32.8.x)
# Sxeet 16
E8.1.x <- simulate.game(S16.1.x, S16.2.x)
E8.2.x <- simulate.game(S16.3.x, S16.4.x)
# Elite 8
F4.x <- simulate.game(E8.1.x, E8.2.x)
##### MIDWEST games Round 1
# Top Round of 64
R32.1.y <- "Y01"
R32.2.y <- "Y09"
R32.3.y <- "Y12"
R32.4.y <- "Y04"
# Bottom Round of 64
R32.5.y <- "Y11"
R32.6.y <- "Y03"
R32.7.y <- "Y10"
R32.8.y <- "Y02"
# Round of 32
S16.1.y <- simulate.game(R32.1.y, R32.2.y)
S16.2.y <- simulate.game(R32.3.y, R32.4.y)
S16.3.y <- simulate.game(R32.5.y, R32.6.y)
S16.4.y <- simulate.game(R32.7.y, R32.8.y)
# Syeet 16
E8.1.y <- simulate.game(S16.1.y, S16.2.y)
E8.2.y <- simulate.game(S16.3.y, S16.4.y)
# Elite 8
F4.y <- simulate.game(E8.1.y, E8.2.y)
##### Z games Round 1
# Top Round of 64
R32.1.z <- "Z01"
R32.2.z <- "Z09"
R32.3.z <- "Z05"
R32.4.z <- "Z04"
# Bottom Round of 64
R32.5.z <- "Z11"
R32.6.z <- "Z03"
R32.7.z <- "Z07"
R32.8.z <- "Z02"
# Round of 32
S16.1.z <- simulate.game(R32.1.z, R32.2.z)
S16.2.z <- simulate.game(R32.3.z, R32.4.z)
S16.3.z <- simulate.game(R32.5.z, R32.6.z)
S16.4.z <- simulate.game(R32.7.z, R32.8.z)
# Szeet 16
E8.1.z <- simulate.game(S16.1.z, S16.2.z)
E8.2.z <- simulate.game(S16.3.z, S16.4.z)
# Elite 8
F4.z <- simulate.game(E8.1.z, E8.2.z)
## Final Four!!! Game Time Baby!!!
# Semi Finals
F4.1 <- simulate.game(F4.w, F4.x)
F4.2 <- simulate.game(F4.y, F4.z)
# Championship Game
Champ.1 <- simulate.game(F4.1, F4.2)
#print(paste0("This is the winner ",Champ.1))
results.all <- c(
i, R32.1.w,
R32.2.w,
R32.3.w,
R32.4.w,
R32.5.w,
R32.6.w,
R32.7.w,
R32.8.w,
R32.1.x,
R32.2.x,
R32.3.x,
R32.4.x,
R32.5.x,
R32.6.x,
R32.7.x,
R32.8.x,
R32.1.y,
R32.2.y,
R32.3.y,
R32.4.y,
R32.5.y,
R32.6.y,
R32.7.y,
R32.8.y,
R32.1.z,
R32.2.z,
R32.3.z,
R32.4.z,
R32.5.z,
R32.6.z,
R32.7.z,
R32.8.z,
S16.1.w,
S16.2.w,
S16.3.w,
S16.4.w,
S16.1.x,
S16.2.x,
S16.3.x,
S16.4.x,
S16.1.y,
S16.2.y,
S16.3.y,
S16.4.y,
S16.1.z,
S16.2.z,
S16.3.z,
S16.4.z,
E8.1.w,
E8.2.w,
E8.1.x,
E8.2.x,
E8.1.y,
E8.2.y,
E8.1.z,
E8.2.z,
F4.w, F4.x, F4.y, F4.z,
F4.1, F4.2, Champ.1
)
simulation.results <- c(simulation.results, results.all)
if( i %% 100 == 0 ) cat(paste("Simulation", i, "complete\n"))
i <- i + 1
}
tictoc::toc() # 5796.496 sec elapsed for 5,000 sims
# Results
sim.results.mat <- matrix(simulation.results, ncol=64, byrow=TRUE)
sim.results.df <- as.data.frame(sim.results.mat)
names(sim.results.df) <- c(
"sim", "R32.1.w",
"R32.2.w",
"R32.3.w",
"R32.4.w",
"R32.5.w",
"R32.6.w",
"R32.7.w",
"R32.8.w",
"R32.1.x",
"R32.2.x",
"R32.3.x",
"R32.4.x",
"R32.5.x",
"R32.6.x",
"R32.7.x",
"R32.8.x",
"R32.1.y",
"R32.2.y",
"R32.3.y",
"R32.4.y",
"R32.5.y",
"R32.6.y",
"R32.7.y",
"R32.8.y",
"R32.1.z",
"R32.2.z",
"R32.3.z",
"R32.4.z",
"R32.5.z",
"R32.6.z",
"R32.7.z",
"R32.8.z",
"S16.1.w",
"S16.2.w",
"S16.3.w",
"S16.4.w",
"S16.1.x",
"S16.2.x",
"S16.3.x",
"S16.4.x",
"S16.1.y",
"S16.2.y",
"S16.3.y",
"S16.4.y",
"S16.1.z",
"S16.2.z",
"S16.3.z",
"S16.4.z",
"E8.1.w",
"E8.2.w",
"E8.1.x",
"E8.2.x",
"E8.1.y",
"E8.2.y",
"E8.1.z",
"E8.2.z",
"F4.w", "F4.x", "F4.y", "F4.z",
"F4.1", "F4.2", "Champ.1"
)
#################################################
##### #####
##### Create a table with all probabilities ##### -----------------------------------------------------------------------------------------------
##### #####
#################################################
# NCAA Champions
champs.df <- chance.df("Champ.1")
# Semi Finals Champions
SF1.df <- chance.df("F4.1")
SF2.df <- chance.df("F4.2")
finals <- rbind(SF1.df, SF2.df)
# Final 4
w.1.df <- chance.df("F4.w")
x.1.df <- chance.df("F4.x")
y.1.df <- chance.df("F4.y")
z.1.df <- chance.df("F4.z")
Final4 <- rbind(w.1.df, x.1.df, y.1.df, z.1.df)
# Elite 8
E8w.1.df <- chance.df("E8.1.w")
E8w.2.df <- chance.df("E8.2.w")
E8x.1.df <- chance.df("E8.1.x")
E8x.2.df <- chance.df("E8.2.x")
E8y.1.df <- chance.df("E8.1.y")
E8y.2.df <- chance.df("E8.2.y")
E8z.1.df <- chance.df("E8.1.z")
E8z.2.df <- chance.df("E8.2.z")
Elite8 <- rbind(E8w.1.df, E8w.2.df, E8x.1.df, E8x.2.df, E8y.1.df, E8y.2.df, E8z.1.df, E8z.2.df)
# Sweet 16
S16w.1.df <- chance.df("S16.1.w")
S16w.2.df <- chance.df("S16.2.w")
S16w.3.df <- chance.df("S16.3.w")
S16w.4.df <- chance.df("S16.4.w")
S16x.1.df <- chance.df("S16.1.x")
S16x.2.df <- chance.df("S16.2.x")
S16x.3.df <- chance.df("S16.3.x")
S16x.4.df <- chance.df("S16.4.x")
S16y.1.df <- chance.df("S16.1.y")
S16y.2.df <- chance.df("S16.2.y")
S16y.3.df <- chance.df("S16.3.y")
S16y.4.df <- chance.df("S16.4.y")
S16z.1.df <- chance.df("S16.1.z")
S16z.2.df <- chance.df("S16.2.z")
S16z.3.df <- chance.df("S16.3.z")
S16z.4.df <- chance.df("S16.4.z")
Sweet16 <- rbind(S16w.1.df, S16w.2.df, S16w.3.df, S16w.4.df, S16x.1.df, S16x.2.df, S16x.3.df, S16x.4.df, S16y.1.df, S16y.2.df, S16y.3.df, S16y.4.df, S16z.1.df, S16z.2.df, S16z.3.df, S16z.4.df)
# Round of 32
R32w.1.df <- chance.df("R32.1.w")
R32w.2.df <- chance.df("R32.2.w")
R32w.3.df <- chance.df("R32.3.w")
R32w.4.df <- chance.df("R32.4.w")
R32w.5.df <- chance.df("R32.5.w")
R32w.6.df <- chance.df("R32.6.w")
R32w.7.df <- chance.df("R32.7.w")
R32w.8.df <- chance.df("R32.8.w")
R32x.1.df <- chance.df("R32.1.x")
R32x.2.df <- chance.df("R32.2.x")
R32x.3.df <- chance.df("R32.3.x")
R32x.4.df <- chance.df("R32.4.x")
R32x.5.df <- chance.df("R32.5.x")
R32x.6.df <- chance.df("R32.6.x")
R32x.7.df <- chance.df("R32.7.x")
R32x.8.df <- chance.df("R32.8.x")
R32y.1.df <- chance.df("R32.1.y")
R32y.2.df <- chance.df("R32.2.y")
R32y.3.df <- chance.df("R32.3.y")
R32y.4.df <- chance.df("R32.4.y")
R32y.5.df <- chance.df("R32.5.y")
R32y.6.df <- chance.df("R32.6.y")
R32y.7.df <- chance.df("R32.7.y")
R32y.8.df <- chance.df("R32.8.y")
R32z.1.df <- chance.df("R32.1.z")
R32z.2.df <- chance.df("R32.2.z")
R32z.3.df <- chance.df("R32.3.z")
R32z.4.df <- chance.df("R32.4.z")
R32z.5.df <- chance.df("R32.5.z")
R32z.6.df <- chance.df("R32.6.z")
R32z.7.df <- chance.df("R32.7.z")
R32z.8.df <- chance.df("R32.8.z")
Round32 <- rbind(R32w.1.df, R32w.2.df, R32w.3.df, R32w.4.df, R32w.5.df, R32w.6.df, R32w.7.df, R32w.8.df,
R32x.1.df, R32x.2.df, R32x.3.df, R32x.4.df, R32x.5.df, R32x.6.df, R32x.7.df, R32x.8.df,
R32y.1.df, R32y.2.df, R32y.3.df, R32y.4.df, R32y.5.df, R32y.6.df, R32y.7.df, R32y.8.df,
R32z.1.df, R32z.2.df, R32z.3.df, R32z.4.df, R32z.5.df, R32z.6.df, R32z.7.df, R32z.8.df)
# Merge all probabilities
all.chances.df <- merge(Round32, Sweet16, by="team")
names(all.chances.df) <- c("team", "Round32", "Sweet16")
all.chances.df %<>% left_join(Elite8, by = "team") %>%
rename(Elite8 = chance) %>%
left_join(Final4, by = "team") %>%
rename(Final4 = chance) %>%
left_join(finals, by = "team") %>%
rename(Finals = chance) %>%
left_join(champs.df, by = "team") %>%
rename(Champs = chance) %>%
arrange(desc(Champs), desc(Finals), desc(Final4), desc(Elite8), desc(Sweet16), desc(Round32))
# Fix percentages
all.chances.df$Sweet16 <- ifelse(is.na(all.chances.df$Sweet16), 0, all.chances.df$Sweet16)
all.chances.df$Elite8 <- ifelse(is.na(all.chances.df$Elite8), 0, all.chances.df$Elite8)
all.chances.df$Final4 <- ifelse(is.na(all.chances.df$Final4), 0, all.chances.df$Final4)
all.chances.df$Finals <- ifelse(is.na(all.chances.df$Finals), 0, all.chances.df$Finals)
all.chances.df$Champs <- ifelse(is.na(all.chances.df$Champs), 0, all.chances.df$Champs)
all.chances.df[,2:7] <- sapply(all.chances.df[,2:7], scales::percent)
# get team names
all.chances.df %<>%
left_join(teams, by = c("team" = "Seed")) %>%
select(TeamName, everything(), -team, -TeamID)
# View results
kable(all.chances.df)
# Write to a file
output_filename <- "Data/MarchMadness_probs_2022r32.csv"
write_csv(all.chances.df, output_filename)