The Price of a Monthly Basket

consumer price index · inflation rate · fixed basket · dictionaries

A consumer price index (CPI) follows the cost of one fixed basket: the quantities a typical household buys in a month, kept the same every year so that only prices change. With c_t the basket's cost in year t and b the base year,

\text{CPI}_t = 100 \cdot \frac{c_t}{c_b} \qquad \text{inflation}_t = \frac{\text{CPI}_t}{\text{CPI}_{t-1}} - 1

basket maps each good to the quantity in the basket. prices[good] is the list of the good's price in years 0, 1, ..., n-1; prices has every good of the basket and may have others, which are not in the basket and do not count. base is the index of the base year.

Write price_index(basket, prices, base) that returns a pair (cpi, inflation): the list of n index values, and the list of the n - 1 inflation rates for years 1, ..., n-1, as fractions (0.05 means 5 %).

Examples

Input:  basket = {"bread": 10, "bus": 20, "rent": 1}
        prices = {"bread": [2.0, 2.2, 2.4], "bus": [1.5, 1.5, 1.8],
                  "rent": [400, 420, 441], "caviar": [90, 50, 20]}
        base = 0
Output: ([100.0, 104.88888888888889, 111.33333333333333],
         [0.04888888888888898, 0.06144067796610164])
Explanation: the basket costs 450, 472 and 501. Caviar is not in the basket.

Input:  the same basket and prices, base = 2
Output: ([89.82035928143712, 94.21157684630738, 100.0],
         [0.04888888888888898, 0.06144067796610164])

Constraints

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

Goals

  • Price a fixed basket of goods year after year
  • Turn basket costs into an index that is 100 in the base year
  • Read the inflation rate as the growth rate of the index
Starting Python…