Betting on Horse Racing with Transformer

Aug 15, 2023

5 min read

Horse racing 🐎 in Hong Kong isn't just about the thrill of the race; it's a popular betting activity that fascinates many due to the delicate balance between predictability and sheer unpredictability. Our venture delves deep into this, focusing on forecasting the winners accurately. With a concoction of neural networks and strategic betting, we aim to maximize profits while mitigating monetary risks.

Data Collection

Data source

We initially sourced our data from NextMedia's Racing website. Though the website is no longer functional, the data it provided was invaluable. The dataset comprises two primary sets of features:

Environmental Features: These encompass the course's distance (in meters), its condition, and its class. Notably, there are five classes, grouping horses with similar ratings to compete against each other.

Rating of horses
Race classes

Horse-specific Features: This category details the horse's ID, age, the jockey riding it, its starting position, the weight it's bearing, the betting odds, and, importantly, its finish time.

Horse features


Bidirectional model

An intriguing aspect is that the start positions of horses are known beforehand, drawing parallels with the sequence-to-sequence problems. With models like LSTM and GRU adept at handling series input, they emerge as viable contenders. However, considering horses influence each other during a race, the transformer model, known for capturing intricate data relationships, might be more apt.

BERT Explained: State of the art language model for NLP | by Rani Horev |  Towards Data Science
Transformer encoder

The challenge then becomes how to measure our model's accuracy. We have both ranking and finish time as potential labels. While predicting finish times through regression seems logical, races often see hairline differences – sometimes as little as 0.4 seconds between the first and second positions!

Choosing a classification approach is similarly challenging. Designating the 1st position horse as the primary target might seem obvious, but the horses in the 2nd or 3rd positions are equally competitive, making the prediction hazy.

Formulation of Learning to Rank

The breakthrough was in viewing this as an "information retrieval ranking problem". The essence of ranking loss is akin to binary cross entropy. Given 'm' horses in a race, there are mC2 outcomes where horse 'i' ranks above horse 'j'. Thus, ranking loss is essentially the cumulative binary cross entropy losses for all possible pairs. For efficiency, we adopted LambdaRank as an acceleration to RankNet.

Betting Strategy

The intricate world of horse racing betting is not just about predicting which horse will cross the finish line first. It's also about understanding the mathematics behind allocating your bet to maximize potential returns. When the predicted winning probabilities of horses are neck-and-neck, the strategy becomes even more crucial. You can't simply place your bet on the horse with the highest predicted probability of winning, because these probabilities often run very close to each other. Moreover, the odds provided by bookmakers play a vital role in determining where your money should go.

Table of notations

One particularly groundbreaking research paper titled "An explicit solution to the problem of optimizing the allocations of a bettor’s wealth when wagering on horse races" dives deep into this subject. The study doesn't merely suggest a strategy; it provides an explicit formula for betting optimally across multiple horses in a race.

The Karush-Kuhn-Tucker (KKT) theory aids this strategy. KKT is a fundamental theorem in the realm of nonlinear programming, offering necessary conditions for a solution to be optimal. Leveraging the power of KKT, the paper outlines an algorithm which employs an iterative method to nail down the optimal allocation fraction for each horse in a race.


Histogram of revenue with the model trained with 10 epochs

After rigorous development and optimization of our predictive model for horse racing betting, we delved into a comprehensive test phase, using data from September 2017 to December 2019. The results were illuminating. On average, for every dollar ($1) we bet in each race, the model’s predictions resulted in a return of $1.12. This implies that for every dollar wagered, we profited by an encouraging margin of 12 cents. When we looked at the scenario where we continually reinvested our returns from one race to the next, thereby compounding our bet, we found an average return rate of 1.07. In essence, when our winnings were used as subsequent bets, our profit rate seemed to stabilize around a commendable 7%.

Return for each race in the test set
Returns by classes

However, the true prowess of our model was revealed when we segmented our results by the class of the race. The most intriguing observation was the stellar performance in the "New" horses race category, which yielded the highest revenue. These races, featuring horses without a substantial racing history, are typically challenging to predict for both the jockey club and regular bettors due to the absence of past performance data. Betting odds and amounts are often tricky to decide in such races because of the inherent unpredictability surrounding relatively unknown horses. Yet, it was in these very races that our model exhibited exceptional prowess. This suggests that our model could discern patterns or crucial factors that might have been overlooked by traditional bettors and perhaps even the jockey club. This exploration into predictive betting underscored the immense potential of utilizing advanced algorithms in horse racing, especially when dealing with races that are traditionally viewed as unpredictable.


In the world of horse racing betting, gut feel or intuition might serve you well occasionally, but a mathematically backed strategy brings consistency to the table. By combining predictive models for race outcomes with advanced betting strategies, one can optimize returns and make informed decisions in the high-stakes game of horse racing.