Mira builds boats. Her earnings are lumpy: little while she learns, a lot in her best years, nothing once she retires. She wants to spend the same amount every period of her working and retired life, and she can save or borrow at the bank at the same interest rate r (a fraction: 5 % is 0.05).
The timeline has periods t = 0, 1, \ldots, T-1, with T = len(incomes). She starts period 0 with savings wealth = a_0. In each period t, in this order: she receives incomes[t], spends c, and whatever is left (negative if she borrowed) earns interest until the next period:
Money in period t is worth 1 / (1 + r)^t of its amount today (period 0 is not discounted). Her lifetime wealth is her savings plus the present value of all her income:
She chooses the one constant c that leaves her with exactly nothing at the end: a_T = 0.
Write lifetime_budget(wealth, incomes, r) that returns a tuple (W, c, lowest), where lowest is the lowest balance among a_0, a_1, \ldots, a_T (negative when she is in debt at some point). Collect the balances and press Run with plot(balances, kind="step") to see her save and draw down.
Examples
Input: wealth = 0, incomes = [20, 40, 60, 0], r = 0.05
Output: (112.51700680, 30.22012123, -10.73112729)
Explanation: W = 20 + 40/1.05 + 60/1.05**2 + 0 = 112.517. She spends 30.22 a period,
so after period 0 she owes 1.05 * (0 + 20 - 30.22) = 10.73: she borrows while young.
Input: wealth = 10, incomes = [20, 40, 60, 0], r = 0
Output: (130.0, 32.5, -2.5)
Explanation: with no interest she simply spreads 130 over 4 periods.
Input: wealth = 100, incomes = [0, 0, 0], r = 0.1
Output: (100.0, 36.55589124, 0.0)
Explanation: she only draws savings down; the lowest balance is the final 0
(a tiny rounding error like 7.8e-15 is fine).
Constraints
- answers are compared with a tolerance of
1e-6; do not round
Goals
- Value a whole stream of income in today's money: $W = a_0 + \sum_t y_t / (1 + r)^t$
- Find the constant spending level whose present value equals that wealth
- Track the bank balance period by period and see when the household borrows