''' Created on Jul 25, 2014 @author: Brett Paufler Copyright Brett Paufler RUNS PRIZE, Chance, RpD Graphs Based on Current Lottery Pickled Data ''' 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: data = pickle.load(f) return data def pbOptions(onlyTwo=True): '''returns a LIST of TWO lotteryGames pbHigh pbLow or if onlyTrue=False, returns complete List of options ''' gD = [] for n in range(5): gD.append(loadData()[0]) gD[0].name = "powerBall\nBase Data" gD[1].name = "powerBall\nHigh Jackpot" del gD[1].prizes[1] #gD[1].printLotteryGameData() #print "\n\n\n3" gD[2].name = "pb HIGH" #Low is Reinvested gD[2].price = float(gD[2].price) - float(gD[2].prizes[1].prize) / (gD[2].prizes[1].odds * gD[2].price) del gD[2].prizes[1] #gD[3].printLotteryGameData() gD[3].name = "powerBall\nLow Jackpot" del gD[3].prizes[0] #gD[2].printLotteryGameData() #print "\n\n\n4" gD[4].name = "pb LOW" #High is Reinvested gD[4].price -= float(gD[4].prizes[0].prize) / (gD[4].prizes[0].odds * gD[4].price) del gD[4].prizes[0] #gD[4].printLotteryGameData() if onlyTwo: gD = gD[2::2] #Returns ONLY # pb LOW (High Reinvested) # pb HIGH (low Reinvested) return gD def lotteryPrizeChart(gD,cutOff): ''' ''' pltText = "" legend = [] prize = [] for g in gD: p = g.prizes[0].prize legendName = "%s\n%d" % (g.name, p) legend.append(legendName) prize.append(p) pltText += g.fileName[:-5] + " $" + str(int(g.price)) + "\n" pltText = pltText.replace(".html", "") #For the Unaltered Data, combining Both PRizes #prize[0] = gD[0].prizes[0].prize + gD[0].prizes[1].prize graphTitle = "California Lottery - Top Prizes" yLabel = "Top Prizes (tens of millions typically)" xText = "Lottery Games with Million Dollar Prizes" n = len(legend) ind = numpy.arange(0.5,n,1.0) width = 0.90 plt.rcParams['figure.figsize'] = 20,10 fig, ax = plt.subplots() plt.bar(range(n), prize, width, color="b") ax.set_title(graphTitle, fontsize=50) ax.set_xticklabels((legend), fontsize=15) plt.xlabel(xText, fontsize=35) ax.set_ylabel(yLabel, fontsize=35) ax.set_xticks(ind) #plt.ylim(0, 50000000) #Added so can tell the games on the fly plt.annotate(pltText, xy=(0.5,0.2), fontsize=24, xycoords='axes fraction') sN = "./lottery/CA-Lottery-Top-Prizes.png" print "Working On %s" % sN plt.savefig(sN) #plt.show() print "FINISHED %s" % sN 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 lotteryRPDChart(gD, cutOff): ''' Analysis of the various PowerBall options using the predefined RPD chart methodology ''' legend = [] rpd = [] for g in gD: r = sum(rpdReinvestData(g, cutOff)) rpd.append(r) legendText = "%s\n%.5f" % (g.name, r) legend.append(legendText) xText = "Lottery Games with Million Dollar Prizes" graphTitle = "California Lottery - Return per Dollar" yLabel = "Return per Dollar\n(long term expected)" n = len(rpd) ind = numpy.arange(0.5,n,1.0) width = 0.90 plt.rcParams['figure.figsize'] = 20,10 fig, ax = plt.subplots() plt.bar(range(n), rpd, width, color="g") ax.set_title(graphTitle, fontsize=50) ax.set_xticklabels((legend), fontsize=15) plt.xlabel(xText, fontsize=35) ax.set_ylabel(yLabel, fontsize=35) ax.set_xticks(ind) #plt.ylim(0, 50000000) sN = "./lottery/CA-Lottery-Top-RPD.png" print "Working On %s" % sN plt.savefig(sN) #plt.show() print "FINISHED %s" % sN def oddsAboveTarget(g, target=100000): '''returns the cumulative chance 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 odds above target ''' 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 def lotteryChanceChart(gD, cutOff): ''' Analysis of the powerBall Options, using a Chance Chart ''' legend = [] chance = [] for g in gD: c = sum(oddsAboveTarget(g,cutOff)) chance.append(c) oT = 1.00/c if c != 0 else 0 legendText = "%s\n%d" % (g.name, oT) legend.append(legendText) print chance graphTitle = "California Lottery - Chance per Dollar" yLabel = "Chance of Winning (1/odds)\nLower Prize Amounts Reinvested" xText = "Lottery Games with Million Dollar Prizes (odds)" n = len(chance) ind = numpy.arange(0.5,n,1.0) width = 0.95 plt.rcParams['figure.figsize'] = 20,10 fig, ax = plt.subplots() plt.bar(range(n), chance, width, color="r") ax.set_title(graphTitle, fontsize=50) ax.set_xticklabels((legend), fontsize=15) plt.xlabel(xText, fontsize=35) ax.set_ylabel(yLabel, fontsize=35) ax.set_xticks(ind) #plt.ylim(0, 50000000) sN = "./lottery/CA-Lottery-Top-Chance.png" print "Working On %s" % sN plt.savefig(sN) #plt.show() print "FINISHED %s" % sN if __name__ == "__main__": '''Draws the three Graphs PRIZE, CHANCE, RpD for topPrize>=$1,000,000 ''' #Can Change to 100000 (or less) but formatting isn't as good prizeCutOff = 1000000 print "Starting, first part can take a moment or two" pB = pbOptions() gD = loadData() gD = gD[1:] gD = pB + gD gD = [g for g in gD if g.prizes[0].prize >= prizeCutOff or g.name == "pb LOW"] gD[2].name = "Mega" gD[3].name = "Super" for g in gD: print g.name print "First Graph starts NOW" lotteryPrizeChart(gD, prizeCutOff) lotteryRPDChart(gD, prizeCutOff) lotteryChanceChart(gD, prizeCutOff) print "FINISHED PRIZE, CHANCE, RpD Graphs"