''' 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'