In [1]:
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np

Approximating expected values, probabilities, etc.

An airline has a flight with 6 seats. They always sell 12 tickets for this flight and ticket holders show up independently with probability 𝑝. Plot the following quantities as a function of 𝑝.

  1. Expected number of ticket holders that show up
  2. Probability that the flight is overbooked
  3. Expected number of ticket holders who show up but don’t fly given that the flight is overbooked

Approximating expected value

In [2]:
results = np.zeros((10, 2))
numTrials = 100000
numTickets = 12
numSeats = 6
for i,p in enumerate(np.linspace(0.1, 1.0, num=10)):
    arrivals = np.random.random((numTickets, numTrials)) < p
    numArrivals = arrivals.sum(axis=0)
    results[i] = [p, numArrivals.mean()]
results
Out[2]:
array([[ 0.1    ,  1.20296],
       [ 0.2    ,  2.40483],
       [ 0.3    ,  3.60764],
       [ 0.4    ,  4.79533],
       [ 0.5    ,  5.99409],
       [ 0.6    ,  7.19751],
       [ 0.7    ,  8.40135],
       [ 0.8    ,  9.59603],
       [ 0.9    , 10.79882],
       [ 1.     , 12.     ]])
In [3]:
plt.plot(results[:,0], results[:,1], 'bo-')
plt.xlabel('Probability of arrival p')
plt.ylabel('Expected value')
plt.title('Expected number of ticket holders that show up')
Out[3]:
Text(0.5,1,'Expected number of ticket holders that show up')

Approximating probability

In [4]:
results = np.zeros((10, 2))
numTrials = 100000
numTickets = 12
numSeats = 6
for i,p in enumerate(np.linspace(0.1, 1.0, num=10)):
    arrivals = np.random.random((numTickets, numTrials)) < p
    numArrivals = arrivals.sum(axis=0)
    indicatorOverbooked = numArrivals > numSeats
    results[i] = [p, indicatorOverbooked.mean()]
results
Out[4]:
array([[1.0000e-01, 6.0000e-05],
       [2.0000e-01, 4.1300e-03],
       [3.0000e-01, 4.0140e-02],
       [4.0000e-01, 1.5833e-01],
       [5.0000e-01, 3.8827e-01],
       [6.0000e-01, 6.6693e-01],
       [7.0000e-01, 8.8345e-01],
       [8.0000e-01, 9.8023e-01],
       [9.0000e-01, 9.9942e-01],
       [1.0000e+00, 1.0000e+00]])
In [5]:
plt.plot(results[:,0], results[:,1], 'bo-')
plt.xlabel('Probability of arrival p')
plt.ylabel('Probability')
plt.title('Probability that flight is overbooked')
Out[5]:
Text(0.5,1,'Probability that flight is overbooked')

Approximating conditional expected value

In [6]:
results = np.zeros((10, 2))
numTrials = 100000
numTickets = 12
numSeats = 6
for i,p in enumerate(np.linspace(0.1, 1.0, num=10)):
    arrivals = np.random.random((numTickets, numTrials)) < p
    numArrivals = arrivals.sum(axis=0)
    indicatorOverbooked = numArrivals > numSeats
    numDontFly = numArrivals[indicatorOverbooked] - numSeats
    results[i] = [p, numDontFly.mean()]
results
Out[6]:
array([[0.1       , 1.        ],
       [0.2       , 1.14634146],
       [0.3       , 1.29309892],
       [0.4       , 1.47904913],
       [0.5       , 1.7461395 ],
       [0.6       , 2.154088  ],
       [0.7       , 2.78374359],
       [0.8       , 3.6825753 ],
       [0.9       , 4.79970785],
       [1.        , 6.        ]])
In [7]:
plt.plot(results[:,0], results[:,1], 'bo-')
plt.xlabel('Probability of arrival p')
plt.ylabel('Conditional expected value')
plt.title("Expected number of ticket holders who show up but don't fly given that the flight is overbooked")
Out[7]:
Text(0.5,1,"Expected number of ticket holders who show up but don't fly given that the flight is overbooked")