bundesliga-predictor slides

(Bundesliga) game prediction

Simple model for the predictions of soccer games: How many goals a team scores.

As data only the results from prior games are used.

In [6]:
#top_games

Poisson distribution

Probability for outcome $k \in \{0, 1, 2, \dots\}$

$$ P_\lambda (k) = \frac{\lambda^k}{k!}\, \mathrm{e}^{-\lambda} $$

with parameter $\lambda>0$

  • $\lambda$ is also the expectation and variance of the distribution
In [20]:
import scipy.stats
k=np.arange(0,10)
lambda_= 3.2

plt.figure(figsize=(8,6))
plt.plot(k, scipy.stats.poisson.pmf(k, lambda_), 'bo', ms=6, label='poisson pmf')
plt.xlabel("k")
plt.ylabel("probability mass")
scipy.stats.poisson.pmf(k, lambda_)
Out[20]:
array([ 0.0407622 ,  0.13043905,  0.20870248,  0.22261598,  0.17809279,
        0.11397938,  0.060789  ,  0.02778926,  0.0111157 ,  0.00395225])

Probabilistic Model

Each team $i$ has a offence and defence strength (distribution). (Note that the average goals per game $\approx 3 \Rightarrow \Delta \mu=1.5$):

$$ offence_i \sim \mathcal N(\mu=1.5, \tau=1) $$$$ defence_i \sim \mathcal N(\mu=0, \tau=1) $$

$\mathcal N$ is the Gaussian distribution with parameters

  • mean: $\mu$
  • precision: $\tau=1/\sigma^2$ (variance: $\sigma^2$)

Model: The number of goals that team $i$ scores against team $j$ is Poisson distributed with

$$ goals_{ij} = Poisson \left(\lambda = (offence_i-defence_j) \right) $$

Graphical representation of the model

In [24]:
plot_model()

Implementation with pymc

In [43]:
offence = [pymc.Normal("offence%i"%i, tau=1., mu=1.5) for i in top_club_ids]
defence = [pymc.Normal("defence%i"%i, tau=1., mu=0.) for i in top_club_ids]

 
home_goals = [pymc.Poisson("home_goals_%i_%i" % (heim,gast), 
                 observed=True, value=goals,
                 mu = pymc.Lambda("home_rate_%i_%i" % (heim, gast),
                 lambda o=offence[heim], d=defence[gast]: low if o-d<0. else o-d))  
              for i,(heim, gast, goals) in enumerate(home_goals_)]

away_goals = [pymc.Poisson("away_goals_%i_%i" % (heim,gast), 
                 observed=True, value=goals,
                 mu = pymc.Lambda("away_rate_%i_%i" % (heim, gast),
                 lambda o=offence[gast], d=defence[heim]: low if o-d<0. else o-d))  
              for i,(heim, gast, goals) in enumerate(away_goals_)]

Sampling with pymc

In [45]:
mcmc = pymc.MCMC([offence, defence, home_goals, away_goals])
nb_samples=12000
burn=4000
mcmc.sample(nb_samples, burn=burn)#, thin=50)
 [-----------------100%-----------------] 12000 of 12000 complete in 96.8 sec
In [52]:
# sampling of probable results according to the model
#print_sample_results(most_probable=False)

