Last active
March 20, 2021 18:12
-
-
Save gowerc/21cfc5fcb7b63ce11f7018cecbd57a37 to your computer and use it in GitHub Desktop.
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 pandas as pd | |
import numpy as np | |
dat = pd.DataFrame({ | |
"player1" : ["A", "A", "A", "A","A","B","B", "B", "C", "C", "C", "C", "C"], | |
"player2" :[ "B", "C", "B", "B", "C", "A", "C", "C", "A", "B", "A", "B", "B"], | |
"player1_won" : [ 1, 0, 1, 1, 1, 0, 1, 0 , 1, 0 , 0, 0, 0] | |
}) | |
names = sorted(set(dat["player1"])) | |
n_players = len(names) | |
def get_wij(i,j): | |
ni = names[i] | |
nj = names[j] | |
dat1 = dat.query("player1 == '{i}' & player2 == '{j}'".format(i=ni,j=nj)) | |
dat2 = dat.query("player1 == '{j}' & player2 == '{i}'".format(i=ni,j=nj)) | |
n = sum(dat1["player1_won"]) + sum(1- dat2["player1_won"]) | |
return n | |
wij = np.matrix( | |
[ [ get_wij(j, i) for i in range(n_players)] for j in range(n_players)] | |
) | |
W = np.array(wij.sum(axis = 1)).flatten() | |
def get_p_next(i, W, wij, p): | |
sum_hold = 0 | |
for j in range(3): | |
if i == j: | |
continue | |
numerator = wij[i,j] + wij[j,i] | |
denominator = p[i] + p[j] | |
sum_hold += (numerator / denominator) | |
return W[i] / sum_hold | |
p = [0.5] * n_players | |
for it in range(50): | |
pnext = [ get_p_next(i , W , wij, p) for i in range(n_players)] | |
p = pnext / sum(pnext) | |
p[0] / (p[0] + p[1]) | |
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 pandas as pd | |
import patsy | |
from sklearn.linear_model import LogisticRegression | |
# Setup test data | |
dat = pd.DataFrame({ | |
"player1" : ["A", "A", "A", "A","A","B","B", "B", "C", "C", "C", "C", "C"], | |
"player2" :[ "B", "C", "B", "B", "C", "A", "C", "C", "A", "B", "A", "B", "B"], | |
"player1_won" : [ 1, 0, 1, 1, 1, 0, 1, 0 , 1, 0 , 0, 0, 0] | |
}) | |
# Generate the design matrix | |
# Left side of formula is unused but has to be specified to satisfy | |
# the dmatricies function. Use "-1" to supress the intercept term | |
design_matrix_1 = patsy.dmatrices( "player1_won ~ player1 - 1", dat)[1] | |
design_matrix_2 = patsy.dmatrices( "player1_won ~ player2 - 1", dat)[1] | |
difference_matrix = design_matrix_1 - design_matrix_2 | |
ddf = pd.DataFrame(difference_matrix) | |
# Remove first column to make the problem tractable | |
# This essentially fixes the parameter for team A to 0 | |
ddf.drop(ddf.columns[0], axis=1, inplace=True) | |
# Configure logistic regression removing regularisation and | |
# supressing the intercept term | |
model = LogisticRegression( | |
penalty = "none", | |
fit_intercept = False | |
) | |
fit = model.fit(ddf, dat["player1_won"]) | |
fit.coef_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment