#Initialize board.
location = {}
for i in range(40) :
location[i] = 0
location[0] = 1
#Dice rolls.
dice2 = {2: 1/36, 3: 2/36,
4: 3/36, 5: 4/36,
6: 5/36, 7: 6/36,
8: 5/36, 9: 4/36,
10: 3/36, 11: 2/36,
12: 1/36}
dice3 = {3: 1/216, 4: 3/216,
5: 6/216, 6: 10/216,
7: 15/216, 8: 21/216,
9: 28/216, 10: 36/216,
11: 36/216, 12: 28/216,
13: 21/216, 14 : 15/216,
15: 10/216, 16 : 6/216,
17: 3/216, 18 : 1/216}
#Takes in prob_dict, updates it with probability of dice rolls.
def roll(location, rolls, dice) :
cum_location = location.copy()
cum_location[0] = 0
#Keep rolling!
for r in range(rolls) :
#What space do you end up on?
new_location = {}
for i in range(40) :
new_location[i] = 0
#Parse through old locations.
for i in range(40) :
p = location[i]
#If there is a probability you are at a location.
if p!= 0 :
#Distribute it amongs dice rolls.
for j in range(min(dice.keys()), max(dice.keys())) :
#Only if you have not completed a full pass around the board.
if i+j < 40:
#New location probabilities.
new_location[i+j] += p*dice[j]
#Cumulative location probabilities.
cum_location[i+j] += p*dice[j]
#Update locations.
location = new_location
return(cum_location)
def oligopoloy(location, dice, string):
matplotlib.rc_file_defaults()
sns.set()
fig = plt.figure(figsize = (12, 7.5))
ax = fig.add_subplot(xlim = (0.5, 39.5),
ylim = (0, 0.2))
#Title setup.
ax.set_title('Probability of Having Landed on an Oligopoly Space', fontsize = 24)
#X-axis setup.
ax.set_xlabel('Board Space', fontsize = 20)
ax.set_xticks(range(1, 40))
#Y-axis setup.
ax.set_ylabel('Cumulative Probability', fontsize = 20)
ax.tick_params(axis = 'both', which = 'major', labelsize = 16)
roll_text = ax.text(24, 0.190, '',
ha = 'left',
size = 'xx-large',
weight = 'bold',
c = '#481567FF')
max_text = ax.text(24, 0.177, '',
ha = 'left',
size = 'xx-large',
weight = 'bold',
c = '#481567FF')
pmax_text = ax.text(24, 0.165, '',
ha = 'left',
size = 'xx-large',
weight = 'bold',
c = '#481567FF')
min_text = ax.text(24, 0.153, '',
ha = 'left',
size = 'xx-large',
weight = 'bold',
c = '#481567FF')
pmin_text = ax.text(24, 0.141, '',
ha = 'left',
size = 'xx-large',
weight = 'bold',
c = '#481567FF')
def animate(i):
data = roll(location, i, dice)
bars0 = plt.bar(x = data.keys(),
height = data.values(),
color = '#21908CFF',
width = 0.7,
edgecolor = 'k')
#Labels.
maxl = 0
minl = 10
if i == 0 :
maxp = 1
minp = 0
else :
maxp = 0
minp = 1
for j in range(1, 40):
prob = bars0[j].get_height()
space = bars0[j].get_xy()[0]
#Update most likely location.
if prob > maxp :
maxp = prob
maxl = j
#Update least likely location.
if (j > 9) & (prob < minp) :
minp = prob
minl = j
roll_text.set_text("Roll : %.0f" % i)
max_text.set_text("Most Likely Space : %.0f" % maxl)
pmax_text.set_text("Cum. Probability : %.5f" % maxp)
min_text.set_text("Least Likely Space : %.0f" % minl)
pmin_text.set_text("Cum. Probability : %.5f" % minp)
return bars0, roll_text, max_text, pmax_text, min_text, pmin_text
#Run animation.
anim = FA(fig, animate, frames = 20, interval = 1000)
#Save animation.
anim.save('2023.12.01' + string + '.mp4');