Advertisement
Guest User

Untitled

a guest
Feb 7th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  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
  8. see fix lot
  9. see martingale
  10. """
  11.  
  12. import numpy as np
  13. import pandas as pd
  14. import matplotlib.pylab as plt
  15.  
  16. class SatoshiDiceGameTester:
  17.     def __init__(self):
  18.         print("="*5+" Satoshidice "+"="*5)
  19.         toss_nb = 60000
  20.         max_nb = 2**16-1 # 65535
  21.         less_than = 8000
  22.         win_odds = float(less_than) / float(max_nb) * 100.0
  23.         price_multiplier = 8
  24.         house_percent = 1.9
  25.         expected_rate_of_return = 100.0 - house_percent
  26.         min_bet = 0.01
  27.         max_bet = 500
  28.        
  29.         print("""Playing {toss_nb} times to Satoshidice game less than {less_than}
  30.    win_odds = {win_odds} %
  31.    price_multiplier = {price_multiplier}
  32.    house_percent = {house_percent} %
  33.    expected_rate_of_return = {expected_rate_of_return} %
  34.    min_bet = {min_bet}
  35.    max_bet = {max_bet}""".format(
  36.         toss_nb=toss_nb,
  37.         less_than=less_than,
  38.         win_odds=win_odds,
  39.         price_multiplier=price_multiplier,
  40.         house_percent=house_percent,
  41.         expected_rate_of_return=expected_rate_of_return,
  42.         min_bet=min_bet,
  43.         max_bet=max_bet,
  44.         ))
  45.        
  46.         self.df = pd.DataFrame(np.random.randint(0,max_nb,toss_nb), columns=['Toss'])
  47.         self.df['TossResults'] = np.where(self.df['Toss']<less_than, 1, -1) # 1 = win -1=lose
  48.         #self.df['TossResults'] = np.where(self.df['Toss']<less_than, 1, -1) # 1 = win -1=lose
  49.        
  50.         self.df['TossResultsCumsum'] = self.df['TossResults'].cumsum()
  51.        
  52.         # Fix size
  53.         self.df['Size'] = 0.01
  54.         initial_balance = 1
  55.         self.df['BalanceVar'] = np.where(self.df['TossResults']>0, self.df['Size']*price_multiplier, -self.df['Size']+0.005 * self.df['Size'])
  56.         self.df['Balance'] = initial_balance+self.df['BalanceVar'].cumsum()
  57.        
  58.         print(self.df.head())
  59.         print(self.df)
  60.         print(self.df.tail())
  61.        
  62.         exp_win = len(self.df[self.df['TossResults']>0])
  63.         exp_loss = toss_nb - exp_win
  64.         exp_win_pc = float(exp_win)/float(toss_nb)*100
  65.         exp_loss_pc = float(exp_loss)/float(toss_nb)*100
  66.         relative_difference = (exp_win_pc-win_odds)/win_odds*100
  67.         print("""Results:
  68.    toss_nb={toss_nb}
  69.    win={win}
  70.    loss={loss}
  71.    win={win_pc} %
  72.    loss={loss_pc} %
  73.    relative difference = {rel_diff} %
  74.    min balance = {balance_min}
  75.    max balance = {balance_max}
  76.    initial deposit = {balance_initial}""".format(
  77.   toss_nb=toss_nb,
  78.   win=exp_win,
  79.   loss=exp_loss,
  80.   win_pc=exp_win_pc,
  81.   loss_pc=exp_loss_pc,
  82.   rel_diff = relative_difference,
  83.   balance_min = self.df['Balance'].min(),
  84.   balance_max = self.df['Balance'].max(),
  85.   balance_initial = initial_balance
  86.   ))
  87.  
  88.         #self.df.to_csv('out.csv')
  89.         self.df.to_excel('out.xls')
  90.  
  91.         fig = plt.figure()
  92.  
  93.         fig.subplots_adjust(bottom=0.1)
  94.         #ax = fig.add_subplot(311)
  95.         #plt.title("Toss")
  96.         #self.df.plot(x=self.df.index, y='Toss', style='*')
  97.        
  98.         #ax = fig.add_subplot(312)
  99.         #plt.title("Toss Results")
  100.         #self.df.plot(x=self.df.index, y='TossResults', style='*')
  101.  
  102.         #ax = fig.add_subplot(313)
  103.         ax = fig.add_subplot(111)
  104.         #plt.title("Toss Results Cumsum")
  105.         #self.df.plot(x=self.df.index, y='TossResultsCumsum')
  106.         plt.title("Balance")
  107.         self.df.plot(x=self.df.index, y='Balance')
  108.  
  109.         #fileOut='out/fig/fig_{0}.png'.format(filename)
  110.         #print("Generating {0}".format(fileOut))
  111.         #plt.savefig(fileOut)
  112.  
  113.         plt.show() # pause    
  114.  
  115.  
  116. g = SatoshiDiceGameTester()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement