Sunday, 3 February 2019

Back to Bayes-ics

As explained last post, to do our football analytics, what we need are some input parameters about how "good" the two teams facing each other in a match are likely to be, on the day. There are two alternative approaches to doing this. One is based on classical statistics. To follow this approach you look back over a load of matches and work out an average scoring rate and an average rate of conceding. You can also estimate, on average, how much better the team performs at home. This approach has some weaknesses though. A team can get better or worse over the season; Its no good at telling how good the team will be today. It also requires quite a lot of data (a lot of matches) and assumes all the teams form are stable over time. In other words, it makes some assumptions that are not true. Which is never good.
A much better approach is to use Bayesian statistics. Thomas Bayes was a statistician with a keen interest in games of chance. Hence his work is very relevant to all sorts of gambling! The formulas he gave us are all about inferring the underlying truth from a series of observations. Each observation modifies our belief in a given hypothesis. To cut a long story short, bayesian inference crops up everywhere, in modern analytics.
The particular method I am deploying for football match analysis is the Particle Filter - a modern development, based entirely on Bayesian inference. You can find a pretty good intro to particle filters in this slide deck. Note the reference to football results analysis on slide 24... Using a PF for football analysis is s a nice party trick that often crops up in tutorial material, although I do it in a slightly more sophisticated way to the standard approach.
Applying a particle filter to the English Premier League works like this:

Each team is represented by a large number of "particles", each of which is a guess at the "model" - i.e. the qualities of the team (its attacking strength, defensive strength etc.)
  • Between fixtures, we "advance" these models, saying in effect. Last week the team was like this, so this week, how might the team have moved on
  • After a fixture, we "filter" the particles, preferentially keeping those that best explain the result. Incidentally, this is where Bayes comes in. His theorem says that instead of asking the hard question, "how good is my particle (model), given the result", we can ask "how likely is my result, given my model". This turns out to be an easier question and one we can answer. Importantly we consider not just the result but the capabilities of the teams involved. Hence all the analysis is interconnected.
  • Now, when two teams face off, we have a set of guesses about the teams capabilities at the present time that is based on all previous results, especially the last result. We can model the game considering the full range of guesses and get the best possible odds prediction, given the evidence.
In a nutshell, that's it. My plan now is to publish some predictions before the weekend fixtures and try to ascertain if we can beat the bookies!. That's my goal. Bookies are there to be beaten after all.

Saturday, 2 February 2019

Monte Carlo Football Analytics - Project Monaco

I'm going to christen this effort Project Monaco. It's about Monte Carlo and Football, so it's a no brainer, right?

I'll explain the basics of the analysis...

Consider two football teams, facing each other. The result is based on a few things. How good is the home team at attacking? Conversely, how good is the away team at defending? These two things will help determine an average rate of goal scoring for the home team. There is another factor in there - the home team advantage. Some teams perform better at home than away. Others do not, and some teams even do a little better away from home (home fans can be off-putting if they're not getting behind the team). On the flip side, how good is the away side at attacking, and how well can the home team defend.
In my method, I put these numbers into a pot, and work out an average expected rate of goal scoring for each team, in the context of them playing each other. I then create a computer model of the fixture, and run about ten thousand trial games, recording the result of each. What I get is a comprehensive odds forecast, covering every score permutation.
The computer model uses a classic statistical method to model the results of each trial game. The binomial distribution. Its hard to argue with the basics of this. The one hard part we are left with is establishing the input data for the match, regarding the strengths and weakenesses of each team. The truth is, we can't be certain about them, and that leads to some complications. What we need to do is consider a range of possibilities regarding this input data, which makes things a little mroe complicated. I will explain how we work out these inputs based on the league results in the next post!

Context Switch - Monte Carlo Football Analytics

It's been a year since m,y last post on Monte Carlo modelling of horse races. The reason for the gap is that I realised, through quite a bit of experimentation, that my methods for predicting the "true odds" of a horse race really weren't fit for purpose. A lot of the time, my odds predictions were quite similar to those from the bookies. But the cases where I predicted a horse should have much shorter odds (i.e. the "value bets" did not win as often as I expected. Essentially there was very little profit to be had. None, in fact, beyond random outbreaks of good luck.
I concluded that, for horse racing, there was a lot of information out there that helps understand how well a horse is likely to perform. Maybe a lot of it is on "back channels" known only to the inner racing community.  But, I concluded, at a minimum, you really needed to look back at a horse's history and see how it fared against each horse it raced, and to know how good each of these other horses were. That, I concluded, was too much like hard work (at least for now). Too much bespoke web scraping to be written for one thing, and life is too short. What I did do, is dream up an analysis method that could genuinely work. Its based on established methods, although it has elements that I don't believe have been tried before. But I decided it would be much easier to operate this on a more restricted field of runners. Like a football league, where a small set of clubs face each other in a very well defined, exhaustive set of fixtures. The Premier League, will be my case study!
The good news is, having tried this, I KNOW I have a method that is at least pretty cool. As I planned to do with the horse racing, I will provide more details about the method, and some of my odds predictions for upcoming games. More posts to follow!

Tuesday, 20 February 2018

Monte Carlo or bust?


Looking around a bit shows I am not unique in using Monte Carlo simulation to estimate the  true odds, in horse racing. For examole, see this monte carlo example. The author has a nice idea of using standard horse ratings, namely Official Rating (OR) and the Racing Post Rating (RPR) as the input to the model. My method is to use the past performance of each horse as the input - which I believe has some neat benefits - but the basic approach is the same.

Basically the first we have to do is define a probability density function (PDF) for the speed we think each horse might run in the race. It might look like this:

It represents the probability the horse will run at any given speed. The peak of the PDF represents the most likely speed for the horse. It may run faster or slower, but each are less likely. The PDF tails off at the edges to show this. The extreme edges are getting pretty unlikely. The shape of the PDF is important. The typical thing to do is to use a Normal or in other words Gaussian form for the PDF. This is not a bad choice, because Gaussian PDFs crop up all over the place in nature, so the likely running speed for a horse probably follows one.

When we execute the Monte Carlo race model we run a lot of imaginary races (maybe 1000 or more) and simply count the times each horse wins. To simulate each race, we draw, at random, example speeds for each horse from the PDF- such that the most likely race speed for each horse is in the middle of its distribution, with the frequency falling off towards the edge of the distribution We then rank the horses based on speed and work out the winner. Here the choice of the Gaussian PDF is handy because computer languages often have a ready made function for generating random samples from a nor distribution. I'm using Python, and it does the job nicely.

So, we have the results of a thousand or so simulated races, so now we can calculate the "true" odds for the horses easily enough. We made a few assumptions along the way, but if these are true, our odds should be good. Just to state those assumptions again:

  • We assume the past performance of the horse provides a good measure of its quality
  • We assume the horse's speed PDF is a Gaussian distribution positioned in proportion to the horse's quality score. 
  • We have to "invent" a width for this Gaussian, (called the standard deviation). This is a bit of a weakness, in that we have to make this up to make the odds look right. Still, it should probably be fairly constant for all races.
So, that's the basis of the approach. I'm working on some improvements that are quite subtle, and I'll introduce in future posts. Now lets see how well it works!

Thursday, 15 February 2018

Horse Race Analytics Explained

As discussed in the previous post, the aim of my horse race analytics is to estimate the "true odds" for each horse in a race, so that we can compare them with what bookmakers are offering, to identify "good value". I put the term "true odds" in quotes (I did it again!) for a reason. It is quite hard to say what the true odds of anything is - let alone a one-off thing like a race. To measure the true odds accurately we would need to run the same race, with the same horses,  the same health, in the same weather and track conditions, a very large number of times and count the outcomes. But the weather is actually an uncertain factor so maybe we'd need to use a few different seasonally appropriate weather conditions. Anyway, clearly this is completely unfeasible, and the true odds are therefore really quite an abstract concept. Its doubtful that a precise number value for the true odds can even be defined in fact. One thing we can do is work out our way of calculating true odds and test it over a large number of real races. Our three to one (3/1) horses should on average win once for every three losses, our 2/1 should win once for every two losses etc. Incidentally if you are not familiar already, it turns out odds are a little different to probabilities. They are quoted as the number of losses vs. the number of wins. The win probability on the other hand will be a number between 0 and 1 defining the likelihood of a win in each race.

My approach to calculating the true odds has two stages. First, we estimate a "quality score" for each horse. The better the horse, the higher the quality score. We need to decide a standard way of doing this - for example we could look at the six previous races and assign a score of 5 for a win, 4 for second etc. I have a more sophisticated approach that I'll describe in a future post, but for now, you get the general ideas. 

Having calculated a quality score for each horse, we then use this to estimate the odds. In simple, made up cases, this might be easy. If three horses race, for example, and they are all exactly equal in terms of quality score then the true odds are 2/1 for each horse, because, on average, in each 3 races, each horse will win one and loose twice. For any significantly complicated example, it gets a lot more difficult. In fact, it rapidly becomes quite impossible to calculate analytically (i.e. using a formula). The way to do it is to do the kind of thing  merchant banks do a lot when analysing trades - Monte Carlo modelling. More about how this works in the next post.

Monday, 12 February 2018

Idle Pursuits

I decided to talk about my latest hobby - horse race analytics. Its something I started thinking about twenty years ago and, after several false starts, I believe I have finally worked out the maths of what I wanted to do and coded up an approach that works. I'm not sure why it took so long!

The basics. Horse racing is an uncertain business. Generally speaking it is never possible to accurately predict the results of a horse race, unless you are a) veeeery lucky or b) own all the horses. The best prediction is usually that the favorite will win. But typically the odds the bookmakers will give you on that happening will not be very good, so if you do it every time, you will, in the long run, loose money. If you back the horse where the bookies are giving the best odds, i.e. they pay you the most for a win, you will also loose money overall, because these horses will win less often. Not never, just less often.

There is, surprisingly, one reliable strategy for making money form horse racing, and that is to pick "value winners" which means that you pick horses where the bookmakers are offering "good value". In other words, they are offering better odds than the quality of the horse would suggest. In yet more other words the horse is more likely to win than they think it is. The "true odds" of the horse are "shorter" than the bookies odds. So, all we have to do is work out the true odds and back horses where the bookies are offering longer odds.

Therein, of course, lies the problem. How to calculate the "true odds". How to calculate odds better than the bookies, whose job it is to do this. They have teams devoted to it; observing races, going to stables, timing, observing, etc. This is what I'm trying to do. It won't be easy - but I think I at least have some maths that can help. I'm planning to use the kind of stuff economists and stock traders know about (at least some of them). To drop in a name, Bayes is the key to this. Bayes is the key to a lot of things.

What I plan to do is develop and refine the method over the coming weeks, publishing what I calculate as the true odds, comparing these to the bookmakers odds and highlighting my betting  recommendation. Over the weeks, we'll see if its working and hopefully refine as we go!

Thursday, 8 February 2018

Bitcoin analytics - reprise

I saw this twitter post from Justin Seitz @jms_dot_py the other day:

https://twitter.com/jms_dot_py/status/958741474572750848

Its good to see this push to uncover the underside of the crypto currency ecosystem going forward.  It again mentions the point that the blockchain is a public ledger, which lists all accounts and all transactions that have ever occurred - forever. That gives quite a bit of time for us to pick over it. De-anonomizing accounts is the only barrier to getting full context on those transactions. Of course, there is only so far it can be taken right now, but in future, with better tools, who knows what we will uncover.

I first came across Justin Seitz through his Black Hat Python book - which is packed full of ingenuity. He also does a lot in the OSINT space and developed the very good Hunchley tool for OSINT investigations. Definitely not a black-hat, but he does like to look under stones. I follow his Hunchley daily dark web report - which is an example of that.