Kestable

The St. Petersburg Paradox

Jan 5th, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. #!/usr/bin/env
  2. # -*- coding: utf-8 -*-
  3. # Description: The game is played by flipping a fair coin until it comes up tails,
  4. # and the total number of flips, n, determines the prize, which equals $2**n.
  5. # Thus if the coin comes up tails the first time, the prize is $2**1 = $2, and the game ends.
  6. # If the coin comes up heads the first time, it is flipped again.
  7. # If it comes up tails the second time, the prize is $2**2 = $4, and the game ends.
  8. # If it comes up heads the second time, it is flipped again. And so on.
  9.  
  10. import random, pylab
  11.  
  12. def simulation():
  13.     power = 0
  14.     while random.random() >= 0.5:
  15.         power += 1
  16.     return 2**power, power+1
  17.  
  18. def monte_carlo(entrence_price = 10, n_sim = 10000):
  19.     equity = [0]
  20.     flips = 0
  21.     for i in xrange(n_sim):
  22.         equity.append(equity[-1] - entrence_price + simulation()[0])
  23.         flips += simulation()[1]
  24.     equity.pop(0)
  25.     return equity, flips
  26.  
  27. if __name__ == "__main__":
  28.     pylab.figure(1)
  29.     pylab.plot(range(1, 10001), monte_carlo()[0])
  30.     pylab.show()
Advertisement
Add Comment
Please, Sign In to add comment