This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | def initialize_true_ratings(team_1_mean = 0.5, team_2_mean = -0.5): | |
| ## since I don't have data, I'm going to simulate using an example where team 1 > team 2 | |
| ## example of "true" skills -> what our ratings should approach | |
| ## I'll use the "true" skills to generate data | |
| team_1_true_ratings = np.random.normal(team_1_mean, 1, 5) | |
| team_2_true_ratings = np.random.normal(team_2_mean, 1, 5) | |
| return team_1_true_ratings, team_2_true_ratings | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | def play_h2h(self, result): | |
| """ | |
| Initialized with player 1 and player 2 object. Uses their means, standard deviations, and the provided result to update their ratings. | |
| """ | |
| ## Finding Z (purple distribution graph) | |
| z = self.p1.mu - self.p2.mu | |
| z_var = self.p1.sigma**2+self.p2.sigma**2 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | def update_multiple_beta_single_game(alphas, betas, outcomes, k=1): | |
| assert(len(alphas)==len(betas)==len(outcomes)) | |
| """ | |
| This function only works one game at a time for multiple players | |
| """ | |
| n = np.ones(len(alphas)) | |
| # Calculate the posterior alpha and beta parameters | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | alpha_dict = { | |
| 'dOppORtg_y':0.038, | |
| 'dORtg_y':0.019, | |
| 'dPace_y':0.1, | |
| 'dTReb%_y':0.02, | |
| # same for other team | |
| 'dOppORtg_x':0.038, | |
| 'dORtg_x':0.019, | |
| 'dPace_x':0.1, | |
| 'dTReb%_x':0.02, | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | sos_cols = ['Season','TeamID','OppTeamID','Loc','ScoreDiff'] | |
| sos_df = df[sos_cols] | |
| # here you could code "Home, Away, Neutral" location with 1, -1, and 0. I'm lazy and will include it in my one hot encoding below. | |
| # will append season by season (prob a way to vectorize this without for loop) | |
| sos_all = pd.DataFrame() | |
| seasons = list(sos_df.Season.unique()) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | data = pbp[['score_differential','play_type']] | |
| data.loc[:,'play_type'] = data.play_type.copy().replace({"run":"0","pass":"1"}).astype(int) | |
| # shuffle data | |
| data = data.sample(frac=1) | |
| # let's use 10,000 observations | |
| data = data[:10000] | |
| plays = data.play_type.values | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | ## estimate pass rate | |
| import pymc3 as pm | |
| data = pbp[['play_type']] | |
| data.loc[:,'play_type'] = data.play_type.copy().replace({"run":"0","pass":"1"}).astype(int) | |
| # shuffle data | |
| data = data.sample(frac=1) | |
| # only need 5000 observations | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import pymc3 as pm, theano.tensor as tt | |
| # gamma priors for qbs | |
| qb_a = qb_proj.shape_.values | |
| qb_b = qb_proj.rate.values | |
| # gamma priors for defenses | |
| dfn_a = dfn_all.shape_.values | |
| dfn_b = dfn_all.rate.values | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | library(tidyverse) | |
| library(tidyr) | |
| library(dplyr) | |
| library(na.tools) | |
| seasons <- seq(2014, 2019); | |
| # iteration count | |
| i = 0 | |
| df <- NULL; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from tqdm import tqdm | |
| seasons = list(range(2009, 2020)) | |
| td_pg = None | |
| for season in tqdm(seasons[-6:]): | |
| path = './data/nflscrapR/play_by_play_data/regular_season/reg_pbp_{}.csv'.format(season) | |
| pbp = pd.read_csv(path) | 
NewerOlder