# Data Driven Fantasy Football Lineup Optimization - Part 2

##### The skills the author demoed here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.

**Click here for Part 1 of Blog:**

#### Blog Part 1

**Part 2**

The goal for this project was to use gathered data to build an application to optimize fantasy sport lineup generation given the input of player salaries and projected fantasy points for each player. While this blog will focus on NFL fantasy sports contests, this framework can be used for other sports as well. The goal is to maximize value (fantasy points) given the weight restrictions (virtual budget for salary).

### Methods

The project will explore 2 point projection methods, the average points scored by each player over the course of the season (outlined in Part 1 of the blog) and the point projections provided by DraftKings. Additionally, 2 lineup optimization methods will be explored.

“Random Walk” which will randomly pick players to fill a team meeting the positional and salary constraints for a set number of trials. The algorithm will then sort the total projected points for each team generated and return the optimal solution.

The second method will use Linear Programming to solve for the optimal solution. Linear programming is a technique for the optimization of a linear objective function, subject to linear equality and linear inequality constraints. The inspiration for this solution is a famous problem in linear programming called the knapsack problem.

Picture a hypothetically knapsack that has a finite capacity to carry a certain amount of weight, the owner of the knapsack wants to maximize the value of survival items held within the knapsack. The problem is defined as given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.

For the fantasy football problem, the total value is defined as the projected fantasy points for a team and the weight is the total salary cap allowed for the contest. The R library lpSolve is used in this case to solve for optimal solution.

### Data from the Algorithms

Sample script below for the Random Walk Algorithm:

The graph below shows the improvement as trials are increased.

After 10,000 trials the Random Walk yielded a maximum value of **136.4 points**.

Sample script below for the lpSolver Algorithm:

lpSolver yielded an optimal solution of **174 points**, a significant increase from the Random Walk approach.

However, this value of 174 points is only a projection and may or may not be reached depending on the accuracy of the predictions for each player. After exploring the projected values it became clear that average points approach was yielding very similar projections to those provided DraftKings.

The histogram below shows the counts of the absolute value of the difference between the player projections defined in the Part 1 blog and the DraftKings projections. Players projected less than 5 fantasy points have been filtered out because they are not likely to be drafted.

The density around 0 on the x-axis indicates that the predictions are relatively close in most cases. After seeing this, I decided to explore filtering out more players on the lower end of points projections.

The histograms shows that the projections are closer for players who are projected more points. There is high correlation (0.95) between the values.

While the application can help optimize lineup selection, the effectiveness of the tool is still limited by the accuracy of the predictions prior to the week of games. The scatter plot below shows lack of linear relationship between the points projections and actual point outcomes each week.

**Conclusion and Future Work**

A major issue that has not been resolved is the high variability in player outcomes each week. Neither the DraftKings nor rolling average approach appear to provide the accuracy needed for sustainable and consistent success. It is clear further analysis is needed to increase the accuracy of predictions. This requires analysis of actual NFL game statistics to develop and test a model.