''' Created on Jul 28, 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 #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 chanceAboveTarget(g, target=1000000): '''returns the cumulative 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 odds above target DON'T ACTALLY NEED FOR THIS CHART ''' 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)) #This is the Chance, highly misnamed return overOdds def bestChanceScratcher(pL=1000000): '''goes through the pickled data and returns the scratchers game with the best chance of winning a prize of the stated value or higher DON'T ACTALLY NEED FOR THIS CHART ''' gD = loadData() sD = [g for g in gD if g.name.startswith('s')] sD = [s for s in sD if not s.name.startswith('super')] sD = [s for s in sD if s.prizes[0].prize >= pL] for s in sD: s.chance = chanceAboveTarget(s,pL) highest = sD[0] print highest for s in sD: if s.chance > highest.chance: highest = s #print s.chance #print highest.chance #print highest.printLotteryGameData() return highest #bestChanceScratcher() def rpdAboveCutoff(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 bestRPDScratcher(pL=1000000): '''goes through the pickled data and returns the scratchers game with the best chance of winning a prize of the stated value or higher ''' gD = loadData() sD = [g for g in gD if g.name.startswith('s')] sD = [s for s in sD if not s.name.startswith('super')] sD = [s for s in sD if s.prizes[0].prize >= pL] for s in sD: s.rpd = sum(rpdAboveCutoff(s,pL)) highest = sD[0] #print highest for s in sD: if s.rpd > highest.rpd: highest = s #print s.rpd #print highest.rpd #print highest.printLotteryGameData() return highest #bestRPDScratcher() 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 gamesOfInterest(): '''Returns a List of the LotteryGame objects of interest in the graph below prize over $1,000,000, only the scratcher with the best RpD ''' gD = pbOptions() dG = loadData() dG = dG[1:3] gD = gD + dG gD.append(bestRPDScratcher()) return gD def dynamicRPDChart(gD, b, e, limit=True): ''' Analysis of the powerBall Options, using a Chance Chart ''' leg = [] rpd = [] #vals = numpy.linspace(b, e) vals = range(b, (e+1), (e-b)/10) for g in gD[:-1]: leg.append(g.name) r = [] for v in vals: g.prizes[0].prize = v r.append(sum(rpdAboveCutoff(g,1000000))) rpd.append(r) x = sum(rpdAboveCutoff(gD[-1],1000000)) r = [] for v in vals: r.append(x) rpd.append(r) leg.append(gD[-1].name) for r in rpd: print r #print rpd graphTitle = "Return Per Dollar" yLabel = "Cumulative RpD for High Prize\n(Lower Prize Amounts Reinvested)" xText = "Current Jackpot" plt.rcParams['figure.figsize'] = 20,10 fig, ax = plt.subplots() for r in rpd: plt.plot(vals, r) plt.legend(leg, loc='upper left') 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(vals) if limit: plt.ylim(0,1) sN = "./lottery/CA-Lottery-Dynamic-RPD-%d-%d.png" % (b,e) print "Working On %s" % sN plt.savefig(sN) #plt.show() print "FINISHED %s" % sN ''' gD = gamesOfInterest() b = 1000000 e = 1000000000 dynamicRPDChart(gD,1000000, 5000000) #for pb low dynamicRPDChart(gD,10000000, 20000000) #for superLotto dynamicRPDChart(gD,75000000, 125000000) #for pB High & Mega dynamicRPDChart(gD,1, 100000000, False) # No Limit ''' #for the PowerBall Highlight Chart at end of page def superLottoChart(): gD = gamesOfInterest() gD = gD[3:] for g in gD: print g.name dynamicRPDChart(gD,10000000, 100000000, False) #superLottoChart() def megaMillionChart(): gD = gamesOfInterest() gD = gD[2::2] for g in gD: print g.name dynamicRPDChart(gD,100000000, 500000000, False) megaMillionChart() def powerBallHighOptions(b,e): '''graph a spectrum of low jackpot return per dollar possibilities ''' gD = [] skip = 4 for n in range(10,-1,-1): tD = loadData() g = tD[0] x = n * 1000000 g.prizes[1].prize = x g.name = "lowJP: %d" % x g.price = float(g.price) - float(g.prizes[1].prize) / (g.prizes[1].odds * g.price) del g.prizes[1] gD.append(g) leg = [] rpd = [] #vals = numpy.linspace(b, e) vals = range(b, (e+1), (e-b)/10) for g in gD: leg.append(g.name) r = [] for v in vals: g.prizes[0].prize = v r.append(sum(rpdAboveCutoff(g,10000000))) rpd.append(r) for r in rpd: print r #print rpd graphTitle = "Power Ball High Jackpot\nReturn Per Dollar" yLabel = "Cumulative RpD for High Jackpot\n(all other prizes reinvested)" xText = "Current High Prize Jackpot" #xTickText = ["", "$50,000,000","", '$200,000,000'] plt.rcParams['figure.figsize'] = 20,10 fig, ax = plt.subplots() for r in rpd: plt.plot(vals, r) plt.legend(leg, loc='upper left') ax.set_title(graphTitle, fontsize=35) plt.xlabel(xText, fontsize=35) ax.set_ylabel(yLabel, fontsize=35) ax.set_xticklabels((vals), fontsize=15) ax.set_xticks(vals) sN = "./lottery/CA-Lottery-powerBall-High-SpreadOptions-%d-%d.png" % (b,e) print "Working On %s" % sN plt.savefig(sN) #plt.show() print "FINISHED %s" % sN #powerBallHighOptions(0, 500000000) #powerBallHighOptions(100000000, 200000000) def powerBallLowOptions(b,e): '''graph a spectrum of low jackpot return per dollar possibilities ''' gD = [] for n in range(10,-1,-1): tD = loadData() g = tD[0] x = n * 25000000 g.prizes[0].prize = x g.name = "HighJP: %d" % x g.price = float(g.price) - float(g.prizes[0].prize) / (g.prizes[0].odds * g.price) del g.prizes[0] gD.append(g) leg = [] rpd = [] #vals = numpy.linspace(b, e) vals = range(b, (e+1), (e-b)/10) for g in gD: leg.append(g.name) r = [] for v in vals: g.prizes[0].prize = v r.append(sum(rpdAboveCutoff(g,250000))) rpd.append(r) for r in rpd: print r #print rpd graphTitle = "Power Ball Low Jackpot\nReturn Per Dollar" yLabel = "Cumulative RpD for Low Jackpot\n(all other prizes reinvested including high jackpot)" xText = "Current Low Jackpot" #xTickText = ["", "$50,000,000","", '$200,000,000'] plt.rcParams['figure.figsize'] = 20,10 fig, ax = plt.subplots() for r in rpd: plt.plot(vals, r) plt.legend(leg, loc='upper left') ax.set_title(graphTitle, fontsize=35) plt.xlabel(xText, fontsize=35) ax.set_ylabel(yLabel, fontsize=35) ax.set_xticklabels((vals), fontsize=15) ax.set_xticks(vals) sN = "./lottery/CA-Lottery-powerBall-Low-SpreadOptions-%d-%d.png" % (b,e) print "Working On %s" % sN plt.savefig(sN) #plt.show() print "FINISHED %s" % sN #powerBallLowOptions(0, 10000000) #powerBallLowOptions(2500000, 5000000)