-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack_analysis.py
119 lines (86 loc) · 3.07 KB
/
blackjack_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 14 12:41:38 2020
@author: Jake Frazer
"""
import numpy as np
import matplotlib.pyplot as plt
# blackjack probabilities - without draws
p_win = 0.464
p_lose = 0.536
# blackjack probabilities - with draws
p_win = 0.4243
p_lose = 0.4909
p_draw = 0.0848
# simulating playing one stretch of hands - true if make it to end - false if hit 0 at some point
def knocked_out(min_bet, value_required, cash_stack):
''' TODO: Make it carry out the final value too so that we can see the money pulled out in
order to calculate EV of strategy
'''
# check to catch if doubling phase has already surpassed betting requirements
if value_required < 0:
return True, cash_stack
hands_to_play = value_required/min_bet
ko_count = -(cash_stack/min_bet)
wl_count = 0
hands = np.random.random(int(hands_to_play))
for h in hands:
# you lose the hand
if h >= p_win + p_draw:
wl_count -= 1
# you draw the hand
elif h >= p_win:
continue
# you win the hand
else:
wl_count += 1
if wl_count == ko_count:
return False, 0
return True, cash_stack + (wl_count*min_bet)
def doubling_phase(n, start_cash):
''' calculates probability of successfully doubling for n hands.
returns the prob of being alive, cash stack at that ponit, total amount
wagered to then
'''
end_cash = start_cash
total_wagered = 0
prob = 0.464**n
for i in range(1,n+1):
total_wagered += end_cash
end_cash *= 2
return prob, end_cash, total_wagered
def calculate_evs(doubling_hands, start_cash, min_bet, value_required):
''' func to work out EV's and probability of getting money
'''
# simulates doubling phase for n hands
prob, end_cash, total_wagered = doubling_phase(doubling_hands, start_cash)
# make this as big as poss but computational pain!
n = 10000
# gives probability of making it to the end with some amount of money
# sums[0] = prob, sums[1] = money
result = np.array([knocked_out(min_bet, (value_required-total_wagered), end_cash) for x in range(n)],dtype='int64')
sums = np.sum(result,0)/n
probability_of_return = prob*sums[0]
return probability_of_return, sums[1]
# gather some results for different starting values -- EV's
n_range = range(1,40)
results = np.array([calculate_evs(n,10,5,3500) for n in n_range])
x_axis = [x-1 for x in n_range]
evs, prob = [], []
for x in results:
evs.append(x[0]*x[1])
prob.append(x[0]*100)
# my situation post doubling phase
my_scenario = calculate_evs(0,480,5,3025)
# ev plot
plt.plot(x_axis, evs)
plt.ylabel("Expectation Value £'s")
plt.xlabel("Initial all-in hands")
plt.title("Expectation value of free blackjack credit")
plt.show()
# prob of getting any return plot
plt.plot(x_axis, prob)
plt.ylabel("Probability of ANY return, %")
plt.xlabel("Initial all-in hands")
plt.title("Likelihood of a return from free blackjack credit")
plt.show()