Together or Instead

perfect complements · perfect substitutes · corner solution · utility maximisation

Two extreme kinds of taste, for two goods bought in any non-negative amounts (litres, kilograms), with income income and prices px and py:

  • kind == "complements": U = \min(x / a, \; y / b). The goods are only useful together, in the proportion a units of x to b units of y (Kai's breakfast always uses a cups of oats per b cups of milk; extra oats without milk add nothing).
  • kind == "substitutes": U = a \, x + b \, y. Either good will do: one unit of x is worth a, one unit of y is worth b (Lea's stove burns either of two fuels, giving a or b hours of heat per litre). If both goods give exactly the same utility per coin, the consumer splits the income equally: half is spent on each.

Write together_or_instead(kind, a, b, income, px, py) that returns the utility-maximising bundle and its utility as a tuple of floats (x, y, U).

Examples

Input:  kind = "complements", a = 1, b = 2, income = 60, px = 4, py = 3
Output: (6.0, 12.0, 6.0)
Explanation: every "portion" of 1 unit of x and 2 units of y costs 4 + 6 = 10, so 60
buys 6 portions: x = 6, y = 12, U = min(6 / 1, 12 / 2) = 6.

Input:  kind = "substitutes", a = 3, b = 2, income = 60, px = 4, py = 2
Output: (0.0, 30.0, 60.0)
Explanation: x gives 3 / 4 = 0.75 per coin, y gives 2 / 2 = 1 per coin: all on y.

Input:  kind = "substitutes", a = 2, b = 1, income = 60, px = 6, py = 3
Output: (5.0, 10.0, 20.0)
Explanation: both give 1/3 per coin, so 30 is spent on each.

Constraints

  • kind is "complements" or "substitutes"
  • a, b, px and py are whole numbers, so equal ratios are exactly equal
  • answers are compared with a tolerance of 1e-6

Goals

  • Buy perfect complements in their fixed proportion, spending the whole income
  • Spend everything on the substitute with more utility per coin (a corner solution)
  • Handle the tie between substitutes with a stated rule
Starting Python…