import math arrivalRate = 0.1 # a parameter modeling the rate of consumer traffic scale = 200.0 # a parameter in the consumer price model shape = 3 # a parameter in the consumer price model def arrivalProb(time): '''Return the probability a potential customer will arrive within an hour. argument: time -- the number of hours remaining before sales must end assumption: the probability is small, so we can neglect the case that two or more potential customers arrive within the same hour''' return arrivalRate # simple version: customer arrival independent of time def purchaseProb(time,price): '''Return the probability an arriving potential customer will buy. arguments: time -- the number of hours remaining before sales must end price -- the price being offered to the potential customer''' return math.exp(-((price/scale)**shape)) # simple version: independent of time def saleProb(time,price): '''Return the probability that a sale will occur within an hour. arguments: time -- the number of hours remaining before sales must end price -- the price being offered to any potential customer''' # for a sale to occur, a potential customer must arrive and buy return arrivalProb(time) * purchaseProb(time,price) def priceAndRevenue(t,s): '''Return whatever fixed price is best paired with the expected revenue. arguments: t -- the number of hours remaining before sales must end s -- the number of seats remaining to sell''' price = 0 bestRevenue = -1 # this surely will be beat bestPrice = None # the price at which the bestRevenue is achieved while True: table = make_table(t+1,s+1) for time in range(t+1): for seats in range(s+1): if time == 0: table[time][seats] = 0 elif seats == 0: table[time][seats] = 0 else: # let sp be the probability we sell a ticket the first hour sp = saleProb(time,price) # scenario 1: we sell a ticket this hour # two sources of revenue: (1) this hour, (2) all future hours revenueThisHourIfSale = price revenueFutureIfSale = table[time-1][seats-1] revenueIfSale = revenueThisHourIfSale + revenueFutureIfSale # scenario 2: we don't sell a ticket this hour revenueThisHourNoSale = 0 revenueFutureNoSale = table[time-1][seats] revenueNoSale = revenueThisHourNoSale + revenueFutureNoSale # putting them together to get our expected revenue: table[time][seats] = sp * revenueIfSale + (1-sp) * revenueNoSale revenue = table[t][s] if revenue < bestRevenue: break bestRevenue = revenue bestPrice = price price = price + 1 return bestPrice, bestRevenue def make_table(n,m): return [[None]*m for i in range(n)]