-
Notifications
You must be signed in to change notification settings - Fork 1
/
prior_pred_check.R
288 lines (231 loc) · 9.62 KB
/
prior_pred_check.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
# Notes -------------------------------------------------------------------
#
# Initialisation ----------------------------------------------------------
rm(list = ls())
set.seed(1559354162) # Reproducibility (different seed use in Stan)
library(HuraultMisc)
library(ggplot2)
library(cowplot)
library(rstan)
rstan_options(auto_write = TRUE) # Save compiled model
options(mc.cores = parallel::detectCores()) # Parallel computing
source("functions.R")
run_prior <- TRUE
run_fake <- TRUE
run_pred <- TRUE
stan_code <- "Model/DC_model.stan"
prior_file <- "Results/prior_mdl1.rds"
fake_file <- "Results/fake_mdl1.rds"
pred_file <- "Results/fake_pred_mdl1.rds"
# Data parameters
n_teams <- 20
teams <- LETTERS[1:n_teams]
id <- game_id(teams)
# MCMC options
n_chains <- 4
n_it <- 2000
# Parameters of interest
param_pop <- c("b", "home_advantage", "sigma_ability")
param_rep <- c(
# "win_home_rep", "win_away_rep",
# "draw_home_rep", "draw_away_rep",
# "lose_home_rep", "lose_away_rep",
# "goal_tot_home_rep", "goal_tot_away_rep",
# "goal_diff_home_rep", "goal_diff_away_rep",
"win_rep", "draw_rep", "lose_rep",
"goal_tot_rep", "goal_diff_rep", "point_rep"
)
param_test <- c(
# "win_home_test", "win_away_test",
# "draw_home_test", "draw_away_test",
# "lose_home_test", "lose_away_test",
# "goal_tot_home_test", "goal_tot_away_test",
# "goal_diff_home_rep", "goal_diff_away_test",
"win_test", "draw_test", "lose_test",
"goal_tot_test", "goal_diff_test", "point_test"
)
param_ind <- c("attack", "defence", param_rep)
param_obs <- c("home_goals_rep", "away_goals_rep")
param <- c(param_pop, param_ind, param_obs) # "home_goals_test", "away_goals_test"
if (run_prior | run_fake | run_pred) {
compiled_model <- stan_model(stan_code)
}
# Simulate from prior ----------------------------------------------------------
data_prior <- list(
N_teams = n_teams,
N_games = n_teams * (n_teams - 1),
home_goals = rep(1, n_teams * (n_teams - 1)), # doesn't matter
away_goals = rep(1, n_teams * (n_teams - 1)), # doesn't matter
home_id = sapply(id[["HomeTeam"]], function(x) {which(x == teams)}),
away_id = sapply(id[["AwayTeam"]], function(x) {which(x == teams)}),
run = 0
)
if (run_prior) {
fit_prior <- sampling(compiled_model, data = data_prior, pars = param,
iter = n_it, chains = n_chains)
saveRDS(fit_prior, file = prior_file)
} else {
fit_prior <- readRDS(prior_file)
}
# shinystan::launch_shinystan(fit_prior)
par_prior <- extract_parameters(fit_prior, param, param_ind, param_obs, teams, id$Game, data_stan)
# pairs(fit_prior, pars = param_pop)
# plot(fit_prior, pars = param_pop, plotfun = "trace")
# saveRDS(par_prior, file = "Results/par_prior_mdl1.rds")
# Prior predictive check ---------------------------------------------------------------
if (FALSE) {
plot(fit_prior, pars = c(param_pop, paste0(param_ind[1:2], "[1]")), plotfun = "hist")
# Exponentiate abilities
lapply(paste(param_ind[1:2], "[1]", sep = ""),
function(x) {
tmp <- extract(fit_prior, pars = x)[[1]]
hist(exp(tmp), breaks = 40, main = paste(x, "rate"))
})
# Number of goals (same distribution regardless of home/away and games)
goals <- extract(fit_prior, pars = c("home_goals_rep[1]"))[[1]]
summary(goals)
hist(goals, breaks = 40)
hist(goals[goals < 20], breaks = 20)
quantile(goals, probs = c(.25, .5 , .75, .9, .99, .999))
mean(goals >= 20) # proportion of games with home/away goals greater than 20
# Probability of wins/draws/lose
pl <- lapply(c("win", "lose", "draw"),
function(x) {
otc <- extract(fit_prior, pars = paste0(x, "_rep[1]"))[[1]]
otc <- table(otc) / length(otc)
ggplot(data = data.frame(otc), aes(x = otc, y = Freq)) +
geom_bar(stat = "identity") +
scale_x_discrete(breaks = 1:(2 * (n_teams - 1))) +
labs(x = paste0("Number of ", x), y = "Prior probability") +
theme_bw(base_size = 15)
})
plot_grid(plotlist = pl, ncol = 1)
# Ranks (should be uniform by symmetry)
rk <- compute_rank(fit_prior, "rep")[, 1]
rk <- table(rk) / length(rk)
ggplot(data = data.frame(rk), aes(x = rk, y = Freq)) +
geom_bar(stat = "identity") +
scale_x_discrete(breaks = 1:n_teams) +
labs(x = "Rank", y = "Prior probability") +
theme_bw(base_size = 15)
}
# Generate fake data ------------------------------------------------------
draw <- 2019 #
# True parameters
true_param_pop <- lapply(extract(fit_prior, pars = param_pop), function(x) {x[draw]})
true_param_ind <- lapply(extract(fit_prior, pars = param_ind), function(x) {x[draw, ]})
true_param <- rbind(
do.call(rbind,
lapply(1:length(true_param_ind),
function(i) {
data.frame(Variable = names(true_param_ind)[i],
True = true_param_ind[[i]],
Team = teams)
})),
do.call(rbind,
lapply(1:length(true_param_pop),
function(i) {
data.frame(Variable = names(true_param_pop)[i],
True = true_param_pop[[i]],
Team = NA)
}))
)
fd <- cbind(id,
data.frame(FTHG = extract(fit_prior, pars = "home_goals_rep")[[1]][draw, ],
FTAG = extract(fit_prior, pars = "away_goals_rep")[[1]][draw, ],
FTR = NA))
fd$FTR[fd$FTHG == fd$FTAG] <- "D"
fd$FTR[fd$FTHG > fd$FTAG] <- "H"
fd$FTR[fd$FTHG < fd$FTAG] <- "A"
fd$FTR <- factor(fd$FTR, levels = c("A", "D", "H"), ordered = TRUE)
heatmap_results(fd)
fstats <- football_stats(fd)
# Fit fake data -----------------------------------------------------------
data_fake <- list(
N_teams = n_teams,
N_games = n_teams * (n_teams - 1),
home_goals = fd$FTHG,
away_goals = fd$FTAG,
home_id = sapply(fd[["HomeTeam"]], function(x) {which(x == teams)}),
away_id = sapply(fd[["AwayTeam"]], function(x) {which(x == teams)}),
run = 1
)
if (run_fake) {
fit_fake <- sampling(compiled_model, data = data_fake, pars = param,
iter = n_it, chains = n_chains)
saveRDS(fit_fake, file = fake_file)
} else {
fit_fake <- readRDS(fake_file)
}
# Fake data check -------------------------------------------------------------
if (FALSE) {
# shinystan::launch_shinystan(fit_fake)
check_hmc_diagnostics(fit_fake)
pairs(fit_fake, pars = param_pop)
plot(fit_fake, pars = param_pop, plotfun = "trace")
print(fit_fake, pars = param_pop)
par_fake <- extract_parameters(fit_fake, param, param_ind, param_obs, teams, fd$Game, data_stan)
# Compare prior to posterior
HuraultMisc::plot_prior_posterior(par_prior, par_fake, param_pop)
# Can we retrieve parameters?
check_estimates(par_fake, true_param, param_pop, param_ind[1:2])
# Coverage of team parameters
HuraultMisc::plot_coverage(do.call(cbind, extract(fit_fake, pars = c("attack", "defence"))),
do.call(c, true_param_ind[c("attack", "defence")]))
# Posterior predictive checks
PPC_football_stats(fit_fake, "win_rep", fstats, teams)
PPC_football_stats(fit_fake, "lose_rep", fstats, teams)
PPC_football_stats(fit_fake, "goal_tot_rep", fstats, teams)
PPC_football_stats(fit_fake, "point_rep", fstats, teams)
PPC_football_stats(fit_fake, "rank_rep", fstats, teams, order = TRUE)
# Posterior rank
stackhist_rank(compute_rank(fit_fake, "rep"), teams)
}
# Fit fake data to test predictions ---------------------------------------
fd_train <- fd[which(rbinom(nrow(fd), 1, 0.7) == 1), ]
data_pred <- list(
N_teams = n_teams,
N_games = nrow(fd_train),
home_goals = fd_train$FTHG,
away_goals = fd_train$FTAG,
home_id = sapply(fd_train[["HomeTeam"]], function(x) {which(x == teams)}),
away_id = sapply(fd_train[["AwayTeam"]], function(x) {which(x == teams)}),
run = 1
)
param_ind <- c("attack", "defence", param_test)
param_obs <- c()
param <- c(param_pop, param_ind, param_obs, "home_goals_test", "away_goals_test")
if (run_pred) {
fit_pred <- sampling(compiled_model, data = data_pred, pars = param,
iter = n_it, chains = n_chains)
saveRDS(fit_pred, file = pred_file)
} else {
fit_pred <- readRDS(pred_file)
}
# Check model and evaluate predictions ------------------------------------
if (FALSE) {
# shinystan::launch_shinystan(fit_pred)
check_hmc_diagnostics(fit_pred)
pairs(fit_pred, pars = param_pop)
plot(fit_pred, pars = param_pop, plotfun = "trace")
print(fit_pred, pars = param_pop)
par_pred <- extract_parameters(fit_pred, param, param_ind, param_obs, teams, fd_train$Game, data_stan)
# Compare prior to posterior
HuraultMisc::plot_prior_posterior(par_pred, par_prior, param_pop)
# Statistics predictions
PPC_football_stats(fit_pred, "win_test", fstats, teams)
PPC_football_stats(fit_pred, "lose_test", fstats, teams)
PPC_football_stats(fit_pred, "point_test", fstats, teams)
stackhist_rank(compute_rank(fit_pred, "test"), teams)
pred0 <- process_predictions(fit_pred, id)
# Evaluate FTR predictions
m1 <- compute_metrics(pred = pred0, act = fd, test_game = setdiff(fd$Game, fd_train$Game), var = "FTR")
l1 <- prepare_predictions(pred = pred0, act = fd, test_game = setdiff(fd$Game, fd_train$Game), var = "FTR")
plot_lift(l1) + theme(legend.position = "top")
plot_calibration(l1, CI = NULL)
plot_calibration(l1, CI = 0.95, pool = TRUE)
# Evaluate goals predictions
# l2 <- prepare_predictions(pred = pred0, act = fd, test_game = setdiff(fd$Game, fd_train$Game), var = "FTHG")
# plot_lift(l2, best_bet = TRUE)
# plot_calibration(l2, CI = NULL) # problem with loess in plot_calibration for l2 (probably because of few outcomes)
}