As the United States heads out of summer and pumpkin spiced everything invades our culinary favorites, American Football takes over. I will now spend some part of my day talking about the NFL, the crazy outcomes, and fantasy football. FANTASY FOOTBALL. I can’t stress this enough: Fantasy Football saved the NFL. I started playing fantasy football with friends from work five years ago and I can’t imagine watching football without it. Fantasy football creates a game on top of the game, like an emergent property of an artificial neural network. If you are not into football, then this project might not be so interesting at least on its surface. But, if you stick with it, you might learn about optimization problems since fantasy football lineups are just optimization problems looking to be solved.
The Problem
Daily Fantasy Sports (DFS) for football is about picking a lineup of players across all of the NFL teams under a set salary cap and competing against other players doing the same thing. It’s easy right? Just pick all of the best players! Profit. Wait up. Everyone else is doing that too. The secret is to use player projections, maximize your utility of the salary cap, and add some chili pepper for your signature taste.
The Solution
Dust off your math skills and remind yourself that everything is a math problem, but others have solved them for you. An optimal DFS lineup is just an optimization problem. I am going to pick optimal DFS lineups using MATLAB and its Optimization Toolbox.
Get the Data
In order to optimize a lineup, you need to get some data. You need a table of players with their positions and score projections. I recommend getting the latest player projects from Daily Fantasy Fuel. They give you access to a table with everything that you want and they update it daily with the latest projections from Draft Kings and Fan Duel. Projections are subjective but they take into account past performance, team matchups, and weather conditions. You can adjust each row of the table with your own projections and put your thumb on the scales so to speak. Maybe you got some insider info that the Rams are not going to start Cam Akers so you adjust their projection down.
- Go to Daily Fantasy Fuel and click on “Download Projects as CSV”
- Save the file to your computer as “DFF_data.csv” into a new folder
A CSV file is just way to save data as a table. It might look intimidating if you open it up, but we are going to import the data into a nice clean table using MATLAB’s import tool.
Access MATLAB
You might have MATLAB installed on your computer, so all you have to do is open MATLAB. If you don’t have MATLAB installed, you can use MATLAB Online at matlab.mathworks.com by signing in and clicking “Open MATLAB Online (basic).”
Import the Data
First, we need to get the data into MATLAB.
- Right-click on the “Current Folder” and click “Upload Files”
- Select the CSV file that you downloaded from Daily Fantasy Fuel
Second, we need to import the data from the CSV file so we can work our fantasy magic.
- Right-click on the DFF_data.csv that we uploaded and click Open
- Click “Import Selection” and “Import Data”
These steps will bring the data into MATLAB in a nice and useable way. All of the default options will work perfectly.
Enter the Code
First, let’s create a new file for our optimizer code.
- Right-click on the Current Folder area, click New, and then Live Script
- Name it “dfs.mlx” and open it
Next, we need to get the code into MATLAB.
- Copy and paste my MATLAB code from GitHub into your new MATLAB Live Script.
- Click “Save”
The next thing that we need to do is select a “Salary Cap” and run the code.
- Change the salaryCap variable to the salary cap to optimize for. 50,000 to 60,000 is a common range.
- Click the Run button on the Live Editor tab
If everything goes to plan, MATLAB will output a table of optimal starting players based on the projected data.
Profit
There’s still some luck involved. Projections are projections. Now that you have a baseline, you can experiment by using different projections data and tweaking individual player projections.
Let me know how you do and if you made some improvements to my optimization code. Good luck.
Resources
- Source code at GitHub – https://github.com/nothans/dfs-optimizer
- Daily Fantasy Fuel – https://www.dailyfantasyfuel.com/nfl/projections/
- MATLAB – https://matlab.mathworks.com/
- Optimization Toolbox documentation – https://www.mathworks.com/help/optim/
error cannot reslove the name DFF_data.position
error in dfsn (line 5) iQB= DFF_data.position ==’Qb’
Thanks for the note. It looks like you have to import the CSV file and rename the workspace variable to “DFF_data”. The script is looking for a workspace variable named DFF_data.