I'm trying to calculate piece weights via applying logistic regression algorithm (from python statsmodels package) to
calculate piece weights.
I have created the following test data set:
Code: Select all
[
{
"P": 8,
"R": 2,
"N": 2,
"B": 2,
"Q": 2,
"p": 8,
"n": 2,
"r": 2,
"b": 2,
"q": 2,
"Result": "1-0"
},
{
"P": 8,
"R": 2,
"N": 2,
"B": 2,
"Q": 2,
"p": 8,
"n": 2,
"r": 2,
"b": 3,
"q": 2,
"Result": "1-0"
},
{
"P": 8,
"R": 2,
"N": 2,
"B": 2,
"Q": 2,
"p": 8,
"n": 2,
"r": 2,
"b": 2,
"q": 2,
"Result": "1-0"
},
...
]
Here's my python code inspired by this post:
https://www.r-bloggers.com/2015/06/big- ... ss-pieces/
Code: Select all
# packages
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
# create data frame from JSON file
positions = pd.read_json('positions.json');
# filter draws
positions = positions[positions['Result'] != '1/2-1/2']
# replace results with binary outcomes
positions['Result'] = positions['Result'].replace('1-0', 1).replace('0-1', -1)
positions['white_win'] = (positions['Result'] == 1)
# define formula variables
win = positions['white_win']
P = positions['P']
N = positions['N']
B = positions['B']
R = positions['R']
Q = positions['Q']
p = positions['p']
n = positions['n']
b = positions['b']
r = positions['r']
q = positions['q']
# define formula
formula = '''
win ~ I(P - p) +
I(N - n) +
I(B - b) +
I(R - r) +
I(Q - q)
'''
# builg model
piece_weights = smf.glm(formula, family=sm.families.Binomial(), data = positions).fit()
print(piece_weights.summary())
Code: Select all
Generalized Linear Model Regression Results
=======================================================================================
Dep. Variable: ['win[False]', 'win[True]'] No. Observations: 1209
Model: GLM Df Residuals: 1203
Model Family: Binomial Df Model: 5
Link Function: logit Scale: 1.0000
Method: IRLS Log-Likelihood: -660.14
Date: Thu, 01 Oct 2020 Deviance: 1320.3
Time: 21:40:23 Pearson chi2: 1.38e+03
No. Iterations: 5
Covariance Type: nonrobust
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
Intercept 0.0514 0.100 0.514 0.607 -0.145 0.248
I(P - p) -0.5222 0.104 -5.022 0.000 -0.726 -0.318
I(N - n) -0.5374 0.131 -4.116 0.000 -0.793 -0.282
I(B - b) -0.4953 0.107 -4.650 0.000 -0.704 -0.287
I(R - r) -0.2619 0.140 -1.870 0.062 -0.536 0.013
I(Q - q) -2.5064 0.259 -9.695 0.000 -3.013 -2.000
==============================================================================
I'm suffering second day in the row and completely stuck.
I just want to calculate piece weights but it feels like I would never achieve that goal.
Code from original post is in R which I don't know:
Code: Select all
games <- as.data.frame(readRDS("milionbase_matrix.rds"))
# filter draws
games <- games[games$result != 0, ]
# create white_win col, assign 1 to it if white wins
games$white_win <- games$result == 1
fit <- glm(white_win ~ I(white_pawn - black_pawn) + I(white_knight - black_knight) +
I(white_bishop - black_bishop) + I(white_rook - black_rook) + I(white_queen - black_queen),
data = games, family="binomial")
I feel very very confused and depressed for my efforts does not help.
How on earth can I calculate piece weights using python??
Please help. Thanks in advance.