Skip to content

Instantly share code, notes, and snippets.

View btatkinson's full-sized avatar

Blake Atkinson btatkinson

View GitHub Profile
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
@btatkinson
btatkinson / EloSR Update Equation
Last active July 19, 2023 09:47
Symbolic Regression Update Equation
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
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
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,
@btatkinson
btatkinson / cbb-SOS
Created March 15, 2020 13:26
SOS end of season adjustment
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())
@btatkinson
btatkinson / pass_one_covar
Created January 27, 2020 13:35
Pass rate as a function of score differential
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
@btatkinson
btatkinson / pass_rate
Created January 25, 2020 19:53
basic binomial model pymc3
## 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
@btatkinson
btatkinson / pymc3_poisson_qb
Created January 20, 2020 21:57
Bayesian modeling of touchdown counts
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
@btatkinson
btatkinson / td_pg_embed_R
Last active September 12, 2023 09:16
touchdown count per game in R using nflscrapR
library(tidyverse)
library(tidyr)
library(dplyr)
library(na.tools)
seasons <- seq(2014, 2019);
# iteration count
i = 0
df <- NULL;
@btatkinson
btatkinson / td_pg_embed
Created January 20, 2020 17:19
Touchdowns Per Game with nflscrapR data
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)