Cake Eating Problem: Consider the problem of a person with x0 units of cake that they are thinking about eating. The trouble is, they have concave preferences over their cake consumption in each period, so they want to ensure that they don’t eat it all at once. In particular, assume that they have CRRA preferences u(c) = c 1−σ/(1 − σ), for σ = 2.5 and that they discount the future at a rate β. We’ll assume that they have access to a flawless refrigeration technology, so the cake does not spoil if they save some for the next day. Moreover, they have a special deal with the bakery: every day, the baker gives them one new unit of cake for free (so they will never run out). Assume our consumer is infinitely lived. Then we can write their problem in sequence form as v(x0) = max {(ct ,xt+1 )} ∞ t=0 ∞ ∑ t=0 β tu(ct) s.t. xt+1 = 1 + xt − ct for all t ≥ 0
def CakeEating(x0, beta, sigma): def u(c): return c**(1.0 - sigma)/(1.0 - sigma) def v(x): if x <= 0: return -np.inf else: return np.log(x) def Bellman(v, x, beta, sigma): c = (1-beta**(1-sigma))*x return u(c) - beta*v(1+x-c) def BellmanIterate(v, x, beta, sigma, num_iter, tol, max_iter): v_new = np.copy(v) i = 0 error = tol + 1 while i < max_iter and error > tol: v = np.copy(v_new) v_new = Bellman(v, x, beta, sigma) error = np.abs(v_new - v) i += 1 return v_new, i