Skip to content

Instantly share code, notes, and snippets.

@prmichaelsen
Last active October 24, 2018 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prmichaelsen/8e0b3cfcf326c8b5432b26e7d6d0fcef to your computer and use it in GitHub Desktop.
Save prmichaelsen/8e0b3cfcf326c8b5432b26e7d6d0fcef to your computer and use it in GitHub Desktop.
#############################################################
# lotto.py
#############################################################
# Description: Compare avg return of 2018-10-23
# mega million lottery compared to number of tickets bought.
# Note: this script doesn't appear to implement the correct
# behavior for doing so. I think my math is off. Oh well, it
# was fun trying.
#############################################################
import np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.use('Agg')
# earnings, 1: odds
odds = [
[1.6e9, 302526350],
[1e6, 12607306],
[1e4, 931001],
[1e2, 38792],
[2e2, 14547],
[10, 606],
[10, 693],
[4, 89],
[2, 37],
]
# multiplier, 1: odds
mpOdds = [
[2, 3],
[3, 2.5],
[4, 5],
[5, 15],
]
# normal, multiplier
cost = [2, 3]
def initChances(odds, n):
chances = []
for odd in odds:
weight = odd[0]
ratio = odd[1]
chance = np.true_divide(np.longdouble(n), np.longdouble(ratio))
chances.append([weight, chance])
# verify precision, if you'd like
# print ratio * chance + "~= 1"
return chances
def weightedAvg(chances):
avgs = []
for odd in chances:
weight = odd[0]
chance = odd[1]
avg = np.multiply(weight, chance)
avgs.append(avg)
return avgs
def multiplierAvgs(avgs, mpOdds):
mpAvgs = []
for winnings in avgs:
wAvg = 0
for mpOdd in mpOdds:
multiplier = mpOdd[0]
ratio = mpOdd[1]
chance = np.true_divide(np.longdouble(1), np.longdouble(ratio))
wOdd = np.multiply(multiplier, winnings)
wAvgReturn = np.multiply(chance, wOdd)
wAvg += wAvgReturn
mpAvgs.append(wAvg)
return mpAvgs
def normalRet(odds, numTickets, cost):
chances = initChances(odds, numTickets)
avgs = weightedAvg(chances)
avgRet = np.sum(avgs)
gross = avgRet - cost
return gross
def multRet(odds, mpOdds, numTickets, cost):
chances = initChances(odds, numTickets)
avgs = weightedAvg(chances)
jackpotAvgs = avgs[0:1]
restAvgs = avgs[1:]
mpAvgs = multiplierAvgs(restAvgs, mpOdds)
multAvgs = jackpotAvgs + mpAvgs
avgMulRet = np.sum(multAvgs)
multGross = avgMulRet - cost
return multGross
# return avg return in general
def avgReturn(x, cost, odds, mpOdds):
return x*multRet(odds, mpOdds, x, cost)
# get avgReturn for normal ticket
def normal(x):
return avgReturn(x, 2, odds, [[1,1]])
# get avgReturn for multi ticket
def multi(x):
return avgReturn(x, 3, odds, mpOdds)
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111)
# total num tickets
N = 302526350
N = 40
r = range(1, N+1)
cost = [2*n for n in r]
ret = [normal(n) for n in r]
ax.plot(cost, color='r', label='cost')
ax.plot(ret, color='g', label='avg return')
ax.legend()
fig.savefig('graph.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment