-
Notifications
You must be signed in to change notification settings - Fork 0
/
probDiff.py
174 lines (152 loc) · 5.11 KB
/
probDiff.py
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
import networkx as nx
import csv
from sets import Set
import random
import numpy as np
from collections import Counter
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# General Maintenance Functions
def loadResults(rfName):
results = []
with open(rfName,'rb') as ifile:
f = csv.reader(ifile)
header = f.next()
for row in f:
results.append(row)
return results
def loadTeamNames(tfName):
names = {}
with open(tfName,'rb') as ifile:
f = csv.reader(ifile)
header = f.next()
for row in f:
names[row[0]] = row[1]
return names
def selTeamGames(data,team):
return [x for x in data if (x[2] == team or x[4] == team)]
def getTIDs(data):
return list(Set([x[2] for x in data] + [x[4] for x in data]))
def flatten(l):
return [x for v in l for x in v]
def getWL(data,t1,t2):
games = selTeamGames(selTeamGames(data,t1),t2)
nGames = len(games)
nT1Win = 0
if int(t1)<int(t2):
for game in games:
if game[2] == t1:
nT1Win+=1
else:
print "teams in wrong order."
if nGames == 0:
return "NA"
else:
return nT1Win/float(nGames)
def getWLDiff(data,t1,t2):
games = selTeamGames(selTeamGames(data,t1),t2)
nGames = len(games)
ptDiffT1 = 0
if int(t1)<int(t2):
for game in games:
if game[2] == t1:
ptDiffT1 += int(game[3]) - int(game[5])
else:
ptDiffT1 -= int(game[3]) - int(game[5])
else:
print "teams in wrong order."
if nGames == 0:
return "NA"
else:
return ptDiffT1/float(nGames)
def getMultiples(games,teams):
nTeams = len(teams)
pairs = {}
for i in range(nTeams):
print teams[i]
for j in range(i+1,nTeams):
t1 = teams[i]
t2 = teams[j]
pair = set([t1,t2])
pairHandle = "{0}_{1}".format(t1,t2)
for game in games:
if pair == set([game[2],game[4]]):
if not pairs.has_key(pairHandle):
pairs[pairHandle] = {1:game}
else:
pairs[pairHandle][len(pairs[pairHandle].keys())+1] = game
multiples = [xKey for xKey in pairs.keys() if len(pairs[xKey].keys()) > 1]
print "Season has {0} pairs of teams that played multiple times.".format(len(multiples))
multipleD = pairs
for item in multipleD.keys():
if item not in multiples:
del multipleD[item]
return multipleD
def genDiffPoints(pairs):
results = []
for pair in pairs:
data = pairs[pair]
teams = pair.split('_')
diffs = np.array([int(x[3])-int(x[5]) for x in data.values()
if x[2] == teams[0]] + [int(x[5])-int(x[3])
for x in data.values() if x[4] == teams[0]])
print diffs
for i in range(len(diffs)):
mine = diffs[i]
rest = diffs[0:i]+diffs[i+1:]
winPct = np.mean([int(x>0) for x in rest])
results.append((mine,winPct))
return np.array(results)
def genAvgDiffPoints(pairs):
results = []
for pair in pairs:
data = pairs[pair]
teams = pair.split('_')
diffs = np.array([int(x[3])-int(x[5]) for x in data.values()
if x[2] == teams[0]] + [int(x[5])-int(x[3])
for x in data.values() if x[4] == teams[0]])
avgDiff = np.mean(diffs)
winPct = np.mean(diffs>0)
results.append((avgDiff,winPct))
return np.array(results)
def sigmoid(x,k):
y = 1/(1+np.exp(-k*(x)))
return y
def lossFx(yTrue,scoreDiff):
k = 0.19
#
pProb = sigmoid(scoreDiff,k)
pPred = scoreDiff>0
pPred = pPred.astype(int)
#
n = len(yTrue)
loss = (-1/n)*np.sum(yTrue*np.log(pProb)+(1-yTrue)*np.log(1-pProb))
return loss
def main():
dbHeader = "C:/Users/Ted/Dropbox"
rsfName = dbHeader + "/Kording Lab/Projects/MarchMadness/Data/regular_season_results.csv"
teamfName = dbHeader + "/Kording Lab/Projects/MarchMadness/Data/teams.csv"
trainName = dbHeader + "/Kording Lab/Projects/MarchMadness/Data/features_D.csv"
featName = dbHeader + "/Kording Lab/Projects/MarchMadness/Data/all_D.csv"
season = 'D'
games = [game for game in loadResults(rsfName) if game[0]==season]
tIDs = sorted(getTIDs(games))
pairs = getMultiples(games,tIDs)
#diffPts = genDiffPoints(pairs)
diffPts = genAvgDiffPoints(pairs)
x1data = diffPts[:,0]
y1data = diffPts[:,1]
x2data = xdata*-1
y2data = 1-ydata
xdata = np.append(x1data,x2data)
ydata = np.append(y1data,y2data)
popt, pcov = curve_fit(sigmoid,xdata,ydata)
slope = popt[0]/4
yprime = sigmoid(xdata,*popt)
distinctDiffs = np.unique(xdata)
binnedAvg = [np.mean(y[(xdata==ind).nonzero()]) for ind in distinctDiffs]
plt.figure(1)
plt.plot(diffPts[:,0],diffPts[:,1],'k.')
plt.plot(xdata,yprime,'b.')
plt.plot(distinctDiffs,binnedAvg,'g.')
plt.show()