def run(self, data, verbose=False): ''' Override function from superclass. :param data: Pandas dataframe on which to test model. :return: void ''' r = self.player_rankings p = self.params glicko = rs.Glicko2(p["mu"], p["phi"], p["sigma"], p["tau"], p["eps"]) probabilities = [] bet_amount = [] P = [] Q = [] for i, row in data.iterrows(): name1 = row["Winner"] name2 = row["Loser"] wsp1 = row["WSP1"] wsp2 = row["WSP2"] # If there is no data on serve win percentage, skip. if math.isnan(wsp1) or math.isnan(wsp2): probabilities.append(0.5) bet_amount.append(0) continue if not r.get(name1, False): r[name1] = [glicko.create_rating(), glicko.create_rating()] if not r.get(name2, False): r[name2] = [glicko.create_rating(), glicko.create_rating()] p = glicko.expect(r[name1][0], r[name2][1]) q = glicko.expect(r[name2][0], r[name1][1]) P.append(p) Q.append(q) win_prob = calc.prob_win_match(p, q, row["Best_of"]) probabilities.append(win_prob) bet_amount.append(1) r[name1][0], r[name2][1] = glicko.rate_1vs1(r[name1][0], r[name2][1], wsp1 ) r[name2][0], r[name1][1] = glicko.rate_1vs1(r[name2][0], r[name1][1], wsp2) self.player_rankings = r if verbose: sez = sorted([[rt[0].mu, rt[1].mu, key] for key, rt in r.iteritems()], key=lambda x: x[0]+x[1], reverse=True) print '\n'.join(['{:>22s} {:8.1f} {:8.1f}'.format(p[2], p[0], p[1]) for p in sez[:20]]) + "\n\n" df = data.copy() df["win_prob"] = probabilities df["bet_amount"] = bet_amount return df
def run(self, data, verbose=False): ''' Override function from superclass. :param data: Pandas dataframe on which to test model. :return: void ''' r = self.player_rankings probabilities = [] bet_amount = [] for i, row in data.iterrows(): name1 = row["Winner"] name2 = row["Loser"] wsp1 = row["WSP1"] wsp2 = row["WSP2"] # If there is no data on serve win percentage, skip. if math.isnan(wsp1) or math.isnan(wsp2): probabilities.append(0.5) bet_amount.append(0) continue if not r.get(name1, False): r[name1] = Player() if not r.get(name2, False): r[name2] = Player() s1, r1 = r[name1].get() s2, r2 = r[name2].get() p = s1 + r2 - 0.4 q = s2 + r1 - 0.4 win_prob = calc.prob_win_match(p, q, row["Best_of"]) probabilities.append(win_prob) bet_amount.append(1) r[name1].add(wsp1, 1.0 - wsp2) r[name2].add(wsp2, 1.0 - wsp1) self.player_rankings = r if verbose: sez = sorted([[rt.get()[0], rt.get()[1], key] for key, rt in r.iteritems()], key=lambda x: x[0]+x[1], reverse=True) print '\n'.join(['{:>22s} {:8.5f} {:8.5f}'.format(p[2], p[0], p[1]) for p in sez[:20]]) + "\n\n" df = data.copy() df["win_prob"] = probabilities df["bet_amount"] = bet_amount return df
def win_probabilities(self, player1, player2, best_of=3): ''' Calculates fair betting odds according to our model and returns them as tuple. :param player1: Class player for player1 :param player2: Class player for player2 :param best_of: How many sets we are playing :return: Tuple (odds1, odds2), representing fair odds for player one or two winning ''' p, q = self.expect(player1, player2) win_prob = calc.prob_win_match(p, q, best_of) return (win_prob, 1 - win_prob, p, q)
def run(self, data, verbose=False): ''' Override function from superclass. :param data: Pandas dataframe on which to test model. :return: void ''' r = self.player_rankings p = self.params glicko = rs.Glicko2(p["mu"], p["phi"], p["sigma"], p["tau"], p["eps"]) probabilities = [] bet_amount = [] P = [] Q = [] for i, row in data.iterrows(): name1 = row["Winner"] name2 = row["Loser"] wsp1 = row["WSP1"] wsp2 = row["WSP2"] # If there is no data on serve win percentage, skip. if math.isnan(wsp1) or math.isnan(wsp2): probabilities.append(0.5) bet_amount.append(0) continue if not r.get(name1, False): r[name1] = [glicko.create_rating(), glicko.create_rating()] if not r.get(name2, False): r[name2] = [glicko.create_rating(), glicko.create_rating()] p = glicko.expect(r[name1][0], r[name2][1]) q = glicko.expect(r[name2][0], r[name1][1]) P.append(p) Q.append(q) win_prob = calc.prob_win_match(p, q, row["Best_of"]) probabilities.append(win_prob) bet_amount.append(1) r[name1][0], r[name2][1] = glicko.rate_1vs1( r[name1][0], r[name2][1], wsp1) r[name2][0], r[name1][1] = glicko.rate_1vs1( r[name2][0], r[name1][1], wsp2) self.player_rankings = r if verbose: sez = sorted([[rt[0].mu, rt[1].mu, key] for key, rt in r.iteritems()], key=lambda x: x[0] + x[1], reverse=True) print '\n'.join([ '{:>22s} {:8.1f} {:8.1f}'.format(p[2], p[0], p[1]) for p in sez[:20] ]) + "\n\n" df = data.copy() df["win_prob"] = probabilities df["bet_amount"] = bet_amount return df
qg = game() p = p_t confirm = calculations._prob_win_game(p) print "p:", p print_table(memo1) print "Confirming with calculations: ", confirm print # Tiebreaker. pt = tiebreaker() confirm = calculations._prob_win_tiebreaker(p, q) print "pt:", pt print_table(memo2) print "Confirming with calculations: ", confirm print # Set. ps = set() confirm = calculations._prob_win_set(p, q) print "pg, qg:", pg, qg print_table(memo3) print "Confirming with calculations: ", confirm print # Match. pm = match(best_of=5) confirm = calculations.prob_win_match(p, q, 5) print "pm, ", pm print_table(memo4) print "Confirming with calculations: ", confirm print
def run(self, data, verbose=False): ''' Override function from superclass. :param data: Pandas dataframe on which to test model. :return: void ''' r = self.player_rankings g = self.player_games elo = rs.Elo(self.params["mu"], self.params["K"]) edge = self.params["edge"] probabilities = [] bet_amount = [] for i, row in data.iterrows(): name1 = row["Winner"] name2 = row["Loser"] wsp1 = row["WSP1"] wsp2 = row["WSP2"] # If there is no data on serve win percentage, skip. if math.isnan(wsp1) or math.isnan(wsp2): probabilities.append(0.5) bet_amount.append(0) continue if not r.get(name1, False): r[name1] = [elo.create_rating(), elo.create_rating()] if not r.get(name2, False): r[name2] = [elo.create_rating(), elo.create_rating()] p = elo.expect(r[name1][0], r[name2][1]) + edge q = elo.expect(r[name2][0], r[name1][1]) + edge win_prob = calc.prob_win_match(p, q, row["Best_of"]) probabilities.append(win_prob) # probabilities.append(random.random()) # bet_amount.append(1) g[name1] = g.get(name1, 0.0) + 1 g[name2] = g.get(name2, 0.0) + 1 if g[name1] > 50 and g[name2] > 50: bet_amount.append(1) else: bet_amount.append(0) r[name1][0], r[name2][1] = elo.match(r[name1][0], r[name2][1], wsp1 - edge) r[name2][0], r[name1][1] = elo.match(r[name2][0], r[name1][1], wsp2 - edge) self.player_rankings = r self.player_games = g if verbose: sez = sorted([[rt[0].rating, rt[1].rating, key] for key, rt in r.iteritems()], key=lambda x: x[0] + x[1], reverse=True) print '\n'.join([ '{:>22s} {:8.1f} {:8.1f}'.format(str(p[2]), p[0], p[1]) for p in sez[:20] ]) + "\n\n" df = data.copy() df["win_prob"] = probabilities df["bet_amount"] = bet_amount return df
def run(self, data, verbose=False): ''' Override function from superclass. :param data: Pandas dataframe on which to test model. :return: void ''' r = self.player_rankings s = self.surface_advantage g = self.player_games elo = rs.Elo(self.params["mu"], self.params["K"]) edge = self.params["edge"] probabilities = [] bet_amount = [] for i, row in data.iterrows(): name1 = row["Winner"] name2 = row["Loser"] wsp1 = row["WSP1"] wsp2 = row["WSP2"] # If there is no data on serve win percentage, skip. if math.isnan(wsp1) or math.isnan(wsp2): probabilities.append(0.5) bet_amount.append(0) continue if not r.get(name1, False): r[name1] = [elo.create_rating(), elo.create_rating()] s[name1] = Player() if not r.get(name2, False): r[name2] = [elo.create_rating(), elo.create_rating()] s[name2] = Player() s1, r1, h11, h12 = s[name1].get_advantage(row["Surface"]) s2, r2, h21, h22 = s[name2].get_advantage(row["Surface"]) h1 = h11 + h22 h2 = h12 + h21 a1 = s1 - r2 if a1 + h1 < 0.0: a1 += h1 elif a1 - h1 > 0.0: a1 -= h1 else: a1 = 0 a2 = s2 - r1 if a2 + h2 < 0.0: a2 += h2 elif a2 - h2 > 0.0: a2 -= h2 else: a2 = 0 p = elo.expect(r[name1][0], r[name2][1]) + edge + a1 q = elo.expect(r[name2][0], r[name1][1]) + edge + a2 s[name1].update(wsp1, 1.0 - wsp2, row["Surface"]) s[name2].update(wsp2, 1.0 - wsp1, row["Surface"]) win_prob = calc.prob_win_match(p, q, row["Best_of"]) g[name1] = g.get(name1, 0.0) + 1 g[name2] = g.get(name2, 0.0) + 1 """ if g[name1]>5 and g[name2]>5: bet_amount.append(1) else: bet_amount.append(0) """ bet_amount.append(1) probabilities.append(win_prob) r[name1][0], r[name2][1] = elo.match(r[name1][0], r[name2][1], wsp1 - edge) r[name2][0], r[name1][1] = elo.match(r[name2][0], r[name1][1], wsp2 - edge) self.surface_advantage = s self.player_rankings = r self.player_games = g if verbose: sez = sorted([[rt[0].rating, rt[1].rating, str(key)] for key, rt in r.iteritems()], key=lambda x: x[0]+x[1], reverse=True) print '\n'.join(['{:>22s} {:8.1f} {:8.1f}'.format(p[2], p[0], p[1]) for p in sez[:20]]) + "\n\n" df = data.copy() df["win_prob"] = probabilities df["bet_amount"] = bet_amount return df
pg = game() p_t = p; p = q; qg = game(); p = p_t confirm = calculations._prob_win_game(p) print "p:", p print_table(memo1) print "Confirming with calculations: ", confirm print # Tiebreaker. pt = tiebreaker() confirm = calculations._prob_win_tiebreaker(p, q) print "pt:", pt print_table(memo2) print "Confirming with calculations: ", confirm print # Set. ps = set() confirm = calculations._prob_win_set(p, q) print "pg, qg:", pg, qg print_table(memo3) print "Confirming with calculations: ", confirm print # Match. pm = match(best_of=5) confirm = calculations.prob_win_match(p, q, 5) print "pm, ", pm print_table(memo4) print "Confirming with calculations: ", confirm print
def run(self, data, verbose=False): """ Override function from superclass. :param data: Pandas dataframe on which to test model. :return: void """ r = self.player_rankings g = self.player_games elo = rs.Elo(self.params["mu"], self.params["K"]) edge = self.params["edge"] probabilities = [] bet_amount = [] for i, row in data.iterrows(): name1 = row["Winner"] name2 = row["Loser"] wsp1 = row["WSP1"] wsp2 = row["WSP2"] # If there is no data on serve win percentage, skip. if math.isnan(wsp1) or math.isnan(wsp2): probabilities.append(0.5) bet_amount.append(0) continue if not r.get(name1, False): r[name1] = [elo.create_rating(), elo.create_rating()] if not r.get(name2, False): r[name2] = [elo.create_rating(), elo.create_rating()] p = elo.expect(r[name1][0], r[name2][1]) + edge q = elo.expect(r[name2][0], r[name1][1]) + edge win_prob = calc.prob_win_match(p, q, row["Best_of"]) probabilities.append(win_prob) # probabilities.append(random.random()) # bet_amount.append(1) g[name1] = g.get(name1, 0.0) + 1 g[name2] = g.get(name2, 0.0) + 1 if g[name1] > 50 and g[name2] > 50: bet_amount.append(1) else: bet_amount.append(0) r[name1][0], r[name2][1] = elo.match(r[name1][0], r[name2][1], wsp1 - edge) r[name2][0], r[name1][1] = elo.match(r[name2][0], r[name1][1], wsp2 - edge) self.player_rankings = r self.player_games = g if verbose: sez = sorted( [[rt[0].rating, rt[1].rating, key] for key, rt in r.iteritems()], key=lambda x: x[0] + x[1], reverse=True, ) print "\n".join(["{:>22s} {:8.1f} {:8.1f}".format(str(p[2]), p[0], p[1]) for p in sez[:20]]) + "\n\n" df = data.copy() df["win_prob"] = probabilities df["bet_amount"] = bet_amount return df
__author__ = 'riko' import time import calculations as calc p = 0.70 q = 0.70 start = time.clock() m1 = calc.prob_win_match(p, q) m2 = calc.monte_carlo_match(p, q) end = time.clock() print m1 print m2 print "Time: ", end - start