Created
December 9, 2020 05:44
-
-
Save mrtolkien/ce6b006431b0d16e3f265e5a7779ab74 to your computer and use it in GitHub Desktop.
Inhouse rank exploration
This file contains 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 itertools | |
import math | |
import tabulate | |
import pandas as pd | |
import matplotlib | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import scipy.stats as stats | |
from collections import defaultdict | |
import trueskill | |
matplotlib.use("Agg") | |
df = pd.read_csv("na_inhouse.csv") | |
## | |
plt.close() | |
fig, ax = plt.subplots(dpi=300) | |
plt.title("NA inhouses rating and rank") | |
ax.set_xlim(-10, 70) | |
ax.set_ylim(0, 0.07) | |
participants = defaultdict(list) | |
for idx, row in enumerate(df.itertuples()): | |
# Preparing our game | |
if len(participants[row.role]) < 2: | |
participants[row.role].append(row) | |
if idx > 4: | |
continue | |
mu = row.trueskill_mu | |
sigma = row.trueskill_sigma | |
x = np.linspace(mu - 5 * sigma, mu + 5 * sigma, 200) | |
plt.plot(x, stats.norm.pdf(x, mu, sigma), label=row.name) | |
plt.bar(mu - 3 * sigma, height=0.08) | |
plt.legend(loc="upper right") | |
plt.savefig("inhouse_graph.png") | |
## | |
def evaluate_game(players) -> float: | |
# player are [[role players],...] | |
blue_team_ratings = [ | |
trueskill.Rating(mu=role_list[0].trueskill_mu, sigma=role_list[0].trueskill_sigma) | |
for role_list in players | |
] | |
red_team_ratings = [ | |
trueskill.Rating(mu=role_list[1].trueskill_mu, sigma=role_list[1].trueskill_sigma) | |
for role_list in players | |
] | |
delta_mu = sum(r.mu for r in blue_team_ratings) - sum(r.mu for r in red_team_ratings) | |
sum_sigma = sum(r.sigma ** 2 for r in itertools.chain(blue_team_ratings, red_team_ratings)) | |
size = len(blue_team_ratings) + len(red_team_ratings) | |
denominator = math.sqrt(size * (trueskill.BETA * trueskill.BETA) + sum_sigma) | |
ts = trueskill.global_env() | |
return ts.cdf(delta_mu / denominator) | |
## | |
# Calculating all possible team compositions | |
role_permutations = [] | |
for role in participants: | |
# two permutations: straight and reversed | |
role_permutations.append([participants[role], participants[role][::-1]]) | |
score = 1 | |
best_composition = None | |
for team_composition in itertools.product(*role_permutations): | |
blue_side_winrate = evaluate_game(team_composition) | |
if abs(blue_side_winrate - 0.5) < score: | |
score = abs(blue_side_winrate - 0.5) | |
best_composition = team_composition | |
## | |
print( | |
f"The best game found has a {round(evaluate_game(best_composition), 4)*100}% estimated winrate for blue side" | |
) | |
print( | |
tabulate.tabulate( | |
{ | |
"BLUE": [ | |
f"{role_list[0].name} | {round(role_list[0].trueskill_mu-3*role_list[0].trueskill_sigma+25, 2)}" | |
for role_list in best_composition | |
], | |
"RED": [ | |
f"{role_list[1].name} | {round(role_list[1].trueskill_mu - 3 * role_list[1].trueskill_sigma + 25, 2)}" | |
for role_list in best_composition | |
], | |
}, | |
headers="keys", | |
) | |
) | |
## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment