-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_weights.js
More file actions
224 lines (198 loc) · 6.01 KB
/
get_weights.js
File metadata and controls
224 lines (198 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
function get_weights(pct_or_loc, type, player){
var coefs_to_use = loc_coefs
var max = .5
if (pct_or_loc === 'pct'){
coefs_to_use = pct_coefs
max = .05
}
var pos = pos_map[player]
var pos_coefs = coefs_to_use[type][pos]
var coefs = coefs_to_use[type][player]
var p_coefs = {}
for(var key in coefs) {
p_coefs[key] = coefs[key] + pos_coefs[key]
}
return p_coefs
}
function get_colors(coefs, pct_or_loc){
var colors = {}
var max = .5
if (pct_or_loc === 'pct'){
max = .03
}
for(var key in coefs) {
colors[key] = get_hex(coefs[key], max);
}
return colors
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function get_hex(value, max_val){
if (value > 0){
r = Math.round(225 + 25 * Math.min((value / max_val), 1))
g = Math.round(225 - 70 * Math.min((value / max_val), 1))
b = Math.round(225 - 225 * Math.min((value / max_val), 1))
}
else{
r = Math.round(225 + 225 * Math.max((value / max_val), -1))
g = Math.round(225 + 110 * Math.max((value / max_val), -1))
b = Math.round(225 + 25 * Math.max((value / max_val), -1))
}
return rgbToHex(r, g, b)
}
function logit_func(x){
return 1./ (1 + Math.exp(-x))
}
function transform_pct(pct_coefs){
var transformed = {}
for(var key in pct_coefs) {
transformed[key] = logit_func(pct_coefs[key] + pct_int[key])
}
return transformed
}
function transform_loc(loc_coefs){
var exp_loc = {}
var loc_sum = 0
for(var key in loc_coefs) {
exp_loc[key] = Math.exp(loc_coefs[key] + loc_int[key])
loc_sum += exp_loc[key]
}
for(var key in loc_coefs) {
exp_loc[key] /= loc_sum
}
return exp_loc
}
function score_series(pct_coefs, loc_coefs){
var pt_series = {
'Above the Break 3': 3,
'Backcourt': 3,
'In The Paint (Non-RA)': 2,
'Left Corner 3': 3,
'Mid-Range': 2,
'Restricted Area': 2,
'Right Corner 3': 3
}
var transformed_pct = transform_pct(pct_coefs)
var transformed_loc = transform_loc(loc_coefs)
var score = 0
for(var key in pct_coefs) {
score += transformed_pct[key] * transformed_loc[key] * pt_series[key]
}
return score
}
function get_base_score(){
if (base_score == -1){
var base = {
'Above the Break 3': 0,
'Backcourt': 0,
'In The Paint (Non-RA)': 0,
'Left Corner 3': 0,
'Mid-Range': 0,
'Restricted Area': 0,
'Right Corner 3': 0
}
var off_g = pct_coefs['off']['Guard']
var off_c = pct_coefs['off']['Center']
var off_f = pct_coefs['off']['Forward']
var def_g = pct_coefs['def']['Guard']
var def_c = pct_coefs['def']['Center']
var def_f = pct_coefs['def']['Forward']
for(var key in base) {
base_series_pct[key] = 2 * off_g[key] + 2 * off_f[key] + off_c[key] + 2 * def_g[key] + 2 * def_f[key] + def_c[key]
}
off_g = loc_coefs['off']['Guard']
off_c = loc_coefs['off']['Center']
off_f = loc_coefs['off']['Forward']
def_g = loc_coefs['def']['Guard']
def_c = loc_coefs['def']['Center']
def_f = loc_coefs['def']['Forward']
for(var key in base) {
base_series_loc[key] = 2 * off_g[key] + 2 * off_f[key] + off_c[key] + 2 * def_g[key] + 2 * def_f[key] + def_c[key]
}
base_score = score_series(base_series_pct, base_series_loc)
}
}
function get_combined_score(){
if ((off_score != -1) & (shoot_score != -1)){
var comb_pct = {}
for(var key in shoot_pct) {
comb_pct[key] = shoot_pct[key] + off_pct[key]
}
var comb_loc = {}
for(var key in shoot_loc) {
comb_loc[key] = shoot_loc[key] + off_loc[key]
}
var comb_score = score_series(comb_pct, comb_loc)
return comb_score
}
}
function get_new_series(player_coefs, player, coefs_dict, base_coefs){
var new_coefs = {}
var pos = pos_map[player]
var pos_coefs = coefs_dict[pos]
for(var key in player_coefs) {
new_coefs[key] = player_coefs[key] + base_coefs[key] - pos_coefs[key]
}
return new_coefs
}
function get_base_shooter(player, coefs_dict, base_coefs){
var new_coefs = {}
var pos = pos_map[player]
var off_coefs_ = coefs_dict['off'][pos]
var shoot_coefs_ = coefs_dict['shoot'][pos]
for(var key in base_coefs) {
new_coefs[key] = base_coefs[key] - off_coefs_[key] + shoot_coefs_[key]
}
return new_coefs
}
function adj_if_both_centers(shooter, off, coefs, base_coefs){
var pos_shooter = pos_map[shooter]
var pos_off = pos_map[off]
if ((pos_shooter == 'Center') & (pos_off == 'Center')){
var new_coefs = {}
var c_coefs = base_coefs['Center']
var f_coefs = base_coefs['Forward']
for (var key in coefs){
new_coefs[key] = coefs[key] + c_coefs[key] - f_coefs[key]
}
return new_coefs
}
return coefs
}
function get_off_score_to_use(shooter){
var pos_shooter = pos_map[shooter]
if (pos_shooter == 'Guard'){
return off_score_guard
}
else if (pos_shooter == 'Forward'){
return off_score_forward
}
else if (pos_shooter == 'Center'){
return off_score_center
}
else {
throw 'Bad position'
}
}
function get_off_base_to_use(pos_shooter){
if (pos_shooter == 'Guard'){
var player_eg = 202691
}
else if (pos_shooter == 'Forward'){
var player_eg = 2544
}
else if (pos_shooter == 'Center'){
var player_eg = 201599
}
else {
throw 'Bad position'
}
var new_base_pct_series1 = get_base_shooter(player_eg, pct_coefs, base_series_pct)
var new_base_loc_series1 = get_base_shooter(player_eg, loc_coefs, base_series_loc)
return new_base_pct_series1, new_base_loc_series1
}