Skip to content

Instantly share code, notes, and snippets.

@mossbanay
Last active March 28, 2019 12:40
Show Gist options
  • Save mossbanay/3abbed54c6a231aa842785c208d1b03a to your computer and use it in GitHub Desktop.
Save mossbanay/3abbed54c6a231aa842785c208d1b03a to your computer and use it in GitHub Desktop.
import random
class Bot():
def __init__(self, money, n_tosses, odds):
self.money = money
self.n_tosses = n_tosses
self.odds = odds
def handle_toss(self, toss_result, bet_return):
pass
def next_action(self):
return {'action':'skip'}
def __repr__(self):
return 'Bot'
class RandomBot(Bot):
"""
RandomBot randomly chooses a side to flip for each time. It will flip
up to `max_tosses` times, betting `wager` each time.
"""
def __init__(self, money, n_tosses, odds):
super().__init__(money, n_tosses, odds)
self.max_tosses = 10
self.played_tosses = 0
self.wager = 5
def __repr__(self):
return 'RandomBot'
def handle_toss(self, toss_result, bet_return):
# Update money and number of tosses
self.money += bet_return
self.played_tosses += 1
def next_action(self):
# Quit if we've played enough games
if self.played_tosses >= self.max_tosses:
return {'action':'skip'}
# Toss a random side
side = random.choice(['H', 'T'])
return {'action':'toss', 'side':side, 'wager':self.wager}
STARTING_MONEY = 100
N_TOSSES = 100
N_ROUNDS = 100000
ODDS = 2.00
import random
import numpy as np
import pandas as pd
from bot import Bot, RandomBot
from greedy import GreedyBot, LessGreedyBot, UpperLowerBot, KellyBot
from constants import *
def main():
bot_classes = [Bot, RandomBot, GreedyBot, LessGreedyBot, UpperLowerBot, KellyBot]
results = [[] for _ in range(len(bot_classes))]
for round_number in range(N_ROUNDS):
# Uniform(0, 1)
# p = random.random()
# Truncated normal
p = np.random.normal(loc=0.5, scale=0.05)
p = max(p, 0)
p = min(p, 1)
bots = [bot(STARTING_MONEY, N_TOSSES, ODDS) for bot in bot_classes]
bot_money = [STARTING_MONEY for _ in range(len(bots))]
for toss_number in range(N_TOSSES):
toss_result = 'H' if random.random() < p else 'T'
for bot_id, bot in enumerate(bots):
resp = bot.next_action()
if resp['action'] == 'skip':
continue
elif resp['action'] == 'toss':
assert(resp['wager'] <= bot_money[bot_id])
bet_return = (ODDS - 1) * resp['wager'] if resp['side'] == toss_result else -1 * resp['wager']
bot.handle_toss(toss_result, bet_return)
bot_money[bot_id] += bet_return
else:
print(f'Unknown action made by {str(bot)}')
for bot_id, bot in enumerate(bots):
results[bot_id].append(bot_money[bot_id])
results_df = pd.DataFrame({'names':[str(bot(0, 0, 0)) for bot in bot_classes],
'average':[np.mean(scores) for scores in results],
'median':[np.median(scores) for scores in results],
'0.05':[np.quantile(scores, q=0.05) for scores in results],
'0.95':[np.quantile(scores, q=0.95) for scores in results],
'stddev':[np.std(scores) for scores in results]})
print(results_df)
if __name__ == '__main__':
main()
@mossbanay
Copy link
Author

mossbanay commented Mar 28, 2019

WHO GIVES A TOSS?

The rules of the game are as follows:

  • We play a total of N_GAMES times
  • In each game, there are N_TOSSES made
  • You can choose to bet on any of those tosses
  • You are given the odds (which are constant for each game) before starting
  • You are given your starting money before starting
  • Each game a new probability of the coin landing heads is chosen from a uniform distribution from [0, 1]
  • The probability is constant for each game
  • You cannot bet more than your current money
  • There is no minimum or maximum bet size

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment