At a harbour market households trade m goods (fish, salt, rope, ...). Household i owns e_{ij} units of good j and the prices are p_j, so its income is the value of what it owns, m_i = \sum_j p_j e_{ij}. Its tastes are Cobb-Douglas with positive weights w_{ij}: it spends the share s_{ij} = w_{ij} / \sum_k w_{ik} of its income on good j, so it demands x_{ij} = s_{ij} m_i / p_j.
The excess demand for good j is what all households want minus what they own between them:
A positive z_j means good j is short at these prices, a negative one that it is left over. Every household's purchases are worth exactly its sales, so the total value of excess demand, \sum_j p_j z_j, should be zero at every price vector, cleared markets or not. This is Walras' law; check it numerically.
Write excess_demand(weights, endowments, prices) that returns a tuple (z, value): the list of excess demands, one per good, and \sum_j p_j z_j. Row i of weights and of endowments belongs to household i; each row has one entry per good, in the same order as prices. With no households, every excess demand is 0.0.
Examples
Input: weights = [[1, 1, 2], [2, 1, 1]], endowments = [[10, 0, 0], [0, 8, 4]], prices = [1, 2, 3]
Output: ([6.5, -3.25, 0.0], 0.0)
Explanation: the fisher's catch is worth 10 and buys 2.5 fish, 1.25 salt and 1.667 rope;
the salt trader's goods are worth 28 and buy 14 fish, 3.5 salt and 2.333 rope. Fish is
short by 6.5, salt over-supplied by 3.25, rope clears; 1 * 6.5 + 2 * (-3.25) + 0 = 0.
Input: weights = [[1, 1]], endowments = [[4, 6]], prices = [3, 2]
Output: ([0.0, 0.0], 0.0)
Explanation: alone at these prices, the household wants exactly what it has.
Your value will usually be a tiny number like 1e-15 instead of 0.0: that is rounding, and it passes.
Constraints
- answers are compared with an absolute and relative tolerance of
1e-6; do not round
Goals
- Add up every household's demand and endowment to get each market's excess demand
- Value the excess demands at the prices and see the total come out at zero (Walras' law)
- Handle any number of goods with weights that are normalised into shares