Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env
- # -*- coding: utf-8 -*-
- # Description: The game is played by flipping a fair coin until it comes up tails,
- # and the total number of flips, n, determines the prize, which equals $2**n.
- # Thus if the coin comes up tails the first time, the prize is $2**1 = $2, and the game ends.
- # If the coin comes up heads the first time, it is flipped again.
- # If it comes up tails the second time, the prize is $2**2 = $4, and the game ends.
- # If it comes up heads the second time, it is flipped again. And so on.
- import random, pylab
- def simulation():
- power = 0
- while random.random() >= 0.5:
- power += 1
- return 2**power, power+1
- def monte_carlo(entrence_price = 10, n_sim = 10000):
- equity = [0]
- flips = 0
- for i in xrange(n_sim):
- equity.append(equity[-1] - entrence_price + simulation()[0])
- flips += simulation()[1]
- equity.pop(0)
- return equity, flips
- if __name__ == "__main__":
- pylab.figure(1)
- pylab.plot(range(1, 10001), monte_carlo()[0])
- pylab.show()
Advertisement
Add Comment
Please, Sign In to add comment