import math intensity = 0.1 # a parameter modeling the intensity 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 intensity # 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) import two_d def revenue(t,s): '''Return the expected revenue, choosing an optimal price in each situation. arguments: t -- the number of hours remaining before sales must end s -- the number of seats remaining to sell''' table = two_d.make_table(t+1,s+1) for time in range(t+1): for seats in range(s+1): if seats == 0: table[time][seats] = 0 elif time == 0: table[time][seats] = 0 else: price = consult_an_oracle() sp = saleProb(time,price) # the probability we sell a ticket this hour # if we sell a ticket this hour, we earn "price" worth of revenue this hour, # and then we expect to earn some more revenue in future hours, namely afterSale = table[time-1][seats-1] revenueWithSale = price + afterSale # if we don't sell a ticket this hour, he earn no money this hour, # and then we expect to earn some more revenue in future hours, namely afterNoSale = table[time-1][seats] revenueWithNoSale = 0 + afterNoSale table[time][seats] = sp*revenueWithSale + (1-sp)*revenueWithNoSale return table[t][s]