1
$\begingroup$

I'm trying to implement a solution to a dynamic programmig exercise (cake eating) in Python.

Consider the following problem. An individual lives for 20 periods. In the first 15 periods he receives an endowment of a non perishable good, say a cake, equal $y(t) = 1 + 0.1 \cdot t$. In the remaining 5 periods he receives nothing. The good is not perishable and does not produce fruits, so that it can only be either consumed or stored for consumption in the following periods. The consumption of the – obviously non-negative – amount $c$ gives utility $c^{1/3}$. Future utility is discounted by the factor $\beta = 0.98$ yearly.

My result seems to be wrong. Here is the code:

import numpy as np
import time
import matplotlib.pyplot as plt
import seaborn as sns

beta = 0.98
nper = 20
T = 20

t = np.arange(1, T + 1)
y_vec = np.where(t <= 15, 1 + 0.1 * t, 0)

a_bounds = [0.0, 0]
n_p = 1000
a_grid = np.linspace(a_bounds[0], a_bounds[1] + np.sum(y_vec), n_p)

V_fun = np.zeros((nper, n_p))
V_new = np.zeros(n_p)
Cons = np.zeros((nper, n_p))
Anext = np.zeros((nper, n_p))
u_temp = np.zeros(n_p)

i_time = time.time()
for iter in range(nper):
    gridt = a_grid + y_vec[iter]
    Cons[iter, 0] = 0.0
    V_new[0] = 0.0
    
    for i in range(1, n_p):
        for j in range(0, i + 1):
            xt1 = a_grid[j]
            xt = gridt[i]
            if xt1 <= xt:
                cons_u = xt - xt1
                u_temp[j] = cons_u**(1/3) + beta * V_fun[iter, j]
        ic = np.argmax(u_temp[0:i+1])
        Cons[iter, i] = a_grid[i] - a_grid[ic]
        Anext[iter, i] = a_grid[ic]
        V_new[i] = u_temp[ic]

    V_fun[iter] = V_new

I think there is a problem in how i defined the grid but i'm not sure.

I expect that the individual smooth consumption over time because the income is certain at time 0. So the consumption should not be zero from t = 15 to t = 20 .

$\endgroup$
4
  • $\begingroup$ Hi! Could you please explain what it is that you are trying to do? :) What do you think this program should achieve? Solve a dynamic programming exercise? $\endgroup$
    – Giskard
    Commented Apr 20 at 10:18
  • $\begingroup$ Just a few comments until you clarify: 'np' is undefined, and 'gridt' and the following lines should be indented if they are inside the for cycle. The code as it is above simply does not run. $\endgroup$
    – Giskard
    Commented Apr 20 at 10:22
  • $\begingroup$ Yeah I'm trying to solve a dynamic programmig exercise. It is the following: Consider the following problem. An individual lives for 20 periods. In the first 15 periods he receives an endowment of a non perishable good, say a cake, equal y(t) = 1 + 0.1·t. In the remaining 5 periods he receives nothing. The good is not perishable and does not produce fruits, so that it can only be either consumed or stored for consumption in the following periods. The consumption of the – obviously non-negative – amount c gives utility c^(1/3). Future utility is discounted by the factor β = 0.98 yearly. $\endgroup$ Commented Apr 20 at 10:33
  • 1
    $\begingroup$ For the code, I messed up with the writing here. Now it should be fine $\endgroup$ Commented Apr 20 at 10:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.