Advertisement
Guest User

Untitled

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