-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevenCompetition.py
More file actions
executable file
·95 lines (63 loc) · 4.96 KB
/
evenCompetition.py
File metadata and controls
executable file
·95 lines (63 loc) · 4.96 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
#!/usr/bin/env python
# TODO Account for draws
import matplotlib.pyplot as plt
import pymysql
import sys
import numpy as np
from winSizeVsLadderPositionCorrelation import genCorrolationScore
from dbConnection import createReaderConnection
dbhandler = createReaderConnection()
cursor = dbhandler.cursor()
years = []
evenness_scores = []
num_upsets = []
margins = []
table_difference_ratio = []
sum_upsets = []
correlation_factors = []
for YEAR in range(1990, 2020 + 1):
years.append(YEAR)
# Number of times lower beats upper
number_of_upsets_sql = "SELECT COUNT(*) FROM (SELECT CAST(ltp.position AS SIGNED) - CAST(wtp.position AS SIGNED) AS Total FROM games INNER JOIN rounds r ON games.round = r.id INNER JOIN seasons s ON s.id = r.season INNER JOIN teams wt ON games.winning_team = wt.id INNER JOIN teams lt ON games.losing_team = lt.id INNER JOIN ladder_entries wtp ON wtp.team = games.winning_team INNER JOIN rounds wr ON wr.id = wtp.round INNER JOIN seasons ws ON ws.id = wr.season INNER JOIN ladder_entries ltp ON ltp.team = games.losing_team INNER JOIN rounds lr ON lr.id = ltp.round INNER JOIN seasons ls ON ls.id = lr.season WHERE s.id = ws.id AND ws.id = ls.id AND s.year = " + str(YEAR) + " AND wtp.position > ltp.position ORDER BY games.start_datetime ASC) x;"
cursor.execute(number_of_upsets_sql)
results = cursor.fetchall()
number_of_upsets = results[0][0]
number_of_games_sql = "SELECT COUNT(*) FROM (SELECT games.id FROM games INNER JOIN rounds r ON games.round = r.id INNER JOIN seasons s ON s.id = r.season WHERE s.year = " + str(YEAR) + ") x"
cursor.execute(number_of_games_sql)
results = cursor.fetchall()
number_of_games = results[0][0]
upset_ratio = (number_of_upsets)/(number_of_games)
num_upsets.append(upset_ratio)
# Average margin
margin_sql = "SELECT AVG(CAST(games.winning_score AS SIGNED)/CAST(games.losing_score AS SIGNED)) AS Margin FROM games INNER JOIN rounds r ON games.round = r.id INNER JOIN seasons s ON s.id = r.season INNER JOIN teams wt ON games.winning_team = wt.id INNER JOIN teams lt ON games.losing_team = lt.id INNER JOIN ladder_entries wtp ON wtp.team = games.winning_team INNER JOIN rounds wr ON wr.id = wtp.round INNER JOIN seasons ws ON ws.id = wr.season INNER JOIN ladder_entries ltp ON ltp.team = games.losing_team INNER JOIN rounds lr ON lr.id = ltp.round INNER JOIN seasons ls ON ls.id = lr.season WHERE s.id = ws.id AND ws.id = ls.id AND s.year = " + str(YEAR) + " ORDER BY games.start_datetime ASC;"
cursor.execute(margin_sql)
results = cursor.fetchall()
margin = float(results[0][0])
margins.append(margin)
# Correlation factor
cor_fact = genCorrolationScore(YEAR)
correlation_factors.append(cor_fact)
# Evenness
evenness = upset_ratio/(margin * cor_fact)
evenness_scores.append(evenness)
plt.plot(years, num_upsets, 'bo')
plt.plot(years, margins, 'yo')
plt.plot(years, correlation_factors, 'ro')
plt.plot(years, evenness_scores, 'go')
plt.title("How Uneven is the Competition?")
plt.ylabel("Uneven Factor")
plt.xlabel("Year")
plt.show()
# # Sum of table difference where lower beats upper
# sum_upset_sql = "SELECT sum(CAST(ltp.position AS SIGNED) - CAST(wtp.position AS SIGNED)) AS Total FROM games INNER JOIN rounds r ON games.round = r.id INNER JOIN seasons s ON s.id = r.season INNER JOIN teams wt ON games.winning_team = wt.id INNER JOIN teams lt ON games.losing_team = lt.id INNER JOIN ladder_entries wtp ON wtp.team = games.winning_team INNER JOIN rounds wr ON wr.id = wtp.round INNER JOIN seasons ws ON ws.id = wr.season INNER JOIN ladder_entries ltp ON ltp.team = games.losing_team INNER JOIN rounds lr ON lr.id = ltp.round INNER JOIN seasons ls ON ls.id = lr.season WHERE s.id = ws.id AND ws.id = ls.id AND s.year = " + str(YEAR) + " AND wtp.position > ltp.position ORDER BY games.start_datetime ASC;"
# cursor.execute(sum_upset_sql)
# results = cursor.fetchall()
# sum_upsets_result = results[0][0]
# # Sum of table difference where upper beats lower
# sum_standard_sql = "SELECT sum(CAST(ltp.position AS SIGNED) - CAST(wtp.position AS SIGNED)) AS Total FROM games INNER JOIN rounds r ON games.round = r.id INNER JOIN seasons s ON s.id = r.season INNER JOIN teams wt ON games.winning_team = wt.id INNER JOIN teams lt ON games.losing_team = lt.id INNER JOIN ladder_entries wtp ON wtp.team = games.winning_team INNER JOIN rounds wr ON wr.id = wtp.round INNER JOIN seasons ws ON ws.id = wr.season INNER JOIN ladder_entries ltp ON ltp.team = games.losing_team INNER JOIN rounds lr ON lr.id = ltp.round INNER JOIN seasons ls ON ls.id = lr.season WHERE s.id = ws.id AND ws.id = ls.id AND s.year = " + str(YEAR) + " AND wtp.position < ltp.position ORDER BY games.start_datetime ASC;"
# cursor.execute(sum_standard_sql)
# results = cursor.fetchall()
# sum_standard_result = results[0][0]
# sum_upsets.append(sum_upsets_result)
# table_difference_ratio.append((-sum_upsets_result/(sum_standard_result - sum_upsets_result)))
# print(sum_upsets_result, sum_standard_result)