I recently had the pleasure of visiting the UK for the MATLAB Expo in Birmingham, England. As part of that visit, I gave a talk introducing the new features in MATLAB 2012b. As much as I enjoyed that, the most exciting part of my day was meeting Yi Cao, one of the real rock stars of the MATLAB Programming Contest. Here we are shortly after my talk.
Yi Cao's analysis of the Peg Solitaire contest (which he won) is still a classic.
Since I was in England talking about the newest version of MATLAB, I was inspired to use some of the latest features to analyze (sorry, analyse) football scores. And by football, I mean the game where you actually kick the ball with your foot.
One of the new features I'm most excited about is the new Data Import tool. I was itching to try it out on some real data. At about this time, by good fortune, one of my MathWorks UK colleagues pointed me to an online CSV file with the latest results from the Premier League.
With the improved Import Tool, iporting the data really is as simple as double-clicking on the CSV file.
I'm interested in only six of a vast forest of columns in the table:
Date
Home team name
Away team name
Home team score, FTHG
Away team score, FTAG
Result, FTR (H = home team win, A = away team win, D = draw)
The Date column is particularly noteworthy because importing dates has always been painful.
A touch of the mouse gave me an IMPORT_FOOTBALL_DATA function that I can embed in this example file.
I can honestly say that I would never have written this data importing code. I'm capable of writing it, and if critical work depended on it, I would have. It's too much of a nuisance most of the time. But now it's so easy with the Data Import tool, away I go.
Number of Goals per Game
With the data safely in hand, we can start making pictures. First of all, how many games are we talking about?
nGames = length(FTR)
nGames =
109
What's the histogram like for total goals in a game?
hist(FTHG + FTAG,0:7)
title('Histogram of Total Goals in a Soccer Game');
Is there an evident home and away skew?
subplot(2,1,1)
hist(FTHG,0:7)
ylim([0 50])
title('Goals by the Home Team');
subplot(2,1,2)
hist(FTAG,0:7)
ylim([0 50])
title('Goals by the Away Team');
Yes there is. The away team is much more likely to be shut out than the home team. No surprises here: you are more likely to win a home game than an away game.
So the most common score is 1-1. That's soccer for you. The preponderance of home team victories is all evident here.
Finally, I wanted to clean up this plot to make it easier to read. I remembered Rob Henson's excellent HEATMAPTEXT contribution to the File Exchange. This does a much nicer job than my poor plot above.
In creating this document, I appreciated the convenient new PUBLISH tab on the MATLAB toolstrip.
Conclusions
None of this is groundbreaking in terms of analysis, but it was a lot of fun and extremely easy. With the Data Import tool, the new toolstrip, publishing, and a little spice from the File Exchange, it brought together so much of what makes MATLAB appealing. Simple code leveraged through a powerful environment with eye-catching results.
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.