Skip to content

Instantly share code, notes, and snippets.

@nagataka
Created May 11, 2020 05:34
Show Gist options
  • Save nagataka/6319f9082860f003f1ef9ceee58a01a7 to your computer and use it in GitHub Desktop.
Save nagataka/6319f9082860f003f1ef9ceee58a01a7 to your computer and use it in GitHub Desktop.
Experiment on coin flipping game
import random
import numpy as np
np.random.seed(0)
def kerri(p, b):
"""https://en.wikipedia.org/wiki/Kelly_criterion
"""
return (p*(b+1)-1 )/b
N = 300
p = 0.6
winning = 0
fund = 25.0
for n in range(N):
ratio = kerri(p, 1)
bet = fund*ratio
#bet = 1 # for fixed betting experiment
if winning + bet > 250:
bet = 250 - winning # simulate $250 award cap
# simulate a coin which shows head 60%
if np.random.rand() < p:
# Player's win
print("Player's win. Got ", bet)
winning += bet
fund += bet
else:
print("Player's lose. Lost ", bet)
winning -= bet
fund -= bet
if winning >= 250:
print("Winning reward reached at the cap in {}th game. Finish.".format(n))
break
print("Total win ${} for skewed coin. Ended with ${}.".format(winning, fund))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment