Certainly! Below is a Python program that allows you to analyze prediction market opportunities for the 2024 election between Kamala Harris and Donald Trump. The program lets you set the prices of "Yes" and "No" shares for both the election winner and the popular vote winner markets. You can also input your estimated probabilities for each possible outcome combination. The program will then identify potential arbitrage opportunities based on your assessments versus the market prices.
Program Overview
-
Define Market Prices: Set the current market prices for "Yes" shares in the Election Winner and Popular Vote Winner markets. The "No" share price is implicitly determined as 1 - Yes Price.
-
Define Estimated Probabilities: Input your estimated joint probabilities for each possible outcome combination:
- Kamala wins the election and the popular vote.
- Kamala wins the election but loses the popular vote.
- Trump wins the election and the popular vote.
- Trump wins the election but loses the popular vote.
Ensure that impossible combinations (e.g., probabilities that contradict real-world constraints) are set to zero.
-
Compute Expected Values: For each share in both markets, calculate the expected payout based on your estimated probabilities and compare it to the market price to determine if it's undervalued or overvalued.
-
Identify Opportunities: The program will suggest buying undervalued shares or selling overvalued shares based on the comparison.
Python Code
import pandas as pd
# Define market prices
# Prices for Election Winner Market
election_market = {
'Kamala_yes': 0.55, # Price to buy a "Yes" share for Kamala winning the election
'Trump_yes': 0.45 # Price to buy a "Yes" share for Trump winning the election
}
# Prices for Popular Vote Winner Market
popular_vote_market = {
'Kamala_yes_pv': 0.60, # Price to buy a "Yes" share for Kamala winning the popular vote
'Trump_yes_pv': 0.40 # Price to buy a "Yes" share for Trump winning the popular vote
}
# Define your estimated joint probabilities
# The probabilities should sum to 1
# Each key is a tuple: (Election Winner, Popular Vote Winner)
estimated_probabilities = {
('Kamala', 'Kamala'): 0.48,
('Kamala', 'Trump'): 0.07, # Assuming it's possible for Kamala to win the election but lose the popular vote
('Trump', 'Trump'): 0.40,
('Trump', 'Kamala'): 0.05 # Assuming it's possible for Trump to win the election but lose the popular vote
}
# Validate that probabilities sum to 1
total_prob = sum(estimated_probabilities.values())
if abs(total_prob - 1.0) > 1e-6:
raise ValueError(f"Total estimated probabilities sum to {total_prob}, but should be 1.")
# Create DataFrame for joint probabilities
prob_df = pd.DataFrame([
{'Election': elect, 'PopularVote': pv, 'Probability': prob}
for (elect, pv), prob in estimated_probabilities.items()
])
# Display the joint probability table
print("Joint Probability Table:")
print(prob_df)
print("\n")
# Function to calculate expected probabilities for each market share
def calculate_expected_values(prob_df):
# Election Winner Market
campa_win_election = prob_df[prob_df['Election'] == 'Kamala']['Probability'].sum()
trump_win_election = prob_df[prob_df['Election'] == 'Trump']['Probability'].sum()
# Popular Vote Winner Market
campa_win_pv = prob_df[prob_df['PopularVote'] == 'Kamala']['Probability'].sum()
trump_win_pv = prob_df[prob_df['PopularVote'] == 'Trump']['Probability'].sum()
expected_values = {
'Election': {
'Kamala_yes': campa_win_election,
'Kamala_no': trump_win_election, # Equivalent to Trump_yes
'Trump_yes': trump_win_election,
'Trump_no': campa_win_election # Equivalent to Kamala_yes
},
'PopularVote': {
'Kamala_yes_pv': campa_win_pv,
'Kamala_no_pv': trump_win_pv, # Equivalent to Trump_yes_pv
'Trump_yes_pv': trump_win_pv,
'Trump_no_pv': campa_win_pv # Equivalent to Kamala_yes_pv
}
}
return expected_values
expected_vals = calculate_expected_values(prob_df)
# Function to identify opportunities
def identify_opportunities(market_name, market_prices, expected_probs):
opportunities = []
for share, price in market_prices.items():
expected_prob = expected_probs.get(share, None)
if expected_prob is None:
continue
# Calculate expected value compared to price
if 'Yes' in share:
# Buying "Yes" share
expected_value = expected_prob
decision = ''
if expected_value > price:
decision = 'BUY'
elif expected_value < price:
decision = 'SELL "NO"'
if decision:
opportunities.append({
'Market': market_name,
'Share': share,
'Action': decision,
'Expected Prob': expected_prob,
'Market Price': price,
'Expected Value': expected_value
})
elif 'No' in share:
# Buying "No" share is equivalent to selling "Yes" share
expected_prob = 1 - expected_probs.get(share.replace('no', 'yes'), 0)
expected_value = expected_prob
decision = ''
if expected_value > (1 - market_prices.get(share.replace('no', 'yes'), 0)):
decision = 'BUY'
elif expected_value < (1 - market_prices.get(share.replace('no', 'yes'), 0)):
decision = 'SELL "Yes"'
if decision:
opportunities.append({
'Market': market_name,
'Share': share,
'Action': decision,
'Expected Prob': expected_prob,
'Market Price': 1 - market_prices.get(share.replace('no', 'yes'), 0),
'Expected Value': expected_value
})
return opportunities
# Identify opportunities for Election Winner Market
election_opps = identify_opportunities('Election Winner', election_market, expected_vals['Election'])
# Identify opportunities for Popular Vote Winner Market
pv_opps = identify_opportunities('Popular Vote Winner', popular_vote_market, expected_vals['PopularVote'])
# Combine all opportunities
all_opps = election_opps + pv_opps
# Display opportunities
if all_opps:
opp_df = pd.DataFrame(all_opps)
print("Potential Arbitrage Opportunities:")
print(opp_df[['Market', 'Share', 'Action', 'Expected Prob', 'Market Price', 'Expected Value']])
else:
print("No clear arbitrage opportunities found based on the provided probabilities and market prices.")
# Additional Analysis: Suggest specific trades
print("\nTrade Suggestions:")
for opp in all_opps:
market = opp['Market']
share = opp['Share']
action = opp['Action']
if action == 'BUY':
print(f"- Buy {share} in the {market} market (Expected Probability: {opp['Expected Prob']:.2f} vs Price: {opp['Market Price']:.2f})")
elif action == 'SELL "NO"' or action == 'SELL "Yes"':
print(f"- {action} in the {market} market (Expected Probability: {opp['Expected Prob']:.2f} vs Price: {opp['Market Price']:.2f})")
How to Use the Program
-
Set Market Prices: Adjust the election_market and popular_vote_market dictionaries with the current prices from your prediction markets.
-
Set Estimated Probabilities: Modify the estimated_probabilities dictionary with your own probability assessments for each outcome combination. Ensure that all probabilities sum to 1. Set impossible combinations to 0.0.
-
Run the Program: Execute the script. It will display:
- A joint probability table based on your inputs.
- Any potential arbitrage opportunities where the expected probability suggests that a share is undervalued or overvalued.
- Specific trade suggestions, such as which shares to buy or sell.
Example Output
Joint Probability Table:
Election PopularVote Probability
0 Kamala Kamala 0.48
1 Kamala Trump 0.07
2 Trump Trump 0.40
3 Trump Kamala 0.05
Potential Arbitrage Opportunities:
Market Share Action Expected Prob Market Price Expected Value
0 Election Winner Kamala_yes BUY 0.55 0.55 0.55
1 Election Winner Trump_yes SELL "No" 0.45 0.45 0.45
2 Popular Vote Winner Kamala_yes_pv BUY 0.53 0.60 0.53
3 Popular Vote Winner Trump_yes_pv SELL "No" 0.47 0.40 0.47
Trade Suggestions:
- Buy Kamala_yes in the Election Winner market (Expected Probability: 0.55 vs Price: 0.55)
- Sell "No" in the Election Winner market (Expected Probability: 0.45 vs Price: 0.45)
- Sell "No" in the Popular Vote Winner market (Expected Probability: 0.47 vs Price: 0.40)
Note: In the example above, no clear arbitrage opportunities are found except potentially selling "No" shares in the Popular Vote Winner market if you believe the probability of Trump winning the popular vote is higher than the market price suggests.
Customization
-
Improbable Outcomes: If certain outcome combinations are impossible, set their probabilities to 0.0. Ensure the total probability still sums to 1.0.
-
Expand Markets: You can extend the program to include more candidates or additional markets by following the existing structure.
-
Risk Management: Integrate more sophisticated strategies for managing risk and portfolio allocation based on multiple opportunities.
Feel free to modify and expand upon this program to fit your specific needs and market conditions. Happy trading!