Skip to content

Instantly share code, notes, and snippets.

@MichaelWS
Created July 25, 2022 00:11
Show Gist options
  • Save MichaelWS/c3591a048390de568803e5db7d537e4f to your computer and use it in GitHub Desktop.
Save MichaelWS/c3591a048390de568803e5db7d537e4f to your computer and use it in GitHub Desktop.
ids = dfs["id"].unique()
# all players:
elo_frame = {}
true_skill_frame = {}
dates = []
count = 0
dates_to_ids = {}
dates = dfs["date"].unique()
elo_df = dfs[dfs[["id", "date"]].is_unique()][["date", "id"]]
elo_df = elo_df.with_columns([pl.lit(1200.0).alias("elo"),
pl.lit(true_skill.MU).alias("true_skill_mu"),
pl.lit(true_skill.SIGMA).alias("true_skill_sigma")])
all_ids = elo_df["id"].unique()
update_ids = []
gc.collect()
dates = dates.sort()
print(dates)
missing_ids = set(dfs[~dfs["opp_id"].is_in(dfs["id"])]["opp_id"].to_list())
for date in dates:
date_to_ids = dfs.filter(pl.col("date") == date)["id"]
if date.year >2023:
break
for col in date_to_ids:
cur_elo = elo_df.filter((pl.col("id") == col) & (pl.col("date") < date))
if cur_elo.shape[0] == 0:
rating1 = 1200.0
cur_mu = true_skill.MU
cur_sigma = true_skill.SIGMA
else:
rating1 = cur_elo["elo"][-1]
cur_mu = cur_elo["true_skill_mu"][-1]
cur_sigma = cur_elo["true_skill_sigma"][-1]
player_data = dfs.filter((pl.col("id") == col) & (pl.col("date") == date))
opp_id = player_data["opp_id"][0]
idx_frame = elo_df.filter((pl.col("id") == opp_id) & (pl.col("date") < date))
if force and not elo_df.filter(pl.col("id") == opp_id)["date"].contains(date):
update_ids.append(opp_id)
if idx_frame.shape[0] > 0:
rating2 = idx_frame["elo"][-1]
opp_mu = idx_frame["true_skill_mu"][-1]
opp_sigma = idx_frame["true_skill_sigma"][-1]
else:
rating2 = 1200.0
opp_mu = true_skill_mma.MU
opp_sigma = true_skill_mma.SIGMA
win_e = win_expectation(rating1, rating2)
result = player_data["outcome"][-1]
new_rating = elo_rating(
rating1, result, win_e, cur_elo.filter(pl.col("elo") != 1200).shape[0]
)
new_mu = true_skill.get_new_mu(
cur_mu, opp_mu, cur_sigma, opp_sigma, result
)
new_sigma = true_skill.get_new_sigma(
cur_mu, opp_mu, cur_sigma, opp_sigma, result
)
print(date)
elo_df = elo_df.with_columns([pl.when((pl.col("id") == col) & (pl.col("date") >= date)).then(pl.lit(new_rating)).otherwise(pl.col("elo")).alias("elo"),
pl.when((pl.col("id") == col) & (pl.col("date") >= date)).then(pl.lit(new_mu)).otherwise(pl.col("true_skill_mu")).alias("true_skill_mu"),
pl.when((pl.col("id") == col) & (pl.col("date") >= date)).then(pl.lit(new_sigma)).otherwise(pl.col("true_skill_sigma")).alias("true_skill_sigma")])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment