A seaside town's bike-rental market: at a price of P dollars per rental, tourists want a - b \cdot P rentals a day and shops offer c + d \cdot P, with b > 0, d > 0 and c <= 0 (no shop rents below -c / d dollars). Read as values, the demand line at quantity q is what the q-th rental is worth to its tourist, and the supply line at q is what it costs its shop. Without any policy the market clears at P^* and Q^* (the inputs promise Q^* > 0), and every rental whose value exceeds its cost takes place.
The council considers one policy:
policy = "tax": a tax ofxdollars per rental. Tourists pay P_b, shops keep P_b - x, and the quantity is where tourists' demand at P_b equals shops' supply at P_b - x, or0if that would be negative.policy = "ceiling": rentals may not cost more thanxdollars. If x \ge P^* nothing changes. Otherwise the quantity is what shops offer atx(at least0), and those rentals go to the tourists who value them most.
Either policy can stop rentals that were worth more to the tourist than they cost the shop. The deadweight loss is the value lost on those missing rentals: the area between the demand line (above) and the supply line (below), from the quantity traded under the policy up to Q^*. (Tax money is not lost; it goes to the town.)
Write deadweight(a, b, c, d, policy, x) that returns the deadweight loss in dollars per day.
Examples
Input: a = 600, b = 10, c = -60, d = 12, policy = "tax", x = 11
Output: 330.0
Explanation: without policy, 300 rentals at 30 dollars. With the tax, tourists pay 36,
shops keep 25, and 240 rentals take place. On the first missing rental the tourist's
value exceeds the shop's cost by 11 dollars, on the last one by 0.
Input: a = 600, b = 10, c = -60, d = 12, policy = "ceiling", x = 20
Output: 1320.0
Input: a = 600, b = 10, c = -60, d = 12, policy = "tax", x = 60
Output: 8250.0
Explanation: the most any tourist pays (60 dollars) is only 55 above the least any shop
accepts (5 dollars), so no rental takes place and all the market's surplus is lost.
Constraints
- answers are compared with a tolerance of
1e-6
Goals
- Find how many units a tax or a binding price ceiling removes from the market
- Measure the surplus lost on those units as the area between the demand and supply lines
- Handle a policy so harsh that nothing is traded at all