-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnlineLearning.R
143 lines (139 loc) · 5.91 KB
/
OnlineLearning.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
rm(list = ls())
#-------------------------------------------------------------------------------
# Libraries and utils
#-------------------------------------------------------------------------------
library(dplyr)
library(tidyverse)
library(rstan)
library(ggplot2)
library(patchwork)
library(DescTools)
#-------------------------------------------------------------------------------
# Global settings and variables
#-------------------------------------------------------------------------------
options(mc.cores = parallel::detectCores())
#rstan_options(auto_write = TRUE)
rstan_options(threads_per_chain = 2)
N_CHAINS=4
N_ITERS=11000
N_WARMUP=1000
DATA_DIR= "data/"
STAN_DIR= "stan/"
SEASON="2122"
ONLINE_MODELS_DIR= paste0("estimated_models/season_",SEASON,"/online_models/")
#-------------------------------------------------------------------------------
# Data import and preparation
#-------------------------------------------------------------------------------
SerieA_data<- read.csv(file= paste0(DATA_DIR,"season_",SEASON,"/SerieA_",SEASON,".csv"))
SerieA_data<- SerieA_data[,c("HomeTeam","AwayTeam","FTHG","FTAG")]
teams<- unique(SerieA_data$HomeTeam)
n_games<- nrow(SerieA_data)
n_teams<- length(teams)
n_matchdays= ceiling(n_games/((n_teams)/2))
ht= unlist(sapply(1:n_games,function (g) which(teams==SerieA_data$HomeTeam[g])))
at= unlist(sapply(1:n_games,function (g) which(teams==SerieA_data$AwayTeam[g])))
teams<- str_replace_all(teams, " ", "")
#-------------------------------------------------------------------------------
# Estimation of the models over time (but with an online approach)
#-------------------------------------------------------------------------------
# Create the folder to store models
if(!file.exists(ONLINE_MODELS_DIR)){
dir.create(ONLINE_MODELS_DIR,recursive = T)
}
# (1) Base step: fit the first model after 1st half of the league
base_training=SerieA_data[1:190,c("HomeTeam","AwayTeam","FTHG","FTAG")]
stan_paramters = list(
n_teams=n_teams,
n_games=nrow(base_training),
home_team= ht[1:nrow(base_training)],
away_team= at[1:nrow(base_training)],
goal_difference = base_training$FTHG-base_training$FTAG,
prev_att_MAP=rep(0,19),
prev_def_MAP=rep(0,19),
prev_mu_MAP=0,
prev_home_advantage_MAP=0,
prev_att_sd=rep(10,19),
prev_def_sd=rep(10,19),
prev_mu_sd=10,
prev_home_advantage_sd=10
)
KN_model <- stan(file = paste0(STAN_DIR,"online.stan"),
data = stan_paramters,
chains = N_CHAINS,
iter = N_ITERS,
warmup = N_WARMUP,
seed = 16
)
dir.create(paste0(ONLINE_MODELS_DIR,"matchday19/"))
save(KN_model,file=paste0(ONLINE_MODELS_DIR,"matchday19/KN_matchday19.rds"))
#...............................................................................
# Note: a totally equivalent way would be
# base_training=SerieA_data[1:190,c("HomeTeam","AwayTeam","FTHG","FTAG")]
# stan_paramters = list(
# n_teams=n_teams,
# n_games=nrow(base_training),
# home_team= ht[1:nrow(base_training)],
# away_team= at[1:nrow(base_training)],
# goal_difference = base_training$FTHG-base_training$FTAG
# )
#
# KN_model <- stan(file = paste0(STAN_DIR,"karlis-ntzoufras.stan"),
# data = stan_paramters,
# chains = N_CHAINS,
# iter = N_ITERS,
# warmup = N_WARMUP,
# seed = 16
# )
#...............................................................................
# (2) Online learning loop
for(i in 20:n_matchdays){
cat("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
cat("...Parameters estimation after matchday n.",i,"...\n")
cat("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
#---------------------------------------------------------------------------
# (1) Training and test set for the current matchday
cat("...Preparing the training set...\n")
training<- SerieA_data[1:(10*i),c("HomeTeam","AwayTeam","FTHG","FTAG")]
training<- na.omit(training) #just to manage if some matches were postponed...
#---------------------------------------------------------------------------
# (2) Retrieving the previous estimates
cat("...Retrieving previous prior information...\n")
load(paste0(ONLINE_MODELS_DIR,"/matchday",i-1,"/KN_matchday",i-1,".rds"))
prev_att_MAP= unlist(sapply(1:(n_teams-1),function (t) mean(as.array(KN_model)[,,paste0("att[",t,"]")])))
prev_def_MAP= unlist(sapply(1:(n_teams-1),function (t) mean(as.array(KN_model)[,,paste0("def[",t,"]")])))
prev_mu_MAP= mean(as.array(KN_model)[,,"mu"])
prev_home_advantage_MAP= mean(as.array(KN_model)[,,"home_advantage"])
prev_att_sd= unlist(sapply(1:(n_teams-1),function (t) sd(as.array(KN_model)[,,paste0("att[",t,"]")])))
prev_def_sd= unlist(sapply(1:(n_teams-1),function (t) sd(as.array(KN_model)[,,paste0("def[",t,"]")])))
prev_mu_sd= sd(as.array(KN_model)[,,"mu"])
prev_home_advantage_sd= sd(as.array(KN_model)[,,"home_advantage"])
#---------------------------------------------------------------------------
# (3) Prepare the parameters for stan
stan_paramters = list(
n_teams=n_teams,
n_games=nrow(training),
home_team= ht[1:nrow(training)],
away_team= at[1:nrow(training)],
goal_difference = training$FTHG - training$FTAG,
prev_att_MAP=prev_att_MAP,
prev_def_MAP=prev_def_MAP,
prev_mu_MAP=prev_mu_MAP,
prev_home_advantage_MAP=prev_home_advantage_MAP,
prev_att_sd=prev_att_sd,
prev_def_sd=prev_def_sd,
prev_mu_sd=prev_mu_sd,
prev_home_advantage_sd=prev_home_advantage_sd
)
# (4) Fit the model
cat("...Fitting the model...\n")
KN_model <- stan(file = paste0(STAN_DIR,"online.stan"),
data = stan_paramters,
chains = N_CHAINS,
iter = N_ITERS,
warmup = N_WARMUP,
seed = 16
)
# (5) Save the model
dir.create(paste0(ONLINE_MODELS_DIR,"matchday",i,"/"))
save(KN_model,file=paste0(ONLINE_MODELS_DIR,"matchday",i,"/KN_matchday",i,".rds"))
}