UEFA Champions League Knockout Phase Draws: Monte Carlo Simulation with R

[This article was first published on Memo's Island, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Draws for the knockout phase of the 2012–13 UEFA Champions League will be held in Nyon on the 20th December 2012. The rules of the draw are simple and are as follows:

  • 8 Group winner teams will be seeded.
  • 8 Group runner-up teams will be unseeded.
  • Teams coming from the same group and from same association can not be drawn against.

One can compute the probabilities of the likely outcomes by using ensemble average, a standard way of finding the frequencies where we identify whole sample space. Here we take a different approach and use time averages. What we do is to run large number of draws (20 million times) using randomly generated draws. I have called this exercise a Monte Carlo simulation, rather a simple sort while there is no fancy sampling of any sort, only rejection of pair up or the given draw if the last remaining pair does not satisfy the rules. A statistical language GNU R is used to generate the likely outcome. We use Mersenne Twister RNG, a default random number generator which is known to have very large cycle.   In result we basically count the likely pair-ups in a matrix (matrix of pairs) and find the frequency of each likely pair up. Simulation takes 3-4 hours on a single CPU Intel Xeon with 16 GB RAM running Ubuntu GNU/Linux. Well, to be honest I haven’t optimized the code.

Qualified teams, their groups and associations definitions, the simulation and results reporting R code are as follows (note that we use xtable for html outputs, for other functions we use, see below codes after tables):

winnerTeams               <- c('Paris-Saint-Germain', 'Schalke-04', 'Malaga', 'Borussia-Dortmund', 'Juventus', 'Bayern-Munich', 'Barcelona', 'Manchester-United');
winnerAssociation <- c('FR', 'DE', 'ES', 'DE', 'IT', 'DE', 'ES', 'ENG');
runnersUpTeams <- c('Porto', 'Arsenal', 'Milan', 'Real-Madrid', 'Shakhtar-Donetsk', 'Valencia', 'Celtic', 'Galatasaray');
runnersUpTeamsAssociation <- c('PT', 'ENG', 'IT', 'ES', 'UA', 'ES', 'SCO', 'TR');
countMatrix <- matrix(0, 8, 8)
row.names(countMatrix) <- winnerTeams;
colnames(countMatrix) <- runnersUpTeams;
many <- 20e6;
system.time(drawMany(winnerTeams, winnerAssociation, runnersUpTeams, runnersUpTeamsAssociation, countMatrix, many))
countMatrix <- countMatrix/many;

Simulations results can be interpreted based on frequencies (probabilities) of pairings, rather intuitively while probabilities are not that far off . For example if we consider Barcelona, Milan and Arsenal score the largest 0.23 and 0.21.  So my guess based on these frequencies, which I select maximums first ,then the second maximum and so on. If all ties I select the highest count using the second table.  Here are the predicted pairs, (ordered with highest probability):

Barcelona – Milan
Malaga – Arsenal
Bayern Munich – Real Madrid
Borissia Dordmund – Valencia
Manchester United – Celtic
Juventus – Galatasaray
Schalke 04 – Porto
PSG – Donetsk

Note that predicted pairs are quite depending on your selection strategy.
Table for the frequencies:

Porto Arsenal Milan Real-Madrid Shakhtar-Donetsk Valencia Celtic Galatasaray
Barcelona0.180.21 0.23

Table for the counts (actually all are integers):

Porto Arsenal Milan Real-Madrid Shakhtar-Donetsk Valencia Celtic Galatasaray

This is the main simulation function:

drawMany <- function(winnerTeams, winnerAssociation, runnersUpTeams, runnersUpTeamsAssociation, countMatrix, many) {
for(i in 1:many) {
repeat {
dr <- drawOneKnock(winnerTeams, winnerAssociation, runnersUpTeams, runnersUpTeamsAssociation,0);
if(sum(dr) > 0) break;
updateCount <- mapply(incMatrix, dr[,1], dr[,2])

A single draw can be generated as follows:

drawOneKnock <- function(winnerTeams, winnerAssociation, runnersUpTeams, runnersUpTeamsAssociation, names=1) {
repeat {
if(k > 1000) return(-1);
blockWin = 1:8 ; # tracking for draw
blockRun = blockWin;
winners = c(); # Draw results
runners = c();
for(i in 1:7) {
kk =1;
repeat {
if(kk > 1000) return(-1);
winner <- sample(blockWin, 1);
runner <- sample(blockRun, 1);
if(!(runner == winner) && !(winnerAssociation[winner] == runnersUpTeamsAssociation[runner])) {
blockWin <- blockWin[-which(blockWin == winner)];
blockRun <- blockRun[-which(blockRun == runner)];
winners <- c(winners, winner);
runners <- c(runners, runner);
winner <- blockWin;
runner <- blockRun;
# check if last remaining is ok, otherwise invalidate draw
if(!(runner == winner) && !(winnerAssociation[winner] == runnersUpTeamsAssociation[runner])) {
winners <- c(winners, blockWin);
runners <- c(runners, blockRun);
if(names) dr <- cbind(winnerTeams[winners], runnersUpTeams[runners]);
if(!names) dr <- cbind(winners, runners);

And counting the pair-ups is performed by a simple function:

incMatrix <- function(i, j) {
countMatrix[i,j] <<- countMatrix[i,j]+1;

To leave a comment for the author, please follow the link and comment on their blog: Memo's Island.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)