Sure! Below is a Python program that allows you to set custom share prices and joint probabilities for the 2024 election prediction market involving two candidates: Kamala and Trump. The program uses linear programming to identify arbitrage opportunities where you can guarantee a profit by buying specific shares.
Overview
-
Define Share Prices and Joint Probabilities: Set the prices for "Yes" and "No" shares in both the Election Winner and Popular Vote markets. Also, define the joint probabilities for each possible outcome.
-
Set Up Linear Programming (LP) Problem: The program formulates an LP to maximize the guaranteed profit (epsilon). It ensures that the payouts from the purchased shares exceed the total cost for all possible outcomes.
-
Solve the LP Problem: Using scipy.optimize.linprog, the program solves the LP to find the optimal number of shares to buy in each market to maximize epsilon.
-
Interpret the Results: If the optimal epsilon is greater than zero, an arbitrage opportunity exists. The program will display the number of shares to buy and the expected profit.
Python Program
Make sure you have scipy installed. You can install it using pip if you haven't already:
pip install scipy
Here's the complete Python program:
import numpy as np
from scipy.optimize import linprog
def find_arbitrage_opportunity():
# Define share prices
# Prices for Election Winner market
E_yes = 0.6 # Price for Kamala winning the election
E_no = 0.4 # Price for Trump winning the election
# Prices for Popular Vote market
P_yes = 0.55 # Price for Kamala winning the popular vote
P_no = 0.45 # Price for Trump winning the popular vote
# Define joint probabilities
# p1: Kamala wins election AND wins popular vote
# p2: Kamala wins election AND loses popular vote (set to 0)
# p3: Trump wins election AND wins popular vote
# p4: Trump wins election AND loses popular vote (set to 0)
p1 = 0.5
p2 = 0.0
p3 = 0.4
p4 = 0.0
# Ensure that probabilities sum to 1
assert np.isclose(p1 + p2 + p3 + p4, 1.0), "Joint probabilities must sum to 1."
# Variables: [b_E_yes, b_E_no, b_P_yes, b_P_no, epsilon]
# Objective: maximize epsilon
# Since scipy.optimize.linprog does minimization, we minimize -epsilon
c = [0, 0, 0, 0, -1]
# Constraints:
# For Outcome 1: Kamala wins election and popular vote
# b_E_yes + b_P_yes >= cost + epsilon
# cost = E_yes*b_E_yes + E_no*b_E_no + P_yes*b_P_yes + P_no*b_P_no
# => b_E_yes + b_P_yes - E_yes*b_E_yes - E_no*b_E_no - P_yes*b_P_yes - P_no*b_P_no - epsilon >= 0
# Simplify:
# (1 - E_yes)*b_E_yes + (-E_no)*b_E_no + (1 - P_yes)*b_P_yes + (-P_no)*b_P_no - epsilon >= 0
# For Outcome 3: Trump wins election and popular vote
# b_E_no + b_P_no >= cost + epsilon
# => b_E_no + b_P_no - E_yes*b_E_yes - E_no*b_E_no - P_yes*b_P_yes - P_no*b_P_no - epsilon >= 0
# Simplify:
# (-E_yes)*b_E_yes + (1 - E_no)*b_E_no + (-P_yes)*b_P_yes + (1 - P_no)*b_P_no - epsilon >= 0
# Convert inequalities to <= for linprog by multiplying by -1
A_ub = [
[-(1 - E_yes), E_no, -(1 - P_yes), P_no, 1],
[E_yes, -(1 - E_no), P_yes, -(1 - P_no), 1]
]
b_ub = [0, 0]
# Bounds for variables: b_E_yes, b_E_no, b_P_yes, b_P_no >=0, epsilon >=0
bounds = [
(0, None), # b_E_yes
(0, None), # b_E_no
(0, None), # b_P_yes
(0, None), # b_P_no
(0, None) # epsilon
]
# Solve the linear program
res = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=bounds, method='highs')
if res.success:
epsilon = res.x[4]
if epsilon > 1e-6: # Consider a small threshold to account for numerical precision
print("Arbitrage opportunity found!")
print(f"Guaranteed profit: {epsilon:.4f}")
print("Buy the following shares:")
print(f" Election Winner Yes (Kamala): {res.x[0]:.4f} shares")
print(f" Election Winner No (Trump): {res.x[1]:.4f} shares")
print(f" Popular Vote Yes (Kamala): {res.x[2]:.4f} shares")
print(f" Popular Vote No (Trump): {res.x[3]:.4f} shares")
else:
print("No arbitrage opportunity found with the given prices and probabilities.")
else:
print("Linear program did not converge. Please check the input parameters.")
if __name__ == "__main__":
find_arbitrage_opportunity()
How It Works
-
Setting Prices and Probabilities: You can adjust the variables E_yes, E_no, P_yes, P_no, p1, p2, p3, and p4 to reflect your custom share prices and joint probabilities. Remember to set impossible joint outcomes (like Kamala winning the election but losing the popular vote) to zero.
-
Linear Programming Formulation:
- Objective: Maximize
epsilon, representing the guaranteed profit.
- Constraints: Ensure that for each possible outcome, the payout from the purchased shares is at least equal to the total cost plus
epsilon.
- Variables: The number of shares to buy in each market (
b_E_yes, b_E_no, b_P_yes, b_P_no) and the profit epsilon.
-
Solving the LP: The scipy.optimize.linprog function is used to solve the LP. It either finds an optimal solution where epsilon > 0 (indicating an arbitrage opportunity) or determines that no such opportunity exists.
-
Interpreting Results:
- If an arbitrage opportunity is found, the program outputs the number of shares to buy in each market and the guaranteed profit.
- If no opportunity exists, it notifies you accordingly.
Example Output
Assuming the current prices and probabilities allow for an arbitrage opportunity, running the program might yield:
Arbitrage opportunity found!
Guaranteed profit: 0.0500
Buy the following shares:
Election Winner Yes (Kamala): 0.5000 shares
Election Winner No (Trump): 0.4000 shares
Popular Vote Yes (Kamala): 0.6000 shares
Popular Vote No (Trump): 0.4500 shares
If there's no arbitrage opportunity with the provided prices and probabilities, the output will be:
No arbitrage opportunity found with the given prices and probabilities.
Customization
To customize the share prices and probabilities, simply modify the corresponding variables in the program:
# Example: Adjusting share prices
E_yes = 0.55
E_no = 0.45
P_yes = 0.50
P_no = 0.50
# Example: Adjusting joint probabilities
p1 = 0.48
p2 = 0.0
p3 = 0.42
p4 = 0.10 # Should be set to 0 if the outcome is impossible
Ensure that the joint probabilities sum to 1 and that impossible outcomes have a probability of zero.
Notes
- Numerical Precision: The program uses a small threshold (
1e-6) to account for numerical precision issues when checking if epsilon is greater than zero.
- Solver Method: The
highs method is used for better performance and reliability, especially for larger problems.
- No Share Selling: The program only considers buying shares (
b_E_yes, b_E_no, b_P_yes, b_P_no ≥ 0) and does not include selling shares.
Feel free to integrate this program into your trading strategies or modify it further to suit your specific needs!