Skip to content

Instantly share code, notes, and snippets.

@pannet1
Last active March 21, 2023 23:09
Show Gist options
  • Save pannet1/cf90a9ea4b8d0243c7996f5617235000 to your computer and use it in GitHub Desktop.
Save pannet1/cf90a9ea4b8d0243c7996f5617235000 to your computer and use it in GitHub Desktop.
chatgpt prediction market
"""
hi, give me python code for prediction market. rules are below
a) participants bet on two binary pots for an event that is going to happen that day.
b) the bet size changes according to the probability of the event
c) one pot bets 'for' the event and another 'against'
d) if probability of the even if 70% then the for pot bet size is 70$ and against pot bet size is 30$
e) can you interpolate the probability automatically based on the amount collected in each pot and/or based on the number of bets
f) since the bet size is now decided by the prediction class, place_bet will be on the market which inturn will be based on the probability.
g) can you maintain a book for prediction market that collects $1 for every bet from the participants. also maintain book for each participants with an option to make deposits and take withdrawal.
"""
class PredictionMarket:
def __init__(self, event_name, for_pot_name, against_pot_name):
self.event_name = event_name
self.for_pot_name = for_pot_name
self.against_pot_name = against_pot_name
self.for_pot_total = 0
self.against_pot_total = 0
self.participants = {}
self.book = {}
self.profit = 0
def add_participant(self, participant_name, initial_funds):
self.participants[participant_name] = initial_funds
self.book[participant_name] = 0
def place_bet(self, participant_name, pot_num):
if pot_num == 1:
bet_size = int(self.interpolate_probability() * 100)
self.for_pot_total += bet_size
elif pot_num == 2:
bet_size = 100 - int(self.interpolate_probability() * 100)
self.against_pot_total += bet_size
self.participants[participant_name] -= bet_size
self.book[participant_name] += 1
self.profit += 1
def interpolate_probability(self):
total = self.for_pot_total + self.against_pot_total
if total == 0:
return 0.5
return self.for_pot_total / total
def close_market(self, event_outcome):
if event_outcome is None:
for participant, amount in self.participants.items():
self.participants[participant] += amount
self.profit += len(self.participants)
return
interpolated_probability = self.interpolate_probability()
if event_outcome:
pot1_winnings = self.against_pot_total * (1 / (1 - interpolated_probability))
pot2_winnings = self.for_pot_total
else:
pot1_winnings = self.for_pot_total * (1 / interpolated_probability)
pot2_winnings = self.against_pot_total
for participant, amount in self.participants.items():
participant_bet_on = participant.get_bet()
if participant_bet_on == 1 and event_outcome:
self.participants[participant] += pot1_winnings * (amount / self.for_pot_total)
elif participant_bet_on == 2 and not event_outcome:
self.participants[participant] += pot2_winnings * (amount / self.against_pot_total)
def get_pot_totals(self):
return (self.for_pot_total, self.against_pot_total)
def deposit(self, participant_name, amount):
self.participants[participant_name] += amount
def withdraw(self, participant_name, amount):
if amount > self.participants[participant_name]:
raise ValueError("Withdrawal amount exceeds available funds")
self.participants[participant_name] -= amount
def get_book(self):
return self.book
def get_profit(self):
return self.profit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment