Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. # Function to verify that columns are probability vectors
  2. def checkcol(P):
  3. for i in [sum(x) for x in P.T]:
  4. if i != 1:
  5. return False
  6. return True
  7.  
  8. # Create blank matrix to fill in
  9. # Matrix notation P[row, col], or P[to, from]
  10. P = matrix(QQ, 44, 44)
  11.  
  12. # Define all possible dice rolls
  13. RollProbability = [1/36, 2/36, 3/36, 4/36, 5/36, 6/36, 5/36, 4/36, 3/36, 2/36, 1/36]
  14. # Possible dice rolls:
  15. # 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
  16. # 1/36, 2/36, 3/36, 4/36, 5/36, 6/36, 5/36, 4/36, 3/36, 2/36, 1/36
  17.  
  18. # Create probabilities for rolling dice
  19. for i in range(28):
  20. P[i+2:i+13, i] = vector(RollProbability)
  21.  
  22. # Account for loop around
  23. for i in range(27, 39):
  24. P[i+2:40, i] = vector(RollProbability)[:38-i]
  25. P[:i-27, i] = vector(RollProbability)[38-i:]
  26.  
  27. # Account for state 39 (didn't slice)
  28. P[1:12, 39] = vector(RollProbability)
  29.  
  30. # Account for staying in jail
  31. # Jail is state 40 --> 43
  32. # Passing jail is state 11
  33. for i in range(40, 43):
  34. P[i+1, i] = 30/36 # likelhood of going to next jail
  35. P[11, i] = 6/36 # likelihood of leaving jail (roll doubles)
  36. # 3 attempts then forced to leave jail
  37. P[11, 43] = 1
  38.  
  39. # Account for chance cards
  40. # Chance states are 8, 22, 36
  41. for i in range(40):
  42. # Find probabilities of getting to chance
  43. chance1 = P[8, i]
  44. chance2 = P[22, i]
  45. chance3 = P[36, i]
  46.  
  47. # Chance of staying on chance tile
  48. # 9 options to move, 7 option to stay
  49. # 11/20 chance to stay (or move)
  50. P[8, i] = chance1 * (7/16)
  51. P[22, i] = chance2 * (7/16)
  52. P[36, i] = chance3 * (7/16)
  53.  
  54. # Add probability of chance card to existing states
  55. # Ex. prob of getting to go is the current dice roll
  56. # plus prob of landing on any chance * 1/20 (20 unique card states)
  57.  
  58. # First nested loop for Go, Illinois, St. Charles, Jail, Boardwalk, and Reading Railroad
  59. for j in [0, 24, 11, 40, 39, 5]:
  60. P[j, i] = P[j, i] + (chance1 + chance2 + chance3)*(1/16)
  61.  
  62. # Head to nearest utility
  63. # Varies with where chance is
  64. # Landing on electric varies only with chance 1 and 3
  65. P[12, i] = P[12, i] + (chance1 + chance3)*(1/16)
  66. # Landing on water only varies with chance 2
  67. P[28, i] = P[28, i] + (chance2)*(1/16)
  68.  
  69. # Head to nearest railroad
  70. # Same as utilities
  71. P[5, i] = P[5, i] + (chance3)*(1/16)
  72. P[15, i] = P[15, i] + (chance1)*(1/16)
  73. P[25, i] = P[25, i] + (chance2)*(1/16)
  74. # Railroad at 35 cannot be advanced to from chance
  75.  
  76. # Back 3 spaces
  77. P[(i-3)%40, i] = P[(i-3)%40, i] + (chance1 + chance2 + chance3)*(1/16)
  78.  
  79. # Account for community chest cards
  80. # community chests only go to go and jail
  81. for i in range(40):
  82. # Find probabilities of getting to community chest
  83. chest1 = P[2, i]
  84. chest2 = P[17, i]
  85. chest3 = P[33, i]
  86.  
  87. # Probability of no movement
  88. P[2, i] = chest1 * (15/17)
  89. P[17, i] = chest2 * (15/17)
  90. P[33, i] = chest3 * (15/17)
  91.  
  92. # Advance to go
  93. P[0, i] = P[0, i] + (chest1 + chest2 + chest3)*(1/17)
  94.  
  95. # Go to jail
  96. P[40, i] = P[40, i] + (chest1 + chest2 + chest3)*(1/17)
  97.  
  98.  
  99. if checkcol(P):
  100. print "all good"
  101. else:
  102. print "not good"
  103. print [sum(x) for x in P.T]
  104.  
  105.  
  106. # Set up player vector
  107. # All players start at Go
  108. player = matrix(QQ, 44, 1)
  109. player[0, 0] = 1
  110.  
  111. # Find steady-state vector
  112. S = P**1000 * player
  113.  
  114. # Turn S into decimal approximation for clarity
  115. S_a = N(S, digits = 4)
  116.  
  117. # Find order of probabilities for questions
  118. order = S_a.list()
  119. for i in range(44, 0, -1):
  120. order[order.index(min(order))] = i
  121.  
  122. print '\nTop 10 tiles - Probability'
  123. for i in range(1, 11):
  124. print "Tile {}, {:.4}%".format(order.index(i), S_a[order.index(i), 0] * 100)
  125.  
  126. print '\nWorst 10 tiles - Probability'
  127. for i in range(44, 34, -1):
  128. print "Tile {}, {:.4}%".format(order.index(i), S_a[order.index(i), 0] * 100)
  129.  
  130. print '\nExpected value of each property set'
  131. S_a = S_a.list()
  132. Brown = 4*S_a[1] + 8*S_a[3]
  133. Cyan = 12*S_a[6] + 12*S_a[8] + 16*S_a[9]
  134. Pink = 20*S_a[11] + 20*S_a[13] + 24*S_a[14]
  135. Orange = 28*S_a[16] + 28*S_a[18] + 32*S_a[19]
  136. Red = 36*S_a[21] + 36*S_a[23] + 40*S_a[24]
  137. Yellow = 44*S_a[26] + 44*S_a[27] + 48*S_a[28]
  138. Green = 52*S_a[31] + 52*S_a[32] + 52*S_a[34]
  139. Blue = 70*S_a[37] + 100*S_a[36]
  140. Util = 70*S_a[12] + 70*S_a[28]
  141. RR = 200*S_a[5] + 200*S_a[15] + 200*S_a[25] + 200*S_a[35]
  142.  
  143. Properties = {'Brown':Brown, 'Cyan':Cyan, 'Pink':Pink, 'Orange':Orange, 'Red':Red, 'Yellow':Yellow, 'Green':Green, 'Blue':Blue, 'Util':Util, 'RR':RR}
  144. print Properties
  145. sum(S_a[x] for x in [40, 41, 42, 43])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement