Repository for a workshop on Bayesian statistics
The World Cup Problem: Germany v. Argentina
Copyright 2016 Allen Downey
MIT License: https://opensource.org/licenses/MIT
This notebook contains a solution to the following problem:
In the final match of the 2014 FIFA World Cup, Germany defeated Argentina 1-0. How much evidence does this victory provide that Germany had the better team? What is the probability that Germany would win a rematch?
Scoring in games like soccer and hockey can be modeled by a Poisson process, which assumes that each team, against a given opponent, will score goals at some goal-scoring rate, , and that this rate does not vary; in other words, the probability of scoring a goal is about the same at any point during the game.
Based on this modeling decision, we can answer the questions by
Defining a prior distribution for each team's goal-scoring rate against the other,
Updating the prior based on the outcome of the game,
Using the posterior distributions to compute the probability that Germany's goal-scoring rate is higher.
Generating a predictive distribution for the number of goals each team would score in a rematch.
My solution uses the ThinkBayes2 framework, which is described in Think Bayes, and summarized in this notebook.
I'll start with Step 2.
Step 2: Updating
If goal-scoring is a Poisson process, the distribution of goals per game is Poisson with parameter . To compute the distribution of we can define a new class that inherits from thinkbayes2.Suite
and provides an appropriate Likelihood
function:
Likelihood
computes the likelihood of data
given hypo
, where data
is an observed number of goals, and hypo
is a hypothetical goal-scoring rate in goals per game. We can compute the likelihood of the data by evaluating the Poisson probability mass function (PMF).
Now we can get back to Step 1.
Step 1: Constructing the prior
Before the game starts, what should we believe about each team's goal scoring rate against each other? We could use previous tournament results to construct the priors, but to keep things simple, I'll just use the average goal-scoring rate from all matches in the tournament, which was 2.67 goals per game (total for both teams).
To construct the prior, I use a gamma distribution with a mean of 1.34 goals per game.
According to this prior, the goal-scoring rates are always greater than zero, with the most likely value (a priori) near 0.5. Goal scoring rates greater than 5 are considered unlikely.
Step 2: Comparing posteriors
The next step is to compute the posteriors for the two teams:
Update
invokes the likelihood function for each hypothetical value of and updates the distribution accordingly.
Since both teams scored fewer goals than the prior mean (1.4), we expect both posterior means to be lower. Germany's posterior mean is 1.2; Argentina's is 0.7. We can plot the posteriors:
To answer the first question, "How much evidence does this victory provide that Germany had the better team?", we can compute the posterior probability that Germany had a higher goal-scoring rate:
Based on the prior distributions, we would have said that Germany had a 50% chance of having the better team, or 1:1 odds. Based on the posteriors, we would say that Germany has a 70% chance. We can use the ratio of the prior and posterior odds to compute the Bayes factor, which measures the strength of the evidence.
The Bayes factor is 2.4, which is generally considered weak evidence.
Now on to Step 4.
Step 4
Exercise: Write a few lines of code to
Choose a random value of
lam
from the posterior distribution of each time.Choose a random number of goals for each team, conditioned on the value of
lam
you chose.Run that "simulation" many times and accumulate the distribution of wins, losses, and ties.
Use the results to estimate the probability that Germany would win a rematch.
Instead of running simulations, you could compute the posterior predictive distributions explicitly.
PredictiveDist
takes the posterior distribution of and a duration (in units of games).
It loops through the hypotheses in suite
, computes the predictive distribution of goals for each hypothesis, and assembles a "meta-Pmf" which is a Pmf that maps from each predictive distribution to its probability.
Finally, it uses MakeMixture
to compute the mixture of the distributions. Here's what the predictive distributions look like.
Using the predictive distributions, we can compute probabilities for the outcomes of a rematch.