# Sample results from a sample of the trace
nb_p, p, _ = print_sample_results(most_probable=True)
FC Bayern München - VfL Wolfsburg / prediction:  2 - 0  / result :  5 : 1
FC Bayern München - Borussia Mönchengladbach / prediction:  2 - 1
FC Bayern München - Bayer Leverkusen / prediction:  2 - 0  / result :  3 : 0
FC Bayern München - FC Augsburg / prediction:  2 - 0  / result :  2 : 1
FC Bayern München - FC Schalke 04 / prediction:  2 - 0
FC Bayern München - Borussia Dortmund / prediction:  2 - 1  / result :  5 : 1
FC Bayern München - 1899 Hoffenheim / prediction:  2 - 0
FC Bayern München - Eintracht Frankfurt / prediction:  2 - 0
FC Bayern München - SV Werder Bremen / prediction:  2 - 0
FC Bayern München - 1. FSV Mainz 05 / prediction:  2 - 0
FC Bayern München - 1. FC Köln / prediction:  2 - 0  / result :  4 : 0
FC Bayern München - Hannover 96 / prediction:  2 - 0
FC Bayern München - VfB Stuttgart / prediction:  3 - 0  / result :  4 : 0
FC Bayern München - Hertha BSC / prediction:  2 - 0  / result :  2 : 0
FC Bayern München - Hamburger SV / prediction:  2 - 0  / result :  5 : 0
FC Bayern München - FC Ingolstadt 04 / prediction:  2 - 0  / result :  2 : 0
FC Bayern München - SV Darmstadt 98 / prediction:  2 - 0
VfL Wolfsburg - FC Bayern München / prediction:  0 - 2
VfL Wolfsburg - Borussia Mönchengladbach / prediction:  1 - 1
VfL Wolfsburg - Bayer Leverkusen / prediction:  1 - 0  / result :  2 : 1
VfL Wolfsburg - FC Augsburg / prediction:  1 - 1
VfL Wolfsburg - FC Schalke 04 / prediction:  1 - 1  / result :  3 : 0
VfL Wolfsburg - Borussia Dortmund / prediction:  1 - 2  / result :  1 : 2
VfL Wolfsburg - 1899 Hoffenheim / prediction:  1 - 0  / result :  4 : 2
VfL Wolfsburg - Eintracht Frankfurt / prediction:  1 - 0  / result :  2 : 1
VfL Wolfsburg - SV Werder Bremen / prediction:  2 - 0  / result :  6 : 0
VfL Wolfsburg - 1. FSV Mainz 05 / prediction:  1 - 1
VfL Wolfsburg - 1. FC Köln / prediction:  1 - 0
VfL Wolfsburg - Hannover 96 / prediction:  1 - 0  / result :  1 : 1
VfL Wolfsburg - VfB Stuttgart / prediction:  2 - 1
VfL Wolfsburg - Hertha BSC / prediction:  1 - 1  / result :  2 : 0
VfL Wolfsburg - Hamburger SV / prediction:  1 - 0  / result :  1 : 1
VfL Wolfsburg - FC Ingolstadt 04 / prediction:  1 - 0
VfL Wolfsburg - SV Darmstadt 98 / prediction:  1 - 0
Borussia Mönchengladbach - FC Bayern München / prediction:  1 - 2  / result :  3 : 1
Borussia Mönchengladbach - VfL Wolfsburg / prediction:  1 - 1  / result :  2 : 0
Borussia Mönchengladbach - Bayer Leverkusen / prediction:  1 - 1
Borussia Mönchengladbach - FC Augsburg / prediction:  2 - 1  / result :  4 : 2
Borussia Mönchengladbach - FC Schalke 04 / prediction:  1 - 1  / result :  3 : 1
Borussia Mönchengladbach - Borussia Dortmund / prediction:  2 - 3
Borussia Mönchengladbach - 1899 Hoffenheim / prediction:  2 - 1
Borussia Mönchengladbach - Eintracht Frankfurt / prediction:  2 - 1
Borussia Mönchengladbach - SV Werder Bremen / prediction:  2 - 1
Borussia Mönchengladbach - 1. FSV Mainz 05 / prediction:  2 - 1  / result :  1 : 2
Borussia Mönchengladbach - 1. FC Köln / prediction:  1 - 1
Borussia Mönchengladbach - Hannover 96 / prediction:  2 - 1  / result :  2 : 1
Borussia Mönchengladbach - VfB Stuttgart / prediction:  2 - 1
Borussia Mönchengladbach - Hertha BSC / prediction:  1 - 1
Borussia Mönchengladbach - Hamburger SV / prediction:  1 - 1  / result :  0 : 3
Borussia Mönchengladbach - FC Ingolstadt 04 / prediction:  1 - 1  / result :  0 : 0
Borussia Mönchengladbach - SV Darmstadt 98 / prediction:  2 - 1  / result :  3 : 2
Bayer Leverkusen - FC Bayern München / prediction:  0 - 2
Bayer Leverkusen - VfL Wolfsburg / prediction:  1 - 1
Bayer Leverkusen - Borussia Mönchengladbach / prediction:  1 - 1  / result :  5 : 0
Bayer Leverkusen - FC Augsburg / prediction:  1 - 1  / result :  1 : 1
Bayer Leverkusen - FC Schalke 04 / prediction:  1 - 1  / result :  1 : 1
Bayer Leverkusen - Borussia Dortmund / prediction:  1 - 2
Bayer Leverkusen - 1899 Hoffenheim / prediction:  1 - 0  / result :  2 : 1
Bayer Leverkusen - Eintracht Frankfurt / prediction:  1 - 0
Bayer Leverkusen - SV Werder Bremen / prediction:  1 - 0
Bayer Leverkusen - 1. FSV Mainz 05 / prediction:  1 - 0  / result :  1 : 0
Bayer Leverkusen - 1. FC Köln / prediction:  1 - 0  / result :  1 : 2
Bayer Leverkusen - Hannover 96 / prediction:  1 - 0
Bayer Leverkusen - VfB Stuttgart / prediction:  1 - 1  / result :  4 : 3
Bayer Leverkusen - Hertha BSC / prediction:  0 - 1
Bayer Leverkusen - Hamburger SV / prediction:  1 - 0
Bayer Leverkusen - FC Ingolstadt 04 / prediction:  0 - 0
Bayer Leverkusen - SV Darmstadt 98 / prediction:  1 - 0  / result :  0 : 1
FC Augsburg - FC Bayern München / prediction:  0 - 2
FC Augsburg - VfL Wolfsburg / prediction:  0 - 1  / result :  0 : 0
FC Augsburg - Borussia Mönchengladbach / prediction:  1 - 2
FC Augsburg - Bayer Leverkusen / prediction:  1 - 1
FC Augsburg - FC Schalke 04 / prediction:  1 - 1  / result :  2 : 1
FC Augsburg - Borussia Dortmund / prediction:  1 - 2
FC Augsburg - 1899 Hoffenheim / prediction:  1 - 1  / result :  1 : 3
FC Augsburg - Eintracht Frankfurt / prediction:  1 - 1
FC Augsburg - SV Werder Bremen / prediction:  1 - 1  / result :  1 : 2
FC Augsburg - 1. FSV Mainz 05 / prediction:  1 - 1  / result :  3 : 3
FC Augsburg - 1. FC Köln / prediction:  1 - 1
FC Augsburg - Hannover 96 / prediction:  1 - 1  / result :  2 : 0
FC Augsburg - VfB Stuttgart / prediction:  1 - 1
FC Augsburg - Hertha BSC / prediction:  0 - 1  / result :  0 : 1
FC Augsburg - Hamburger SV / prediction:  1 - 1
FC Augsburg - FC Ingolstadt 04 / prediction:  0 - 0  / result :  0 : 1
FC Augsburg - SV Darmstadt 98 / prediction:  1 - 1  / result :  0 : 2
FC Schalke 04 - FC Bayern München / prediction:  0 - 2  / result :  1 : 3
FC Schalke 04 - VfL Wolfsburg / prediction:  1 - 1
FC Schalke 04 - Borussia Mönchengladbach / prediction:  1 - 2
FC Schalke 04 - Bayer Leverkusen / prediction:  1 - 1
FC Schalke 04 - FC Augsburg / prediction:  1 - 1
FC Schalke 04 - Borussia Dortmund / prediction:  1 - 2
FC Schalke 04 - 1899 Hoffenheim / prediction:  1 - 1  / result :  1 : 0
FC Schalke 04 - Eintracht Frankfurt / prediction:  1 - 1  / result :  2 : 0
FC Schalke 04 - SV Werder Bremen / prediction:  1 - 0
FC Schalke 04 - 1. FSV Mainz 05 / prediction:  1 - 1  / result :  2 : 1
FC Schalke 04 - 1. FC Köln / prediction:  1 - 0  / result :  0 : 3
FC Schalke 04 - Hannover 96 / prediction:  1 - 1  / result :  3 : 1
FC Schalke 04 - VfB Stuttgart / prediction:  2 - 1
FC Schalke 04 - Hertha BSC / prediction:  0 - 1  / result :  2 : 1
FC Schalke 04 - Hamburger SV / prediction:  1 - 0
FC Schalke 04 - FC Ingolstadt 04 / prediction:  1 - 0  / result :  1 : 1
FC Schalke 04 - SV Darmstadt 98 / prediction:  1 - 0  / result :  1 : 1
Borussia Dortmund - FC Bayern München / prediction:  1 - 2
Borussia Dortmund - VfL Wolfsburg / prediction:  2 - 1
Borussia Dortmund - Borussia Mönchengladbach / prediction:  3 - 2  / result :  4 : 0
Borussia Dortmund - Bayer Leverkusen / prediction:  2 - 1  / result :  3 : 0
Borussia Dortmund - FC Augsburg / prediction:  2 - 1  / result :  5 : 1
Borussia Dortmund - FC Schalke 04 / prediction:  2 - 1  / result :  3 : 2
Borussia Dortmund - 1899 Hoffenheim / prediction:  2 - 1
Borussia Dortmund - Eintracht Frankfurt / prediction:  2 - 1  / result :  4 : 1
Borussia Dortmund - SV Werder Bremen / prediction:  3 - 1
Borussia Dortmund - 1. FSV Mainz 05 / prediction:  2 - 1
Borussia Dortmund - 1. FC Köln / prediction:  2 - 1
Borussia Dortmund - Hannover 96 / prediction:  2 - 1
Borussia Dortmund - VfB Stuttgart / prediction:  3 - 1  / result :  4 : 1
Borussia Dortmund - Hertha BSC / prediction:  2 - 1  / result :  3 : 1
Borussia Dortmund - Hamburger SV / prediction:  2 - 0
Borussia Dortmund - FC Ingolstadt 04 / prediction:  2 - 0
Borussia Dortmund - SV Darmstadt 98 / prediction:  2 - 0  / result :  2 : 2
1899 Hoffenheim - FC Bayern München / prediction:  0 - 2  / result :  1 : 2
1899 Hoffenheim - VfL Wolfsburg / prediction:  0 - 1
1899 Hoffenheim - Borussia Mönchengladbach / prediction:  1 - 2  / result :  3 : 3
1899 Hoffenheim - Bayer Leverkusen / prediction:  0 - 1
1899 Hoffenheim - FC Augsburg / prediction:  1 - 1
1899 Hoffenheim - FC Schalke 04 / prediction:  1 - 1
1899 Hoffenheim - Borussia Dortmund / prediction:  1 - 2  / result :  1 : 1
1899 Hoffenheim - Eintracht Frankfurt / prediction:  1 - 1  / result :  0 : 0
1899 Hoffenheim - SV Werder Bremen / prediction:  1 - 1  / result :  1 : 3
1899 Hoffenheim - 1. FSV Mainz 05 / prediction:  1 - 1
1899 Hoffenheim - 1. FC Köln / prediction:  1 - 1
1899 Hoffenheim - Hannover 96 / prediction:  1 - 1  / result :  1 : 0
1899 Hoffenheim - VfB Stuttgart / prediction:  1 - 1  / result :  2 : 2
1899 Hoffenheim - Hertha BSC / prediction:  0 - 1
1899 Hoffenheim - Hamburger SV / prediction:  1 - 1  / result :  0 : 1
1899 Hoffenheim - FC Ingolstadt 04 / prediction:  0 - 0
1899 Hoffenheim - SV Darmstadt 98 / prediction:  1 - 1
Eintracht Frankfurt - FC Bayern München / prediction:  0 - 2  / result :  0 : 0
Eintracht Frankfurt - VfL Wolfsburg / prediction:  0 - 1
Eintracht Frankfurt - Borussia Mönchengladbach / prediction:  1 - 2  / result :  1 : 5
Eintracht Frankfurt - Bayer Leverkusen / prediction:  0 - 1  / result :  1 : 3
Eintracht Frankfurt - FC Augsburg / prediction:  1 - 1  / result :  1 : 1
Eintracht Frankfurt - FC Schalke 04 / prediction:  1 - 1
Eintracht Frankfurt - Borussia Dortmund / prediction:  1 - 2
Eintracht Frankfurt - 1899 Hoffenheim / prediction:  1 - 1
Eintracht Frankfurt - SV Werder Bremen / prediction:  1 - 1  / result :  2 : 1
Eintracht Frankfurt - 1. FSV Mainz 05 / prediction:  1 - 1
Eintracht Frankfurt - 1. FC Köln / prediction:  1 - 1  / result :  6 : 2
Eintracht Frankfurt - Hannover 96 / prediction:  1 - 1
Eintracht Frankfurt - VfB Stuttgart / prediction:  1 - 1
Eintracht Frankfurt - Hertha BSC / prediction:  0 - 1  / result :  1 : 1
Eintracht Frankfurt - Hamburger SV / prediction:  1 - 1
Eintracht Frankfurt - FC Ingolstadt 04 / prediction:  0 - 0
Eintracht Frankfurt - SV Darmstadt 98 / prediction:  1 - 1  / result :  0 : 1
SV Werder Bremen - FC Bayern München / prediction:  0 - 3  / result :  0 : 1
SV Werder Bremen - VfL Wolfsburg / prediction:  0 - 2
SV Werder Bremen - Borussia Mönchengladbach / prediction:  1 - 2  / result :  2 : 1
SV Werder Bremen - Bayer Leverkusen / prediction:  0 - 1  / result :  0 : 3
SV Werder Bremen - FC Augsburg / prediction:  1 - 1
SV Werder Bremen - FC Schalke 04 / prediction:  0 - 1  / result :  0 : 3
SV Werder Bremen - Borussia Dortmund / prediction:  1 - 2  / result :  1 : 3
SV Werder Bremen - 1899 Hoffenheim / prediction:  1 - 1
SV Werder Bremen - Eintracht Frankfurt / prediction:  1 - 1
SV Werder Bremen - 1. FSV Mainz 05 / prediction:  1 - 1
SV Werder Bremen - 1. FC Köln / prediction:  0 - 1  / result :  1 : 1
SV Werder Bremen - Hannover 96 / prediction:  1 - 1
SV Werder Bremen - VfB Stuttgart / prediction:  1 - 1
SV Werder Bremen - Hertha BSC / prediction:  0 - 1
SV Werder Bremen - Hamburger SV / prediction:  0 - 1  / result :  1 : 3
SV Werder Bremen - FC Ingolstadt 04 / prediction:  0 - 1  / result :  0 : 1
SV Werder Bremen - SV Darmstadt 98 / prediction:  1 - 1
1. FSV Mainz 05 - FC Bayern München / prediction:  0 - 2  / result :  0 : 3
1. FSV Mainz 05 - VfL Wolfsburg / prediction:  1 - 1  / result :  2 : 0
1. FSV Mainz 05 - Borussia Mönchengladbach / prediction:  1 - 2
1. FSV Mainz 05 - Bayer Leverkusen / prediction:  0 - 1
1. FSV Mainz 05 - FC Augsburg / prediction:  1 - 1
1. FSV Mainz 05 - FC Schalke 04 / prediction:  1 - 1
1. FSV Mainz 05 - Borussia Dortmund / prediction:  1 - 2  / result :  0 : 2
1. FSV Mainz 05 - 1899 Hoffenheim / prediction:  1 - 1  / result :  3 : 1
1. FSV Mainz 05 - Eintracht Frankfurt / prediction:  1 - 1  / result :  2 : 1
1. FSV Mainz 05 - SV Werder Bremen / prediction:  1 - 1  / result :  1 : 3
1. FSV Mainz 05 - 1. FC Köln / prediction:  1 - 1
1. FSV Mainz 05 - Hannover 96 / prediction:  1 - 1  / result :  3 : 0
1. FSV Mainz 05 - VfB Stuttgart / prediction:  1 - 1  / result :  0 : 0
1. FSV Mainz 05 - Hertha BSC / prediction:  0 - 1
1. FSV Mainz 05 - Hamburger SV / prediction:  1 - 1
1. FSV Mainz 05 - FC Ingolstadt 04 / prediction:  0 - 0  / result :  0 : 1
1. FSV Mainz 05 - SV Darmstadt 98 / prediction:  1 - 1
1. FC Köln - FC Bayern München / prediction:  0 - 2
1. FC Köln - VfL Wolfsburg / prediction:  0 - 1  / result :  1 : 1
1. FC Köln - Borussia Mönchengladbach / prediction:  1 - 1  / result :  1 : 0
1. FC Köln - Bayer Leverkusen / prediction:  0 - 1
1. FC Köln - FC Augsburg / prediction:  1 - 1  / result :  0 : 1
1. FC Köln - FC Schalke 04 / prediction:  0 - 1
1. FC Köln - Borussia Dortmund / prediction:  1 - 2  / result :  2 : 1
1. FC Köln - 1899 Hoffenheim / prediction:  1 - 1  / result :  0 : 0
1. FC Köln - Eintracht Frankfurt / prediction:  1 - 1
1. FC Köln - SV Werder Bremen / prediction:  1 - 0
1. FC Köln - 1. FSV Mainz 05 / prediction:  1 - 1  / result :  0 : 0
1. FC Köln - Hannover 96 / prediction:  1 - 1  / result :  0 : 1
1. FC Köln - VfB Stuttgart / prediction:  1 - 1
1. FC Köln - Hertha BSC / prediction:  0 - 1
1. FC Köln - Hamburger SV / prediction:  1 - 0  / result :  2 : 1
1. FC Köln - FC Ingolstadt 04 / prediction:  0 - 0  / result :  1 : 1
1. FC Köln - SV Darmstadt 98 / prediction:  1 - 0
Hannover 96 - FC Bayern München / prediction:  0 - 2  / result :  0 : 1
Hannover 96 - VfL Wolfsburg / prediction:  0 - 1
Hannover 96 - Borussia Mönchengladbach / prediction:  1 - 2
Hannover 96 - Bayer Leverkusen / prediction:  0 - 1  / result :  0 : 1
Hannover 96 - FC Augsburg / prediction:  1 - 1
Hannover 96 - FC Schalke 04 / prediction:  1 - 1
Hannover 96 - Borussia Dortmund / prediction:  1 - 2  / result :  2 : 4
Hannover 96 - 1899 Hoffenheim / prediction:  1 - 1
Hannover 96 - Eintracht Frankfurt / prediction:  1 - 1  / result :  1 : 2
Hannover 96 - SV Werder Bremen / prediction:  1 - 1  / result :  1 : 0
Hannover 96 - 1. FSV Mainz 05 / prediction:  1 - 1
Hannover 96 - 1. FC Köln / prediction:  1 - 1
Hannover 96 - VfB Stuttgart / prediction:  1 - 1  / result :  1 : 3
Hannover 96 - Hertha BSC / prediction:  0 - 1  / result :  1 : 3
Hannover 96 - Hamburger SV / prediction:  1 - 1
Hannover 96 - FC Ingolstadt 04 / prediction:  0 - 0  / result :  4 : 0
Hannover 96 - SV Darmstadt 98 / prediction:  1 - 1
VfB Stuttgart - FC Bayern München / prediction:  0 - 3
VfB Stuttgart - VfL Wolfsburg / prediction:  1 - 2  / result :  3 : 1
VfB Stuttgart - Borussia Mönchengladbach / prediction:  1 - 2  / result :  1 : 3
VfB Stuttgart - Bayer Leverkusen / prediction:  1 - 1
VfB Stuttgart - FC Augsburg / prediction:  1 - 2  / result :  0 : 4
VfB Stuttgart - FC Schalke 04 / prediction:  1 - 2  / result :  0 : 1
VfB Stuttgart - Borussia Dortmund / prediction:  1 - 3
VfB Stuttgart - 1899 Hoffenheim / prediction:  1 - 1
VfB Stuttgart - Eintracht Frankfurt / prediction:  1 - 1  / result :  1 : 4
VfB Stuttgart - SV Werder Bremen / prediction:  1 - 1  / result :  1 : 1
VfB Stuttgart - 1. FSV Mainz 05 / prediction:  1 - 1
VfB Stuttgart - 1. FC Köln / prediction:  1 - 1  / result :  1 : 3
VfB Stuttgart - Hannover 96 / prediction:  1 - 1
VfB Stuttgart - Hertha BSC / prediction:  0 - 2
VfB Stuttgart - Hamburger SV / prediction:  1 - 1
VfB Stuttgart - FC Ingolstadt 04 / prediction:  1 - 1  / result :  1 : 0
VfB Stuttgart - SV Darmstadt 98 / prediction:  1 - 1  / result :  2 : 0
Hertha BSC - FC Bayern München / prediction:  0 - 2
Hertha BSC - VfL Wolfsburg / prediction:  1 - 1
Hertha BSC - Borussia Mönchengladbach / prediction:  1 - 1  / result :  1 : 4
Hertha BSC - Bayer Leverkusen / prediction:  1 - 0  / result :  2 : 1
Hertha BSC - FC Augsburg / prediction:  1 - 0
Hertha BSC - FC Schalke 04 / prediction:  1 - 0
Hertha BSC - Borussia Dortmund / prediction:  1 - 2
Hertha BSC - 1899 Hoffenheim / prediction:  1 - 0  / result :  1 : 0
Hertha BSC - Eintracht Frankfurt / prediction:  1 - 0
Hertha BSC - SV Werder Bremen / prediction:  2 - 0  / result :  1 : 1
Hertha BSC - 1. FSV Mainz 05 / prediction:  1 - 0  / result :  2 : 0
Hertha BSC - 1. FC Köln / prediction:  1 - 0  / result :  2 : 0
Hertha BSC - Hannover 96 / prediction:  1 - 0
Hertha BSC - VfB Stuttgart / prediction:  2 - 0  / result :  2 : 1
Hertha BSC - Hamburger SV / prediction:  1 - 0  / result :  3 : 0
Hertha BSC - FC Ingolstadt 04 / prediction:  1 - 0
Hertha BSC - SV Darmstadt 98 / prediction:  1 - 0
Hamburger SV - FC Bayern München / prediction:  0 - 2
Hamburger SV - VfL Wolfsburg / prediction:  0 - 1
Hamburger SV - Borussia Mönchengladbach / prediction:  1 - 1
Hamburger SV - Bayer Leverkusen / prediction:  0 - 1  / result :  0 : 0
Hamburger SV - FC Augsburg / prediction:  1 - 1  / result :  0 : 1
Hamburger SV - FC Schalke 04 / prediction:  0 - 1  / result :  0 : 1
Hamburger SV - Borussia Dortmund / prediction:  1 - 2  / result :  3 : 1
Hamburger SV - 1899 Hoffenheim / prediction:  1 - 1
Hamburger SV - Eintracht Frankfurt / prediction:  1 - 1  / result :  0 : 0
Hamburger SV - SV Werder Bremen / prediction:  1 - 0
Hamburger SV - 1. FSV Mainz 05 / prediction:  1 - 1  / result :  1 : 3
Hamburger SV - 1. FC Köln / prediction:  0 - 1
Hamburger SV - Hannover 96 / prediction:  1 - 1  / result :  1 : 2
Hamburger SV - VfB Stuttgart / prediction:  1 - 1  / result :  3 : 2
Hamburger SV - Hertha BSC / prediction:  0 - 1
Hamburger SV - FC Ingolstadt 04 / prediction:  0 - 0
Hamburger SV - SV Darmstadt 98 / prediction:  1 - 0
FC Ingolstadt 04 - FC Bayern München / prediction:  0 - 2
FC Ingolstadt 04 - VfL Wolfsburg / prediction:  0 - 1  / result :  0 : 0
FC Ingolstadt 04 - Borussia Mönchengladbach / prediction:  1 - 1
FC Ingolstadt 04 - Bayer Leverkusen / prediction:  0 - 0  / result :  0 : 1
FC Ingolstadt 04 - FC Augsburg / prediction:  0 - 0
FC Ingolstadt 04 - FC Schalke 04 / prediction:  0 - 1
FC Ingolstadt 04 - Borussia Dortmund / prediction:  0 - 2  / result :  0 : 4
FC Ingolstadt 04 - 1899 Hoffenheim / prediction:  0 - 0  / result :  1 : 1
FC Ingolstadt 04 - Eintracht Frankfurt / prediction:  0 - 0  / result :  2 : 0
FC Ingolstadt 04 - SV Werder Bremen / prediction:  1 - 0
FC Ingolstadt 04 - 1. FSV Mainz 05 / prediction:  0 - 0
FC Ingolstadt 04 - 1. FC Köln / prediction:  0 - 0
FC Ingolstadt 04 - Hannover 96 / prediction:  0 - 0
FC Ingolstadt 04 - VfB Stuttgart / prediction:  1 - 1
FC Ingolstadt 04 - Hertha BSC / prediction:  0 - 1  / result :  0 : 1
FC Ingolstadt 04 - Hamburger SV / prediction:  0 - 0  / result :  0 : 1
FC Ingolstadt 04 - SV Darmstadt 98 / prediction:  0 - 0  / result :  3 : 1
SV Darmstadt 98 - FC Bayern München / prediction:  0 - 2  / result :  0 : 3
SV Darmstadt 98 - VfL Wolfsburg / prediction:  0 - 1  / result :  0 : 1
SV Darmstadt 98 - Borussia Mönchengladbach / prediction:  1 - 2
SV Darmstadt 98 - Bayer Leverkusen / prediction:  0 - 1
SV Darmstadt 98 - FC Augsburg / prediction:  1 - 1
SV Darmstadt 98 - FC Schalke 04 / prediction:  0 - 1
SV Darmstadt 98 - Borussia Dortmund / prediction:  1 - 2
SV Darmstadt 98 - 1899 Hoffenheim / prediction:  1 - 1  / result :  0 : 0
SV Darmstadt 98 - Eintracht Frankfurt / prediction:  1 - 1
SV Darmstadt 98 - SV Werder Bremen / prediction:  1 - 1  / result :  2 : 1
SV Darmstadt 98 - 1. FSV Mainz 05 / prediction:  1 - 1  / result :  2 : 3
SV Darmstadt 98 - 1. FC Köln / prediction:  0 - 1  / result :  0 : 0
SV Darmstadt 98 - Hannover 96 / prediction:  1 - 1  / result :  2 : 2
SV Darmstadt 98 - VfB Stuttgart / prediction:  1 - 1
SV Darmstadt 98 - Hertha BSC / prediction:  0 - 1  / result :  0 : 4
SV Darmstadt 98 - Hamburger SV / prediction:  0 - 1  / result :  1 : 1
SV Darmstadt 98 - FC Ingolstadt 04 / prediction:  0 - 0

