Guest User

Untitled

a guest
Feb 7th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 martingales ...
  8. """
  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.         less_than = 8000
  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['TossResults'] = np.where(self.df['Toss']<less_than, 1, -1) # 1 = win -1=lose
  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.         initial_balance = 1
  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['Balance'] = initial_balance+self.df['BalanceVar'].cumsum()
  56.        
  57.         #print(self.df.head())
  58.         #print(self.df)
  59.         #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_loss_pc = float(exp_loss)/float(toss_nb)*100.0
  65.         relative_difference = (exp_win_pc-win_odds)/win_odds*100.0
  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.    max balance = {balance_max}
  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.   rel_diff = relative_difference,
  86.   balance_min = self.df['Balance'].min(),
  87.   balance_max = self.df['Balance'].max(),
  88.   balance_initial = initial_balance,
  89.   balance_final = balance_final,
  90.   house_pc=house_pc_exp_final,
  91.   ))
  92.  
  93.         #self.df.to_excel('out.xls') # only 65535 rows
  94.         #self.df.to_csv('out.csv')
  95.  
  96.         #Only Uncomment plot for small tosses number
  97.         #fig = plt.figure()
  98.         #fig.subplots_adjust(bottom=0.1)
  99.        
  100.         ##ax = fig.add_subplot(311)
  101.         ##plt.title("Toss")
  102.         ##self.df.plot(x=self.df.index, y='Toss', style='*')
  103.        
  104.         ##ax = fig.add_subplot(312)
  105.         ##plt.title("Toss Results")
  106.         ##self.df.plot(x=self.df.index, y='TossResults', style='*')
  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.         #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()
Advertisement
Add Comment
Please, Sign In to add comment