In :# Configure Jupyter so figures appear in the notebook %matplotlib inline # Configure Jupyter to display the assigned value after an assignment %config InteractiveShell.ast_node_interactivity='last_expr_or_assign' # import classes from thinkbayes2 from thinkbayes2 import Pmf, Suite import thinkbayes2 import thinkplot import numpy as np from scipy.special import gamma
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, $\lambda$, 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
I'll start with Step 2.
In :class Soccer2(thinkbayes2.Suite): """Represents hypotheses about goal-scoring rates.""" def Likelihood(self, data, hypo): """Computes the likelihood of the data under the hypothesis. hypo: goal rate in goals per game data: goals scored in a game """ # FILL THIS IN! return 1
In :# Solution from scipy.stats import poisson class Soccer2(thinkbayes2.Suite): """Represents hypotheses about goal-scoring rates.""" def Likelihood(self, data, hypo): """Computes the likelihood of the data under the hypothesis. hypo: goal rate in goals per game data: goals scored in a game """ return poisson.pmf(data, hypo)
Likelihood computes the likelihood of
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.
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.
In :from thinkbayes2 import MakeGammaPmf xs = np.linspace(0, 8, 101) pmf = MakeGammaPmf(xs, 1.3) thinkplot.Pdf(pmf) thinkplot.decorate(xlabel='Goal-scoring rate (λ)', ylabel='PMF') pmf.Mean()
In :suite = Soccer2(pmf);
In :germany = suite.Copy(label='Germany') argentina = suite.Copy(label='Argentina') thinkplot.Pdf(germany) thinkplot.Pdf(argentina) thinkplot.decorate(xlabel='Goal-scoring rate (λ)', ylabel='PMF') pmf.Mean()
In :germany = suite.Copy(label='Germany') argentina = suite.Copy(label='Argentina') germany.Update(1) argentina.Update(0) print('posterior mean Germany', germany.Mean()) print('posterior mean Argentina', argentina.Mean())
posterior mean Germany 1.1506263832709267 posterior mean Argentina 0.6693817986970533
Update invokes the likelihood function for each hypothetical value of $\lambda$ 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.
Here are the posteriors:
In :thinkplot.Pdf(germany) thinkplot.Pdf(argentina) thinkplot.decorate(xlabel='Goal-scoring rate (λ)', ylabel='PMF')
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:
In :post_prob = germany.ProbGreater(argentina) print('posterior prob Germany > Argentina', post_prob)
posterior prob Germany > Argentina 0.6983938606019376
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.
In :prior_odds = 1 post_odds = post_prob / (1 - post_prob) print('posterior odds Germany > Argentina', post_odds) k = post_odds / prior_odds print('Bayes factor', k)
posterior odds Germany > Argentina 2.315582375066283 Bayes factor 2.315582375066283
The Bayes factor is about 2.3, which is generally considered weak evidence.
Now on to Step 4.
Exercise: Write a few lines of code to
Choose a random value of
lam from the posterior distribution of each team.
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.
In :# Solution gdr_goals = poisson.rvs(germany.Sample(1000)) arg_goals = poisson.rvs(argentina.Sample(1000)) np.mean(gdr_goals > arg_goals)
In :# Solution np.mean(gdr_goals == arg_goals)
In :# Solution np.mean(gdr_goals < arg_goals)
Instead of running simulations, you could compute the posterior predictive distributions explicitly.
Write a function called
PredictiveDist that takes the posterior distribution of $\lambda$ and a duration (in units of games).
It should loop through the hypotheses in
suite, compute the predictive distribution of goals for each hypothesis, and assemble a "meta-Pmf" which is a Pmf that maps from each predictive distribution to its probability.
Finally, it should use
MakeMixture to compute the mixture of the predictive distributions.
In :# Solution def PredictiveDist(suite, duration=1, label='pred'): """Computes the distribution of goals scored in a game. returns: new Pmf (mixture of Poissons) """ metapmf = thinkbayes2.Pmf() for lam, prob in suite.Items(): pred = thinkbayes2.MakePoissonPmf(lam * duration, 10) metapmf[pred] = prob mix = thinkbayes2.MakeMixture(metapmf, label=label) return mix
In :germany_pred = PredictiveDist(germany, label='germany') argentina_pred = PredictiveDist(argentina, label='argentina');
In :thinkplot.Hist(germany_pred, width=0.45, align='right') thinkplot.Hist(argentina_pred, width=0.45, align='left') thinkplot.decorate(xlabel='Predicted # goals', ylabel='Pmf')
Using the predictive distributions, we can compute probabilities for the outcomes of a rematch.
In :win = germany_pred.ProbGreater(argentina_pred) lose = germany_pred.ProbLess(argentina_pred) tie = 1 - (win + lose) print('Posterior prob Germany wins rematch', win) print('Posterior prob tie', tie) print('Posterior prob Argentina wins rematch', lose)
Posterior prob Germany wins rematch 0.44773969920578044 Posterior prob tie 0.3270825241713642 Posterior prob Argentina wins rematch 0.2251777766228554
In [ ]: