Last active
March 28, 2019 12:40
-
-
Save mossbanay/3abbed54c6a231aa842785c208d1b03a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
STARTING_MONEY = 100 | |
N_TOSSES = 100 | |
N_ROUNDS = 100000 | |
ODDS = 2.00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WHO GIVES A TOSS?
The rules of the game are as follows: