ML4T/martingale/martingale.py

135 lines
4.4 KiB
Python
Raw Normal View History

2020-08-06 02:10:38 +02:00
"""Assess a betting strategy.
Copyright 2018, Georgia Institute of Technology (Georgia Tech)
Atlanta, Georgia 30332
All Rights Reserved
Template code for CS 4646/7646
Georgia Tech asserts copyright ownership of this template and all derivative
works, including solutions to the projects assigned in this course. Students
and other users of this template code are advised not to share it with others
or to make it available on publicly viewable websites including repositories
such as github and gitlab. This copyright statement should not be removed
or edited.
We do grant permission to share solutions privately with non-students such
as potential employers. However, sharing with other current or future
students of CS 7646 is prohibited and subject to being investigated as a
GT honor code violation.
-----do not edit anything above this line---
Student Name: Tucker Balch (replace with your name)
GT User ID: tb34 (replace with your User ID)
GT ID: 900897987 (replace with your GT ID)
"""
import numpy as np
2020-08-07 00:49:49 +02:00
import matplotlib.pyplot as plt
2020-08-06 02:10:38 +02:00
def author():
return 'tb34' # replace tb34 with your Georgia Tech username.
def gtid():
return 900897987 # replace with your GT ID number
def get_spin_result(win_prob):
result = False
if np.random.random() <= win_prob:
result = True
return result
2020-08-07 00:49:49 +02:00
def run_experiment(max_winnings=80, max_bets=1000, initial_credit=0):
2020-08-06 02:10:38 +02:00
win_prob = 18 / 38 # 18 black numbers out of 38 total numbers
2020-08-07 00:49:49 +02:00
current_bet, episode_winnings, bet_amount = 0, 0, 1
winnings = np.zeros(max_bets)
# Keep making bets until we reach desired winnings or betting limit
while episode_winnings < max_winnings and current_bet < max_bets:
if get_spin_result(win_prob):
episode_winnings += bet_amount
bet_amount = 1
else:
episode_winnings -= bet_amount
bet_amount *= 2
winnings[current_bet] = episode_winnings
current_bet += 1
# Handle experiment 2 where we have a initial maximum bankroll
if initial_credit > 0:
current_credit = initial_credit + episode_winnings
if current_credit <= 0:
break
if bet_amount > current_credit:
bet_amount = current_credit
# Fill remaining fields with last value
while current_bet < max_bets:
winnings[current_bet] = episode_winnings
current_bet += 1
return winnings
def configure_plot():
plt.figure()
axes = plt.gca()
axes.set_xlim([0, 300])
axes.set_ylim([-256, 100])
plt.xlabel("#bets []")
plt.ylabel("win [$]")
2020-08-06 02:10:38 +02:00
2020-08-07 00:49:49 +02:00
def experiment_1_figure_1(number_runs=10):
configure_plot()
for _ in range(number_runs):
winnings = run_experiment()
plt.plot(winnings)
plt.savefig('figure_1.png')
2020-08-06 02:10:38 +02:00
2020-08-07 00:49:49 +02:00
def experiment_1_figure_2(number_runs=1000):
configure_plot()
runs = np.array([run_experiment() for _ in range(number_runs)])
winnings_mean = runs.mean(axis=0)
winnings_std = runs.std(axis=0)
2020-08-06 02:10:38 +02:00
2020-08-07 00:49:49 +02:00
plt.plot(winnings_mean, linewidth=0.7)
plt_std_setting = {'ls': '-', 'color': 'blue', 'linewidth': 0.3}
plt.plot(winnings_mean + winnings_std, **plt_std_setting)
plt.plot(winnings_mean - winnings_std, **plt_std_setting)
plt.savefig('figure_2.png')
experiment_1_figure_3(runs)
def experiment_1_figure_3(runs, figurename='figure_3.png'):
configure_plot()
winnings_median = np.median(a=runs, axis=0)
winnings_std = runs.std(axis=0)
2020-08-07 00:49:49 +02:00
plt.plot(winnings_median, linewidth=0.7)
plt_std_setting = {'ls': '-', 'color': 'blue', 'linewidth': 0.3}
plt.plot(winnings_median + winnings_std, **plt_std_setting)
plt.plot(winnings_median - winnings_std, **plt_std_setting)
plt.savefig(figurename)
def experiment_2_figure_4(number_runs=1000):
configure_plot()
runs = np.array([run_experiment(initial_credit=256)
for _ in range(number_runs)])
winnings_mean = runs.mean(axis=0)
winnings_std = runs.std(axis=0)
2020-08-07 00:49:49 +02:00
plt.plot(winnings_mean, linewidth=0.7)
plt_std_setting = {'ls': '-', 'color': 'blue', 'linewidth': 0.3}
plt.plot(winnings_mean + winnings_std, **plt_std_setting)
plt.plot(winnings_mean - winnings_std, **plt_std_setting)
plt.savefig('figure_4.png')
experiment_1_figure_3(runs, figurename='figure_5.png')
def test_code():
np.random.seed(gtid()) # do this only once
experiment_1_figure_1()
experiment_1_figure_2()
experiment_2_figure_4()
2020-08-06 02:10:38 +02:00
if __name__ == "__main__":
test_code()
2020-08-07 00:49:49 +02:00