-
Notifications
You must be signed in to change notification settings - Fork 0
/
knockouts.stan
75 lines (63 loc) · 1.74 KB
/
knockouts.stan
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
functions {
real tau(int[] goals, real[] mu, real rho){
// The adjustment factor for the probability of low-scoring games.
// Cf. the original Dixon-Coles paper.
if ((goals[1] == 0) && (goals[2] == 0))
return 1.0 - rho*mu[1]*mu[2];
else if ((goals[1] == 1) && (goals[2] == 0))
return 1.0 + rho*mu[2];
else if ((goals[1] == 0) && (goals[2] == 1))
return 1.0 + rho*mu[1];
else if ((goals[1] == 1) && (goals[2] == 1))
return 1.0 - rho;
else
return 1.0;
}
real dixoncoles_log(int[] goals, real[] mu, real rho){
return poisson_lpmf(goals[1] | mu[1]) +
poisson_lpmf(goals[2] | mu[2]) +
log(tau(goals, mu, rho));
}
}
data {
int<lower=2> n_teams;
int<lower=1> n_games;
int<lower=1, upper=n_teams> team1[n_games];
int<lower=1, upper=n_teams> team2[n_games];
int<lower=0> goals1[n_games];
int<lower=0> goals2[n_games];
int<lower=0, upper=1> neutral[n_games];
int<lower=0, upper=1> worldcup[n_games];
}
parameters {
// Offense, defence and HFA are in log space
real off_raw[n_teams-1];
real def[n_teams];
real hfa;
real rho;
}
transformed parameters {
// Introduce sum-to-zero constraint on offence ratings
real off[n_teams];
for (i in 1:n_teams - 1){
off[i] = off_raw[i];
}
off[n_teams] = -sum(off_raw);
}
model {
int score[2];
real mu[2];
off ~ normal(0, 1);
def ~ normal(0, 1);
hfa ~ normal(0.25, 0.1);
rho ~ normal(0, 0.1);
for (i in 1:n_games){
score[1] = goals1[i];
score[2] = goals2[i];
mu[1] = exp(off[team1[i]] + def[team2[i]] + (1 - neutral[i])*hfa);
mu[2] = exp(off[team2[i]] + def[team1[i]]);
for (j in 1:(2*worldcup[i] + 1)){ // Count World Cup games thrice
score ~ dixoncoles(mu, rho);
}
}
}