''' Created on Jul 5, 2014 @author: Brett Paufler Copyright Brett Paufler ''' import pickle from lotto_class import prizeLevel from lotto_class import lotteryGame import matplotlib.pyplot as plt import numpy def loadData(): '''loads the 01-lottoPickleData.txt file made by the htmlDataExtracton module ''' with open(r'.\lottery\01-lottoPickleData.txt', 'rb') as f: print "Got HERE" data = pickle.load(f) for d in data: d.printLotteryGameData() print '\n\n' return data #gD = loadData() #print gD def rpdAboveTarget(g, target=100000): '''returns the cumulative returnPerDollar for target price and above g = a lotteryGame dataObject target = prize level desired (or better) returns cumulative returnPerDollar ''' print "\nrpdAboveTarget started" # a = returnPerDollar < target a = 0 for p in g.prizes: if p.prize < target: a += p.returnPerDollar #print "Cumulative RPD: %f \t Current RPD: %f \t Prize: %d" % (a, p.returnPerDollar, p.prize) #TODO THIS IS NOT WORKING d = 0.0000000001 #print "%.9f" % d print "%.10f" % d #return per dollar for prize over target z = 0.00 a = 1.00 + a #used as a multiplier down the line ''' print r"(above) Non-Winning Carried Forward (below) Winners after taking above into account" for p in g.prizes: if p.prize >= target: t = a * p.returnPerDollar z += t print "Cumulative RPD %f \t Current RPD %f \t Prize: %d" % (z, t, p.prize) #print '%f %f %d' % (z,t,p.prize) print "Cumulative RPD for %d or better = %f" % (target, z) ''' return z #gD = loadData() #rpdAboveTarget(gD[0]) def oddsAboveTarget(g, target=100000): '''returns the cumulative CHANCE (1/odds) for target price and above per DOLLAR (so a \$2 ticket's true odds is 2x return value) g = a lotteryGame dataObject target = prize level desired (or better) returns cumulative chance above target per dollar ''' print "\noddsAboveTarget started" delta = 0.000000000000001 underOdds = [] prizes = [] for p in g.prizes: pO = p.returnPrizeOdds() prizes.append(pO) print pO prizes.sort() print prizes print pO for prize, odds in prizes: a = 0.00 print "prize: %d \t odds: %d" % (prize, odds) if prize < target: chance = 1.00 / odds #print "prizeOdds: %.15f" % chance a = chance * prize / g.price dT = chance c = 1 print "dT loop dT %.15f Count %d AccumulatedrOdds: %.15f" % (dT, c, a) while dT > delta: c += 1 dT = chance ** c a += dT * prize / g.price print "dT loop dT %.15f Count %d AccumulatedrOdds: %.15f" % (dT, c, a) print "A:ODDS = %.15f" % a underOdds.append(a) print "WINNING PRIZE LEVELS FOLLOW" a = 1 + sum(underOdds) print "1 + sum(stacked) = %.15f" % a overOdds = [] for prize, odds in prizes: z = 0.00 if prize >= target: chance = 1.00 / odds / g.price z += a * chance print "Z+=%.15f after %d prize at raw chance of %.15f a=%.15f" % (z, prize, chance, a) overOdds.append(z) print "underOdds %.15f %s" % (sum(underOdds), str(underOdds)) print "overOdds %.15f %s" % (sum(overOdds), str(overOdds)) return overOdds #gD = loadData() #gD[0].printLotteryGameData() #oddsAboveTarget(gD[0], 1000000) def runFullAnalysis(): '''The cumulation of analytical code creates a gD object runs the {rpd,odds}AboveTarget for all returns gD I think this might be what we need for analysis ''' print "Starting runFullAnalysis All Games" gD = loadData() for g in gD: g.rpd20000 = rpdAboveTarget(g, target=20000) g.rpd100000 = rpdAboveTarget(g, target=100000) g.rpd1000000 = rpdAboveTarget(g, target=1000000) g.rpd10000000 = rpdAboveTarget(g, target=10000000) g.odds20000 = oddsAboveTarget(g, target=20000) g.odds100000 = oddsAboveTarget(g, target=100000) g.odds1000000 = oddsAboveTarget(g, target=1000000) g.odds10000000 = oddsAboveTarget(g, target=10000000) print "\n\ngame RPD odds" print "20000 \t\t 100,000 \t 1,000,000 \t 10,000,000" for g in gD: print g.name print "%f \t %f \t %f \t %f" % (g.rpd20000, g.rpd100000 , g.rpd1000000, g.rpd10000000) print "%d \t %d \t %d \t %d" % (g.odds20000, g.odds100000 , g.odds1000000, g.odds10000000) return gD #with open(r'.\lottery\05-runFullAnalysis.txt', 'wb') as f: #gD = runFullAnalysis() #print gD def chanceChart(gD, goal="\$1,000,000", allGames=True): ''' This creates the Layered Chance Charts This is the one on the site gD = loadData() typically ''' target = goal.replace("\$","") target = target.replace(",","") target = int(target) game = [] odds = [] wData = [] if allGames: test = gD else: test = [g for g in gD if sum(oddsAboveTarget(g, target)) > 0] for t in test: print "%s \t %s" % (t.name, t.fileName) overTarget = oddsAboveTarget(t, target) c = sum(overTarget) oT = 1.00/float(c) if c else 0 game.append("%s\nchance\n%.8f\n(odds/\$)\n%.1f" % (t.name, c, oT)) tOdds = [] for v in range(0, 20, 1): if v < len(overTarget): tOdds.append(overTarget[v]) else: tOdds.append(0) odds.append(tOdds) #For Text File, Formats for HTML Table t1 = r"" t2 = r"" r1 = r"" r2 = r"" gN = "%s%s%s" % (r1, t.name, r2) gFn = t.fileName.replace("DRAW-", "") gFn = gFn.replace("SGAME-", "") gFn = gFn.replace(".html", "") gF = "%s%s%s" % (r1, gFn, r2) gO = "%s1:%f%s" % (r1, oT, r2) gC = "%s%.10f%s" % (r1, c, r2) wData.append("%s%s%s%s%s%s" % (t1,gN,gF,gC,gO,t2) ) g1 = "\n(normalized for ticket price)\n(lower prize amounts reinvested)" if allGames: g2 = "" else: g2 = "\n(only games that have a prize at this level are shown)" graphTitle = "Chance Per Dollar of Winning a Prize Over %s%s%s" % (goal, g1,g2) L2 = "\n(note Scientific Notation in Top Left Corner for Higher Prize Levels)" L1 = "\n(as a number between 0.00 & 1.00 -- 0.50 = 50%, 0.25 = 25% - higher is better)" yLabel = "Cumulative Chance of Winning%s%s" % (L1,L2) n = len(game) ind = numpy.arange(0.5,len(game),1.0) print ind width = 0.95 #plt.rcParams['figure.figsize'] = 2*len(odds), len(odds) plt.rcParams['figure.figsize'] = 50,25 fig, ax = plt.subplots() graphOdds = [] for x in range(0,20,1): graphOdds.append([]) for i in (range(0,n)): #print odds[i][x] graphOdds[x].append(odds[i][x]) bM = [] bM.append([]) for x in (range(0,n)): bM[0].append(0) for x in range(1,20,1): bM.append([]) for y in range(0,n): bM[x].append((graphOdds[x-1][y] + bM[x-1][y])) p = [] p.append({}) p[0] = plt.bar(range(0,n), graphOdds[0], width, color="r") for i in range(1,20,1): p.append({}) if i % 3 == 0: chartColor = "r" elif i % 3 == 1: chartColor = "g" else: chartColor = "b" p[i] = plt.bar(range(0,n), graphOdds[i], width, bottom=bM[i], color=chartColor) ax.set_title(graphTitle, fontsize=40) ax.set_xticks(ind) ax.set_xticklabels((game), fontsize=10) ax.set_ylabel(yLabel, fontsize=30) plt.xlabel("\nChance = 1/Odds", fontsize=36) if allGames: allText = "ALL" else: allText = "partial" sN = "./lottery/%d-lotteryCumulative_ODDS_%s.png" % (target, allText) print "Working On %s" % sN plt.savefig(sN) #plt.show() sN = "./lottery/%d-lotteryCumulative_ODDS_%s.txt" % (target, allText) with open(sN, 'wb') as txt: txt.write(graphTitle) txt.write("\n\n\n") txt.write(("%s%sName%s%sGame%s%sChance%s%sOdds%s%s\n" % (t1, r1, r2, r1, r2, r1, r2,r1, r2, t2))) for d in wData: txt.write(d) txt.write("\n") print "FINISHED %s" % sN #gD = loadData() #for am in ["\$0","\$10","\$100","\$1,000","\$10,000","\$100,000", "\$1,000,000", "\$10,000,000"]: # chanceChart(gD, am, allGames=True) # chanceChart(gD, am, allGames=False) #chanceChart(gD, "\$5,000", allGames=True) #chanceChart(gD, "\$1,000,000", allGames=True) def chanceSplatterChart(gD, low=0, high=100000000000): ''' ''' gC = [] gP = [] for g in gD: for p in g.prizes: if p.prize / float(g.price) <= high and p.prize >= low: gC.append(1.0 / (p.odds * g.price)) gP.append(p.prize / float(g.price)) #print len(gC) #print gC #print len(gP) #print gP print "Low: %d\tHigh: %d\tNumber: %d" % (low, high, len(gC)) plt.rcParams['figure.figsize'] = 20,10 fig, ax = plt.subplots() plt.scatter(gP, gC) #, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, hold) #g1 = "\n(normalized for ticket price)\n(lower prize amounts reinvested)" #g2 = "" #L2 = "\n(note Scientific Notation in Top Left Corner for Higher Prize Levels)" #L1 = "\n(as a number between 0.00 & 1.00 -- 0.50 = 50%, 0.25 = 25% - higher is better)" yLabel = "CHANCE (1/Odds) of Winning Stated Prize" ax.set_ylabel(yLabel, fontsize=16) graphTitle = "All Lottery Games\nChance Of Winning Various Prize Levels\n\\$%d to \\$%d Prizes Shown" % (low, high) ax.set_title(graphTitle, fontsize=20) xLabel = "Prize Level Range (in dollars)" plt.xlabel(xLabel, fontsize=16) #ax.set_xticks(ind) #ax.set_xticklabels("XTicks", fontsize=10) # sN = "./lottery/scatterPlot_%d-%d.png" % (low, high) print "Working On %s" % sN plt.savefig(sN) print "FINISHED %s" % sN gD = loadData() #for low, high in [(0,10),(0,5),(0,100),(0,1000),(0,10000),(0,100000),(0,1000000),(0,1000000000)]: #for n in [0, 5,10,100,1000,10000,100000,1000000,10000000]: #chanceSplatterChart(gD, n) print "ALL DONE"