This post was originally posted on Towards Data Science - My journey applying AI to horse racing

My journey into machine learning began in the summer of 2016. It all started at a barbecue party at the home of my fiancé’s aunt and uncle’s in northern Stockholm. I was sitting outside at a garden table together with the older men of her family. These are old and tough Finish men, her granddad (96 years old) fought in the war against the Russians. As you can imagine, as the new kid on the block, I was keeping a low profile and my mouth shut. They were discussing their greatest pastime — harness racing.

Harness racing

Harness racing is one of the largest sports in Sweden and Finland. It’s a kind of horse racing, yet different to regular horse racing. In harness racing, the driver does not sits on top of the horse. Instead, the driver sit on a cart which is attached to the horse. The horses are not allowed to run as fast as they want. If a horse gallops, it’s disqualified. The horses have to run at a trot. It is similar to the walking competitions that you see at the Olympic Games, where competitors are not allowed to run. A sport as much of control as of speed.

Harness racing is popular because of betting.

In 2018 a staggering 16 billion Swedish kronor was put on betting tickets. That’s equivalent to about 1.8 billion USD.
If harness racing was a country, then the Country of Harness Racing would rank as the 170th greatest in the world. A bit smaller than Belize.

Harness racing as a country

Back to the barbecue party. I was sitting around listening to these men discuss the upcoming races. I was semi-conscious and was zoning in and out of the conversation.
Then suddenly, an idea struck me. The best idea of my life. At least that’s what I thought at the time.

AI and harness Racing

This was the summer of 2016. A couple of months earlier an artificial intelligence called AlphaGo beat the world’s best Go-players. It was all over the news.

This Chinese board game is so complex that it had so far eluded being modeled. Late 2015 this changed. But it was not the first time that a computer beat humans at a game like this. In 1997 an AI called Deep Blue beat Garry Kasparov, the chess grand master. IBM’s artificial intelligence Watson smashed two grand champions of Jeopardy back in 2011.

So, on that warm summer day, this was the idea that struck me. If artificial intelligence can beat the smartest players in these games, then betting on harness racing should be a walk in the park.
Betting on harness racing is not like playing at the Casino. At the Casino, you cannot win. You will be losing sooner or later. Probably sooner. You are up against the full force of statistics. There can’t be any winning strategies.

In harness racing, you are playing against all the other players. You are not playing the house. The odds of any given horse winning are directly related to the amount of money on it.
It is in theory possible to come up with a winning strategy. It just needs to be better than everyone else’s approaches.

The second fact that was going for this idea was the incredible amounts of data available. Every detail about every race is recorded and made available online. Everything you could ever want to know about the horses, drivers, trainers, tracks, weather conditions, and records. With about 10 to 15 races per day in Sweden, it’s heaven for a data scientist. You can even find videos of the horses warming up before the race. The complete genealogy of every horse is available. Their complete family-trees all available online!

But, the fact that made me the most confident about this idea was that harness racing is not sexy one bit.
If you are an up and coming machine learning researcher, that’s into sports betting, then you are not looking at harness racing. You might be looking at football, basketball, baseball or even regular horse racing. But not harness racing.
My investigation confirmed this. Not a single mention of harness racing.

This was the perfect setup for applying AI. The only problem was: I didn’t know a single thing about artificial intelligence.

Early morning studying

So, I got studying.
I started to wake up early in the mornings to get a couple of hours in before my family woke up and I had to leave for my nine-to-five. At five in the mornings I was taking online courses, and I read books.

Studying

I spent my entire family vacation in Greece, much to my family chagrin, re-reading my old statistics books from University. I read books on deep learning, data science, and data mining. I learned Python, TensorFlow and Sci-kit learn. I was trying to soak up as much as possible in as short time as possible to solve this one specific problem. I attended meet-ups, seminars, lectures, and went to conferences.

What I found surprised me. It was not that hard to get started. I could achieve a lot with very little time invested. Sure, there are 5-year masters programs teaching these things at every university worldwide. I am sure those are great educations. But training like that was not needed to get started solving my problem. Machine learning and AI is so much more than math. There is also the craftsmanship and engineering. Theory is one thing, but to build an AI or machine learning application from scratch, takes engineering skills. Good thing that I had that part of the puzzle already in place — with a software engineering education and ten years of experience building enterprise software.
I did not need to understand all the ins and outs of what was happening under the hood. The entry barrier was much lower than I had expected. It was possible for me, and I would guess for most, to jump right in.

Winning strategy

Back to the Harness racing.
The odds of a particular horse winning a race is a direct function of the amount of money on that horse. Which means that the odds reflect the consensus of those betting. Average Joe. John and Jane Doe.
So, how often are they correct? How often is the consensus right? How often does the favorite horse win a race?
I looked at all the races in Sweden since 1995 and found that the favorite horse won about 37% of the time. That’s not bad.
This number became a fixation of mine. An obsession. It was the number to beat. The only thing I thought about for several months. While looking at those historic races, I also simulated betting a dollar on each of them. Picking the favorite horse to win in every one of those races. The result was staggering. A fictive betting account would be making a hefty profit with that simple strategy. I did not even simulate reinvesting the earnings as there would not have been enough money in the world to cover the winnings. It was straight forward flat betting of a dollar per race across about 26 000 races.

Simulated flat betting 1$ on 26 000 races over 15 years shows a hefty return on investment

Betting on the favorite horse in every race is a winning strategy. How can that be? Is that even possible?
Honestly, I don’t know. But I have a theory.

The odds fluctuate right up until the race starts reflecting the betting that is taking place. The odds that are recorded and published, which I was using, are the very last odds quoted before the race starts. Someone at the race track putting in their bets minutes before the race will have more information and make better predictions than someone betting a week in advance.
To take advantage of this strategy, you are going to need a speedy car. You are going to be driving all across Sweden every day, be at the tracks and be the very last person to put their bets in. By the way, don’t make your bet too big as you will be affecting the odds themselves.
Not such an attractive life after all.

Modelling the problem

There are about 10–15 races per day in Sweden. All year round. I had data going back to 1995, which meant that I had about 100 000 races in total. There was data on the trainers, the horses, the drivers, and the tracks. I got data on the weather conditions of the race days, the quality of the race tracks and much more. I used Googles Maps API to calculate how far horses and their drivers had to travel on race day. I cleaned the data. I built an elaborate data pipe-line with normalization, imputation, augmentation, and lots of other tricks of the trade.
Within the first couple of weeks, I hit 20%. Which pleased me. That meant that my machine had the correct horse to win in every fifth race. Not too bad.
After tweaking it for a couple months, I got it up to 32%. Then it jumped to 47%. A week later — 50%
At that point, I was excited! Honey — pack your bags, we’re moving to Mauritius.

When I had calmed down a bit and remembered to breathe, my pre-frontal cortex came back online. Is it possible that a machine, or anyone for that matter, could predict the right horse to win half of the time? Doesn’t chance play a more significant role than that? Something had to be wrong… And indeed, something was wrong.
When doing my data pre-processing, I had accidentally committed one of the cardinal sins of machine learning.
Typically, what you do is to split your data set into different parts. One of those parts you use to train your model and the other you keep for evaluating and testing your model. I had broken that rule by doing my data augmentation before I split the data-set. If you don’t understand what that means, just think of it like this: my AI had already seen all the races and still only got 50% of the winners correct. That is a lousy betting machine.
I was bummed out. I did not want to have anything to do with this project anymore. I left it — angrily, frustrated and disappointed. My GitHub commit graph is a testament to that.

GitHub commit graph showing how I left the project completely between December and February

Every green square in the GitHub commit graph above represents a day where I worked on this project. As you can see, there is a gap between December of 2016 and February of 2017. During those weeks I did not go up at 5 AM, and I did not think about beating that 37%.

The field is open to discoveries

Why am I telling this story?
To me, the field of machine learning and AI is open to discovery. It feels as I imagine the natural sciences during the enlightenment. For every stone that Leonardo da Vinci and his contemporaries turned, they would make some novel discovery. They would see something never seen before and make one grand discovery after another.
Today, the state of the art machine learning is as likely to be coming out of a dorm room of some enthusiastic 18 years old as from one of the big research papers.

At Kaggle (not affiliated), a platform where companies and organizations can post data-related problems with rewards attached to them, the competition is fierce. People compete trying to solve these problems with machine learning, AI and data science.

Kaggle — Platform for competing at data science

After each competition, all submissions are open for all to see. Which means that if you are competing here with some tried and tested approach, then you will be up against hundreds of others doing the exact same thing. You won’t win.
To win one of these competitions, it is almost always necessary to come up something that no one has ever done before.
Machine learning is advancing here competition by competition.
That is what makes AI and machine learning so exciting to me!

This idea was confirmed last year when I was supervising two master thesis students. Two young guys, directly out of university, with a very sound foundation of mathematics but no experience in programming, machine learning or anything related to AI. They looked at estimations of covariance matrices for wealth management.
By spending a couple of weeks on this problem, their work showed promising results for beating existing methods. Two young kids from school!
Had they had more experience going into this, or more time, then I am sure that they would have come up with something beating all current models and methods.

Front page of the master thesis report of the two students that I worked with

AI Hype at its peak

The Gartner Hype Cycle describes the different phases that hype around new technology goes through.

Gartner’s Hype Cycle

AI and machine learning are right now at the very peak. The so-called Peak of inflated expectations. Everyone is talking about it and everyone wants to get in on it.
Venture capital is pouring in. The crazier the idea the more money the AI start-ups seem to get. And there are plenty of crazy ideas going around.
But, what happens is that a start-up or two falls through. Fail. Go bust. The AI hype will start to descend into the Through of Disillusionment. This is precisely what we are heading towards. I know. It feels unthinkable that something like that is possible. But the AI hype will subside.
We don’t want the hype to be at the very bottom of that trough for too long, because when the hype is down there, it is hard to attract top talent and capital. We will also likely see a decline of new applications and adoption rates will slow down.
But, the good news is that it is possible to short-circuit this hype curve. I’ve written about this here: Fast track to the other side of the AI hype collapse. To make AI and machine learning come out the other end of the Gartner’s hype cycle quicker we need to jump right ahead and start implementing useful applications right now.
We shouldn’t restrict ourselves to the very grandest of ideas. Ideas that will move the bedrock of society. Ideas like self-driving cars. Instead, let’s find smaller, more accessible, and quicker wins. It might mean we tackle more mundane and dull problems, but many of those can create value right here and now.

Taking automation to the next level

Straight Through Processing

My day job is in IT in Capital Markets. For as long as I can remember we have been talking about Straight Through Processing. A fancier name for automation. We set up elaborate and complex machines. We program them up front with rules to handle all the problems and scenarios that the real world can throw at it. But many of the problems that occur in the real world does not have any clear solutions. Some have many solutions, some solution which are is probabilistic in nature, and others have solutions that change over time. Our current machines are useless for those types of problems. So, we create exceptions or tasks which then needs to be handled by humans.
Exception handling, making decisions based on probability, and learning over time is precisely what AI and machine learning does best!
Why are IT technicians, like myself, on expensive on-call contracts still being called up in the middle of the night? Waking up only to follow some predefined procedure for fixing some IT-breakdown. Often those decisions are trivial.
I see a similar situation in back-office operations in banks. I don’t mean to say that the work of back-office operations is trivial or easy. Quite the opposite. Especially nowadays. But many smaller day-to-day decisions that someone at back-office makes could be made by an “intelligent” machine.
Two examples from my daily work. I bet there are plenty of these types of possible applications all over the place. Situations where AI and machine learning could take us to the next level of automation and Straight Through Processing.
We need to keep our eyes out for them, and when we spot them, we need to have the guts to go after them.

What happened to the betting AI?

So how did it all turn out for my harness racing betting AI?
I bought a new PC and hid it in a wardrobe in the hallway of our apartment. It was great. My fiancé did not share that sentiment as our monthly electricity bill shot through the roof, and she wasn’t very amused to find our hallway blink like a Christmas tree because of all the LEDs (what’s up with all the blinking LEDs of computers anyway?).
My energy and passion came back and once more I was getting up early in the mornings to work on this project.
The last results, after remodeling the problem, came to be on par with the odds themselves. Around 37%. This time my AI was as good as consensus at betting harness racing. After reaching my goal, and “solving” my problem. My passion for this project ebbed out, and I have since moved on to other problems.
What this journey has taught me, and what I hope that you take away from this long read, is that even though AI and machine learning seems complicated with lots of math, and difficult acronyms — it is in fact not black magic. It is quite easy to get started and to apply these technologies to problems right here and right now.
AI and machine learning are open to exploration, and I encourage you to dive right in.
Our world is full of opportunities, big and small, where we could benefit from applying machine learning and AI.