What the Honey Market Is Worth

consumer surplus · producer surplus · equilibrium · triangle area

At a farmers' market, jars of honey trade at the equilibrium of two lines (price P in dollars per jar, quantities in jars per month):

  • demand: Q_d = a - b \cdot P, with a > 0, b > 0;
  • supply: Q_s = c + d \cdot P, with d > 0 and c <= 0 (beekeepers offer nothing below the price -c / d).

Read the lines the other way round and they say what each jar is worth to someone. The demand line at quantity q is the most the buyer of the q-th jar would pay; the supply line at q is the least its seller would accept. At the equilibrium price P^* every buyer pays P^* and every seller receives it, and the market quantity is Q^*.

  • Consumer surplus is what buyers gain over what they pay: the area between the demand line and the horizontal line at P^*, from quantity 0 to Q^*.
  • Producer surplus is what sellers gain over the least they would accept: the area between the horizontal line at P^* and the supply line, from 0 to Q^*.

Write surplus(a, b, c, d) that returns the pair (consumer_surplus, producer_surplus) in dollars per month. If the lines cross at a negative quantity (the most any buyer pays is below the least any beekeeper accepts), nothing is traded: return (0.0, 0.0).

Examples

Input:  a = 100, b = 2, c = -30, d = 3
Output: (576.0, 384.0)
Explanation: P* = 26 and Q* = 48. The first jar is worth 50 dollars to its buyer
and costs its seller at least 10, so the triangles are 48 * (50 - 26) / 2 = 576
and 48 * (26 - 10) / 2 = 384.

Input:  a = 60, b = 1, c = 0, d = 2
Output: (800.0, 400.0)

Input:  a = 20, b = 2, c = -30, d = 1
Output: (0.0, 0.0)
Explanation: no buyer pays more than 10 dollars; no beekeeper sells below 30.

Constraints

  • answers are compared with a tolerance of 1e-6

Goals

  • Read the demand line as what buyers would pay and the supply line as what sellers need
  • Compute consumer and producer surplus as triangle areas at the equilibrium
  • Return zero surplus when nothing is traded
Starting Python…