forked from gruenatodes/HORST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
horst.py
593 lines (542 loc) · 23.3 KB
/
horst.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import sqlite3
from datetime import datetime
import json
import urllib2
import iso8601
from collections import defaultdict
import statsmodels.api as sm
import numpy as np
import postguess
import pyper as pr
import math
db_name = 'data/example.db'
avg_days = [1, 5, 17, 34]
reg_names = []
for avg_len in avg_days:
for team in ['home', 'guest']:
for sc in ['scored', 'conceded']:
reg_name = team + sc + str(avg_len)
reg_names.append(reg_name)
conn = sqlite3.connect(db_name)
conn.row_factory = sqlite3.Row
c = conn.cursor()
def find_prev_and_next_day():
c.execute('''SELECT season, day, date FROM bundesliga
WHERE datetime(date)>datetime('now', '+2hours')
ORDER BY date ASC LIMIT 1''')
row = c.fetchone()
next_day = row['day']
season = row['season']
c.execute('''SELECT season, day, date FROM bundesliga
WHERE datetime(date)<datetime('now', '+2hours')
AND NOT (season>=? AND day>=?)
ORDER BY date DESC LIMIT 1''', (season, next_day))
row = c.fetchone()
prev_day = row['day']
prev_season = row['season']
return prev_day, prev_season, next_day, season
def enter_results_for_day(day, season):
url = "http://openligadb-json.heroku.com/api/matchdata_by_group_league_saison?league_saison=%s&league_shortcut=bl1&group_order_id=%s" % (season-1, day)
daydata = urllib2.urlopen(url).read()
daydata = json.loads(daydata)['matchdata']
# good thing we put home and guest id in the table! :)
for game in daydata:
home_id = game['id_team1']
guest_id = game['id_team2']
goals_home = game['points_team1']
goals_guest = game['points_team2']
c.execute('''UPDATE bundesliga SET scorehome=?, scoreguest=?
WHERE season=? AND day=? AND home_id=? AND guest_id=?''',
(goals_home, goals_guest, season, day, home_id, guest_id))
conn.commit()
print "Ergebnisse von Spieltag %s in Saison %s aktualisiert." %(day,season)
def update_matchtimes_for_day(season, day):
c.execute('''SELECT hometeam, guestteam, date, home_id, guest_id
FROM bundesliga
WHERE season=? AND day=?''', (season, day))
db_data = c.fetchall()
url = "http://openligadb-json.heroku.com/api/matchdata_by_group_league_saison?league_saison=%s&league_shortcut=bl1&group_order_id=%s" % (season-1, day)
daydata = urllib2.urlopen(url).read()
daydata = json.loads(daydata)['matchdata']
for db_game in db_data:
for game in daydata:
db_home_id = int(db_game['home_id'])
db_guest_id = int(db_game['guest_id'])
if (db_home_id == int(game['id_team1']) and
db_guest_id == int(game['id_team2'])):
db_date = db_game['date']
datetm = game['match_date_time']
changes = []
if iso8601.parse_date(db_date) == iso8601.parse_date(datetm):
pass
else:
newdate = str(iso8601.parse_date(datetm))
newdate = newdate[:newdate.find('+')]
c.execute('''UPDATE bundesliga
SET date=?
WHERE season=? AND day=? AND home_id=? AND guest_id=?''',
(newdate, season, day, db_home_id, db_guest_id))
changes.append((db_game['hometeam'], db_game['guestteam']))
break
conn.commit()
print "Anstosszeiten fuer Spieltag %s, %s \
wurden geaendert:" %(day,season), changes
def get_avg_goals(tname, season, day, length=1, sc='scored'):
c.execute('''SELECT day FROM bundesliga
WHERE season=? GROUP BY day
ORDER by day DESC LIMIT 1''', (season-1,))
last_day_prev = c.fetchone()[0]
if sc == 'scored':
goalnames = ('scorehome', 'scoreguest')
if sc == 'conceded':
goalnames = ('scoreguest', 'scorehome')
query = "SELECT %s as 'teamscore' FROM bundesliga " % goalnames[0]
query += "WHERE hometeam='%s' " %tname
query += "AND ((season=%s AND day<%s AND day>=%s) " % (season, day,
day-length)
query += "OR (season=%s AND day>=%s)) " % (season-1, last_day_prev-
length+day)
query += "UNION ALL "
query += "SELECT %s as 'teamscore' FROM bundesliga " % goalnames[1]
query += "WHERE guestteam='%s' " %tname
query += "AND ((season=%s AND day<%s AND day>=%s) " % (season, day,
day-length)
query += "OR (season=%s AND day>=%s))" % (season-1, last_day_prev-
length+day)
goallist = c.execute(query).fetchall()
try:
su = sum([game[0] for game in goallist]) / (len(goallist) + 0.0)
except ZeroDivisionError:
su = None
except TypeError:
su = None
return su
def update_regressors_for_day(season, day):
# get info on day:
rows = c.execute('''SELECT hometeam, guestteam FROM bundesliga
WHERE season=%s AND day=%s''' % (season, day))
rows = rows.fetchall()
# First do new/onew
res = c.execute('''SELECT hometeam FROM bundesliga
WHERE season=%s GROUP BY hometeam''' % (season-1,))
teams_prev_season = [game['hometeam'] for game in res]
if len(teams_prev_season) > 0:
for row in rows:
reg_new, reg_onew = (0,0)
if row['hometeam'] not in teams_prev_season:
reg_new = 1
if row['guestteam'] not in teams_prev_season:
reg_onew = 1
c.execute('''UPDATE bundesliga
SET new =?, onew=?
WHERE season=? AND day=? AND hometeam=?''',
(reg_new, reg_onew, season, day, row['hometeam']))
# Then do goals for/against
for row in rows:
query = "UPDATE bundesliga SET "
for avg_len in avg_days:
for team in ['home', 'guest']:
tname = row[team + 'team']
for sc in ['scored', 'conceded']:
reg_name = team + sc + str(avg_len)
avg_goals = get_avg_goals(tname, season, day, avg_len, sc)
if avg_goals == None:
avg_goals = 'NULL'
else:
avg_goals = round(avg_goals, 2)
query += (reg_name + '=' + str(avg_goals) + ", ")
query = query[:-2] + " " # remove last comma
query += ("WHERE season=%s AND day=%s AND hometeam='%s'"
% (season, day, row['hometeam']))
c.execute(query)
conn.commit()
def get_data(this_season, next_day):
c.execute('''SELECT season FROM bundesliga GROUP BY season
ORDER BY season ASC LIMIT 1''')
first_season = int(c.fetchone()[0])
query = '''SELECT * FROM bundesliga
WHERE season>=? AND NOT (season >=? AND day >=?)
ORDER BY season, day, date, hometeam'''
data = []
c.execute(query, (first_season+1, this_season, next_day))
rows = c.fetchall()
keys = rows[0].keys()
for game in rows:
dic = {}
for k in keys:
dic[k] = game[k]
if dic[k] == None:
dic[k] = -1
data.append(dic)
return data
def encode_results(results, method='results'):
unique_results = list(set(results))
histo = defaultdict(int)
for x in results:
histo[x] += 1
## for x in sorted(histo, key=histo.get, reverse=True):
## print x, histo[x]
outcome_codes = {} # this maps results into integer codes
if method == 'results':
for result in unique_results:
if result[0] == result[1]: # tie: (0,0): 0, (1,1): 1, (2,2) or higher: 2
if result[0] <= 1:
outcome_codes[result] = result[0] # 0,1 - 0:0, 1:1
else:
outcome_codes[result] = 2 # 2 - 2:2+
elif result[0] > result[1]: # win: sort by (goal diff, goals scored)
if result[0] == result[1] + 1:
outcome_codes[result] = 2 + min(result[0], 2) # 3,4 - 1:0, 2:1+
elif result[0] == result[1] + 2:
outcome_codes[result] = 3 + min(result[0], 3) # 5,6 - 2:0, 3:1+
elif result[0] == result[1] + 3:
outcome_codes[result] = 7 # 7 - 3:0+
else: # result[0] >= result[1] + 4
outcome_codes[result] = 12 # 13 - 4:0+
else: # loss
if result[0] == result[1] - 1:
outcome_codes[result] = 7 + min(result[1], 2) # 8,9 - 0:1, 1:2+
elif result[0] == result[1] - 2:
outcome_codes[result] = 10 # 10 - 0:2+
else: # result[0] >= result[1] - 3
outcome_codes[result] = 11 # 11 - 0:3+
elif method == 'toto':
for result in unique_results:
if result[0] == result[1]: #tie
outcome_codes[result] = 0
elif result[0] > result[1]: #win
outcome_codes[result] = 1
elif result[0] < result[1]: #loss
outcome_codes[result] = 2
outcomes = map(lambda x: outcome_codes[x], results)
return outcomes, outcome_codes
def give_predictions(y, X, X_predict, decode, rows):
print 'Now regressing...'
estimators = sm.MNLogit(y, X).fit()
## print estimators.summary()
y_predict = np.round(estimators.predict(X_predict), 4)
# find max element in prediction
tips = []
for i in range(len(y_predict)):
dic = {}
dic['home_id'] = rows[i]['home_id']
dic['teams'] = (rows[i]['hometeam'], rows[i]['guestteam'])
dic['pred_array'] = y_predict[i]
dic['pred'] = decode[list(y_predict[i]).index(max(y_predict[i]))]
print rows[i]['hometeam'], rows[i]['guestteam'], dic['pred']
tips.append(dic)
return tips
# this function does logistic regression and prediction in python
def regress_and_predict(season, day, method='results'):
data = get_data(season, day)
results = [(game['scorehome'],game['scoreguest']) for game in data]
outcomes, code_dict = encode_results(results, method)
decode = {}
for k, v in code_dict.iteritems():
decode[v] = decode.get(v, [])
decode[v].append(k)
if None in decode: del decode[None]
for k in decode:
decode[k] = max(decode[k], key=lambda x: -x[0]-x[1])
histo = defaultdict(int)
for x in outcomes:
histo[x] += 1
print "Ein bisschen Geschichte:"
for x in sorted(histo, key=histo.get, reverse=True):
print x, decode[x], histo[x]
# Construct regressors
X = [] # list of lists
y = [] # just a vector
i = 0
for game in data:
regs = [1] # a constant
for team, new in [['home', 'new'], ['guest', 'onew']]:
for avg_len in avg_days:
for sc in ['scored', 'conceded']:
reg_name = team + sc + str(avg_len)
regs.append((1-game[new]) * game[reg_name])
regs.append(game['new'])
regs.append(game['onew'])
X.append(regs)
y.append(outcomes[i])
i += 1
X = np.array(X)
y = np.array(y)
# predictors:
query = ('''SELECT * FROM bundesliga
WHERE season=%s AND day=%s
ORDER BY date ASC, hometeam ASC'''
% (season, day) )
c.execute(query)
rows = c.fetchall()
X_predict = []
for game in rows:
regs = [1] # a constant
for team, new in [['home', 'new'], ['guest', 'onew']]:
for avg_len in avg_days:
for sc in ['scored', 'conceded']:
reg_name = team + sc + str(avg_len)
if game[reg_name] != None:
reg = game[reg_name]
else:
reg = -1
regs.append((1-game[new]) * reg)
regs.append(game['new'])
regs.append(game['onew'])
X_predict.append(regs)
X_predict = np.array(X_predict)
tips = give_predictions(y, X, X_predict, decode, rows)
return tips, decode
# do regression in R:
def reg_and_pred_R(season,day,method='results'):
data = get_data(season, day)
results = [(game['scorehome'],game['scoreguest']) for game in data]
outcomes, code_dict = encode_results(results, method)
decode = {}
for k, v in code_dict.iteritems():
decode[v] = decode.get(v, [])
decode[v].append(k)
if None in decode: del decode[None]
for k in decode:
decode[k] = max(decode[k], key=lambda x: -x[0]-x[1])
histo = defaultdict(int)
for x in outcomes:
histo[x] += 1
print "Ein bisschen Geschichte:"
for x in sorted(histo, key=histo.get, reverse=True):
print x, decode[x], histo[x]
# Construct regressors
X = [] # list of lists
y = [] # just a vector
i = 0
for game in data:
regs = [1] # a constant
for team, new in [['home', 'new'], ['guest', 'onew']]:
for avg_len in avg_days:
for sc in ['scored', 'conceded']:
reg_name = team + sc + str(avg_len)
regs.append((1-game[new]) * game[reg_name])
regs.append(game['new'])
regs.append(game['onew'])
X.append(regs)
y.append(outcomes[i])
i += 1
X = np.array(X)
y = np.array(y)
# predictors:
query = ('''SELECT * FROM bundesliga
WHERE season=%s AND day=%s
ORDER BY date ASC, hometeam ASC'''
% (season, day) )
c.execute(query)
rows = c.fetchall()
X_predict = []
for game in rows:
regs = [1] # a constant
for team, new in [['home', 'new'], ['guest', 'onew']]:
for avg_len in avg_days:
for sc in ['scored', 'conceded']:
reg_name = team + sc + str(avg_len)
if game[reg_name] != None:
reg = game[reg_name]
else:
reg = -1
regs.append((1-game[new]) * reg)
regs.append(game['new'])
regs.append(game['onew'])
X_predict.append(regs)
X_predict = np.array(X_predict)
# talk to R and do regression
r = pr.R(use_numpy=True)
r["buli"] = X
r("buli <- as.data.frame(buli)")
r["data_y"] = y
r("buli$res <- data_y")
r["data_X_pred"] = X_predict
r["helperlevels <- 0:%s" % 12]
r['helperlabels <- c("0:0", "1:1", "2:2", \
"1:0", "2:1+", "2:0", "3:1+", "3:0+", \
"0:1", "1:2+", "0:2", "1:3+", "0:3+")']
r["buli$res <- factor(buli$res, helperlevels, helperlabels)"]
r["rm(helperlevels, helperlabels)"]
r['mlognests <- list(tie = c("0:0", "1:1", "2:2+"), \
win = c("1:0", "2:1", "3:2+", "2:0", "3:1+", "3:0+"), \
loss= c("0:1", "1:2+", "0:2", "1:3+", "0:3+") )']
r("library(mlogit)")
r('bulireg <- mlogit.data(buli, shape="wide", choice="res")')
r('mlog.res <- mlogit(formula = result ~ 0 | \
homescored34 + homeconceded34 + guestscored34 + guestconceded34 + \
homescored17 + homeconceded17 + guestscored17 + guestconceded17 + \
homescored5 + homeconceded5 + guestscored5 + guestconceded5 + \
homescored1 + homeconceded1 + guestscored1 + guestconceded1, \
data=buli.reg, reflevel="0:0", nests=mlog.nests)')
return X, y, X_predict, r
def poisson_reg(season, day):
data = get_data(season, day)
results = [(game['scorehome'],game['scoreguest']) for game in data]
outcomes, code_dict = encode_results(results)
decode = {}
for k, v in code_dict.iteritems():
decode[v] = decode.get(v, [])
decode[v].append(k)
if None in decode: del decode[None]
for k in decode:
decode[k] = max(decode[k], key=lambda x: -x[0]-x[1])
# Construct regressors:
X = [] # list of lists (N x P matrix)
y = [] # also list of lists: N x 2 matrix. 2 outcomes per game (gamma_scorehome and gamma_scoreguest)
i = 0
for game in data:
regs = [1]
for team, new in [['home', 'new'], ['guest', 'onew']]:
for avg_len in avg_days:
for sc in ['scored', 'conceded']:
reg_name = team + sc + str(avg_len)
regs.append((1-game[new]) * game[reg_name])
regs.append(game['new'])
regs.append(game['onew'])
X.append(regs)
y.append([ results[i][0], results[i][1] ])
i += 1
X = np.array(X)
y = np.array(y)
# predictors:
query = ('''SELECT * FROM bundesliga
WHERE season=%s AND day=%s
ORDER BY date ASC, hometeam ASC'''
% (season, day) )
c.execute(query)
rows = c.fetchall()
X_predict = []
for game in rows:
regs = [1] # a constant
for team, new in [['home', 'new'], ['guest', 'onew']]:
for avg_len in avg_days:
for sc in ['scored', 'conceded']:
reg_name = team + sc + str(avg_len)
if game[reg_name] != None:
reg = game[reg_name]
else:
reg = -1
regs.append((1-game[new]) * reg)
regs.append(game['new'])
regs.append(game['onew'])
X_predict.append(regs)
X_predict = np.array(X_predict)
tips = poisson_predictions(y, X, X_predict, decode, rows)
return tips, decode
def poisson_predictions(y, X, X_predict, decode, rows):
y_home = y[:,0]
y_guest = y[:,1]
mod_home = sm.Poisson(y_home, X)
mod_guest = sm.Poisson(y_guest, X)
est_home = mod_home.fit()
est_guest = mod_guest.fit()
lambdas_home = est_home.predict(X_predict) # these are expected values - parameter lambda of the Poisson distribution
lambdas_guest = est_guest.predict(X_predict)
tips = []
for i in range(len(lambdas_home)):
dic = {}
dic['home_id'] = rows[i]['home_id']
dic['teams'] = (rows[i]['hometeam'], rows[i]['guestteam'])
pred_array = []
for code, res in decode.iteritems():
pred_array.append(poisson_llh(res, lambdas_home[i], lambdas_guest[i]))
pred_array = [(j+0.0) / sum(pred_array) for j in pred_array] # normalize to 1
dic['pred_array'] = np.round(pred_array,4)
dic['pred'] = decode[pred_array.index(max(pred_array))]
print rows[i]['hometeam'], rows[i]['guestteam'], dic['pred'], lambdas_home[i], lambdas_guest[i]
tips.append(dic)
return tips
def poisson_llh(result, lambda_h, lambda_g):
pmf = lambda x,lambd: lambd ** (x+0.0) / (math.factorial(x) * math.exp(lambd+0.0))
return pmf(result[0], lambda_h) * pmf(result[1], lambda_g)
def maximize_expected_points(tips, decode):
point_dist = [5,3,2]
lis = sorted(decode.keys())
print "Nach Beruecksichtigung der erwarteten Punkte:"
for j in range(len(tips)):
game = tips[j]
probs = {}
for i in range(len(lis)):
probs[lis[i]] = round(game['pred_array'][i],4)
exp_pts = {}
for outcome in sorted(decode.keys()):
# find results that bring highest points (equality)
exp_pts[outcome] = point_dist[0] * probs[outcome]
# finc results that bring 2nd highest points (same goal diff)
prob = 0
for other in sorted(decode.keys()):
if (decode[other][0] - decode[other][1] ==
decode[outcome][0] - decode[outcome][1] and
other != outcome):
prob += probs[other]
exp_pts[outcome] += point_dist[1] * prob
# find results that bring 3rd highest points (same tendency)
prob = 0
outc_tend = 0
if decode[outcome][0] > decode[outcome][1]:
outc_tend = 1
elif decode[outcome][0] < decode[outcome][1]:
outc_tend = 2
for other in sorted(decode.keys()):
other_tend = 0
if decode[outcome][0] > decode[outcome][1]:
other_tend = 1
elif decode[outcome][0] < decode[outcome][1]:
other_tend = 2
if (decode[other][0] - decode[other][1] !=
decode[outcome][0] - decode[outcome][1] and
other != outcome and
outc_tend == other_tend):
prob += probs[other]
exp_pts[outcome] += point_dist[2] * prob
exp_pts[outcome] = round(exp_pts[outcome], 3)
best_guess = max(exp_pts, key=exp_pts.get)
tips[j]['pred'] = decode[best_guess]
tips[j]['exp_pts'] = exp_pts[best_guess]
print game['teams'][0], game['teams'][1], decode[best_guess], exp_pts[best_guess]
return tips
def submit_guess_for_day(season, day, tips, dest='botliga'):
url = "http://openligadb-json.heroku.com/api/\
matchdata_by_group_league_saison?\
league_saison=%s&league_shortcut=bl1&group_order_id=%s" % (season-1, day)
daydata = urllib2.urlopen(url).read()
daydata = json.loads(daydata)['matchdata']
submission = {}
if dest == 'botliga':
for game in daydata:
match_id = int(game['match_id'])
home_id = int(game['id_team1'])
for game_dic in tips:
if game_dic['home_id'] == home_id:
result_string = (str(game_dic['pred'][0]) + ":" +
str(game_dic['pred'][1]))
submission[match_id] = result_string
break
print 'Uebertrage Tipps an botliga...'
print postguess.botliga_post(submission)
elif dest == 'botligaPoisson':
for game in daydata:
match_id = int(game['match_id'])
home_id = int(game['id_team1'])
for game_dic in tips:
if game_dic['home_id'] == home_id:
result_string = (str(game_dic['pred'][0]) + ":" +
str(game_dic['pred'][1]))
submission[match_id] = result_string
break
print 'Uebertrage Tipps an botliga...'
print postguess.botliga_poisson_post(submission)
elif dest == 'prozent':
for game in daydata:
match_id = int(game['match_id'])
home_id = int(game['id_team1'])
for game_dic in tips:
if game_dic['home_id'] == home_id:
prob_triple = tuple(map(lambda x: round(x, 4),
game_dic['pred_array']))
submission[match_id] = [prob_triple, season, day]
break
print 'Uebertrage Tipps an die Prozentrunde...'
print postguess.prozent_post(submission)