Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- simulationCount = 100000
- counts = {
- '2a': 0,
- '2b': 0,
- '3': 0,
- '4': 0,
- '5': 0,
- '6-2a': 0,
- '6-a/2': 0,
- 'EV-relativeToA': 0,
- 'EV-relativeToT': 0,
- }
- def f(key):
- return str(round(counts[key] / simulationCount * 100, 2)) + '%'
- for i in range(simulationCount):
- # A person is given two indistinguishable envelopes. One envelope contains twice as much money as the other.
- amounts = [100, 200]
- # The person may pick one envelope and keep whatever amount it contains.
- # They pick one envelope at random.
- # Denote by a the amount in the player's selected envelope.
- # Before they open it they are given a chance to take the other envelope (b) instead.
- random.shuffle(amounts)
- a = amounts[0]
- b = amounts[1]
- # 2) The probability that a is the smaller amount is 1/2, and that it is the larger amount is also 1/2
- if a < b:
- counts['2a'] += 1
- if a > b:
- counts['2b'] += 1
- # 3) The other envelope (b) may contain either 2A or A/2
- if b == 2*a or b == a/2:
- counts['3'] += 1
- # 4) If a is the smaller amount, then the other envelope (b) contains 2a
- if (a < b and b == 2*a) or not a < b:
- counts['4'] += 1
- # 5) If a is the larger amount, then the other envelope (b) contains a/2
- if (a > b and b == a/2) or not a > b:
- counts['5'] += 1
- # 6) The other envelope (b) contains 2a with probability 1/2 and a/2 with probability 1/2
- if (b == 2*a):
- counts['6-2a'] += 1
- if (b == a/2):
- counts['6-a/2'] += 1
- # 7) The expected value of switching from a to b, relative to a, is positive: 0.5*2a + 0.5*a/2 - a = 0.25a
- counts['EV-relativeToA'] += (b/a - 1)
- # ! The expected value of switching from a to b, relative to T, is still zero.
- T = amounts[0] + amounts[1]
- counts['EV-relativeToT'] += (b-a)/T
- print('2a: the probability that a is the smaller amount is 50%, observed:', f('2a'))
- print('2b: the probability that a is the larger amount is 50%, observed:', f('2b'))
- print('3: the other envelope (b) may contain either 2a or a/2, observed:', f('3'))
- print('4: if a is the smaller amount, then the other envelope contains 2a, observed:', f('4'))
- print('5: if a is the larger amount, then the other envelope contains a/2, observed:', f('5'))
- print('6-2a: the other envelope contains 2a with probability 50%, observed:', f('6-2a'))
- print('6-a2: the other envelope contains a/2 with probability 50%, observed:', f('6-a/2'))
- print('7: the expected value of switching from a to b, relative to a, is +25%, observed:', f('EV-relativeToA'))
- print('!: the expected value of switching from a to b, relative to T, is 0%, observed:', f('EV-relativeToT'))
Advertisement
Add Comment
Please, Sign In to add comment