Skip to content

Instantly share code, notes, and snippets.

@gnmerritt gnmerritt/gamble.py
Created Aug 30, 2019

Embed
What would you like to do?
EV is a lie
import random
import statistics
random.seed("FOOBAR")
def won_round():
return random.choice([True, False])
START = 10
WIN = 1.5
LOSE = 0.6
PER_TRIAL = 1000
TRIALS = 100
def trial(win=WIN, lose=LOSE):
bankroll = START
for _ in range(0, PER_TRIAL):
if won_round():
bankroll *= win
else:
bankroll *= lose
return bankroll
print(f"running 50/50 bet with win={WIN}, lose={LOSE}, bankroll starting at {START}")
ev = .5 * (WIN + LOSE)
print(f"EV per round = .5 * {WIN} + .5 * {LOSE} = {ev}")
print(f"EV across {PER_TRIAL} rounds = start * {ev}**{PER_TRIAL} = {START * ev**PER_TRIAL}")
print("")
last_mean = -1
num_trials = TRIALS
trials = []
while last_mean < 0:
trials = trials + [trial() for _ in range(0, num_trials - len(trials))]
print(f"after {num_trials} trials")
print(f" min = {min(trials)}, max = {max(trials)}")
last_mean = statistics.mean(trials)
print(f" mean = {last_mean}, median = {statistics.median(trials)}")
try:
print(f" mode = {statistics.mode(trials)}")
except statistics.StatisticsError:
pass
print("")
num_trials *= 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.