Sampling histograms

In [56]:
nb_clubs = top_club_ids.max() + 1
bins=40
fig, axes = plt.subplots(nrows=nb_clubs, ncols=2, figsize=(10, 50))

for i in top_club_ids: 
    title = "Offence of " + clubs[clubs.index==i]["Name"][i].decode('latin')
    axes[i, 0].set_title(title)
    axes[i, 0].hist(mcmc.trace("offence%i"%i)[:], bins=bins, range=(0,4.2))
    
    axes[i, 1].hist(mcmc.trace("defence%i"%i)[:], bins=bins, range=(-2.,2.2))
    title = "Defence of " + clubs[clubs.index==i]["Name"][i].decode('latin')
    axes[i, 1].set_title(title)

#fig.suptitle("Offence and defence distribution of the clubs.")
fig.subplots_adjust(hspace=0.5)
fig.tight_layout()

Exercise: Distribution of expected goals

Use the model and the mcmc trace to predict how many goals a teams scores agains anouther team. Programm the corresponding python (plot) functions, e.g.

In [64]:
# id 0: Bayern München
# id 17: SV Darmstadt
plot_goal_diffs(0, 17)
# probability that Bayern München scores 0,1,2, ... goals against SV Darmstadt
# array([  6.71642520e-02,   1.75088332e-01,   2.34376889e-01, ...

(Possible) Solution

In [32]:
# Hierachical model
offence_mean = pymc.Normal("offence_mean", mu=1.5, tau=1.)

offence = [pymc.Normal("offence%i"%i, mu=offence_mean, tau=1.) for i in top_club_ids]
defence = [pymc.Normal("defence%i"%i, mu=0., tau=1.) for i in top_club_ids]
#midfield = [pymc.Normal("midfield%i"%i, mu=1., tau=1.) for i in top_club_ids]


om = offence #[offence[i] * midfield[i] for i in top_club_ids]
dm = defence #[defence[i] * midfield[i] for i in top_club_ids]

home_advantage = ha = pymc.Normal("home_advantage", tau=10., mu=1.0)


#noise_prior_g = pymc.HalfNormal("noise_prior_g", tau=1.) 
#noise_h = [pymc.Normal("noise_h_%i_%i"% (heim, gast), mu=0, tau=noise_prior_g)
#            for (heim, gast, goals) in home_goals_]
#noise_a = [pymc.Normal("noise_a_%i_%i"% (heim, gast), mu=0, tau=noise_prior_g)
#            for (heim, gast, goals) in away_goals_]

home_goals = [pymc.Poisson("home_goals_%i_%i" % (heim,gast), 
                 observed=True, value=goals,
                 mu = pymc.Lambda("home_rate_%i_%i" % (heim, gast),
                 lambda o=om[heim]*ha, d=dm[gast], n=0.: low if o-d+n<0. else o-d+n))  
              for i, (heim, gast, goals) in enumerate(home_goals_)]
 
