Guest User

Untitled

a guest
Jun 2nd, 2022
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. import random
  2.  
  3. simulationCount = 100000
  4.  
  5. counts = {
  6. '2a': 0,
  7. '2b': 0,
  8. '3': 0,
  9. '4': 0,
  10. '5': 0,
  11. '6-2a': 0,
  12. '6-a/2': 0,
  13. 'EV-relativeToA': 0,
  14. 'EV-relativeToT': 0,
  15. }
  16.  
  17. def f(key):
  18. return str(round(counts[key] / simulationCount * 100, 2)) + '%'
  19.  
  20. for i in range(simulationCount):
  21. # A person is given two indistinguishable envelopes. One envelope contains twice as much money as the other.
  22. amounts = [100, 200]
  23.  
  24. # The person may pick one envelope and keep whatever amount it contains.
  25. # They pick one envelope at random.
  26. # Denote by a the amount in the player's selected envelope.
  27. # Before they open it they are given a chance to take the other envelope (b) instead.
  28. random.shuffle(amounts)
  29. a = amounts[0]
  30. b = amounts[1]
  31.  
  32. # 2) The probability that a is the smaller amount is 1/2, and that it is the larger amount is also 1/2
  33. if a < b:
  34. counts['2a'] += 1
  35. if a > b:
  36. counts['2b'] += 1
  37.  
  38. # 3) The other envelope (b) may contain either 2A or A/2
  39. if b == 2*a or b == a/2:
  40. counts['3'] += 1
  41.  
  42. # 4) If a is the smaller amount, then the other envelope (b) contains 2a
  43. if (a < b and b == 2*a) or not a < b:
  44. counts['4'] += 1
  45.  
  46. # 5) If a is the larger amount, then the other envelope (b) contains a/2
  47. if (a > b and b == a/2) or not a > b:
  48. counts['5'] += 1
  49.  
  50. # 6) The other envelope (b) contains 2a with probability 1/2 and a/2 with probability 1/2
  51. if (b == 2*a):
  52. counts['6-2a'] += 1
  53. if (b == a/2):
  54. counts['6-a/2'] += 1
  55.  
  56. # 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
  57. counts['EV-relativeToA'] += (b/a - 1)
  58.  
  59. # ! The expected value of switching from a to b, relative to T, is still zero.
  60. T = amounts[0] + amounts[1]
  61. counts['EV-relativeToT'] += (b-a)/T
  62.  
  63. print('2a: the probability that a is the smaller amount is 50%, observed:', f('2a'))
  64. print('2b: the probability that a is the larger amount is 50%, observed:', f('2b'))
  65. print('3: the other envelope (b) may contain either 2a or a/2, observed:', f('3'))
  66. print('4: if a is the smaller amount, then the other envelope contains 2a, observed:', f('4'))
  67. print('5: if a is the larger amount, then the other envelope contains a/2, observed:', f('5'))
  68. print('6-2a: the other envelope contains 2a with probability 50%, observed:', f('6-2a'))
  69. print('6-a2: the other envelope contains a/2 with probability 50%, observed:', f('6-a/2'))
  70. print('7: the expected value of switching from a to b, relative to a, is +25%, observed:', f('EV-relativeToA'))
  71. 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