import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import numpy as np
import pandas as pd
from pandas.api.types import CategoricalDtype
import seaborn as sns
#Creates an nXn rhombus with all probabilities, probability of knocking down next pin is p.
def probRhombus(n, p) :
#list of lists of probabilities
rhombus = [[1]]
#n-1 steps ascending
asc = n-1
for a in range(asc) :
#Append 0's to beginning and end of list for consistency.
new = getProbs([0] + rhombus[a] + [0], p)
rhombus.append(new)
#n-1 steps descending.
desc = n-1
for d in range(desc):
new = getProbs(rhombus[asc+d], p)
rhombus.append(new)
return(rhombus)
#Takes a list of n probabilities and spits out a list of n-1 probabilities.
def getProbs(probs, p) :
newprobs = []
for i in range(len(probs)-1) :
#inclusion exclusion
newprob = p*probs[i] + p*probs[i+1] - p*p*probs[i]*probs[i+1]
newprobs.append(newprob)
return(newprobs)
ns = [2, 5, 10, 20, 50, 100, "∞"]
probabilities = [x/400 for x in range(200,400)]
final_pins = []
for n in ns:
for p in probabilities :
if n == "∞" :
final_pins.append(((2*p-1) / p**2))
else :
r = probRhombus(n, p)
final_pins.append(r[2*n-2][0])
data = {'Dimension': np.repeat(np.array(ns), [200]),
'KD': probabilities * 7,
'FP': final_pins}
df = pd.DataFrame(data)
df['Dimension'] = df['Dimension'].astype(CategoricalDtype(categories = ['2', '5', '10', '20', '50', '100', "∞"],
ordered=True))
sns.set()
#Plot.
fig = plt.figure(figsize = (13, 8))
ax = fig.add_subplot()
scatter = plt.scatter(x = df['KD'], y = df['FP'], s = 35, c = df['Dimension'].cat.codes, cmap = 'viridis_r')
ax.set_title("End Behavior of N^2 Bowling", fontsize = 24)
ax.set_xlabel("Probability of Knocking Down Next Pin", fontsize = 22)
ax.xaxis.set_major_formatter(mtick.PercentFormatter(1))
ax.set_ylabel("Probability of Knocking Down Final Pin", fontsize = 22)
ax.xaxis.set_major_formatter(mtick.PercentFormatter(1))
ax.tick_params(axis = 'both', labelsize = 18)
plt.legend(title = " Rhombus\nDimension (N)",
title_fontsize = 20,
handles = scatter.legend_elements()[0],
labels = ns,
ncol = 2,
loc = "lower right",
fontsize = 18)
plt.savefig("2022.08.05 Classic.png");