Finish project 1 coding.

This commit is contained in:
2020-08-06 18:49:49 -04:00
parent f1443f7c57
commit 63fe29e781
8 changed files with 98 additions and 19 deletions

View File

@@ -23,3 +23,6 @@ might add a makefile to automize this later.
unzip -n 20Spring_martingale.zip
```
[Here](https://pythonprogramming.net/candlestick-ohlc-graph-matplotlib-tutorial/)
is a tutorial for how to plot candlestick data. Will come in handy later.

BIN
martingale/figure_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

BIN
martingale/figure_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

BIN
martingale/figure_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
martingale/figure_4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
martingale/figure_5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

7
martingale/martingale.md Normal file
View File

@@ -0,0 +1,7 @@
# Report
![](figure_1.png)
![](figure_2.png)
![](figure_3.png)
![](figure_4.png)
![](figure_5.png)

View File

@@ -26,6 +26,7 @@ GT ID: 900897987 (replace with your GT ID)
"""
import numpy as np
import matplotlib.pyplot as plt
def author():
return 'tb34' # replace tb34 with your Georgia Tech username.
@@ -39,27 +40,95 @@ def get_spin_result(win_prob):
result = True
return result
def test_code():
def run_experiment(max_winnings=80, max_bets=1000, initial_credit=0):
win_prob = 18 / 38 # 18 black numbers out of 38 total numbers
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 [$]")
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')
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 = winnings_mean.std()
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 = winnings_median.std()
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 = winnings_mean.std()
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
episode_winnings = 0
winnings = [0]
while episode_winnings < 80:
won = False
bet_amount = 1
while not won:
won = get_spin_result(win_prob)
if won:
episode_winnings += bet_amount
else:
episode_winnings -= bet_amount
bet_amount *= 2
winnings.append(episode_winnings)
print(winnings)
# add your code here to implement the experiments
experiment_1_figure_1()
experiment_1_figure_2()
experiment_2_figure_4()
if __name__ == "__main__":
test_code()