SHOW:
|
|
- or go back to the newest paste.
1 | #!/usr/bin/env python | |
2 | ||
3 | """ | |
4 | This script was made to understand probabilities behind SatoshiDice game | |
5 | http://satoshidice.com/ | |
6 | ||
7 | - | ToDo: implement MoneyManagement |
7 | + | ToDo: implement martingales ... |
8 | - | see fix lot |
8 | + | |
9 | - | see martingale |
9 | + | |
10 | import numpy as np | |
11 | import pandas as pd | |
12 | import matplotlib.pylab as plt | |
13 | ||
14 | class SatoshiDiceGameTester: | |
15 | def __init__(self): | |
16 | print("="*5+" Satoshidice "+"="*5) | |
17 | toss_nb = 65536 | |
18 | max_nb = 2**16-1 # 65535 | |
19 | - | toss_nb = 60000 |
19 | + | |
20 | win_odds = float(less_than) / float(max_nb) * 100.0 | |
21 | price_multiplier = 8 | |
22 | house_percent = 1.9 | |
23 | expected_rate_of_return = 100.0 - house_percent | |
24 | min_bet = 0.01 | |
25 | max_bet = 500 | |
26 | ||
27 | print("""Playing {toss_nb} times to Satoshidice game less than {less_than} | |
28 | win_odds = {win_odds} % | |
29 | price_multiplier = {price_multiplier} | |
30 | house_percent = {house_percent} % | |
31 | expected_rate_of_return = {expected_rate_of_return} % | |
32 | min_bet = {min_bet} | |
33 | max_bet = {max_bet}""".format( | |
34 | toss_nb=toss_nb, | |
35 | less_than=less_than, | |
36 | win_odds=win_odds, | |
37 | price_multiplier=price_multiplier, | |
38 | house_percent=house_percent, | |
39 | expected_rate_of_return=expected_rate_of_return, | |
40 | min_bet=min_bet, | |
41 | max_bet=max_bet, | |
42 | )) | |
43 | ||
44 | toss = np.random.randint(0,max_nb,toss_nb) | |
45 | self.df = pd.DataFrame(toss, columns=['Toss']) | |
46 | - | self.df = pd.DataFrame(np.random.randint(0,max_nb,toss_nb), columns=['Toss']) |
46 | + | |
47 | #self.df['TossResults'] = np.where(self.df['Toss']<less_than, 1, -1) # 1 = win -1=lose | |
48 | ||
49 | self.df['TossResultsCumsum'] = self.df['TossResults'].cumsum() | |
50 | ||
51 | # Fix size | |
52 | self.df['Size'] = 1 | |
53 | - | self.df['Size'] = 0.01 |
53 | + | |
54 | self.df['BalanceVar'] = np.where(self.df['TossResults']>0, self.df['Size']*(price_multiplier-1), -self.df['Size']+0.005 * self.df['Size']) | |
55 | - | self.df['BalanceVar'] = np.where(self.df['TossResults']>0, self.df['Size']*price_multiplier, -self.df['Size']+0.005 * self.df['Size']) |
55 | + | |
56 | ||
57 | #print(self.df.head()) | |
58 | - | print(self.df.head()) |
58 | + | #print(self.df) |
59 | - | print(self.df) |
59 | + | #print(self.df.tail()) |
60 | - | print(self.df.tail()) |
60 | + | |
61 | exp_win = len(self.df[self.df['TossResults']>0]) | |
62 | exp_loss = toss_nb - exp_win | |
63 | exp_win_pc = float(exp_win)/float(toss_nb)*100.0 | |
64 | - | exp_win_pc = float(exp_win)/float(toss_nb)*100 |
64 | + | exp_loss_pc = float(exp_loss)/float(toss_nb)*100.0 |
65 | - | exp_loss_pc = float(exp_loss)/float(toss_nb)*100 |
65 | + | relative_difference = (exp_win_pc-win_odds)/win_odds*100.0 |
66 | - | relative_difference = (exp_win_pc-win_odds)/win_odds*100 |
66 | + | balance_final = self.df['Balance'].irow(toss_nb-1) |
67 | house_pc_exp_final = -balance_final/self.df['Size'].sum()*100.0 | |
68 | print("""Results: | |
69 | toss_nb={toss_nb} | |
70 | win={win} | |
71 | loss={loss} | |
72 | win={win_pc} % | |
73 | loss={loss_pc} % | |
74 | relative difference = {rel_diff} % | |
75 | min balance = {balance_min} | |
76 | - | initial deposit = {balance_initial}""".format( |
76 | + | |
77 | initial deposit = {balance_initial} | |
78 | final balance = {balance_final} | |
79 | house_percent (final) = {house_pc} %""".format( | |
80 | toss_nb=toss_nb, | |
81 | win=exp_win, | |
82 | loss=exp_loss, | |
83 | win_pc=exp_win_pc, | |
84 | loss_pc=exp_loss_pc, | |
85 | - | balance_initial = initial_balance |
85 | + | |
86 | balance_min = self.df['Balance'].min(), | |
87 | balance_max = self.df['Balance'].max(), | |
88 | balance_initial = initial_balance, | |
89 | - | self.df.to_excel('out.xls') |
89 | + | balance_final = balance_final, |
90 | house_pc=house_pc_exp_final, | |
91 | - | fig = plt.figure() |
91 | + | |
92 | ||
93 | - | fig.subplots_adjust(bottom=0.1) |
93 | + | #self.df.to_excel('out.xls') # only 65535 rows |
94 | - | #ax = fig.add_subplot(311) |
94 | + | |
95 | - | #plt.title("Toss") |
95 | + | |
96 | - | #self.df.plot(x=self.df.index, y='Toss', style='*') |
96 | + | #Only Uncomment plot for small tosses number |
97 | #fig = plt.figure() | |
98 | - | #ax = fig.add_subplot(312) |
98 | + | #fig.subplots_adjust(bottom=0.1) |
99 | - | #plt.title("Toss Results") |
99 | + | |
100 | - | #self.df.plot(x=self.df.index, y='TossResults', style='*') |
100 | + | ##ax = fig.add_subplot(311) |
101 | ##plt.title("Toss") | |
102 | - | #ax = fig.add_subplot(313) |
102 | + | ##self.df.plot(x=self.df.index, y='Toss', style='*') |
103 | - | ax = fig.add_subplot(111) |
103 | + | |
104 | - | #plt.title("Toss Results Cumsum") |
104 | + | ##ax = fig.add_subplot(312) |
105 | - | #self.df.plot(x=self.df.index, y='TossResultsCumsum') |
105 | + | ##plt.title("Toss Results") |
106 | - | plt.title("Balance") |
106 | + | ##self.df.plot(x=self.df.index, y='TossResults', style='*') |
107 | - | self.df.plot(x=self.df.index, y='Balance') |
107 | + | |
108 | ##ax = fig.add_subplot(313) | |
109 | #ax = fig.add_subplot(111) | |
110 | ##plt.title("Toss Results Cumsum") | |
111 | ##self.df.plot(x=self.df.index, y='TossResultsCumsum') | |
112 | #plt.title("Balance") | |
113 | - | plt.show() # pause |
113 | + | #self.df.plot(x=self.df.index, y='Balance') |
114 | ||
115 | #fileOut='out/fig/fig_{0}.png'.format(filename) | |
116 | #print("Generating {0}".format(fileOut)) | |
117 | #plt.savefig(fileOut) | |
118 | ||
119 | #plt.show() # pause | |
120 | ||
121 | ||
122 | g = SatoshiDiceGameTester() |