''' Created on Jul 11, 2014 @author: Brett Paufler Copyright Brett Paufler Creates Game Objects for Alternate Games 50-50 (coin toss) ''' import scipy.misc import sympy import decimal from lotto_class import prizeLevel from lotto_class import lotteryGame #import math def blackjack(): '''returns a 1 layer deep blackjack or 21 game ''' a = lotteryGame() a.name = 'blackjack' a.fileName = 'blackjack twenty one' a.price = 1 a.prizes.append(prizeLevel()) a.prizes[0].prize = 2 a.prizes[0].odds = 1.00 / 0.4636 a.prizes[0].returnPerDollar = float(a.prizes[0].prize)/a.prizes[0].odds return a def coinToss(): '''returns a 1 layer deep 50-50 (coin toss) type game ''' a = lotteryGame() a.name = 'coinToss' a.fileName = 'simple 50-50 coin toss type game' a.price = 1 a.prizes.append(prizeLevel()) a.prizes[0].prize = 2 a.prizes[0].odds = 2 a.prizes[0].returnPerDollar = float(a.prizes[0].prize)/a.prizes[0].odds return a def craps(): '''returns a 1 layer deep craps style game, betting on the cum line (pass) ''' a = lotteryGame() a.name = 'craps' a.fileName = 'craps near even money on the pass line' a.price = 1 a.prizes.append(prizeLevel()) a.prizes[0].prize = 2 a.prizes[0].odds = 1 / 0.492929 a.prizes[0].returnPerDollar = float(a.prizes[0].prize)/a.prizes[0].odds return a def roulette(): '''returns a 1 layer deep roulette game, betting straight up on a number American Style (0-00) ''' a = lotteryGame() a.name = 'roulette' a.fileName = 'simple 35to1 roulette straight up' a.price = 1 a.prizes.append(prizeLevel()) a.prizes[0].prize = 36 a.prizes[0].odds = 38 a.prizes[0].returnPerDollar = float(a.prizes[0].prize)/a.prizes[0].odds return a def casinoGames(): '''returns a list of all the standard casino games ''' return [coinToss(),craps(),blackjack(),roulette()] def letItRideBROKEN(g, n=2): '''taking a simple game (any of the above) as input reconfigures odds and prize if all winning bets 'ride' for the given number of iterations g = lotteryGame object with 1 prize level n = number of times to let it ride letItRide(1) returns the original object letItRide(2) returns odds for two straight wins, all money left on the table TODO - WOULD NEED TO IMPLEMENT A .copy function to work a = g, g = a, writing over the passed value ''' #print n a = g a.prizes[0].odds = g.prizes[0].odds ** n a.prizes[0].prize = g.prizes[0].prize ** n #g.printLotteryGameData() return a def simpleGamblersRuin(g, s, f): '''given a simple game g (one prize level) returns the chance (1/odds) that player will win f before they lose s That is the odds a player will with bankroll s=start will increase their money to f=finish while making $1 bets game = lotteryGame Ojbect (only first prize level is evaluated) s = starting cash f = desired finish cash goal returns probability (1/odds) (0.00 to 1.00) of this happening prob = (1 - (q/p) ** s) / (1 - (q/p) ** (s+f) where p = g.prizes[0].odds Formula From (BOTH): https://www.math.umass.edu/~lr7q/ps_files/teaching/math456/Week4.pdf http://www.columbia.edu/~ks20/FE-Notes/4700-07-Notes-GR.pdf ''' s = float(s) f = float(f) p = float(1.00 / g.prizes[0].odds) print p q = 1 - p if q == p: prob = s / (f) print "Fifty Fifty Math" else: t = (1 - (q/p) ** s) #print t b = (1 - (q/p) ** (f)) #print b prob = t / b #print prob return prob #grindOdds = gamblersRuin #print gamblersRuin(coinToss(), 3, 6 ) #print (2 ** 2) / 3.0 #print grindOdds(coinToss(), 3, 6 ) #print 1.0 / simpleGamblersRuin(craps(), 500,525) ''' game = blackjack() game.printLotteryGameData() game = craps() game.printLotteryGameData() game = coinToss() game.printLotteryGameData() print "RIDE ON COMMENCING" game = roulette() game.printLotteryGameData() rideOn = letItRide(game,3) print rideOn.printLotteryGameData print "Grind Odds" ''' #print grindOdds(coinToss(), 10) #print grindOdds(craps(), 10) def funRoulette(C, p, w): ''' The iterative part of the Gambler's Ruin for non- 1to1 bets hardwired for roulette w = number of chips = 35 p = 0.475 ''' decimal.getcontext().prec = 100 C = decimal.Decimal(C) p = decimal.Decimal(p) w = decimal.Decimal(w) q = decimal.Decimal(1-p) r = (C-1)/(w+1) + 1 print "r equals %d" % r MC = decimal.Decimal(0) print "MC = %.100f" % MC for i in range(0, int(((C-1)/(w+1) + 1)), 1): i = decimal.Decimal(i) #print "i %d" % i neg = (-1)**i cC = int(C - i*w - 1) #print "Icw1 = %d" % cC combo = decimal.Decimal(scipy.misc.comb(cC, i, exact=True)) #print combo #TODO PERHAPS CHANCE THIS BACK ''' PERHAPS HERE ''' pq = (p*(q**w))**i #print "pq = %.100f" % pq #print neg MC += neg * combo * pq print "MC = %.100f" % MC MC = abs(MC) print "funRoulette returning %.100f" % MC return MC #return 1 def gamblersRuin(p,w, j, C): ''' 1. j is the initial capital of the gambler, j>=0, 2. C is the ultimate fortune which the gambler wants to accumulate, C>=j>=0, 3. w is the number of chips the gambler may win in every single game, w>=1, 4. p is the probability of winning w chips in a single game. In every single game the gambler is either winning w chips, with probability p, or losing one chip, with probability q = 1 - p. After a series of single games, the game terminates when, 5. either the total fortune of the gambler for the 2rst time reaches or exceeds C chips, or 6. his fortune drops down to 0 chips. P(C,j) = 1 - q**j (FM(C-j))/FM(C) 'http://ac.els-cdn.com/S0304414902001060/ 1-s2.0-S0304414902001060-main.pdf?_tid=f17a7f10-0d61-11e4-9dd9 -00000aacb35f&acdnat=1405567684_41bce5d2b0c8eb5623c3ed85c5a99c0e' ''' decimal.getcontext().prec = 100 p = decimal.Decimal(p) q = decimal.Decimal(1 - p) C = decimal.Decimal(C) j = decimal.Decimal(j) w = decimal.Decimal(w) #edge cases, no chance if have no money #a certainty if start with more money than goal if j <= 0: print "No Funds Automatically Loses, returning 0" return 0 if j >= C: print "Funds exceed Target, returning 1" return 1 if abs(p-q) <= .0000000001: fF = j/C print "odds close to fifty-fifty, j/C = %.100f" % fF return fF top = funRoulette((C-j),p,w) bot = funRoulette(C,p,w) qj = (q ** j) Pcj = decimal.Decimal(1) - qj * top / bot Pcj = abs(Pcj) print "top = %.100f" % top print "bot = %.100f" % bot print "qj = %.100f" % qj print "PcJ = %.100f" % Pcj return Pcj #gamblersRuin(p=0.0263157894737, w=35.0, j=5, C=50) #roulette odds = 0.0263157894737 #print scipy.misc.comb(14,3) #print 5.0/38.0 #if 1 == 1.00: #print "We have a match" def mC(p, q, w, C): ''' ''' r = int((C-1)/(w+1)) pqw = p * (q**w) mc = [] for i in range(0, (r+1), 1): cC = C - i*w - 1 cD = scipy.misc.comb(cC, i, exact=True) n = (-1)**i mc.append(n * cD * pqw**i) rV = abs(sum(mc)) print "Running mC for %.5f %.5f %d %d :RETURNING %.100f" % (p,q,w,C,rV) return rV def gR(p, w, j, C): ''' ''' #print "gR Called with %.5f %d %d %d" % (p, w,j, C) if 2 * C > 25: precision = 2 * C else: precision = 25 decimal.getcontext().prec = precision p = decimal.Decimal(p) q = decimal.Decimal(1-p) w = decimal.Decimal(w) j = decimal.Decimal(j) C = decimal.Decimal(C) print '\n\n' if j <= 0: print "j = %d, No Chance Returning 0" % j return 0 if j >= C: print "j >= C (%d >= %d), A Certainty, Returning 1" % (j, C) return 1 if abs(p-q) < .0000000001: print "Fifty-Fifty returning j/C" return j/C t = decimal.Decimal(mC(p,q,w,(C-j))) print "t = %.100f" % t b = decimal.Decimal(mC(p,q,w,C)) print "b = %.100f" % b tb = decimal.Decimal((t / b)) print "tb = %.100f" % tb qj = decimal.Decimal(q ** j) print "qj = %.100f" % qj tq = decimal.Decimal(tb * qj) print "tq = %.100f" % tq pF = decimal.Decimal(1 - tq) print pF print "gR Called with %.5f %d %d %d :RETURNING %.100f" % (p, w,j, C, pF) return pF p = 1.0 / 38.0 print "Roulette 10 to 1500" #print gR(p, 35,10, 1500) print "Roulette 10 to 1500" ''' r1-1000=0.00014531113491820637976, odds=6881 r10-1000=0.0014733204939778766014, odds=678.7389 r1-1500=0.00003032796164555663731, odds=32972.8720871 r10-1500=0.00030749747745157266, odds=3252.0591983 r1-2000=0.0000065143704886973528, odds=153506.774252 r10-2000=0.0000660496909047261146517796, odds=15140.1162716 ''' print 1 / 0.00030749747745157266