away_goals = [pymc.Poisson("away_goals_%i_%i" % (heim,gast), 
                 observed=True, value=goals,
                 mu = pymc.Lambda("away_rate_%i_%i" % (heim, gast),
                 lambda o=om[gast], d=dm[heim]*ha, n=0.: low if o-d+n<0. else o-d+n))  
              for i, (heim, gast, goals) in enumerate(away_goals_)]


### total result (tie, home win, home loose)

noise_prior = pymc.HalfNormal("noise_prior", tau=1.)
noise = [pymc.Normal("noise_%i_%i"%(heim,gast), tau=1./(noise_prior**2))
            for i, (heim, gast, goals) in enumerate(home_goals_)]

defence_scale = pymc.HalfNormal("defence_scale", tau=1.)
skills = [(offence[i] + defence_scale * defence[i]) for i in top_club_ids]
skills_difference = [pymc.Lambda("skill_difference_%i_%i" % (heim, gast),
                     lambda ho_s=skills[heim]*ha, aw_s=skills[gast], n=noise[i]:ho_s-aw_s+n)           
                     for i, (heim, gast, result) in enumerate(results_)]

params = pymc.HalfNormal("params", tau=1., size=(3))    
pymc.abs(skills_difference[i]) 

# soft_max outputs
draw_t = [pymc.exp(params[0] - params[1]  * pymc.abs(skills_difference[i]))
              for i in range(len((results_)))]

