import pymc as pm %pylab inline figsize(12,6) avg_goals_per_team = 1.34 duration_of_game = 93. german_prior = pm.Exponential('german_prior', duration_of_game/avg_goals_per_team) arg_prior = pm.Exponential('arg_prior', duration_of_game/avg_goals_per_team) sample = np.array([german_prior.random() for i in range(10000)]) hist(sample, bins=30, normed=True, histtype='stepfilled'); plt.title('Prior distribution: Exponential with mean equal to observed mean'); germany = pm.Poisson('germany_obs', german_prior, observed=True, value=[1]) argentina = pm.Poisson('arg_obs', arg_prior, observed=True, value=[0]) germany_predictive = pm.Poisson('germany_predictive', duration_of_game*german_prior) arg_predictive = pm.Poisson('arg_predictive', duration_of_game*arg_prior) mcmc = pm.MCMC([germany, argentina, german_prior, arg_prior, germany_predictive, arg_predictive]) mcmc.sample(20000, 5000) german_lambda_trace = mcmc.trace('german_prior')[:] arg_lambda_trace = mcmc.trace('arg_prior')[:] hist(german_lambda_trace, bins=45, histtype='stepfilled', label='Germany', alpha=0.9, normed=True); hist(arg_lambda_trace, bins=45, histtype='stepfilled', label='Argentina', alpha=0.8, normed=True); plt.legend(); plt.title('Posteriors of average goals/minute of the two teams'); german_post_trace = mcmc.trace('germany_predictive')[:] arg_post_trace = mcmc.trace('arg_predictive')[:] hist(german_post_trace, bins=10, histtype='stepfilled', label='Germany', alpha=0.9, normed=True); hist(arg_post_trace, bins=10, histtype='stepfilled', label='Argentina', alpha=0.8, normed=True); plt.legend(); plt.title('Posteriors of Goals per Team for a 93 minute game'); plt.ylabel('probability') plt.xlabel('Number of goals') print "Probability of Germany winning: %.3f"%(german_post_trace > arg_post_trace).mean() print "Probability of Argentina winning: %.3f"%(german_post_trace < arg_post_trace).mean() print "Probability of tie: %.3f"%(german_post_trace == arg_post_trace).mean()