Packages¶

In [1]:
import math
from math import sqrt as mS
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter as PF
import seaborn as sns

Constants & Functions¶

In [2]:
def createDistDF() :

    """
    List of x, y, and z values

    Parameters:
    None.
    
    Returns:
    df - Dataframe of values.
    """   
    
    a = 2
    Xs = []
    Ys = []
    Z1s = []
    Z2s = []
    
    for x in [i/500 for i in range(501)] :
        for y in [j/500 for j in range(501)] :
            
            b = -x + 2*y - 1
            c = -x*y + x - 2*y
            
            #Discriminant
            d = b*b - 4*a*c
            
            if d >= 0 :
                z1 = (-b-mS(d))/4
                z2 = (-b+mS(d))/4
                if z1 > 0 :
                    Z1s.append(z1)
                else :
                    Z1s.append(z2)
                Z2s.append(z2)
            else :
                Z1s.append(None)
                Z2s.append(None)
                
            Xs.append(x)
            Ys.append(y)
            
    df = pd.DataFrame({'X' : Xs,
                       'Y' : Ys,
                       'Z1' : Z1s,
                       'Z2' : Z2s})
    return(df)





def heatMap(Z) :
    fig = plt.figure(figsize = (10, 8))  
    ax = fig.add_subplot(xlim = (0, 1),
                         ylim = (0, 1))
    fig.subplots_adjust(left = 0.02, bottom = 0.02, right = 0.98, top = 0.98)
    
    df = createDistDF()
   
    cmap = plt.cm.viridis_r
    cmap.set_bad(color = '#444444FF')
    
    heatmap = plt.scatter(x = df['X'],
                          y = df['Y'],
                          c = df['Z' + str(Z)],
                          cmap = cmap,
                          marker = "s",
                          s = 1,
                          vmin = 0,
                          vmax = 1,
                          plotnonfinite = True)
    #Title setup.
    ax.set_title('The Randy Hall Problem', fontsize = 24)

    #X-axis setup.
    ax.set_xlabel("Remain at Door 2 (Odd Presses) %", fontsize = 22)
    ax.xaxis.set_major_formatter(PF(xmax = 1))
    #Y-axis setup.
    ax.set_ylabel("Remain at Door 2 (Even Presses) %", fontsize = 22)
    ax.yaxis.set_major_formatter(PF(xmax = 1))
    ax.tick_params(axis = 'both', which = 'major', labelsize = 16)

    #Answer.
    ax.scatter(0.2, 0.5, lw = 5, c = 'k', zorder = 2)
    
    text = [("(20%, 50%, 72.268%)", (0.2, 0.52), 'k', 0),
            ("x²+4xy+4y²-6x+12y+1 = 0", (0.56, 0.04), '#3CBB75FF', 15)]
        
    if Z == 1 :
            text.append(("xy-x+2y = 0", (0.55, 0.22), '#FDE725FF', 15))

    for t in text :
            
        plt.annotate(text = t[0],
                     xy = t[1],
                     c = t[2],
                     size = 16,
                     ha = 'center',
                     rotation = t[3],
                     zorder = 4)
   
    #Colorbar.
    cb = plt.colorbar(heatmap, ax = ax, format = PF(xmax = 1))
    cb.set_label('Remain at Door 1 (or Door 3) %',
                 labelpad = -100,
                 fontsize = 20)
    cb.ax.tick_params(labelsize = 16)

    
    fig.savefig("2025.11.07 EC" + str(Z) + ".png", bbox_inches = "tight")

Helper Functions¶

In [3]:
heatMap(1)
In [4]:
heatMap(2)

Rohan Lewis¶

2022.11.10¶