Last active
May 20, 2019 05:51
-
-
Save sshariff01/382a3388ea755c41693be371c36fadba to your computer and use it in GitHub Desktop.
ML4T - Project 1
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
"""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: Shoabe Shariff | |
GT User ID: sshariff3 | |
GT ID: 903272097 | |
""" | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import pdb | |
def author(): | |
return 'sshariff3' | |
def gtid(): | |
return 903272097 | |
def get_spin_result(win_prob): | |
result = False | |
if np.random.random() <= win_prob: | |
result = True | |
return result | |
def test_code(): | |
win_prob = 0.4737 # set appropriately to the probability of a win | |
np.random.seed(gtid()) # do this only once | |
print get_spin_result(win_prob) # test the roulette spin | |
# add your code here to implement the experiments | |
# Experiment 1 | |
figure1(win_prob) | |
figure2and3(win_prob) | |
# Experiment 2 | |
figure4and5(win_prob) | |
def run_test_simulation(win_prob): | |
number_of_total_spins_plus_one = 1001 | |
winnings = [0] | |
total_winnings = 0 | |
while total_winnings < 80 and len(winnings) < number_of_total_spins_plus_one: | |
won = False | |
bet_amount = 1 | |
while not won: | |
won = get_spin_result(win_prob) | |
if won == True: | |
total_winnings = total_winnings + bet_amount | |
else: | |
total_winnings = total_winnings - bet_amount | |
bet_amount = bet_amount * 2 | |
winnings.append(total_winnings) | |
while len(winnings) < number_of_total_spins_plus_one: | |
winnings.append(total_winnings) | |
return np.array(winnings) | |
def run_realistic_simulation(win_prob): | |
number_of_total_spins_plus_one = 1001 | |
winnings = [0] | |
total_winnings = 0 | |
max_loss = -256 | |
while total_winnings < 80 and \ | |
total_winnings > max_loss and \ | |
len(winnings) < number_of_total_spins_plus_one: | |
won = False | |
bet_amount = 1 | |
while not won and len(winnings) < number_of_total_spins_plus_one: | |
won = get_spin_result(win_prob) | |
if won == True: | |
total_winnings = total_winnings + bet_amount | |
else: | |
total_winnings = total_winnings - bet_amount | |
bet_amount = bet_amount * 2 | |
if total_winnings - bet_amount < max_loss: | |
bet_amount = total_winnings - max_loss | |
winnings.append(total_winnings) | |
while len(winnings) < number_of_total_spins_plus_one: | |
if total_winnings == 80: | |
winnings.append(80) | |
elif total_winnings == max_loss: | |
winnings.append(max_loss) | |
return np.array(winnings) | |
def figure1(win_prob): | |
simulations = [] | |
while len(simulations) < 10: | |
simulations.append(run_test_simulation(win_prob)) | |
x_coords = np.arange(0, 1001) | |
plt.plot(x_coords, simulations[0], label="Simulation 1") | |
plt.plot(x_coords, simulations[1], label="Simulation 2") | |
plt.plot(x_coords, simulations[2], label="Simulation 3") | |
plt.plot(x_coords, simulations[3], label="Simulation 4") | |
plt.plot(x_coords, simulations[4], label="Simulation 5") | |
plt.plot(x_coords, simulations[5], label="Simulation 6") | |
plt.plot(x_coords, simulations[6], label="Simulation 7") | |
plt.plot(x_coords, simulations[7], label="Simulation 8") | |
plt.plot(x_coords, simulations[8], label="Simulation 9") | |
plt.plot(x_coords, simulations[9], label="Simulation 10") | |
plt.title("Number of Spins vs Episode Winnings for 10 Simulations") | |
plt.xlabel("Number of Spins") | |
plt.ylabel("Episode Winnings") | |
plt.xlim([0, 300]) | |
plt.ylim([-256, 100]) | |
plt.legend(loc="lower right", prop={'size': 10}) | |
plt.savefig("figure1.png") | |
plt.close() | |
def figure2and3(win_prob): | |
# Gather Data | |
simulations = [] | |
while len(simulations) < 1000: | |
simulations.append(run_test_simulation(win_prob)) | |
# Figure 2 | |
mean = np.mean(simulations, axis=0) | |
standard_deviation = np.std(simulations, axis=0) | |
mean_plus = np.add(mean, standard_deviation) | |
mean_minus = np.subtract(mean, standard_deviation) | |
x_coords = np.arange(0, 1001) | |
plt.plot(x_coords, mean, label="Mean") | |
plt.plot(x_coords, mean_plus, label="Mean + Std Dev") | |
plt.plot(x_coords, mean_minus, label="Mean - Std Dev") | |
plt.title("Mean Winnings vs Spin Number for 1000 Spins") | |
plt.xlabel("Spin Number") | |
plt.ylabel("Mean Winnings") | |
plt.xlim([0, 300]) | |
plt.ylim([-256, 100]) | |
plt.legend(loc="lower right", prop={'size': 10}) | |
plt.savefig("figure2.png") | |
plt.close() | |
# Figure 3 | |
median = np.median(simulations, axis=0) | |
standard_deviation = np.std(simulations, axis=0) | |
median_plus = np.add(median, standard_deviation) | |
median_minus = np.subtract(median, standard_deviation) | |
x_coords = np.arange(0, 1001) | |
plt.plot(x_coords, median, label="Median") | |
plt.plot(x_coords, median_plus, label="Median + Std Dev") | |
plt.plot(x_coords, median_minus, label="Median - Std Dev") | |
plt.title("Median Winnings vs Spin Number for 1000 Spins") | |
plt.xlabel("Spin Number") | |
plt.ylabel("Median Winnings") | |
plt.xlim([0, 300]) | |
plt.ylim([-256, 100]) | |
plt.legend(loc="lower right", prop={'size': 10}) | |
plt.savefig("figure3.png") | |
plt.close() | |
def figure4and5(win_prob): | |
# Gather Data | |
simulations = [] | |
while len(simulations) < 1000: | |
simulations.append(run_realistic_simulation(win_prob)) | |
# Figure 4 | |
mean = np.mean(simulations, axis=0) | |
standard_deviation = np.std(simulations, axis=0) | |
mean_plus = np.add(mean, standard_deviation) | |
mean_minus = np.subtract(mean, standard_deviation) | |
x_coords = np.arange(0, 1001) | |
plt.plot(x_coords, mean, label="Mean") | |
plt.plot(x_coords, mean_plus, label="Mean + Std Dev") | |
plt.plot(x_coords, mean_minus, label="Mean - Std Dev") | |
plt.title("Mean Winnings vs Spin Number for 1000 Spins (Realistic)") | |
plt.xlabel("Spin Number") | |
plt.ylabel("Mean Winnings") | |
plt.xlim([0, 300]) | |
plt.ylim([-256, 100]) | |
plt.legend(loc="lower right", prop={'size': 10}) | |
plt.savefig("figure4.png") | |
plt.close() | |
# Figure 5 | |
median = np.median(simulations, axis=0) | |
standard_deviation = np.std(simulations, axis=0) | |
median_plus = np.add(median, standard_deviation) | |
median_minus = np.subtract(median, standard_deviation) | |
x_coords = np.arange(0, 1001) | |
plt.plot(x_coords, median, label="Median") | |
plt.plot(x_coords, median_plus, label="Median + Std Dev") | |
plt.plot(x_coords, median_minus, label="Median - Std Dev") | |
plt.title("Median Winnings vs Spin Number for 1000 Spins (Realistic)") | |
plt.xlabel("Spin Number") | |
plt.ylabel("Median Winnings") | |
plt.xlim([0, 300]) | |
plt.ylim([-256, 100]) | |
plt.legend(loc="lower right", prop={'size': 10}) | |
plt.savefig("figure5.png") | |
plt.close() | |
def question1(): | |
win_prob = 0.4737 # set appropriately to the probability of a win | |
np.random.seed(gtid()) # do this only once | |
success = np.empty(10000) | |
for i in range(0, 10000): | |
test = run_test_simulation(0.4737) | |
if test[-1] == 80: | |
success[i] = 1 | |
else: | |
success[i] = 0 | |
success_prob = np.mean(success) | |
print "Probability of Winning = %f" % success_prob | |
def question5(): | |
win_prob = 0.4737 # set appropriately to the probability of a win | |
np.random.seed(gtid()) # do this only once | |
success = np.empty(10000) | |
for i in range(0, 10000): | |
test = run_realistic_simulation(0.4737) | |
if test[-1] == 80: | |
success[i] = 1 | |
else: | |
success[i] = 0 | |
success_prob = np.mean(success) | |
print "Probability of Winning = %f" % success_prob | |
if __name__ == "__main__": | |
test_code() | |
# question1() | |
# question5() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment