-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathafl_test_mc_sims.R
131 lines (111 loc) · 2.84 KB
/
afl_test_mc_sims.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
# Run all the prep stuff first
source("test.R")
# Getting 2019 fixture
fixture.2019 <- ScrapeAFLTablesSeasonFixture(2019)
fixture.2019[108, "team.away"] <- "St Kilda"
fixture.2019[108, "ground"] <- "Riverway Stadium"
# Get current ladder
ladder.data <- ScrapeAflTablesLadder(2019)
# Limit fixture to start at next round, need to edit manually for now
fixture.2019 <- ExtractRemainingSeasonFixture(fixture.2019, 17)
print("--- Test MC many full season sims")
print("Team ratings before starting sim")
team.data.run %>%
rownames_to_column("team") %>%
filter(yes.active) %>%
arrange(desc(rating)) %>%
dplyr::select(team, rating) %>%
print()
print(ladder.data)
# Testing MC sims
n.itns <- 5000
print(Sys.time())
print(paste0("Starting MC sims for ", n.itns, " iterations"))
print(paste0("Estimated time = ", n.itns/4/60, " mins"))
tic()
sim.many <-
SimulateFullSeasonEloMany(
2019,
fixture.2019,
n.itns,
team.data.run,
ground.data.run,
ground.location,
team.dictionary,
travel.distance,
elo.params,
rating.time.series.run,
score.params$margin.error.sigma,
score.params$lose.score.mu,
score.params$lose.score.sigma,
ladder.data
)
toc()
print("Iterations complete")
print("--- Postproc of results")
check.team <- "essendon"
plt <-
sim.many$ladder.many %>%
filter(team == check.team) %>%
group_by(ladder.posn) %>%
count() %>%
mutate(p = n/n.itns) %>%
ggplot(aes(ladder.posn, p)) +
geom_col() +
ggtitle(
paste0(
"Probability of end ladder position for ",
team.dictionary.reverse[check.team]
)
) +
xlab("Ladder position") +
ylab("Probability")
print(plt)
n.flags <-
sim.many$finals.many %>%
filter(rnd == "GF", winner == check.team) %>%
count() %>%
pull(n)
p.flag <- n.flags / n.itns
print(
paste0(
"Probability of ",
team.dictionary.reverse[check.team],
" winning the flag = ",
p.flag
)
)
# ladder_posn_prob <-
# sim.many$ladder.many %>%
# filter(team == team_of_interest) %>%
# group_by(ladder.posn) %>%
# count() %>%
# mutate(p = n/n_itns)
#
# ladder_posn_prob %>%
# ggplot(aes(ladder.posn, p)) +
# geom_col() +
# ggtitle(
# paste0(
# "Probability of end ladder position for ",
# team_of_interest
# )
# ) +
# xlab("Ladder position") +
# ylab("Probability")
#
# tibble(
# event = c(
# "Top of ladder (Minor premiers)",
# "Top 4 (2nd chance in finals)",
# "Top 8 (Play in finals)"
# #"Bottom of ladder (Wooden spoon)"
# ),
# p = c(
# ladder_posn_prob %>% filter(ladder.posn == 1) %>% pull(p),
# ladder_posn_prob %>% filter(ladder.posn <= 4) %>% pull(p) %>% sum(),
# ladder_posn_prob %>% filter(ladder.posn <= 8) %>% pull(p) %>% sum()
# ladder_posn_prob %>% filter(ladder.posn == 16) %>% pull(p)
# )
# )
#