''' Created on Jul 21, 2014 @author: Brett Paufler CopyRight Return Per Dollar Chart ''' import pickle from lotto_class import prizeLevel from lotto_class import lotteryGame import matplotlib.pyplot as plt import numpy import lotto_benchmarks as lotto 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 cD = lotto.casinoGames() def rpdData(gameData): '''returns a stacked list of the 'Return per Dollar' values for every prize level in a lotteryGame ''' rpdList = [] for pL in gameData.prizes: rpd = float(pL.prize) / (pL.odds * gameData.price) rpdList.append(rpd) return rpdList #for c in cD: # a = rpdData(c)[0] # print "%s\t%.5f\t%.5f" % (c.name, 1-a,a ) def rfdLayeredChart(gD): ''' This creates the Layered Return per Dollar Charts gD = loadData() typically This is the one on the site gD = loadData() typically RIGHT NOW, LEGEND IS SET FOR $1,000,000 No sense optimizing for different conditions as likely already have all the conditions I care about ''' allGames=True game = [] odds = [] wData = [] test = gD for t in test: rfd = rpdData(t) rfd.reverse() print rfd tOdds = [] for v in range(0, 20, 1): if v < len(rfd): tOdds.append(rfd[v]) else: tOdds.append(0) odds.append(tOdds) game.append("%s\ntotal rpd\n%.5f" % (t.name, sum(rfd) )) graphTitle = "Return Per Dollar\nAll California Lottery Games\n(and roulette)With Prizes Over $1,000,000" L1 = "\n(Higher Prize Levels Stacked at the Top)\n" yLabel = "Cumulative Return per Dollar%s" % (L1) n = len(game) ind = numpy.arange(0.5,len(game),1.0) print ind width = 0.95 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=50) ax.set_xticklabels((game), fontsize=10) plt.xlabel("\nCalifornia Lottery Draw & Scratchers Games\nOver $1,000,000", fontsize=36) ax.set_ylabel(yLabel, fontsize=30) ax.set_xticks(ind) plt.ylim(0, 1.0) sN = "./lottery/rfdLayeredChart.png" print "Working On %s" % sN plt.savefig(sN) #plt.show() print "FINISHED %s" % sN #CODE AS SET FOR MILLION DOLLAR PLUS #gD = loadData() #gD = [g for g in gD if g.prizes[0].prize > 1000000] #rfdLayeredChart(gD) #gD.append(lotto.coinToss()) #gD.append(lotto.roulette()) #adders = lotto.casinoGames() #for a in adders: # gD.append(a) #print rpdData(gD[0]) #gD[0].printLotteryGameData() #print gD[0].prizes[0].prize / gD[0].price / float(gD[0].prizes[0].odds) #print gD[0].prizes[1].prize / gD[0].price / float(gD[0].prizes[1].odds) def rpdCutOffData(gameData, cutOff): '''returns a stacked list of the 'Return per Dollar' values for every prize level in a lotteryGame over cutOff value prizes at or below cutOff are summed together (sumV) and returned as one value ''' rpdList = [] sumV = 0.0 for pL in gameData.prizes: if pL.prize > cutOff: rpd = float(pL.prize) / (pL.odds * gameData.price) rpdList.append(rpd) else: sumV += float(pL.prize) / (pL.odds * gameData.price) rpdList.append(sumV) return rpdList, sumV def rfdCutOffChart(gD, cutOff): ''' This creates the Layered Return per Dollar Charts gD = loadData() typically cutOff is the prizeLevel below which point all rfd values are grouped as one This is the one on the site with the orange bottom gD = loadData() typically RIGHT NOW, LEGEND IS SET FOR $1,000,000 No sense optimizing for different conditions as likely already have all the conditions I care about ''' allGames=True game = [] odds = [] wData = [] test = gD for t in test: rfd, lowVal = rpdCutOffData(t, cutOff) rfd.reverse() print rfd tOdds = [] for v in range(0, 20, 1): if v < len(rfd): tOdds.append(rfd[v]) else: tOdds.append(0) odds.append(tOdds) game.append("%s\nreal rpd\n%.5f" % (t.name, (sum(rfd)-lowVal))) graphTitle = "Return Per Dollar\nAll California Lottery Games (and roulette)\nPrizes $%d or Lower Stacked Together at Bottom" % cutOff L1 = "\nHigher Prize Levels Stacked at the Top\nPrizes at $%d or Lower Stacked Together in Red at Bottom" % cutOff yLabel = "Cumulative Return per Dollar%s" % (L1) n = len(game) ind = numpy.arange(0.5,len(game),1.0) print ind width = 0.90 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") first = True for i in range(1,20,1): p.append({}) if i % 3 == 0: chartColor = "y" elif i % 3 == 1: chartColor = "g" else: chartColor = "b" if i == 0: chartColor = "r" p[i] = plt.bar(range(0,n), graphOdds[i], width, bottom=bM[i], color=chartColor) ax.set_title(graphTitle, fontsize=50) ax.set_xticklabels((game), fontsize=10) plt.xlabel("\nCalifornia Lottery Draw & Scratchers Games\nPrize of $%d or Lower Stacked Together at Bottom" % cutOff, fontsize=36) ax.set_ylabel(yLabel, fontsize=30) ax.set_xticks(ind) plt.ylim(0, 1.0) sN = "./lottery/rfdCutOffLayeredChart-%d.png" % cutOff print "Working On %s" % sN plt.savefig(sN) #plt.show() print "FINISHED %s" % sN #gD = loadData() #gD.append(lotto.roulette()) #gD = [g for g in gD if g.prizes[0].prize > 1000000] #rfdCutOffChart(gD, 100) #rfdCutOffChart(gD, 1000) #CURRENT GRAPH _ REINVEST def rpdReinvestData(gameData, cutOff): '''returns a stacked list of the 'Return per Dollar' values for every prize level in a lotteryGame over cutOff value prizes at or below cutOff are summed together (sumV) and returned as one value ''' rpdList = [] lowRPD = 0.0 for pL in gameData.prizes: print "first Loop" if pL.prize < cutOff: lowRPD += float(pL.prize) / (pL.odds * gameData.price) print "lowRPD += %.10f at Prize: %d for cutOff %d" % (lowRPD, pL.prize, cutOff) for pL in gameData.prizes: print "second Loop" if pL.prize >= cutOff: iRPD = float(pL.prize) / (pL.odds * gameData.price) multi = lowRPD t = iRPD while multi * iRPD > 0.0000000001: multi = multi * lowRPD t += multi * iRPD print "iRPD %.8f multi %.8f t %.8f" %(iRPD, multi, t) rpdList.append(t) return rpdList def rfdReinvestChart(gD, cutOff): ''' This creates the Layered Return per Dollar Charts with lower RPD reinvested gD = loadData() cutOff is the prizeLevel below which point prizes are reinvested No red on this graph gD = loadData() ''' game = [] odds = [] test = gD for t in test: rfd = rpdReinvestData(t, cutOff) rfd.reverse() print rfd tOdds = [] for v in range(0, 20, 1): if v < len(rfd): tOdds.append(rfd[v]) else: tOdds.append(0) odds.append(tOdds) game.append("%s\nTrue RpD\n%.5f" % (t.name, (sum(rfd)))) graphTitle = "Return Per Dollar\nAll California Lottery Games (and roulette)\nPrizes Under $%d Reinvested" % cutOff L1 = "\nHigher Prize Levels Stacked at the Top\nPrizes Under $%d Reinvested" % cutOff yLabel = "Cumulative Return per Dollar%s" % (L1) n = len(game) ind = numpy.arange(0.5,len(game),1.0) print ind width = 0.90 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="y") first = True for i in range(1,20,1): p.append({}) if i % 3 == 0: chartColor = "y" elif i % 3 == 1: chartColor = "g" else: chartColor = "b" #if i == 0: # chartColor = "r" p[i] = plt.bar(range(0,n), graphOdds[i], width, bottom=bM[i], color=chartColor) ax.set_title(graphTitle, fontsize=50) ax.set_xticklabels((game), fontsize=10) plt.xlabel("\nCalifornia Lottery Draw & Scratchers Games\nPrizes Under $%d Reinvested" % cutOff, fontsize=36) ax.set_ylabel(yLabel, fontsize=30) ax.set_xticks(ind) plt.ylim(0, 1.0) sN = "./lottery/rfdReinvestedChart-%d.png" % cutOff print "Working On %s" % sN plt.savefig(sN) #plt.show() print "FINISHED %s" % sN ''' gD = loadData() gD.append(lotto.roulette()) #gD = [g for g in gD if g.prizes[0].prize > 1000000] rfdReinvestChart(gD, 0) rfdReinvestChart(gD, 1000000) rfdReinvestChart(gD, 100000) rfdReinvestChart(gD, 10000) rfdReinvestChart(gD, 1000) rfdReinvestChart(gD, 100) rfdReinvestChart(gD, 10) ''' #gD = loadData() #gD = [g for g in gD if g.prizes[0].prize >= 1000000] #rfdReinvestChart(gD, 1000000) def rfdDailyDerby(g): ''' This creates a sequential Return per Dollar Chart with lower RPD reinvested for dailyDerby ONLY g = dailyDerby lotteryGame object ''' game = [] odds = [] cR = [0, 10,100,1000,10000,100000,1000000] for c in cR: rfd = rpdReinvestData(g, c) rfd.reverse() print rfd tOdds = [] for v in range(0, 20, 1): if v < len(rfd): tOdds.append(rfd[v]) else: tOdds.append(0) if tOdds[0] == 0: tOdds[0] = 0.0000000000001 odds.append(tOdds) game.append("$%d" % c) graphTitle = "Daily Derby Sequential Return Per Dollar" L1 = "\nHigher Prize Levels Stacked at the Top" yLabel = "Cumulative Return per Dollar%s" % (L1) n = len(game) ind = numpy.arange(0.5,len(game),1.0) print ind width = 0.90 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="y") first = True for i in range(1,20,1): p.append({}) if i % 3 == 0: chartColor = "y" elif i % 3 == 1: chartColor = "g" else: chartColor = "b" #if i == 0: # chartColor = "r" p[i] = plt.bar(range(0,n), graphOdds[i], width, bottom=bM[i], color=chartColor) ax.set_title(graphTitle, fontsize=50) ax.set_xticklabels((game), fontsize=35) plt.xlabel("\nDaily Derby Return per Dollar\nPrizes Below Amount Noted Reinvested", fontsize=36) ax.set_ylabel(yLabel, fontsize=30) ax.set_xticks(ind) plt.ylim(0, 1.0) sN = "./lottery/rfdDailyDerby.png" print "Working On %s" % sN plt.savefig(sN) #plt.show() print "FINISHED %s" % sN ''' gD = loadData() gD = [g for g in gD if g.name == "dailyDerby"] g = gD[0] g.printLotteryGameData() rfdDailyDerby(g) ''' def millionDollarPrizeChart(): ''' This Chart prints out the current prizeLevel This creates a sequential Return per Dollar Chart with lower RPD reinvested for dailyDerby ONLY g = dailyDerby lotteryGame object ''' gD = loadData() gD = [g for g in gD if g.prizes[0].prize >= 1000000] legend = [] prize = [] for g in gD: legendName = "%s" % g.name legend.append(legendName) prize.append(g.prizes[0].prize) #Two add the second Prize layer p2 = [] for g in gD: p2.append(0) p2[0] = gD[0].prizes[1].prize prize[0] += p2[0] graphTitle = "$1,000,000 +\nCalifornia Lottery Prizes\n(7-11-14 Data)" yLabel = "Top Prize in Tens of Millions of Dollars\n(I'm a gonna be rich!!!)" n = len(legend) ind = numpy.arange(0.5,n,1.0) width = 0.90 plt.rcParams['figure.figsize'] = 50,25 fig, ax = plt.subplots() plt.bar(range(n), prize, width, color='y') plt.bar(range(n), prize, width, bottom=p2, color='b') ax.set_title(graphTitle, fontsize=50) ax.set_xticklabels((legend), fontsize=24) plt.xlabel("\nMillion Dollar Plus California Lottery Games", fontsize=36) ax.set_ylabel(yLabel, fontsize=30) ax.set_xticks(ind) #plt.ylim(0, 50000000) sN = "./lottery/millionDollarPrizeChart.png" print "Working On %s" % sN plt.savefig(sN) #plt.show() print "FINISHED %s" % sN millionDollarPrizeChart() '''From here down, might just be garbage ''' #OLD FOR REFERENCE 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]) #OLD FOR REFERENCE def rpdChart(gD, goal="$1,000,000", allGames=False): ''' ''' target = goal.replace("$","") target = target.replace(",","") target = int(target) game = [] rpd = [] wData = [] if allGames: test = gD else: test = [g for g in gD if rpdAboveTarget(g, target) > 0] for t in test: t.printLotteryGameData() print "%s \t %s" % (t.name, t.fileName) rD = rpdAboveTarget(t, target) game.append("%s\n%.8f" % (t.name, rD)) rpd.append(rD) #(1.00 / float()) #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) gRD = "%s%.8f%s" % (r1, rD, r2) wData.append("%s%s%s%s%s" % (t1,gN,gF,gRD,t2) ) #wData.append(r" CASHCASH1.000000 ") g1 = "\n(normalized for ticket price)\n(winnings reinvested until declared prize level is reached)" if allGames: g2 = "" else: g2 = "\n(only games that have a prize at this level are shown)" graphTitle = "Return Per Dollar Playing for %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 = "RETURN PER DOLLAR INVESTED" #% (L1) n = len(game) ind = numpy.arange(0.5,len(game),1.0) print ind width = 0.95 plt.rcParams['figure.figsize'] = 2*len(rpd), len(rpd) fig, ax = plt.subplots() bL = ax.bar(range(0,n), rpd, width, color="lightblue") bL[n-1].set_color("g") mL = [0.5] * n mLine = ax.plot(range(0,n), mL) ax.set_title(graphTitle) ax.set_xticks(ind) ax.set_xticklabels((game)) ax.set_ylabel(yLabel) ax.set_yscale('linear') print game if any(g.startswith("CASH") for g in game): print "YES CASH WAS IN LIST" #ax.set_color('r') print len(game) print len(ind) if allGames: allText = "ALL" else: allText = "partial" sN = "./lottery/%d-lotteryCumulative_RPD_%s.png" % (target, allText) print "Working On %s" % sN plt.savefig(sN) plt.show() sN = "./lottery/%d-lotteryCumulative_RPD_%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%sRPD%s%s\n" % (t1, r1, r2, r1, r2, r1, r2, t2))) for d in wData: txt.write(d) txt.write("\n") #gD = loadData() #chanceChart(gD, "$1,000,000", True) #gD.append(lotteryGame().cash()) #rpdChart(gD, "$1,000,000", True) ''' amounts = ["$10,000,000", "$1,000,000", "$100,000", "$10,000", "$1,000", "$100", "$10", "$0"] for a in amounts: chanceChart(gD, a, True) chanceChart(gD, a, False) ''' #gD = loadData()