(Linearly) Optimising Fantasy Premier League Teams

Joseph O'Connor
4 min readAug 9, 2019

--

I’ve been messing around with forecasting and optimisation tools for the fantasy premier league game for a few years. It’s a really rich problem and I’ve more than once transferred tools from FPL to real-world projects.

Being good at fantasy football may seem to be all about accurately predicting player performances, however even with perfect forecasts the problem of selecting the optimal team is not easy — mostly you decide on a bunch of players, shuffle them in and out of the team until you get the budget right. Introducing strategies involving transfers over several weeks adds another layer of complexity.

In this post, I will show how to optimally select a “static” FPL team given perfect player score forecasts. Later posts will address transfer strategies, ways to correctly deal with forecast uncertainties, how to construct useful forecasts and how they interact with team optimisation.

It turns out that selecting an FPL team is a constrained optimisation problem that is quite easily formulated as something called a Linear Program (more accurately an Integer Linear Program or Zero-One Linear Program, but the distinction is not important for our purposes).

Linear Programming

A linear program allows the efficient exact optimisation of an objective function subject to constraints, where the objective function and the constraints are linear in the decision variables. This is a really broad class of problems, and these things are used all over the place.

The beautiful thing about an LP is that once you’ve managed to write down a linear objective and constraints, you’ve already finished. Just plug it into a solver and profit.

An Example

There are 100 different items in a shop, each with an associated cost 𝐶_i and value 𝑉_i. We indicate whether we buy each item or not with a binary variable 𝑥_i. We want to maximise the total value of the items we buy. However:

  1. We can only spend 100 pounds
  2. We can buy no more than 10 items

To turn this description into a linear program, we have to write down the objective — the quantity we want to maximise/minimise — and the constraints. The objective is the sum of the values of the selected products and the constraints are listed above. In maths:

PuLP

PuLP is a linear programming library in Python. It allows you to write down an objective function and constraints in a very intuitive way and instantly solve them. Let’s see an example with the above problem.

What About Picking a Fantasy Premier League Team?

We are now going to apply the same techniques to solve a simplified team selection task. The simplifications we will make are as follows:

  • No substitutes
  • No transfers
  • Assume we have perfect forecasts

We can revisit some of these simplifications later. We need to add the following features to the model we wrote earlier:

  • Formation constraints: max and min number of players in each position
  • Team constraints: max 3 players from each club
  • Captaincy: extra decision array for picking a captain, with associated objective function contribution and constraints:

I’m going to skip the step of writing out the maths here because I think the code is clearer. Here it is:

A linear program for a simplified FPL team selection

The core of the model has not changed much from the toy example — we have just added an extra set of decisions and a few more constraints.

Adding Real Data

The FPL app allows you to access all player data through several API endpoints. For simplicity, we are using a ready-made data file from the archive collected by vaastav.

This file tells us the price, position (`element_type`) and last season’s points total for each player, as well as a whole bunch of other stuff we will ignore for now. For this demo, I’m going to use last season’s points total as a forecast of next season’s points total.

Sadly we can’t pick this team since we also need to pay for subs. For now, we will hack subs into the model by treating them like regular players whose scores are reduced by a fudge factor.

A linear program for a slightly-less-simplified FPL team selection

The Result

This team is built under the assumption of no transfers and with very basic whole-season forecasts. I’d expect it to do a good job if you want to pick a single team now and forget about it. Swap out Edward Nketiah though, he’s on loan at Leeds…

Next time

We will start to include transfers into the model, continuing to use the framework of linear programming to plan transfer strategies over several weeks.

Notebooks and code are available at https://github.com/joconnor-ml/forecasting-fantasy-football.

--

--