-
Notifications
You must be signed in to change notification settings - Fork 2
/
stats.py
139 lines (109 loc) · 4.78 KB
/
stats.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from constants import DEALER_UP_CARD_FEATURE, PLAYER_HAND_FEATURE, PLAYER_RESULT_FEATURE, \
PLAYER_CURRENT_TOTAL, PLAYER_CURRENT_ACTION
from constants import NUM_SIMULATIONS
# Method to generate the NN model and all other charts
def generate_stats(strategy_name):
model_df = generate_model()
if not os.path.exists(os.path.join("stats", strategy_name)):
os.makedirs(os.path.join("stats", strategy_name))
stats_folder = os.path.join("stats", strategy_name)
plot_win_vs_dealer_up_card(model_df, stats_folder)
plot_win_vs_player_hand(model_df, stats_folder)
plot_player_hand_vs_dealer_up_card(model_df, stats_folder)
# Plotting a chart of winning probability versus dealer's face up card
def plot_win_vs_dealer_up_card(model_df, stats_folder):
data = 1 - (model_df.groupby(by='dealer_card').sum()['loss'] /
model_df.groupby(by='dealer_card').count()['loss'])
fig, ax = plt.subplots(figsize=(10, 6))
ax = sns.barplot(x=data.index,
y=data.values)
ax.set_xlabel("Dealer's Card", fontsize=16)
ax.set_ylabel("Probability of Tie or Win", fontsize=16)
plt.tight_layout()
plt.savefig(fname=os.path.join(stats_folder, 'dealer_card_probs'), dpi=50)
# Plotting a chart of winning probability versus player's hand total
def plot_win_vs_player_hand(model_df, stats_folder):
data = 1 - (model_df.groupby(by='player_total_initial').sum()['loss'] /
model_df.groupby(by='player_total_initial').count()['loss'])
fig, ax = plt.subplots(figsize=(10, 6))
ax = sns.barplot(x=data[:-1].index,
y=data[:-1].values)
ax.set_xlabel("Player's Hand Value", fontsize=16)
ax.set_ylabel("Probability of Tie or Win", fontsize=16)
plt.tight_layout()
plt.savefig(fname=os.path.join(stats_folder, 'player_hand_probs'), dpi=50)
# Plotting a chart of player's hand total versus dealer's face up card
def plot_player_hand_vs_dealer_up_card(model_df, stats_folder):
pivot_data = model_df[model_df['player_total_initial'] != 21]
losses_pivot = pd.pivot_table(pivot_data, values='loss',
index=['dealer_card_num'],
columns=['player_total_initial'],
aggfunc=np.sum)
games_pivot = pd.pivot_table(pivot_data, values='loss',
index=['dealer_card_num'],
columns=['player_total_initial'],
aggfunc='count')
heat_data = 1 - losses_pivot.sort_index(ascending=False) / games_pivot.sort_index(ascending=False)
fig, ax = plt.subplots(figsize=(16, 8))
sns.heatmap(heat_data, square=False, cmap="PiYG")
ax.set_xlabel("Player's Hand Value", fontsize=16)
ax.set_ylabel("Dealer's Card", fontsize=16)
plt.savefig(fname=os.path.join(stats_folder, 'heat_map_random'), dpi=50)
# Plotting chart of win rate tracked over simulations
def plot_chart(win_rates, stats_folder):
plt.style.use('ggplot')
plt.figure(figsize=(12, 6))
plt.plot(range(NUM_SIMULATIONS), win_rates)
plt.title("Blackjack Probability")
plt.ylabel("Win rate")
plt.xlabel("Games Played")
plt.ylim([0, 100])
plt.xlim([0, NUM_SIMULATIONS])
plt.savefig(fname=os.path.join(stats_folder, 'player_win_rate'), dpi=50)
# Generating model dataframe containing features
def generate_model():
model_df = pd.DataFrame()
model_df['dealer_card'] = DEALER_UP_CARD_FEATURE
model_df['player_total_initial'] = [hand.get_initial_hand_total() for hand in PLAYER_HAND_FEATURE]
model_df['Y'] = [res[0] for res in PLAYER_RESULT_FEATURE]
model_df['hit?'] = PLAYER_CURRENT_ACTION
loss = []
for res in model_df['Y']:
if res == -1:
loss.append(1)
else:
loss.append(0)
model_df['loss'] = loss
has_ace = []
for hand in PLAYER_HAND_FEATURE:
if 'A' in [card.get_face() for card in hand.hand_cards]:
has_ace.append(1)
else:
has_ace.append(0)
model_df['has_ace'] = has_ace
dealer_card_num = []
for d_card in model_df['dealer_card']:
if d_card == 'A':
dealer_card_num.append(11)
else:
dealer_card_num.append(d_card)
model_df['dealer_card_num'] = dealer_card_num
correct = []
for i, val in enumerate(model_df['loss']):
if val == 1:
if PLAYER_CURRENT_ACTION[i] == 1:
correct.append(0)
else:
correct.append(1)
else:
if PLAYER_CURRENT_ACTION[i] == 1:
correct.append(1)
else:
correct.append(0)
model_df['correct_action'] = correct
return model_df