-
Notifications
You must be signed in to change notification settings - Fork 2
/
one_match_simulator.py
87 lines (70 loc) · 2.5 KB
/
one_match_simulator.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
import pandas as pd
import numpy as np
import time
import math
import method
def promoted_teams_attack(old_param):
parameter = 0.53683956207924333 * old_param + 0.049598077388848139
return parameter
def promoted_teams_defense(old_param):
parameter = -0.88138228873841129 * old_param + 2.2188585603647821
return parameter
def pois(x, lambdaa):
result = (np.power(lambdaa, x) * np.exp(-lambdaa)) /math.factorial(x)
return result
def tau(x, y, lambdaa, mu, rho):
if x == 0 and y == 0:
result = 1 - (lambdaa*mu*rho)
elif x == 0 and y == 1:
result = 1 + (lambdaa*rho)
elif x == 1 and y == 0:
result = 1 + (mu*rho)
elif x == 1 and y == 1:
result = 1 - rho
else:
result = 1
return result
def bivpois2(lambdaa, mu, rho):
max_goals=6
weights = []
population = []
for i in range(0, max_goals+1):
for j in range(0, max_goals+1):
population.append([i,j])
weights.append(tau(i, j, lambdaa, mu, rho)*pois(i, lambdaa)*pois(j, mu))
return population, weights
def predictor2(population, weights):
new = random.choices(population, weights)
return new
def calculate_win_perc(population, weights):
homeperc, drawperc, awayperc = 0, 0, 0
for i in range(0, len(population)):
if population[i][0] > population[i][1]:
homeperc += weights[i]
elif population[i][0] == population[i][1]:
drawperc += weights[i]
elif population[i][0] < population[i][1]:
awayperc += weights[i]
return [homeperc, drawperc, awayperc]
def calculate_goal_line(population, weights):
result = 0
for i in range(0, len(population)):
if population[i][0] + population[i][1] > 2:
result += weights[i]
return result, 1-result
team_ratings = method.read_in_team_ratings()
home_team = input('Enter the home team: ')
time.sleep(0.5)
away_team = input('Enter the away team: ')
home_attack = team_ratings.loc[home_team]['HomeAttack']
home_defense = team_ratings.loc[home_team]['HomeDefense']
away_attack = team_ratings.loc[away_team]['AwayAttack']
away_defense = team_ratings.loc[away_team]['AwayDefense']
population, weights = bivpois2(home_attack*away_defense, away_attack*home_defense, 0.15)
percentages = calculate_win_perc(population, weights)
overs, unders = calculate_goal_line(population, weights)
print(home_team, 1/percentages[0])
print('Draw', 1/percentages[1])
print(away_team, 1/percentages[2])
print('O 2.5', 1/overs)
print('U 2.5', 1/unders)