home_win_t = [pymc.exp(skills_difference[i] * params[2])
              for i in range(len((results_)))]

away_win_t = [pymc.exp(- skills_difference[i] * params[2])
              for i in range(len((results_)))]

sm_denominator = [draw_t[i]+home_win_t[i]+away_win_t[i] for i in range(len((results_)))]

p_ = [(draw_t[i]/sm_denominator[i], home_win_t[i]/sm_denominator[i], away_win_t[i]/sm_denominator[i]) 
       for i in range(len((results_)))]

prediction = [pymc.Categorical("prediction_%i_%i" % (heim, gast), 
                 observed=True, value=result, 
                 p=p_[i]) 
                for i, (heim, gast, result) in enumerate(toto_results_)]

#prediction_ = [pymc.Categorical("prediction_%i_%i_" % (heim, gast), 
#                p=p_[i]) 
#                for i, (heim, gast, result) in enumerate(toto_results_)]
In [34]:
mcmc = pymc.MCMC([offence_mean, offence, defence, home_goals, away_goals, home_advantage, params, prediction, noise_prior, noise, defence_scale])

nb_samples=10000
burn=2000
mcmc.sample(nb_samples, burn=burn)
 [-----------------100%-----------------] 10001 of 10000 complete in 1669.8 sec
In [35]:
mcmc.trace("defence_scale")[:].mean(axis=0)
Out[35]:
1.5057820063503518

Offence

The offence (mean) shift corresponds to the number of goals per game (times 2):

In [37]:
plt.hist(mcmc.trace("offence_mean")[:], bins=20)
plt.title("Offence mean distribution")
Out[37]:
<matplotlib.text.Text at 0x11266d210>

Home advantage

In [38]:
plt.hist(mcmc.trace("home_advantage")[:], bins=20)
plt.title("Home advantage distribution")
Out[38]:
<matplotlib.text.Text at 0x112821b10>
In [39]:
#mcmc.trace("defence0")
#offence