Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Jupyter notebook OS3307_Lab5_Group1.ipynb

Views: 25
Kernel: Python 2 (SageMath)
# load packages import numpy as np import pandas as pd import scipy.stats as stats import matplotlib.pyplot as plt import matplotlib as mlab import os # set up directory structure cwd= os.getcwd() wd = os.path.join(cwd, 'output') rd = os.path.join(cwd, 'data')
# setting up bernoulli for 10,000 trials n = 10000 p = .8 over_booked = np.random.binomial(n, p, size=None) regular = n - over_booked print(over_booked) print(regular)
7960 2040
MINOR_THRESHOLD = 9691.05 MAJOR_THRESHOLD = 7627.47 p2 = .9 people = 100 total_people = 0 sum_overbooked = 0 count = 0 total_profit = 0 penalty = [] profit = [] bumpd_ppl = [] total = [] minor = 0 major = 0 for i in range(over_booked): total_people = np.random.binomial(people, p2, size=None) total.append(total_people) if(total_people < 85): over = 0 else: count += 1 over = total_people - 85 bumpd_ppl.append(over) penalty = [] for j in range(over): penalty.append(np.random.normal(loc=500, scale=50, size=None)) profit.append(150*people - sum(penalty)) if ((150*people - sum(penalty)) <= MINOR_THRESHOLD): minor+=1 if ((150*people - sum(penalty)) <= MAJOR_THRESHOLD): major+=1 for k in range(regular): profit.append(150*85) print('Avg amount of people who show: %d' %np.mean(total)) print('flights with people bumped: %d' %count) print('proportion of flights with at least one person bumped: %f' %(count /10000.00)) print('Total profit: $%f' %sum(profit)) print('Avg profit: $%f' %np.average(profit)) print('Mean number of bumped passengers: %d' %np.mean(bumpd_ppl)) print('Median number of bumped passengers: %d' %np.median(bumpd_ppl)) print('Minor: ',minor) print('Major: ',major)
Avg amount of people who show: 90 flights with people bumped: 7664 proportion of flights with at least one person bumped: 0.766400 Total profit: $125106458.689245 Avg profit: $12510.645869 Mean number of bumped passengers: 5 Median number of bumped passengers: 5 ('Minor: ', 169) ('Major: ', 1)
import scipy as stats # Histogram of avg profits plt.hist(profit, bins=20, color='b', label = "Profits") plt.xlabel('Profit per flight in dollars') plt.ylabel('Frequency') plt.show()
Image in a Jupyter notebook
import scipy as stats # Boxplot of avg profits fig, ax = plt.subplots() ax.set_ylabel('Avg Profit per Flight') ax.boxplot(profit) plt.show() df = pd.DataFrame(profit) df.describe()
Image in a Jupyter notebook
0
count 10000.000000
mean 12510.645869
std 1264.216672
min 7473.379200
25% 11664.940479
50% 12750.000000
75% 13097.768792
max 15000.000000
# Boxplot of number of people bumped fig, ax = plt.subplots() ax.set_ylabel('People Bumped per Flight') ax.boxplot(bumpd_ppl) plt.show()
Image in a Jupyter notebook
# Boxplot of people who show for a flight fig, ax = plt.subplots() ax.set_ylabel('People Who Show to Overbooked Flights') ax.boxplot(total) plt.show()
Image in a Jupyter notebook
# If never overbooked raw_profit = (85*150*10000) print('profit if never overbooked: $%d' %raw_profit) print('avg profit per flight: $%d' %(raw_profit/10000))
profit if never overbooked: $127500000 avg profit per flight: $12750
# Extra Credit to optimize profit p3 = .9 flights = 10000 people2 = 93 total_people2 = 0 count2 = 0 penalty2 = [] optimal = [] bumpd_ppl2 = [] for j in range(flights): total_people2 = np.random.binomial(people2, p3, size=None) if(total_people2 < 85): over2 = 0 elif(total_people2 > 100): count2 += 1 over2 = 100 else: count2 += 1 over2 = total_people2 - 85 bumpd_ppl2.append(over2) penalty2 = [] for j in range(over2): penalty2.append(np.random.normal(loc=500, scale=50, size=None)) optimal.append(150*people2 - sum(penalty2)) print('flights with people bumped: %d' %count2) print('proportion of flights with at least one person bumped: %f' %(count2 /10000.00)) #print(total_profit) print('Total profit: $%f' %sum(optimal)) print('Avg profit: $%f' %np.average(optimal)) print('Mean number of bumped passengers: %f' %np.mean(bumpd_ppl2)) print('Median number of bumped passengers: %f' %np.median(bumpd_ppl2)) print('Optimal number of tickets: %d' %people2) # Histogram plt.hist(optimal, bins=20, color='b', label = "Profits") plt.xlabel('Profit per flight in dollars') plt.ylabel('Frequency') plt.show()
flights with people bumped: 4031 proportion of flights with at least one person bumped: 0.403100 Total profit: $136540559.097811 Avg profit: $13654.055910 Mean number of bumped passengers: 0.590900 Median number of bumped passengers: 0.000000 Optimal number of tickets: 93
Image in a Jupyter notebook