-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinprog2.py
128 lines (95 loc) · 4.56 KB
/
linprog2.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
# -*- coding: utf-8 -*-
"""LinProg.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1imsK0lsIKdHyHf-tUW6ptQ-ugD8lYhds
"""
from bf_lightweight import pd, np, funds
from calculate_payoffs import payoffs_total
from get_bet_sizes import bet_size_df
from market_catalogues import market_catalogue_filtered
# from get_market_books import runners_df
from current_orders import payoffs_bets_df, hedge_scenarios
from read_outcome_table import home_score, away_score
import pulp
# payoffs = pd.read_csv('/content/Payoffs.csv', index_col=[0,1,2], header=[0,1])
payoffs = payoffs_total
# bet_sizes = pd.read_csv('/content/Bet_Size.csv', index_col=[0,1,2])
bet_sizes = bet_size_df
# market_catalogue = pd.read_csv('/content/market_catalogue.csv', index_col=[0])
market_catalogue = market_catalogue_filtered
market_list = market_catalogue['Market Name'].values
bs_array = bet_sizes.to_numpy().T
up_bounds, low_bounds = bs_array[0], bs_array[1]
df_bounds = pd.DataFrame({'Low': low_bounds, 'High': up_bounds}, index = payoffs.index)
# df_bounds.to_csv('bounds.csv')
p = payoffs.to_numpy()
if len(payoffs_bets_df) > 0:
bets_all_scenarios = payoffs_bets_df.iloc[:-1, :]
bets_hedged_scenarios = hedge_scenarios.to_numpy().T
else:
bets_hedged_scenarios = []
def linprog(bets, payoffs, up_bounds, low_bounds, amount=funds, home_score_ = 0, away_score_ = 0, score_goals = 9, hedge_goals = 9):
p = payoffs.to_numpy()
p_scores = payoffs.loc[:, pd.IndexSlice[(range(home_score_, score_goals+1)), (range(away_score_, score_goals+1))]].to_numpy()
p_constraints = payoffs.loc[:, pd.IndexSlice[(range(home_score_, hedge_goals+1)), (range(away_score_, hedge_goals+1))]].to_numpy()
# Get the shape of the p array
num_rows, num_cols = p.shape
num_rows_scores, num_cols_scores = p_scores.shape
n_rows_x, n_cols_x = p_constraints.shape
print(p_constraints.shape)
# Define the problem
problem = pulp.LpProblem("Maximize_Sum_Rz", pulp.LpMaximize)
# Define the decision variables
R = [pulp.LpVariable(f'R_{i}', lowBound=low_bounds[i], upBound=up_bounds[i], cat='Continuous') for i in range(num_rows)]
# Objective function: Maximize sum(R[j] * z[i, j])
objective = pulp.lpSum([R[i] * p_scores[i, j] for i in range(num_rows_scores) for j in range(num_cols_scores)])
# objective = pulp.lpSum([R @ z])
problem += objective
# Print the objective function
# print("Objective function: ", objective)
loss_allowance = 0.0
# Constraints: sum(R[j] * z[i, j]) >= 0 for each row i in z
if len(bets) > 0:
for j in range(n_cols_x):
# constraint = pulp.lpSum([R[i] * p[i, j] for i in range(num_rows)]) >= 0
constraint = pulp.lpSum([R[i] * p_constraints[i, j] for i in range(n_rows_x)] + list(bets[j])) >= 0
# print(f"Constraint for row {j}: {constraint}")
problem += constraint
else:
for j in range(n_cols_x):
# constraint = pulp.lpSum([R[i] * p[i, j] for i in range(num_rows)]) >= 0
constraint = pulp.lpSum([R[i] * p_constraints[i, j] for i in range(n_rows_x)]) >= 0
# print(f"Constraint for row {j}: {constraint}")
problem += constraint
# Add the constraint that the sum of R values must be less than or equal to 1000
sum_constraint = pulp.lpSum(R[j] for j in range(num_rows)) <= amount
problem += sum_constraint
# Solve the problem
problem.solve()
# Print the status of the solution
# print(f"Status: {pulp.LpStatus[problem.status]}")
# Print the values of R
R_values = np.zeros(num_rows)
for i in range(num_rows):
R_values[i] = R[i].varValue
return R_values
# print("R array:")
# print("Objective value:", pulp.value(problem.objective))
R_values = linprog(bets_hedged_scenarios, payoffs, up_bounds, low_bounds, funds, home_score, away_score)
# print(R_values)
bet_values = pd.DataFrame(R_values, index=payoffs.index, columns=['Bet Value'])
bet_values.to_csv('Bet_Values.csv')
from calculate_payoffs import payoffs_total
from event_IDs import home_team, away_team
payoffs = payoffs_total
payoffs_calc = R_values[:, None] * payoffs.to_numpy()
payoffs_df = pd.DataFrame(payoffs_calc, index = payoffs.index, columns = payoffs.columns)
if len(hedge_scenarios) > 0:
payoffs_df = pd.concat([payoffs_df, hedge_scenarios])
else:
pass
payoffs_df.loc['Total', :] = payoffs_df.sum().values
payoffs_df_filtered = payoffs_df.loc[~(payoffs_df == 0).all(axis=1)]
csv_filename = f'Payoffs_{home_team}_v_{away_team}.csv'
payoffs_df_filtered.to_csv(csv_filename)