项目作者: papagorgio23

项目描述 :
Daily Fantasy Football Lineup Optimizer
高级语言: Jupyter Notebook
项目地址: git://github.com/papagorgio23/FantasyFootball.git
创建时间: 2018-05-20T01:57:47Z
项目社区:https://github.com/papagorgio23/FantasyFootball

开源协议:MIT License

下载



title: “Lineup Optimizer”
author: “Jason Lee”
date: “6/2/2018”

output: html_document

Daily Fantasy Football

If you are like me and love fantasy football but aren’t always positive which lineup is the opitmal lineup to use in a given week - Then this optimizer is for you!

Updated Python Version included with Notebook here

A better version will be included in my upcoming Applied Sports Analytics book. Chapter 8 - Lineup Optimization Using Linear Programming

(add versions to R packages)


Math


We are going to solve:
maximize f’x subject to A*x with constraints equal to b where:

  • x: is the variable to solve for: a vector of 0 or 1:
    • 1 when the player is selected, 0 otherwise
  • f: is your objective vector
  • A: is a matrix
  • dir: a vector of “<=”, “==”, or “>=”
  • b: a vector defining your linear constraints.

  1. # load library
  2. library(Rglpk)

Load Data

We need to get the weekly fantasy football prices and projected scores.

Here we have data from Yahoo’s Daily fantasy tournments.

  1. QB <- read.csv("Data/QBs-Table 1.csv",header=TRUE,stringsAsFactors=FALSE)
  2. RB <- read.csv("Data/RBs-Table 1.csv",header=TRUE,stringsAsFactors=FALSE)
  3. WR <- read.csv("Data/WRs-Table 1.csv",header=TRUE,stringsAsFactors=FALSE)
  4. TE <- read.csv("Data/TEs-Table 1.csv",header=TRUE,stringsAsFactors=FALSE)
  5. DEF <- read.csv("Data/DEFS-Table 1.csv",header=TRUE,stringsAsFactors=FALSE)

Append and Combine Data

We need to tag each of the players with their position.

We also need to combine all the players into a single dataset.

  1. QB$pos <- rep("QB")
  2. RB$pos <- rep("RB")
  3. WR$pos <- rep("WR")
  4. TE$pos <- rep("TE")
  5. DEF$pos <- rep("DEF")
  6. ALL <- rbind(QB, RB, WR, TE, DEF) # combind into one data.frame

View the data

To give you an idea of what data set you would need to input when you are customizing and updating the equation for your current week.

Here’s what my dataset looks like to start

  1. head(ALL)
  1. ## name cost projPts pos
  2. ## 1 Aaron Rodgers 42 20.5550 QB
  3. ## 2 Andrew Luck 42 23.9883 QB
  4. ## 3 Andy Dalton 31 17.9616 QB
  5. ## 4 Ben Roethlisberger 43 19.5577 QB
  6. ## 5 Blake Bortles 23 16.5012 QB
  7. ## 6 Cam Newton 34 19.7308 QB

As you can see it is very simple. We only need 4 key elements to run the algorithm:

  • Player Name - this could be a player name or playerID
  • Position - Needs to be either QB, RB, WR, TE, or DEF
  • Cost - how much the player costs for the given week
  • Projected Points - you can choose whatever site or projections you want
    • I will have another project posted showing you how to create your own projections

Set contraints

  1. # count of all the players
  2. num.players <- length(ALL)
  3. # objective:
  4. f <- ALL$projPts
  5. # the variables are all booleans
  6. var.types <- rep("B", num.players)
  7. # the constraints
  8. A <- rbind(as.numeric(ALL$pos == "QB"), # num QB
  9. as.numeric(ALL$pos == "RB"), # num RB
  10. as.numeric(ALL$pos == "WR"), # num WR
  11. as.numeric(ALL$pos == "TE"), # num TE
  12. as.numeric(ALL$pos == "DEF"), # num DEF
  13. ALL$cost) # total cost
  14. dir <- c("==",
  15. "==",
  16. "==",
  17. "==",
  18. "==",
  19. "<=") # this is for the total team salary, which is why it is less than or equal

Customize Constraints

Here is the part that needs to be adjusted depending on how you want your lineup to be and what the total salary limit is for the team.

  • QB: It’s normal to have only one QB but hey if there’s a daily league with more you can change this.
  • RB: There is usually 2 required plus a possible flex position.
    • If you want that flex position to be a RB then this needs to be at 3.
    • If not then set it to 2.
  • WR: There is usually 3 required plus a possible flex position.
    • If you want that flex position to be a RB then this needs to be at 4.
    • If not then set it to 3.
  • TE: There is usually 1 required plus a possible flex position.
    • If you want that flex position to be a RB then this needs to be at 2.
    • If not then set it to 1.
  • DEF: There is usually one 1 defense required.
  • Salary: The salary limit could be very different depending on the league.
    • Yahoo is $200
    • Draft Kings is $60,000
    • Fan Duel is $50,000

3 Examples

I will run the algorithm 3 different ways.

  1. 1. The flex position be an extra RB
  2. 2. The flex position be an extra WR
  3. 3. The flex position be an extra TE

3 Running Back Lineup

  1. b <- c(1, # QB
  2. 3, # RB
  3. 3, # WR
  4. 1, # TE
  5. 1, # DEF
  6. 200) # cost

Results

The first thing to check is that the status = 0. If it does not then it did not find an optimal solution.

  1. # Solve the math problem
  2. sol.3rb <- Rglpk_solve_LP(obj = f, mat = A, dir = dir, rhs = b,
  3. types = var.types, max = TRUE)
  4. # Check that Status = 0 and that there is an optimum value
  5. sol.3rb
  1. ## $optimum
  2. ## [1] 130.6174
  3. ##
  4. ## $solution
  5. ## [1] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
  6. ## [36] 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  7. ## [71] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
  8. ## [106] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
  9. ## [141] 0 0 0 0 0 0 0 0 0 0 0
  10. ##
  11. ## $status
  12. ## [1] 0
  13. ##
  14. ## $solution_dual
  15. ## [1] NA
  16. ##
  17. ## $auxiliary
  18. ## $auxiliary$primal
  19. ## [1] 1 3 3 1 1 200
  20. ##
  21. ## $auxiliary$dual
  22. ## [1] NA
  1. # View the selected players
  2. ALL$name[sol.3rb$solution == 1]
  1. ## [1] "Andrew Luck" "Ameer Abdullah" "Carlos Hyde"
  2. ## [4] "Doug Martin" "Brandin Cooks" "Larry Fitzgerald"
  3. ## [7] "Terrance Williams" "Tyler Eifert" "Rams"
These guys are your best possible lineup with 3 Running Backs

4 Wide Receivers Lineup

  1. b1 <- c(1, # QB
  2. 2, # RB
  3. 4, # WR
  4. 1, # TE
  5. 1, # DEF
  6. 200) # cost

Results

  1. # Solve the math problem
  2. sol.4wr <- Rglpk_solve_LP(obj = f, mat = A, dir = dir, rhs = b1,
  3. types = var.types, max = TRUE)
  4. # Check that Status = 0 and that there is an optimum value
  5. sol.4wr
  1. ## $optimum
  2. ## [1] 130.1095
  3. ##
  4. ## $solution
  5. ## [1] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  6. ## [36] 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  7. ## [71] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
  8. ## [106] 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
  9. ## [141] 0 0 0 0 0 0 0 0 0 0 0
  10. ##
  11. ## $status
  12. ## [1] 0
  13. ##
  14. ## $solution_dual
  15. ## [1] NA
  16. ##
  17. ## $auxiliary
  18. ## $auxiliary$primal
  19. ## [1] 1 2 4 1 1 200
  20. ##
  21. ## $auxiliary$dual
  22. ## [1] NA
  1. # View the selected players
  2. ALL$name[sol.4wr$solution == 1]
  1. ## [1] "Andrew Luck" "Chris Johnson" "DeMarco Murray"
  2. ## [4] "Brandin Cooks" "Larry Fitzgerald" "Mike Wallace"
  3. ## [7] "Terrance Williams" "Tyler Eifert" "Rams"
These guys are your best possible lineup with 4 Wide Receivers.

2 Tight Ends Lineup

  1. b2 <- c(1, # QB
  2. 2, # RB
  3. 3, # WR
  4. 2, # TE
  5. 1, # DEF
  6. 200) # cost

Results

  1. # Solve the math problem
  2. sol.2te <- Rglpk_solve_LP(obj = f, mat = A, dir = dir, rhs = b2,
  3. types = var.types, max = TRUE)
  4. # Check that Status = 0 and that there is an optimum value
  5. sol.2te
  1. ## $optimum
  2. ## [1] 130.3089
  3. ##
  4. ## $solution
  5. ## [1] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  6. ## [36] 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  7. ## [71] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
  8. ## [106] 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0
  9. ## [141] 0 0 0 0 0 0 0 0 0 0 0
  10. ##
  11. ## $status
  12. ## [1] 0
  13. ##
  14. ## $solution_dual
  15. ## [1] NA
  16. ##
  17. ## $auxiliary
  18. ## $auxiliary$primal
  19. ## [1] 1 2 3 2 1 200
  20. ##
  21. ## $auxiliary$dual
  22. ## [1] NA
  1. # View the selected players
  2. ALL$name[sol.2te$solution == 1]
  1. ## [1] "Andrew Luck" "DeMarco Murray" "Doug Martin"
  2. ## [4] "Larry Fitzgerald" "Mike Wallace" "Terrance Williams"
  3. ## [7] "Jordan Cameron" "Tyler Eifert" "Rams"
These guys are your best possible lineup with 2 Tight Ends.

Best Lineups

  1. # 3 RB lineup
  2. sol.3rb$optimum
  1. ## [1] 130.6174
  1. ALL$name[sol.3rb$solution == 1]
  1. ## [1] "Andrew Luck" "Ameer Abdullah" "Carlos Hyde"
  2. ## [4] "Doug Martin" "Brandin Cooks" "Larry Fitzgerald"
  3. ## [7] "Terrance Williams" "Tyler Eifert" "Rams"
  1. # 4 WR lineup
  2. sol.4wr$optimum
  1. ## [1] 130.1095
  1. ALL$name[sol.4wr$solution == 1]
  1. ## [1] "Andrew Luck" "Chris Johnson" "DeMarco Murray"
  2. ## [4] "Brandin Cooks" "Larry Fitzgerald" "Mike Wallace"
  3. ## [7] "Terrance Williams" "Tyler Eifert" "Rams"
  1. # 2 TE lineup
  2. sol.2te$optimum
  1. ## [1] 130.3089
  1. ALL$name[sol.2te$solution == 1]
  1. ## [1] "Andrew Luck" "DeMarco Murray" "Doug Martin"
  2. ## [4] "Larry Fitzgerald" "Mike Wallace" "Terrance Williams"
  3. ## [7] "Jordan Cameron" "Tyler Eifert" "Rams"

Obviously these lineups are from a few years ago but you can change the inputs and try to compete against me next season!


Good Luck