Win (or Do Better) at DFS by Optimizing Your Fantasy Football Lineups

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.

MATLAB table of the optimal DFS lineup for September 15, 2022, led by Cooper Kupp, Matthew Stafford, and Justin Jefferson
Optimal DFS Football Team for September 15, 2022

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
MATLAB Online Current Folder menu with Upload Files highlighted and DFF_data.csv uploaded
Uploading file to MATLAB Online

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.

MATLAB Online Import tab showing DFF_data.csv as a table of players with position, team, opponent, and spread columns
Imported CSV file in MATLAB

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
MATLAB Online Current Folder right-click menu with New and then Live Script selected
New MATLAB Live Script

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.

MATLAB Live Editor running dfs.mlx, with salary cap constraints, intlinprog, and the resulting 9-player OptimalTeam table
Executed MATLAB Code Showing the Optimal DFS Lineup

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.

OptimalTeam lineup table from MATLAB with the MATLAB logo over a background of stacked dollar bills
DFS Fat Stacks Using MATLAB

Let me know how you do and if you made some improvements to my optimization code. Good luck.

Resources

Update, November 2026: The script in this post grew into a full project in the same GitHub repo, nothans/dfs-optimizer. There’s an explorer app now, DFSOptimizerApp, with cash and GPP presets, player locks and excludes, exposure against field ownership, and a “copy as code” button. Under the hood is a +dfs function library that does everything the app does, from a script. It handles DraftKings and FanDuel rules, QB stacks, multi-lineup tournament portfolios, and Monte Carlo runs. The dfs.m script from this post still runs. It just calls the same library now. I wrote about rebuilding it with coding agents in DFS Fantasy Football Optimization (Agentic AI + MATLAB Optimization Toolbox). Grab it on GitHub or File Exchange, or open it in MATLAB Online.

2 comments

    1. 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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.