Show code
import os
import numpy as np
import pandas as pd
import math
Show code
import plotly as py
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import plotly.express as px
import plotly.figure_factory as ff
Show code
def calculate_p_of_n_hitless_games(p_h,n,g,a):
    """Probability of a hit in an AB = p_h, 
    Number of games = n, 
    Number hitless games = g,
    At bats per game = a"""
    hitless_games = math.pow(math.pow((1-p_h),a),g)
    games_w_hits = math.pow((1-math.pow((1-p_h),a)), n-g)
    return hitless_games * games_w_hits * math.comb(n,g)
Show code
p_h_joe, p_h_normal = 0.277, 0.22
a_joe, a_normal = 5, 4.2
n = 56
needed_p_success = 0.50
Show code
p_success_dimaggios = []
p_success_normals = []
for g in range(n):
    p_success_dimaggios.append(
        np.sum([calculate_p_of_n_hitless_games(p_h_joe,n,k,a_joe) for k in range(g+1)])
    )
    p_success_normals.append(
        np.sum([calculate_p_of_n_hitless_games(p_h_normal,n,k,a_normal) for k in range(g+1)])
    )
Show code
# Build figure
fig = go.Figure()

# Average Joes
fig.add_trace(
    go.Scatter(
        x=[i+1 for i in range(n)],
        y=p_success_normals,
        mode='lines',
        name='average_joes'
    )
)

# Joltin' Joes
fig.add_trace(
    go.Scatter(
        x=[i+1 for i in range(n)],
        y=p_success_dimaggios,
        mode='lines',
        name='joltin_joes'
    )
)

# Baselines
fig.add_trace(
    go.Scatter(
        mode='markers',
        x=[i/2+1 for i in range(n*2)],
        y=[needed_p_success for p in range(n*2)],
        marker=dict(
            color='Green',
            size=3,
            opacity=0.6,
            line=dict(
                color='DarkGreen',
                width=1
            )
        ),
        name= f'p_success = {needed_p_success}',
        showlegend=True
    )
)



fig.layout.update(title="Average Joes vs. Joltin' Joes", hovermode= 'closest', 
                xaxis = dict(title= '# of Allowed Failures', range=[0,n], zeroline= False),
                yaxis = dict(title= 'Probability of Project Success'))
fig.show()