Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- This script was made to understand probabilities behind SatoshiDice game
- http://satoshidice.com/
- ToDo: implement MoneyManagement
- see fix lot
- see martingale
- """
- import numpy as np
- import pandas as pd
- import matplotlib.pylab as plt
- class SatoshiDiceGameTester:
- def __init__(self):
- print("="*5+" Satoshidice "+"="*5)
- toss_nb = 60000
- max_nb = 2**16-1 # 65535
- less_than = 8000
- win_odds = float(less_than) / float(max_nb) * 100.0
- price_multiplier = 8
- house_percent = 1.9
- expected_rate_of_return = 100.0 - house_percent
- min_bet = 0.01
- max_bet = 500
- print("""Playing {toss_nb} times to Satoshidice game less than {less_than}
- win_odds = {win_odds} %
- price_multiplier = {price_multiplier}
- house_percent = {house_percent} %
- expected_rate_of_return = {expected_rate_of_return} %
- min_bet = {min_bet}
- max_bet = {max_bet}""".format(
- toss_nb=toss_nb,
- less_than=less_than,
- win_odds=win_odds,
- price_multiplier=price_multiplier,
- house_percent=house_percent,
- expected_rate_of_return=expected_rate_of_return,
- min_bet=min_bet,
- max_bet=max_bet,
- ))
- self.df = pd.DataFrame(np.random.randint(0,max_nb,toss_nb), columns=['Toss'])
- self.df['TossResults'] = np.where(self.df['Toss']<less_than, 1, -1) # 1 = win -1=lose
- #self.df['TossResults'] = np.where(self.df['Toss']<less_than, 1, -1) # 1 = win -1=lose
- self.df['TossResultsCumsum'] = self.df['TossResults'].cumsum()
- # Fix size
- self.df['Size'] = 0.01
- initial_balance = 1
- self.df['BalanceVar'] = np.where(self.df['TossResults']>0, self.df['Size']*price_multiplier, -self.df['Size']+0.005 * self.df['Size'])
- self.df['Balance'] = initial_balance+self.df['BalanceVar'].cumsum()
- print(self.df.head())
- print(self.df)
- print(self.df.tail())
- exp_win = len(self.df[self.df['TossResults']>0])
- exp_loss = toss_nb - exp_win
- exp_win_pc = float(exp_win)/float(toss_nb)*100
- exp_loss_pc = float(exp_loss)/float(toss_nb)*100
- relative_difference = (exp_win_pc-win_odds)/win_odds*100
- print("""Results:
- toss_nb={toss_nb}
- win={win}
- loss={loss}
- win={win_pc} %
- loss={loss_pc} %
- relative difference = {rel_diff} %
- min balance = {balance_min}
- max balance = {balance_max}
- initial deposit = {balance_initial}""".format(
- toss_nb=toss_nb,
- win=exp_win,
- loss=exp_loss,
- win_pc=exp_win_pc,
- loss_pc=exp_loss_pc,
- rel_diff = relative_difference,
- balance_min = self.df['Balance'].min(),
- balance_max = self.df['Balance'].max(),
- balance_initial = initial_balance
- ))
- #self.df.to_csv('out.csv')
- self.df.to_excel('out.xls')
- fig = plt.figure()
- fig.subplots_adjust(bottom=0.1)
- #ax = fig.add_subplot(311)
- #plt.title("Toss")
- #self.df.plot(x=self.df.index, y='Toss', style='*')
- #ax = fig.add_subplot(312)
- #plt.title("Toss Results")
- #self.df.plot(x=self.df.index, y='TossResults', style='*')
- #ax = fig.add_subplot(313)
- ax = fig.add_subplot(111)
- #plt.title("Toss Results Cumsum")
- #self.df.plot(x=self.df.index, y='TossResultsCumsum')
- plt.title("Balance")
- self.df.plot(x=self.df.index, y='Balance')
- #fileOut='out/fig/fig_{0}.png'.format(filename)
- #print("Generating {0}".format(fileOut))
- #plt.savefig(fileOut)
- plt.show() # pause
- g = SatoshiDiceGameTester()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement