''' Created on Jul 14, 2014 @author: Brett Paufler Copyright Brett Paufler ''' import matplotlib.pyplot as plt import numpy import math Wpop = 7000000000 #world population USpop = 309000000 #us population mM1 = 258890850 #megaMillion first prize pB1 = 175223510 #powerBall first prize Wbr = 131000000 #world birthRate sL1 = 41416353 #superLotto first prize mM2 = 18492204 #megaMillion second Prize pB2 = 5153633 #powerBall second Prize USbr = 4240000 #US birthRate names = ['World population\nOdds 1 in\n%d' % Wpop, 'US population\nOdds 1 in\n%d' % USpop, 'megaMillions 1st\nOdds 1 in\n%d' % mM1, 'powerBall 1st\nOdds 1 in\n%d' % pB1, 'World birthRate\nOdds 1 in\n%d' % Wbr, 'superLotto 1st\nOdds 1 in\n%d' % sL1, 'megaMillions 2nd\nOdds 1 in\n%d' % mM2, 'powerBall 2nd\nOdds 1 in\n%d' % pB2, 'US birthRate\nOdds 1 in\n%d' % USbr ] chances = [1.00/Wpop, 1.00/USpop, 1.00/mM1, 1.00/pB1, 1.00/Wbr, 1.00/sL1, 1.00/mM2, 1.00/pB2, 1.00/USbr] #print names #print chances def longOddsChartPerspective1(names, chances): ''' makes a simple informative graph first for the webpage Bar Graph from low odds to high of one time events names and chance, per above ''' n = len(names) ind = numpy.arange(0.5,len(names),1.0) width = 0.95 plt.rcParams['figure.figsize'] = 2*n, n fig, ax = plt.subplots() bL = ax.bar(range(0,n), chances, width) #bL[n-1].set_color("g") graphTitle = "Chance for Select One Time Events" ax.set_title(graphTitle) ax.set_xticks(ind) ax.set_xticklabels((names)) yLabel = r"Chance (1/Odds)" ax.set_ylabel(yLabel) ax.set_yscale('linear') fN = "longOddsChartPerspective1" sN = "./lottery/%s.png" %fN print "Working On ./lottery/%s.png" % fN plt.savefig(sN) #plt.show() sN = "./lottery/%s.txt" % fN with open(sN, 'wb') as txt: txt.write(graphTitle) txt.write("\n\n\n") txt.write("Names \t Chance") for i in range(0,n,1): txt.write("%s\n%s\n\n" %(names[i], chances[i] )) #longOddsChartPerspective1(names, chances) def longOddsRepeatedAttempts(names, chances, sN, r): ''' 1 - q ** x ''' print "starting long odds" #fig = plt.figure() n = len(names) ind = numpy.arange(0.5,n,1.0) width = 0.95 plt.rcParams['figure.figsize'] = 2*n, n lC = [] for c in range(n): p = chances[c] rC = [] for x in r: q = 1 - (1 - p) ** x rC.append(q) plt.plot(r, rC) lC.append(rC) #print r graphTitle = "Chance for Repeated Events" plt.title(graphTitle) plt.ylabel("Chance (1/Odds)") leg = [] for na in names: leg.append(na.split('\n')[0]) plt.legend(leg, loc='lower right') fN = "longOddsRepeatedTrials-%s" % (sN) sN = "./lottery/%s.png" %fN print "Working On ./lottery/%s.png" % fN plt.savefig(sN) #plt.show() plt.close('all') sN = "./lottery/%s.txt" % fN with open(sN, 'wb') as txt: txt.write(graphTitle) txt.write("\n\n\n") txt.write("Names \t Chance") for i in range(0,n,1): txt.write("%s\n%s\n%.20f\n\n" %(names[i], chances[i], lC[i][len(lC[i]) -1])) #names.reverse() #chances.reverse() #longOddsRepeatedAttempts(names, chances, "lin1-100000000", numpy.linspace(1, 100000000, 100)) #longOddsRepeatedAttempts(names, chances, "powerBall-300000000", numpy.linspace(1, 300000000, 100)) ''' pW = 1 - (1 - p) ** x (1 - p) ** x = 1 - pW since a**b = c reduces to b = log(c) / log(a) x = log (1-pW)/log(1-p) ''' def numberOfTrialsForConfidence(pW=0.95, N=names, C=chances): '''returns the number of trials to achieve the given confidence level of success (pW) and the given probability per trial (p) ''' print "\n\n" tr1 = r'' tr2 = r'' td1 = r'' td2 = r'' out = "%s%s%s%s%s%s%s%s%s%s%s" % (tr1, td1, "Event", td2, td1, "pW50", td2, td1, "pW95", td2, tr2) print out for c in range(len(chances)): pW50 = math.log(1-0.50)/math.log(1-C[c]) pW95 = math.log(1-0.95)/math.log(1-C[c]) out = "%s%s%s%s%s%d%s%s%d%s%s" % (tr1, td1, N[c].split('\n')[0], td2, td1, pW50, td2, td1, pW95, td2, tr2) print out #numberOfTrialsForConfidence() ''' Odds CategoryOddsConfidence Level = 0.50Confidence Level = 0.95 World population7000000000485202932320970121850 US population309000000214182479925681275 megaMillions 1st258890850179449463775567678 powerBall 1st175223510121455681524922720 World birthRate13100000090802280392440927 superLotto 1st4141635328707627124072303 megaMillions 2nd184922041281781855397690 powerBall 2nd5153633357222515438903 US birthRate4240000293894312701903 ''' def graphYears(low, high, data): ''' nCL = 52 * 2 * y * pA y = nCL / 52 / 2 / pA Turns out I did use this How Many years it would take to buy x tickets Where x achieves a 95% level of confidence Tied in to data following function definition ''' pA = numpy.linspace(low, high) for d in data.keys(): y = [] for p in pA: y.append(data[d]/p/52/2) plt.plot(y, pA) leg = [] for g in data.keys(): print g leg.append(g.replace('-', " ")) #print "\n\n\n leg" #print leg plt.legend(leg, loc='upper right') graphTitle = "Years of Lottery Play Required\nto reach indicated confidence at wager noted" plt.title(graphTitle) plt.ylabel("Dollars Wagered Per Draw") plt.xlabel("Years of Playing Required") # sN = "./lottery/graphYears-%d-%d-Low.png" % (low, high) print "Working On" plt.savefig(sN) plt.close('all') ''' cdLow = { 'powerBall-0.50': 121455681 * 2, 'megaMillion-0.50': 179449463, 'superLotto-0.50': 28707627, } cdHigh = {'powerBall-0.95': 524922720 * 2, 'megaMillion-0.95':775567678, 'superLotto-0.95': 124072303 } d = cdLow graphYears(1,100, d) graphYears(100,1000, d) graphYears(1000,10000, d) ''' def makeHTMLYearsChart(): ''' How many years would it take to buy that many tickets And having made the chart, I'll likely never revisit ''' #N = name N = ['powerBall (95%)', 'powerBall (50%)', 'megaMillion (95%)', 'megaMillion (50%)', 'superLotto (95%)', 'superLotto (50%)' ] print len(N) #D = required draws D = [524922720 * 2, 121455681 * 2, 775567678, 179449463, 124072303, 28707627] print len(D) #W = wagers W = [1.0,5.0,10.0,50.0,100.0,1000.0] print "\n\n" beg = r'' end = r'' mid = r'' print "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" % (beg, 'NAME (confidence level)', mid, '$1/draw', mid,'$5/draw',mid,'$10/draw', mid,'$50/draw',mid,'$100/draw',mid,'$1000/draw', end) for d in range(len(D)): t = beg + N[d] for w in W: t += mid t += str(int(D[d]/w/52/2.00)) t += end print t #makeHTMLYearsChart() #data[d]/p/52/2